CLICK HERE FOR FREE BLOGGER TEMPLATES, LINK BUTTONS AND MORE! »

Monday - 30 July 2012


Serial Communication trough RS232 cable

Simple program code for Serial Communication

#include <pic.h>
#use delay(clock=20000000)
#fuses hs, nowdt, nolvp, noprotect
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7, parity=n)

int mystatus=0; //here can't declare byte coz not detected by compiler
char mydata;

void main()
{
int count=0;

set_tris_c(0b10001000); //here c6 must be 0, c7 must be 1 (input)
printf("Enter your character : A or S or Z or X");

while(true)
{
if(kbhit()) //monitor incoming charactor from RS232
{
   mydata=toupper(getch()); //receive charactor dan save in mydata
   printf("\n\rYou entered: %c",mydata); //display on hyper terminal
   count++;
   mystatus=1;
}
if (mystatus==1)
{
if(mydata=='A')
{
    output_high(pin_c4); //led on
}
if (mydata=='S')
{
    output_low(pin_c4); //led off
}
if (mydata=='Z')
{
    output_high(pin_c1); //buzzer on
}
if (mydata=='X')
{
    output_low(pin_c1);  //buzzer off
}
mystatus=0;
}//end if
}//end while
}//end void main

Explanation for Example Program Code Above

  • Kbhit ()
Kbhit () is use to check or monitor the incoming character from hardware RS232. When a key is pressed, this function will returns to true or non-zero. When nothings pressed, kbhit () will returns false or zero.

  • Toupper ()
Toupper () is special function to convert input lowercase letter to uppercase letter. If the input data is ‘a’ so that the toupper () will convert the character to uppercase ‘A’.

  • Getch ()
Getch () is returns an 8-bit character. This function is to gets a single-byte (8-bit) character from the terminal. This functions only allow a single character as an input from hardware RS232 to the PIC 16F877A.

0 comments:

Post a Comment