Advertisement
Electgpl

8051 - Eco UART Silabs

Sep 28th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. //*******************************************************
  2. // Sample echo program for UART0.
  3. //*******************************************************
  4. #include C8051F120.h
  5. typedef unsigned char uchar;
  6. //*******************************************************
  7. // Globals
  8. //*******************************************************
  9. uchar character;
  10. bit charready, stopped;
  11. sbit LED = P1^6;
  12. int count = 0;
  13. //*******************************************************
  14. // config
  15. // Configure the UART and start the interrupt timer.
  16. //*******************************************************
  17. void config(){
  18.     int n;
  19.     SFRPAGE = CONFIG_PAGE;
  20.     WDTCN = 0x07;                   // watchdog timer control
  21.     WDTCN = 0xDE;                   // disable wdt
  22.     WDTCN = 0xAD;
  23.     XBR0 = 0x04;                    // UART0 connects
  24.     XBR2 = 0x40;                    // enable crossbar
  25.     P0MDOUT = 0x01;                 // P0.0 is push-pull TX output
  26.     P1MDIN = 0xFF;                  // input config for P1
  27.                                     // P0.0 = UART TX0 (push-pull output/input)
  28.                                     // P0.1 = UART RX0 (open-drain output/input)
  29.     P1MDOUT |= 0x40;                // enable onboard LED
  30.     OSCXCN = 0x77;                  // external oscillator
  31.     for(n=0;n<3000;n++);            // startup interval
  32.     while((OSCXCN&0x80)==0);        // wait for oscillator
  33.     SCON0 = 0x50;                   // serial port control
  34.     SCON0 &= 0xFC;                  // clear pending interrupt flags
  35.     CKCON = 0x08;                   // clock control
  36.     SFRPAGE = TIMER01_PAGE;
  37.     TH1 = 0xDC;                     // Timer1 UART clock
  38.     TL1 = 0x00;
  39.     TMOD = 0x20;                    // Timer mode register
  40.     TCON = 0x40;                    // Timer control register
  41.     SFRPAGE = LEGACY_PAGE;
  42.     IE = 0x90;                      // Interrupt enable
  43. }
  44. //*******************************************************
  45. // initializeUART
  46. // Initialize the UART and set flags.
  47. //*******************************************************
  48. void initializeUART(){
  49.     charready=0;
  50.     stopped=1;
  51.     config();
  52.     LED = 0;
  53. }
  54. //*******************************************************
  55. // UART0int
  56. // UART0 interrupt routine.
  57. //*******************************************************
  58. void UART0int() interrupt 4 using 1 {
  59.     LED=1;
  60.     if(RI0==1){
  61.         character=SBUF0;
  62.         charready=1;
  63.         if(stopped){
  64.             TI0=1;
  65.             stopped=0;
  66.         }
  67.         RI0=0;
  68.     }
  69.     if(TI0==1){
  70.         if(charready){
  71.             SBUF0=character;
  72.             charready=0;
  73.         }else{
  74.             stopped=1;
  75.         }
  76.         TI0=0;
  77.     }
  78. }
  79. //*******************************************************
  80. // main
  81. //*******************************************************
  82. void main(){
  83.     initializeUART();
  84.     for (; / {
  85.     // count++;
  86.     // if (!count)
  87.     // LED = ~LED;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement