当前位置:首页 > STM8串口1主程序(库文件)
/*如有疑问请寻4 玖 6 2 7 叁 1 0 9 -------提问TD*/
#include \#include %u8 Res;
/*简单的延时子程序*/ void delay(u16 z) {
u16 x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--); }
/*修改后的发送一个字节数据*/
void UART1_sendchar(unsigned char c) {
UART1_SendData8(c);
while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET); }
/*程序中要使用printf必须加上此段程序*/ #define PUTCHAR_PROTOTYPE int putchar (int c) PUTCHAR_PROTOTYPE {
/* 发送一个字符 c 到UART1 */ UART1_sendchar(c);
/* 等待发送完毕 */
while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET); return (c); }
void main(void) {
/*串口引脚初始化,否则上电之后串口发出乱码*/
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_FAST); GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT); /*对应串口所有寄存器初始化,好的习惯*/ UART1_DeInit();
/*根据STM8代码生成工具生成的相应串口初始化代码,不罗嗦*/
UART1_Init((uint32_t)115200,UART1_WORDLENGTH_8D,UART1_STOPBITS_1,UART1_PARITY_NO,UART1_SYNCMODE_CLOCK_DISABLE,UART1_MODE_TXRX_ENABLE); UART1_ITConfig(UART1_IT_RXNE_OR,ENABLE); UART1_Cmd(ENABLE); /*开中断,(“_rim_()”)*/ enableInterrupts();
while (1) {
/*打印字符 \\换行\\回车*/ printf(\ delay(3000); } }
/*中断接收代码,在主程序中使用,需将stm8s_it.c中对应程序屏蔽,否则报错*/ INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18) {
if(UART1_GetITStatus(UART1_IT_RXNE )!= RESET) {
Res =UART1_ReceiveData8(); UART1_sendchar(Res); } }
/*后面的程序不得修改*/ #ifdef USE_FULL_ASSERT /**
* @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name
* @param line: assert_param error line source number * @retval : None */
void assert_failed(u8* file, u32 line) {
/* User can add his own implementation to report the file name and line number, ex: printf(\
/* Infinite loop */ while (1) { } }
#endif
共分享92篇相关文档