serial doc

4
#include <REGX51.H> unsigned char UARTRead(void); void UARTInit(void); void UARTWrite(unsigned char); sbit LED_RED=P1^0; sbit LED_GREEN=P1^1; void main (void) { unsigned char ch ='*' ; long l; UARTInit(); LED_RED=0; LED_GREEN=0; while(1) { ch=UARTRead(); if(ch == 'Y'|| ch=='y') { for(l=0;l<5000;l++); //delay UARTWrite('G'); LED_GREEN=1; for(l=0;l<5000;l++); //delay for(l=0;l<5000;l++); //delay LED_GREEN=0; } else if (ch == 'N'|| ch=='n') { for(l=0;l<5000;l++); //delay UARTWrite('R'); LED_RED=1; for(l=0;l<5000;l++); //delay for(l=0;l<5000;l++); //delay LED_RED=0; } else { for(l=0;l<5000;l++); //delay // UARTWrite('R'); } } } void UARTInit(void) { SCON = 0x52; //mode 1, enable receiver PCON = 0x00; //SMOD = 0 TMOD = 0x20; //(baudrategener ator) TH1 = 250; //reload value BAUDRATE=4800 TL1 = TH1; TR1 = 1; //start baudrategenera tor TI = 1; //indicates free to tx. } Yeh function h timer set karny ka or Baud rate set kary ga TH1; TH1 per value set karni parti hain Best crystal is 11.0592 and baud rate 4800, 9600 best hain Simulations me TH1 ki value different hoti ha or actual me different rakhty han jab controller per burn karna ho

Upload: nomi77

Post on 17-Oct-2015

7 views

Category:

Documents


0 download

DESCRIPTION

sdsd

TRANSCRIPT

  • #include

    unsigned char UARTRead(void);

    void UARTInit(void);

    void UARTWrite(unsigned char);

    sbit LED_RED=P1^0;

    sbit LED_GREEN=P1^1;

    void main (void)

    {

    unsigned char ch ='*' ;

    long l;

    UARTInit();

    LED_RED=0;

    LED_GREEN=0;

    while(1)

    {

    ch=UARTRead();

    if(ch == 'Y'|| ch=='y')

    {

    for(l=0;l

  • unsigned char UARTRead(void)

    {

    unsigned char ret_val = '*';

    if(RI == 1)

    {

    ret_val =1;

    ret_val = SBUF; //read serial data

    RI = 0; //reset interrupt. Vital!

    }

    return ret_val;

    }

    void UARTWrite(unsigned char ret_val)

    {

    if(TI == 1)

    {

    TI = 0;

    SBUF = ret_val; //writes data to serial buffer

    ret_val= 1;

    TI =1;

    }

    }

    Then double click on virtual terminal and select baud rate as given below

  • Press Play button and see simulation

    Press Y or N to see LEDs will blink and G or R will come up on screen