STC15 串口一 接收 发送 基本程序
2019-02-09 16:35
281 查看
使用串口一方便通过串口助手测试某些功能
//textUart1.c #include <STC15F2Kxx.H> #include "textUart1.h" char UAR[20],temp,rec_flag=0; static void Uart1_Init(void) //@11.0592MHz { EA=0; ACC = P_SW1; · ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=0 P_SW1 = ACC; //(P3.0/RxD, P3.1/TxD) AUXR |= 0x40; //定时器1为1T模式 AUXR &= 0xFE; //选择定时器1为波特率发生器 TMOD = 0x00; //定时器1为模式0(16位自动重载) TL1 = (65536 - (FOSC/4/BAUD)); //设置波特率重装值 TH1 = (65536 - (FOSC/4/BAUD))>>8; SCON = 0x50; //8位可变波特率 TR1 = 1; //定时器1开始启动 ES = 1; EA=1; } /***************************************************************************/ void send1_Byte(unsigned char c) { SBUF = c; while(!TI); //发送完会自动置1 TI=0; } /*---------------------------- 发送字符串 ----------------------------*/ void Send1_String(char *s) { while (*s){ //检测字符串结束标志 send1_Byte(*s++); } } /************************************************************* 串口接收到的数据存入UAR数组中,当接收到0x0D时表示数据接收结束 **************************************************************/ void UART1_Interrupt() interrupt 4 //串口中断函数接收程序 { static unsigned char i; if (RI){ RI=0; temp = SBUF; UAR[i]=temp;//接收到的数据存入数组UAR中 i++; if(temp==0x0D){ i=0; rec_flag=1;//定义接收完成标志位 } } } /***********************************************************************/ void UART1_config() { Uart1_Init();//串口一初始化 Send1_String("STC15F4K48S4\r\nUart is ok !\r\n");//发送字符串检测是否初始化成功 } /*******************************************************************************/ void uart1_printf(const char *fmt,...) //串口打印函数 { va_list ap; char xdata string[500]; va_start(ap,fmt); vsprintf(string,fmt,ap); Send1_String(string); va_end(ap); }
//textUart1.h #ifndef __textUart1_H__ #define __textUart1_H__ #include <STC15F2Kxx.H> #include "stdio.h" #include <stdarg.h> #include <string.h> #define S1_S0 0x40 #define S1_S1 0x80 #define FOSC 11059200 //系统时钟 #define BAUD 115200 //波特率设定115200,也可设置9600 extern char UAR[20],rec_flag;//全局量 void send1_Byte(unsigned char c); void Send1_String(char *s); void UART1_config(); void uart1_printf(const char *fmt,...); #endif
主函数 #include <STC15F2Kxx.H> #include "textUart1.h" void main(void) { P3M0 = 0X00; //配置端口工作模式(有推挽模式,准双向口工作模式等) P3M1 = 0X00; UART1_config(); //串口1初始化 uart1_printf("UART1 is good! \r\n"); while(1); }
效果:
相关文章推荐
- 51单片机串口程序,字符串/16进制发送与接收
- 串口通信程序中十六进制格式发送和接收实现
- 写一个串口接收发送程序
- keil编译的程序 “汉字显示不全、串口发送汉字接收后乱码缺失”问题
- [15单片机] STC15F104W开发入门及模拟串口程序
- 单片机IO口模拟串口程序(发送+接收 )
- stc不断用串口向外发送数据,如何下载程序
- ht46ru232 串口接收、发送程序 C语言
- PIC24串口发送和接收程序
- 单片机IO口模拟串口程序(发送+接收
- 第一次创建STC15串口程序模板
- C51串口中断接收和发送程序
- MFC串口通信设置及发送、中断接收程序
- STM32的接收中断和发送中断同时使能会导致程序死在初始化串口那里。
- 一个简易的51单片机串口接收和发送程序
- ATMEGA16串口发送和中断接收CVAVR程序
- STC51单片机串口发送程序
- stm32应用-简单的串口接收与发送程序
- 串口通信程序中十六进制格式发送和接收实现
- 串口通信程序中十六进制格式发送和接收实现