STM8 波特率
STM8的串口波特用115200就乱码;我还是用的外部16晶振,求助帮顶 把代码发上来看看 一定是代码的问题,我用115200用的好好的,可以贴上你的代码看看 同问.... #include "uart.h"
unsigned char USART2_RX_BUF={0}; //接收缓冲,最大USART2_MAX_RECV_LEN个字节. 串口接收缓存区
unsigned char USART2_RX_STA=0;
unsigned char Uart2Time=0; //uart2接收数据
/*****************************************************************************
Prototype : Init_UART2
Description: 串口初始化函数
Input : 波特率
Output : None
Return Value : void
Calls :
Called By :
*****************************************************************************/
void Init_UART2(unsigned long baud)
{
unsigned long tmpData = SYS_UART_CLOCK / baud;
UART1_CR1=0x00;
UART1_CR2=0x00;
UART1_CR3=0x00;
UART1_BRR1 = (unsigned char)((tmpData >> 4) & 0x0ff);
UART1_BRR2 = (unsigned char)((tmpData & 0x00f) | ((tmpData >> 8) & 0x0f0)); //设置波特率
UART1_CR2=0x2c;//允许接收,发送,开接收中断
UART1_CR4 = 0x00;
}
/*****************************************************************************
Prototype : UART2_sendchar
Description: 串口发送一个字符
Input : sendData
Output : None
Return Value : void
Calls :
Called By :
*****************************************************************************/
void UART2_sendchar(unsigned char sendData)
{
CLEARBIT(UART1_SR, 6);
UART1_DR = sendData;
while(!CHECKBIT(UART1_SR, 6))
{
}
}
/*****************************************************************************
Prototype : UART2_sendStr
Description: 串口发送一个字符串
Input : sendData,length
Output : 发送出去的数据长度
Return Value : void
Calls :
Called By :
*****************************************************************************/
unsigned char UART2_sendStr(unsigned char* sendStr,unsigned char length)
{
unsigned char iLoop = 0;
if((sendStr != NULL)&&(length >0))
{
for(iLoop = 0; iLoop < length; iLoop++)
{
UART2_sendchar(*(sendStr + iLoop));
}
}
else{ }
return iLoop;
}
/*****************************************************************************
Prototype : UART2_R_OR_IRQHandler
Description: 串口中断接受函数
Input : void
Output : none
Return Value : void
Calls :
Called By :
*****************************************************************************/
#pragma vector= UART1_R_OR_vector//0x19
__interrupt void UART1_R_OR_IRQHandler(void)
{
//if(USART2_RECV_END==0) //当前可以接收数据
//{
unsigned char temp;
temp=UART1_DR;
OpenTimer2();
//UART2_sendchar(temp);
if(USART2_RX_STA<USART2_MAX_RECV_LEN)
{
USART2_RX_BUF=temp;
}
else
{
USART2_RECV_END=1;//接收数据超过缓存区 强制标记接收完成 丢弃多余的数据
USART2_RX_STA=0;
CloseTimer2();//关闭TIM2
}
//}
} ughbss 发表于 2015-10-14 09:27
把代码发上来看看
代码发出来了,6楼 ughbss 发表于 2015-10-14 09:27
把代码发上来看看
代码发出来了,6楼 刚声打火机 发表于 2015-10-14 10:10
一定是代码的问题,我用115200用的好好的,可以贴上你的代码看看
代码贴出来了,6楼;SYS_UART_CLOCK=16000000 你用的那个型号的STM8
页:
[1]
2