|
想单片机接收一个PC发送过来的指定字符(例如's'),LED就亮,否则LED灭,字符接收发送正常,但是LED就是没有反应,请教...新手...呜呜!
- #include
- #include "serial2.h"
- bit recFlag=0;
- uchar ch ;
- sbit D1= P1^7;
- bit led_on=0;
- void init_serialcom( void )
- {
- SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
- TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
- PCON &= 0xef ; //SMOD=0;
- TH1 = 0xFD ; //Baud:19200 fosc=11.0592MHz
- IE |= 0x90 ; //Enable Serial Interrupt
- TR1 = 1 ; // timer 1 run
- TI=1;
- }
- //向串口发送一个字符
- void send_char_com( uchar ch)
- {
-
- SBUF=ch;
- while (TI== 0);
- TI= 0 ;
- }
- //串口接收中断函数
- void serial () interrupt 4 using 3
- {
- EA = 0;
- if (RI)
- {
- RI = 0 ;
-
- ch = SBUF;
- recFlag= 1 ; //就置位取数标志
- }
- if(ch=='s')
- {
- led_on=1;
- }
- EA = 1;
- }
- main()
- { D1=1;
- init_serialcom(); //初始化串口
- while ( 1 )
- {
- if( recFlag )
- {
- recFlag=0;
- send_char_com( ch );
-
- }
- if(led_on)
- {
- while(1)
- {
- D1=0;
- }
- }
- }
- }
复制代码
|
|