新手上路
- 积分
- 37
- 金钱
- 37
- 注册时间
- 2014-6-26
- 在线时间
- 2 小时
|
5金钱
开发环境: STM8L052R8 + IAR For STM8
目的: 把串口接收中断调通
问题: 用串口助手给单片机发一个字符, 如0x55, 仿真一下, 看到USART1_DR寄存器的值是0x55, 再看一下USART1_SR的值是0xd0, 也就是说, USART1_SR_RXNE位为0. 所以没有进中断,因为就没有产生中断.
Status register (USART_SR)
Address offset: 0x00
Reset value: 0xC0
bit 7
|
bit 6
|
bit 5
|
bit 4
|
bit 3
|
bit 2
|
bit 1
|
bit 0
|
TXE
|
TC
|
RXNE
|
IDLE
|
OR
|
NF
|
FE
|
PE
|
r
|
rc_w0
|
r
|
r
|
r
|
r
|
r
|
r
|
bit 5: RXNE This bit is set by hardware when the content of the RDR shift register has been transferred to the Bit 5 RXNE: Read data register not empty
USART_DR register. An interrupt is generated if RIEN=1 in the USART_CR2 register. It is cleared by
a read to the USART_DR register.
0: Data is not received
1: Received data is ready to be read.
接受字符前寄存器的值如下图
然后用串口助手给单片机发个0x55, 寄存器中的值如下:
由于USART1_SR的值为0xd0, 所以不会产生中断.
而且此时在主函数死循环中的写的LED灯也不正常工作了, 程序在某个地方卡死了.但是不知道问题出在哪里,希望大家帮帮忙.
谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!
#include "uart.h"
int fputc(int ch, FILE *f)
{
//将Printf内容发往串口
USART1_DR=(unsigned char)ch;
while(!(USART1_SR & 0x80)); //等待发送完成
return (ch);
}
/*******************************************************************************
****输入:无
****输出:无
****说明:串口初始化
****作者:
*******************************************************************************/
void uart_init(void)
{
// unsigned int baud_div = 0;
//USART1初始化
CLK_PCKENR1_PCKEN15 = 1; //开启USART1串口时钟
//初始化IO口
PC_DDR_DDR3 = 1;
PC_CR1_C13 = 1;
PC_CR2_C23 = 1;
PC_DDR_DDR2 = 0;
PC_CR1_C12 = 1;
PC_CR2_C22 = 0;
//初始化USART1寄存器
USART1_CR1 = 0x00;
USART1_CR2 = 0x00;
USART1_CR3 = 0x00;
USART1_BRR2 = 0x03;
USART1_BRR1 = 0x68;
USART1_CR2 = 0x2c; //使能usart1发送和接收,开启接收中断
ITC_SPR8_VECT28SPR = 0; //设置串口接收中断优先级
}
void uart1_send_char(unsigned char ch)
{
while(!USART1_SR_TXE); //等待发送完成
USART1_DR = ch;
}
int main(void)
{
sysclk_init();
delay_init(16);
main_power_init();
led_init();
beep_init();
adc_init();
uart_init();
MAIN_PWR = 1; //主电源打开
led_active();
asm("rim");//开中断,sim为关中断
printf("Hello:\n");
while(1)
{
LED6 = !LED6;
delay_ms(500);
}
//串口接收中断函数
#pragma vector = USART1_R_RXNE_vector
__interrupt void USART1_R_RXNE_IRQHandler(void)
{
unsigned char uart1_rx = 0;
uart1_rx = USART1_DR;
LED1 = 0;
LED2 = !LED2;
printf("OK!\n");
} |
|