串口波特率發(fā)送數(shù)據(jù)程序
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned int uint16;
uint8 num = 0;
void delay(uint16 n) //延時(shí)函數(shù)
{
while(n--);
}
void delay(uint16 n) //延時(shí)函數(shù)
{
while(n--);
}
void UART_init(void)
{
SCON = 0x50; //串口控制寄存器在方式1條件下,M1 = 1;M0 = 0 REN = 1,打開允許串口接收數(shù)
TMOD = 0x20; //將定時(shí)器1置為自動(dòng)重裝模式。即方式2
TH1 = 0xFD; //TH1 = TL1 = 256 - 11059200/(12 * 32 * 9600) (12為12個(gè)時(shí)鐘周期)
TL1 = 0xFD;
TR1 = 1; //允許串口接收數(shù)據(jù)
}
void UART_send_byte(uint8 dat)
{
SBUF = dat;
while(!TI); //發(fā)送停止位時(shí)由硬件置1必須軟件清零
TI = 0;
}
main()
{
UART_init(); //串口初始化
while(1)
{
UART_send_byte(num++);
delay(20000);
}
}
{
SCON = 0x50; //串口控制寄存器在方式1條件下,M1 = 1;M0 = 0 REN = 1,打開允許串口接收數(shù)
TMOD = 0x20; //將定時(shí)器1置為自動(dòng)重裝模式。即方式2
TH1 = 0xFD; //TH1 = TL1 = 256 - 11059200/(12 * 32 * 9600) (12為12個(gè)時(shí)鐘周期)
TL1 = 0xFD;
TR1 = 1; //允許串口接收數(shù)據(jù)
}
void UART_send_byte(uint8 dat)
{
SBUF = dat;
while(!TI); //發(fā)送停止位時(shí)由硬件置1必須軟件清零
TI = 0;
}
main()
{
UART_init(); //串口初始化
while(1)
{
UART_send_byte(num++);
delay(20000);
}
}
評(píng)論