使用STM8S003的UART1作为LIN主模式,配置之后,低电平输出异常,代码如下,请大侠们看下
/*******************************************************************************
**函数名称:LinMasterInit()
**功能描述:Uart Lin Master Mode Initial
**入口参数:unsigned int baudrate Uart波特率
**输出:无
*******************************************************************************/
void LinMasterInit(unsigned int baudrate)
{
unsigned int baud; //
UART1_CR1_bit.M = 0; //set word length
UART1_CR3_bit.STOP = 0; //set stop bit num.
UART1_CR3_bit.CKEN = 0;
UART1_CR5_bit.SCEN = 0;
UART1_CR5_bit.HDSEL = 0;
UART1_CR5_bit.IREN = 0;
baud = 16000000 / baudrate; //设定串口相应波特率与串口时钟的分频数
//先写BRR2 , 再写BRR1
UART1_BRR2 = ((unsigned char)((baud & 0xf000) >> 8 )) | ((unsigned char)(baud & 0x000f)); //先写波特比率的高4位与低4位
UART1_BRR1 = ((unsigned char)((baud & 0x0ff0) >> 4)); //后写波特比率的中间8位;
UART1_CR3_bit.LINEN = 1; //enable LIN mode
UART1_CR2_bit.TEN = 1; //enable transmit
UART1_CR2_bit.REN = 1; //enable receive
}
/* 主函数 */
int main(void)
{
CLK_CKDIVR = 0x00; //内部时钟为1分频 = 16Mhz
LinMasterInit(19200); //调用LED灯初始化函数,
while(1)
{
UART1_CR2_bit.SBK = 1;
while(!(UART1_SR&0X80));
UART1_DR = 0x01;
while(!UART1_SR_bit.TC);
while(!(UART1_SR&0X80));
UART1_DR = 0x00;
while(!UART1_SR_bit.TC);
delay(300);
}
} |