训练001、单片机与PC机通信,11.0592M晶振,波特率9600。实现任意字符发送。
#include <reg52.h> bit busy; void SendData(unsigned char dat) { while(busy);//判断是否忙 busy =1; SBUF = dat; } void SendString(char *s) { while (*s) //Check the end of the string { SendData(*s++); //Send current char and increment string ptr } } void main() { SCON = 0x40; //配置工作方式1,禁止接收 PCON |= 0x80; //SMOD=1,波特率加倍 TMOD = 0x20; //T1工作于方式2,定时方式 TH1 = 0xfa; //定时器装初值 TL1 = 0xfa; TR1 = 1; //开定时器1 ES = 1; //开串口中断 EA = 1; //开总中断 SendString("STC89C52RC\r\nUart Test !\r\n"); while(1); } void uart() interrupt 4 { if(TI) { TI = 0; busy = 0; } }
训练002、片机与PC机通信,11.0592M晶振,波特率9600。实现任意字符发送。并且可以通过串行调试助手接收端返回显示(SCON=0x50允许接收),数码管显示出来对应数字
#include <reg52.h> unsigned char Tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92, 0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //共阳极8段 //#define FOSC 11059200L //System frequency //#define BAUD 9600 //UART baudrate //TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule char uart_data; void main (void) { SCON = 0x50; /* uart in mode 1 (8 bit), REN=1 */ TMOD = TMOD | 0x20 ; /* Timer 1 in mode 2 */ TH1 = 0xFD; /* 9600 Bds at 11.059MHz */ TL1 = 0xFD; /* 9600 Bds at 11.059MHz */ ES = 1; /* Enable serial interrupt*/ EA = 1; /* Enable global interrupt */ TR1 = 1; /* Timer 1 run */ while(1); /* endless */ } void serial_IT(void) interrupt 4 { if (RI == 1) { /* if reception occur */ RI = 0; /* clear reception flag for next reception */ uart_data = SBUF; /* Read receive data */ P2 = Tab[uart_data]; SBUF = uart_data; /* Send back same data on uart*/ } else TI = 0; /* if emission occur */ } /* clear emission flag for next emission*/
训练003、单片机与PC机通信,11.0592M晶振,波特率9600。实现任意字符发送。不开中断
#include <reg52.h> unsigned char Tab[]={'1','2','3','4','5','o','k'};//定义了向PC机发送的数组 unsigned char i; void main() { SCON = 0x40; //配置工作方式1,禁止接收 PCON |= 0x80; //SMOD=1,波特率加倍 TMOD = 0x20; //T1工作于方式2,定时方式 TH1 = 0xfa; //定时器装初值 TL1 = 0xfa; TR1 = 1; //开定时器1 while(1) { for(i=0;i<7;i++) { SBUF = Tab[i]; while(!TI);//等待发送完 TI=0; //清除发送结束标志 } SBUF= '\r' ; //回车 while(!TI); //等待发送完 TI=0; //清除发送结束标志 // SBUF= '\n' ; //换行 // while(!TI); //等待发送完 // TI=0; //清除发送结束标志 } }
训练004、实现单片机与PC机通信,11.0592M晶振,波特率9600。实现;两个功能。
1、单片机按键按下,向PC机发送4组1–5五个数,共20
2、PC机控制单片机LED灯的亮灭。
标签:PC机,9600,uart,void,UAV2101,char,串行,波特率,2105 From: https://www.cnblogs.com/yilangUAV/p/17923799.html