- 1. uart.h
- 2. uart.c
使用软件:IAR FOR STM8
编程方式:固件库
硬件配套:STM8S105K4T6最小系统板
注意事项:需在IAR工程配置中:在 General Options->Library Configuration->Library 选择 Full;在 General Options->Library Options->Printf formatter 选择 Large,Math functions 选择 Default;另外不能串口打印小数,需进一步处理数据
这是我在做课设时参考的代码,现记录如下:
1. uart.h#ifndef __UART_H #define __UART_H #include "stm8s.h" #include "stdio.h" #ifdef _RAISONANCE_ #define PUTCHAR_PROTOTYPE int putchar (char c) #define GETCHAR_PROTOTYPE int getchar (void) #elif defined (_COSMIC_) #define PUTCHAR_PROTOTYPE char putchar (char c) #define GETCHAR_PROTOTYPE char getchar (void) #else #define PUTCHAR_PROTOTYPE int putchar (int c) #define GETCHAR_PROTOTYPE int getchar (void) #endif void UART2_Config(void); void UART2_SendByte(u8 data); void UART2_SendStr(const unsigned char *p_str); void Delay(uint32_t nCount); #endif2. uart.c
#include "UART.h" void UART2_Config(void) { CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART2, ENABLE); UART2_DeInit(); UART2_Init((u32)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO , UART2_SYNCMODE_CLOCK_DISABLE , UART2_MODE_TXRX_ENABLE); UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE); UART2_Cmd(ENABLE); } void Delay(uint32_t nCount) { while (nCount != 0) { nCount--; } } void UART2_SendByte(u8 data) { UART2_SendData8((unsigned char)data); while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); } void UART2_SendStr(const unsigned char *p_str) { // u8 i = 0; while(*p_str != '') { UART2_SendByte(*p_str); // Delay(1000); p_str++; } } PUTCHAR_PROTOTYPE { UART2_SendData8(c); while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); return (c); } GETCHAR_PROTOTYPE { #ifdef _COSMIC_ char c = 0; #else int c = 0; #endif while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET); c = UART2_ReceiveData8(); return (c); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)