STM8S105K4 UART问题
时间:10-02
整理:3721RD
点击:
我用STM8S105K4写 UART程序, 单个UART 程序 串口发送没问题, 但是 和ADC ,中断,等一些程序整合成一个程序时 ,发送的就是乱码。大师们是怎么回事?
单个程序:
#include"stm8s105k4.h"
#include"stdio.h"
void CLK_Init(void)
{
CLK_CKdivR = 0x00;
}
void UART2_Init(void)
{
UART2_CR3 = 0x00;
UART2_CR2 = 0x00;
UART2_CR3 = 0x00;
UART2_BRR2 = 0x0b; //115200
UART2_BRR1 = 0x08;
}
char putchar(char c)
{
while(!(UART2_SR&0x40));
UART2_DR = c;
while(!(UART2_SR&0x40));
return (c);
}
main()
{
unsigned char c;
CLK_Init();
UART2_Init();
UART2_CR2 = 0x0c;
printf("1");
/*while(1)
{
printf("\n\r请输入一个按键");
while(!(UART2_SR&0x20));
c = UART2_DR;
printf("\n\r输入按键为%c。",c);
}*/
}
整合后的程序:
#include "stm8s105k4.h"
#include "stdio.h"
#include "delay.h"
#include "smg.h"
#include "adc.h"
#include "interrupt.h"
#include "uart.h"
void CLK_Init(void)
{
CLK_CKdivR |= 0x00; //时钟初始化为内部16M
}
main()
{
_asm("sim"); //对中断进行初始化需要加 头汇编语句
CLK_Init(); //时钟初始化 16M
GPIO_SMG_IN(); //数码管IO初始化
TIM1_Init(); //中断初始化
ADC_Init(); //ADC初始化
UART2_Init(); //串口初始化
_asm("rim"); //对中断进行初始化需要加 尾汇编语句
delayms(1000);
printf("");
}
单个程序:
#include"stm8s105k4.h"
#include"stdio.h"
void CLK_Init(void)
{
CLK_CKdivR = 0x00;
}
void UART2_Init(void)
{
UART2_CR3 = 0x00;
UART2_CR2 = 0x00;
UART2_CR3 = 0x00;
UART2_BRR2 = 0x0b; //115200
UART2_BRR1 = 0x08;
}
char putchar(char c)
{
while(!(UART2_SR&0x40));
UART2_DR = c;
while(!(UART2_SR&0x40));
return (c);
}
main()
{
unsigned char c;
CLK_Init();
UART2_Init();
UART2_CR2 = 0x0c;
printf("1");
/*while(1)
{
printf("\n\r请输入一个按键");
while(!(UART2_SR&0x20));
c = UART2_DR;
printf("\n\r输入按键为%c。",c);
}*/
}
整合后的程序:
#include "stm8s105k4.h"
#include "stdio.h"
#include "delay.h"
#include "smg.h"
#include "adc.h"
#include "interrupt.h"
#include "uart.h"
void CLK_Init(void)
{
CLK_CKdivR |= 0x00; //时钟初始化为内部16M
}
main()
{
_asm("sim"); //对中断进行初始化需要加 头汇编语句
CLK_Init(); //时钟初始化 16M
GPIO_SMG_IN(); //数码管IO初始化
TIM1_Init(); //中断初始化
ADC_Init(); //ADC初始化
UART2_Init(); //串口初始化
_asm("rim"); //对中断进行初始化需要加 尾汇编语句
delayms(1000);
printf("");
}