STM8学习笔记---串口uart1
使用uart1串口,需要用到stm8s_uart1.c和stm8s_uart1.h兩個文件
1、建立工程目錄結構如下:
2、編寫uart.h文件如下:
#ifndef __UART_H
#define?__UART_H
#include "stm8s.h"
#include "stm8s_clk.h"
void USART_Configuration(void); ? //串口配置函數
void UART_send_string(uint8_t *Buffer);//發送一個字符串函數
#endif /* __UART_H */
3、編寫uart.c文件如下:
#include "uart.h"
void USART_Configuration(void)//串口初始化函數
? { ?
? ? UART1_DeInit(); //清除之前的串口配置
? ? UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, \
? ? UART1_PARITY_NO , UART1_SYNCMODE_CLOCK_DISABLE , UART1_MODE_TXRX_ENABLE);
? ? //串口配置:波特率115200,字節數8,1個停止位,無奇偶效驗位,非同步模式,允許接受和發送
??
? ? UART1_Cmd(ENABLE ); ?//啟用串口
? ?}
void UART_send_string(uint8_t *Buffer) //發送一個字符
? ? {
? ? ? ?uint8_t *String;
? ? ? ? String=Buffer;
? ? ? ? while(*String!='\0')
? ? ? ?{
? ? ? ? ? UART1_SendData8(*String);
? ? ? ? ? while (UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET);
? ? ? ? ? String++;
? ? ? ? }
? ? }
4、編寫主函數如下:
#include "stm8s.h"
#include "stm8s_clk.h"
#include "uart.h"?
? ?
static void delay (int cnt)?
{
? while (cnt--);
}
int main(void)
{?
? CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
? ?
? USART_Configuration();//串口配置
? while (1)
? {
? ? ?UART_send_string("LIKE");
? ? ?UART1_SendData8('\n');
? ? ?while (UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET);
delay(30000);
? ? ? ? ? ? ? ? delay(30000);
? ? ? ? ? ? ? ? delay(30000);
? }
}
運行結果:
總結
以上是生活随笔為你收集整理的STM8学习笔记---串口uart1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm8s串口奇偶校验学习笔记
- 下一篇: stm8s003 8K空间不够用,出现报