#include"stm8s.h"
#include"stm8s_UART1.h"
#include"stm8s_clk.h"
#include"stm8s_conf.h"
#include"iostm8s003k3.h"
typedef unsigned char uint8;
typedef unsigned char uint16;
uint8 num=0;
/************************************************************************/
- 功能:时钟配置初始化
- 形参:无
-
返回:无
/************************************************************************/
void CLK_Init(void)
{/*对于内部高速时钟HSI[16MHz],其逻辑关系是HSI---[HSIDIV分频]---[时钟选择门]---
-
主时钟Fmaster---{[CPUDIV分频]---Fcpu时钟;其他外设时钟}
*/
CLK_ICKR = 0x01; //Internal clock control register。复位值,使能HSI时钟,快速唤醒禁止
CLK_ECKR = 0x00; //External clock control register。HSE禁止。
CLK_SWR = 0xE1; //Clock master switch register。选择HSI为主时钟。
CLK_SWCR = 0x00; //Clock switch control register。时钟切换,中文数据手册翻译似乎有误/*Clock divider register。分频Fmaster和Fcpu。
默认是8和1,现在是2和1,因此Fmaster=8M,Fcpu=8M */
CLK_CKDIVR = 0x08;
CLK_PCKENR1 = 0xFF; //Peripheral clock gating register 1。Fmaster与外设时钟连接
CLK_PCKENR2 = 0x0; //同上
CLK_CSSR = 0x0; //Clock security system register。时钟安全检测关
CLK_CCOR = 0x0; //Configurable clock control register。时钟输出关闭
}
//****************************************/
// 函数名称:Init_Uart1
// 函数功能:串口寄存器初始化
// 入口参数:波特率值
// 出口参数:无
// 返 回 值:无
//***************************************/
void Uart1_Init(uint16 Tcon)
{
uint8 Tmp1 = 0 ;
uint8 Tmp2 = 0 ;//禁止UART发送和接收
UART1_CR2 =0 ;
//M=0 8个数据位 b2=0,禁止校验 b5=0,UART使能
UART1_CR1 = 0 ;
UART1_CR3 = 0 ; // b5 b4 = 00 ,一个停止位//波特率设置,先写UART_BRR2 再写UART_BRR1
// F = 9600, 分频为:16M/9600 = 1666.7 -> 0X0683
Tmp1 = (uint8)((Tcon >> 4) & 0x00ff) ;
Tmp2 = (uint8)((Tcon & 0x000f)|((Tcon >> 8) & 0x00f0));
UART1_BRR2 = Tmp2;
UART1_BRR1 = Tmp1 ;//允许发送 允许接收 接收中断使能
× UART1_CR2 |= (REN | TEN | RIEN);}
main()
{
CLK_Init();
· UART1_init();
while(1);}
//***************************************/
// 函数名称:UartSendByte
// 函数功能:串口发送一字节数据
// 入口参数:要发送的数据
// 出口参数:无
// 返 回 值:无
//***************************************/
void UartSendByte(uint8 num)
{
× while(!(UART1_SR & TXE));//发送数据寄存器为非空 等待 .
UART1_DR =num ;
}//*************************************************************/
// 函数名称:Uart1ReceByte
// 函数功能:串口接收一字节数据
// 入口参数:无
// 出口参数:接收到的数据
// 返 回 值:返回是否接收到数据 接收到数据返回1,未接到返回0
//*************************************************************/
void Uart1ReceByte(uint8 num)
{
× if((UART1_SR & RXNE) != 0);//读数据寄存器为非空 说明有数据进来
{
× UART1_SR &= ~RXNE ;
num = UART1_DR ;
num++;
UART1_DR =num ;
× REN=0;
}
} -
(×:表示有错的地方; · :表示警告)
编译结果:
Building configuration: mbv - Debug
Updating build tree...
main.c
Warning[Pe009]: nested comment is not allowed I:\我的项目\项目1\项目内容\MBV\main.c 17
Error[Pe020]: identifier "REN" is undefined I:\我的项目\项目1\项目内容\MBV\main.c 65
Error[Pe020]: identifier "TEN" is undefined I:\我的项目\项目1\项目内容\MBV\main.c 65
Error[Pe020]: identifier "RIEN" is undefined I:\我的项目\项目1\项目内容\MBV\main.c 65
Warning[Pe223]: function "UART1_init" declared implicitly I:\我的项目\项目1\项目内容\MBV\main.c 72
Error[Pe020]: identifier "TXE" is undefined I:\我的项目\项目1\项目内容\MBV\main.c 87
Error[Pe020]: identifier "RXNE" is undefined I:\我的项目\项目1\项目内容\MBV\main.c 100
Error[Pe020]: identifier "RXNE" is undefined I:\我的项目\项目1\项目内容\MBV\main.c 102
Error[Pe020]: identifier "REN" is undefined I:\我的项目\项目1\项目内容\MBV\main.c 106
Error while running C/C++ Compiler
Total number of errors: 8
Total number of warnings: 2