#include<iostm8l152c6.h>
void UART1_Init(unsigned int baudrate)
{
unsigned int baud; //
CLK_PCKENR1_bit.PCKEN15 = 1; //使能USART模块时钟
baud = 16000000 / baudrate; //设定串口相应波特率与串口时钟的分频数
//先写BRR2 , 再写BRR1
USART1_BRR2 = ((unsigned char)((baud & 0xf000) >> 8 )) | ((unsigned char)(baud & 0x000f)); //先写波特比率的高4位与低4位
USART1_BRR1 = ((unsigned char)((baud & 0x0ff0) >> 4)); //后写波特比率的中间8位
USART1_CR1_bit.USARTD = 0; //使能UART0
USART1_CR2_bit.RIEN = 0; //使能中断接收
USART1_CR2_bit.TEN = 1; //使能发送
USART1_CR2_bit.REN = 0; //接收使能
}
void Uart_SendData(unsigned char data)
{
while(!(USART1_SR&0X80)); //判断发送数据寄存器是否为空
USART1_DR = data; //向发送寄存器写入数据
}
int main(void)
{
CLK_CKDIVR = 0x00;
UART1_Init(9600);
while(1)
{
Uart_SendData(0xff);
}
}
这是程序,下载后串口助手没有收到数据是什么原因?
也找了两个人看,都没看出问题来,所以来论坛找找大神,请大神给予指点!谢谢了!!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include<iostm8l152k4.h>
void UART1_Init(unsigned int baudrate)
{
unsigned int baud; //
CLK_PCKENR1_bit.PCKEN15 = 1;
baud = 16000000 / baudrate;
USART1_BRR2 = ((unsigned char)((baud & 0xf000) >> 8 )) | ((unsigned char)(baud & 0x000f));
USART1_BRR1 = ((unsigned char)((baud & 0x0ff0) >> 4));
USART1_CR1_bit.USARTD = 0;
USART1_CR2_bit.RIEN = 1;
USART1_CR2_bit.TEN = 1;
USART1_CR2_bit.REN = 1;
}
void Uart_SendData(unsigned char data)
{
while(!(USART1_SR&0X80)); //判断发送数据寄存器是否为空
USART1_DR = data; //向发送寄存器写入数据
}
/* 主函数 */
int main(void)
{
asm("sim");
CLK_CKDIVR = 0x00;
UART1_Init(9600);
asm("rim");
while(1)
{
;
}
}
/* 串口接收数据中断服务函数 */
#pragma vector = USART_R_RXNE_vector //设置串口接收中断向量号 = USART_R_RXNE_vector
__interrupt void UART1_RX_RXNE(void)
{
unsigned char ch1;
if(USART1_SR_bit.RXNE == 1) //
{
ch1 = USART1_DR; //读出串口接收到的数据 清除中断标志
Uart_SendData(ch1); //把接收到的数据再通过串口发送出去
}
}
一周热门 更多>