STC实验箱4 IAP15W4K58S4 Keil uVision V5.29.0.0 PK51 Prof.Developers Kit Version:9.60.0.0
硬知识
摘自《STC库函数使用参考》
串口初始化函数USART_Configuration COMx_InitDefine的定义见于文件"USART.H"。
typedef struct{u8 UART_Mode; u8 UART_BRT_Use; u32 UART_BaudRate; u8 Morecommunicate; u8 UART_RxEnable; u8 BaudRateDouble;u8 UART_Interrupt; u8 UART_Polity; u8 UART_P_SW; u8 UART_RXD_TXD_Short;} COMx_InitDefine;UART_Mode:设置USART的工作模式: UART_BRT_Use:使用的波特率发生器: UART_BaudRate:使用的波特率,比如:
COMx_InitStructure.UART_BaudRate = 115200ul; //UL表示是unsigned longMorecommunicate:多机通讯允许: UART_RxEnable:接收允许: BaudRateDouble:波特率加倍(仅仅用于USART1): UART_Interrupt:中断允许或禁止: UART_Polity:中断的优先级: UART_P_SW:切换IO:对于串口1的取值: UART_P_SW:切换IO:对于串口2的取值: UART_RXD_TXD_Short:内部TXD与RXD同相缓冲输出做中继:对于串口1的取值:
串口1写缓冲函数TX1_write2buff
串口2写缓冲函数TX2_write2buff
串口1写数据块函数PrintString1
串口2写数据块函数PrintString2
模拟串口字节发送函数TxSend
模拟串口写数据块函数PrintString
测试硬件串口 #include "./Drivers/config.h"#include "./Drivers/delay.h"#include "./Drivers/USART.h"#include <stdio.h>char putchar(char Char){TX1_write2buff(Char);return Char;}void UART_config(void){COMx_InitDefine COMx_InitStructure;//结构定义COMx_InitStructure.UART_Mode = UART_8bit_BRTx;//模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTxCOMx_InitStructure.UART_BRT_Use = BRT_Timer1;//使用波特率, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)COMx_InitStructure.UART_BaudRate = 115200ul;//波特率, 一般 110 ~ 115200COMx_InitStructure.UART_RxEnable = ENABLE;//接收允许, ENABLE或DISABLECOMx_InitStructure.BaudRateDouble = DISABLE;//波特率加倍, ENABLE或DISABLECOMx_InitStructure.UART_Interrupt = ENABLE;//中断允许, ENABLE或DISABLECOMx_InitStructure.UART_Polity = PolityLow;//中断优先级, PolityLow,PolityHighCOMx_InitStructure.UART_P_SW = UART1_SW_P30_P31;//切换端口, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17(必须使用内部时钟)COMx_InitStructure.UART_RXD_TXD_Short = DISABLE;//内部短路RXD与TXD, 做中继, ENABLE,DISABLEUSART_Configuration(USART1, &COMx_InitStructure);//初始化串口1 USART1,USART2}void main(void){u8 i;UART_config();EA = 1;PrintString1("IAP15W4K58S4 UART1 Test Prgramme!\r\n");//USART1发送一个字符串printf("printf测试:%d\r\n", (short)1);while (1){delay_ms(1);if(COM1.RX_TimeOut > 0)//超时计数{if(--COM1.RX_TimeOut == 0){if(COM1.RX_Cnt > 0){for(i=0; i<COM1.RX_Cnt; i++)TX1_write2buff(RX1_Buffer[i]);//收到的数据原样返回}PrintString1(RX1_Buffer);//收到的数据原样返回 另一种形式COM1.RX_Cnt = 0;}}}} 模拟串口
在soft_uart.c中修改所用引脚:
soft_uart.c /*************功能说明**************本文件为模拟串口发送程序, 一般为测试监控用.串口参数:9600,8,n,1.可以根据主时钟自动适应.******************************************/#include"soft_uart.h"sbitP_TXD = P3^1;//定义模拟串口发送端,可以是任意IOchar putchar(char Char){TxSend(Char);return Char;}//========================================================================// 函数: voidBitTime(void)// 描述: 位时间函数。// 参数: none.// 返回: none.// 版本: VER1.0// 日期: 2013-4-1// 备注: //========================================================================voidBitTime(void){u16 i;i = ((MAIN_Fosc / SOFT_UART_BAUD) * 1000) / 13000L - 1;//根据主时钟来计算位时间while(--i);}//========================================================================// 函数: voidTxSend(uchar dat)// 描述: 模拟串口发送一个字节。9600,N,8,1// 参数: dat: 要发送的数据字节.// 返回: none.// 版本: VER1.0// 日期: 2013-4-1// 备注: //========================================================================voidTxSend(u8 dat){u8i;EA = 0;P_TXD = 0;BitTime();for(i=0; i<8; i++){if(dat & 1)P_TXD = 1;elseP_TXD = 0;dat >>= 1;BitTime();}P_TXD = 1;EA = 1;BitTime();BitTime();}//========================================================================// 函数: void PrintString(unsigned char code *puts)// 描述: 模拟串口发送一串字符串。9600,N,8,1// 参数: *puts: 要发送的字符指针.// 返回: none.// 版本: VER1.0// 日期: 2013-4-1// 备注: //========================================================================void PrintString(unsigned char code *puts){for (; *puts != 0;puts++) TxSend(*puts);} soft_uart.h #ifndef__SOFT_UART_H#define__SOFT_UART_H#include "config.h"#include <stdio.h>#define SOFT_UART_BAUD 9600voidTxSend(u8 dat);void PrintString(unsigned char code *puts);#endif main.c #include "./Drivers/config.h"#include "./Drivers/delay.h"#include "./Drivers/soft_UART.h"void main(void){while (1){PrintString("IAP15W4K58S4 Soft UART Test Prgramme!\r\n");printf("printf测试:%d\r\n", (short)1);delay_ms(500);}}