#include <reg51.h>
#include <string.h>
#define INBUF_LEN 4 //數據長度
unsigned char ch;
// unsigned char checksum,count3;
bit read_flag= 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 |= 0x80 ; //SMOD=1;
TH1 = 0xFA ; //Baud:9600 fosc=11.0592MHz
IE |= 0x90 ; //Enable Serial Interrupt
TR1 = 1 ; // timer 1 run
TI=1;
}
//向串口發送一個字符
void send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0);
TI= 0 ;
}
/*
//向串口發送一個字符串,strlen為該字符串長度
void send_string_com( unsigned char *str, unsigned int strlen)
{
unsigned int k= 0 ;
do { send_char_com(*(str + k)); k++; }
while (k < strlen);
}
*/
//串口接收中斷函數
void serial () interrupt 4 using 3
{
if (RI)
{
RI = 0 ;
ch=SBUF;
read_flag= 1 ; //就置位取數標志
}
}
main()
{
init_serialcom(); //初始化串口
while ( 1 )
{
if (read_flag) //如果取數標志已置位,就將讀到的數從串口發出
{
read_flag= 0 ; //取數標志清0
send_char_com(ch);
}
}
}
|