STM8學習筆記
使用uart1串口,需要用到stm8s_uart1.c和stm8s_uart1.h兩個文件
1、建立工程目錄結搆如下:
2、編寫uart.h文件如下:
#ifndef __UART_H
#define __UART_H
#include"stm8s.h"
#include"stm8s_clk.h"
voidUSART_Configuration(void); //串口配置函數
void UART_send_string(uint8_t *Buffer);//發送一個字符串函數
#endif
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);
}
}
運行結果:
0條評論