gpt4 book ai didi

c - STM8中断串行接收

转载 作者:行者123 更新时间:2023-11-30 15:04:15 24 4
gpt4 key购买 nike

我是 STM8 新手,正在尝试使用 IAR Embedded Workbench 来使用 STM8S103F3。使用C语言时,我喜欢直接使用寄存器。我需要 14400 波特率、8N2 的串行,并且获得 UART 传输很容易,因为网上有许多好的教程和示例。然后需要让 UART 在中断时接收,其他什么都做不了。那就是问题所在。根据iostm8s103f3.h(IAR),0x14 vector 上有5个中断UART1_R_IDLE、UART1_R_LBDF、UART1_R_OR、UART1_R_PE、UART1_R_RXNE

根据 Silverlight 开发人员的说法:在 STM8 上注册,

Vector 19 (0x13) = UART_RX

根据意法半导体STM8S.h

#define UART1_BaseAddress       0x5230
#define UART1_SR_RXNE ((u8)0x20) /*!< Read Data Register Not Empty mask */
#if defined(STM8S208) ||defined(STM8S207) ||defined(STM8S103) ||defined(STM8S903)
#define UART1 ((UART1_TypeDef *) UART1_BaseAddress)
#endif /* (STM8S208) ||(STM8S207) || (STM8S103) || (STM8S903) */

根据STM8S引用手册RM0016RXNE 标志(Rx 缓冲区不为空)在最后一个采样时钟边沿设置,当数据从移位寄存器传输到 Rx 缓冲区时。它表示已准备好从 SPI_DR 寄存器读取数据。Rx 缓冲区不为空 (RXNE)当设置时,该标志表明 Rx 缓冲区中有有效的接收数据。当读取 SPI_DR 时该标志被重置。然后我写道:

#pragma vector = UART1_R_RXNE_vector //as iostm8s103f3 is used, that means 0x14
__interrupt void UART1_IRQHandler(void)
{ unsigned character recd;
recd = UART1_SR;
if(1 == UART1_SR_RXNE) recd = UART1_DR;

等等。不好,我不断收到中断,UART1_SR_RXNE 已设置,但 UART1_DR为空,并且没有发生 UART 接收。我已禁用所有其他中断我可以看到可以 vector 到此,但仍然没有好处。SPI也设置了这个标志,想必UART和SPI都不能使用一起。我非常需要让这个串行接收中断继续进行。请帮忙。谢谢

最佳答案

问题是 UART1 设置中有一点不正确。STM8S103F3 中 UART1 的完整设置现在是(IAR):

  void InitialiseUART()
{
unsigned char tmp = UART1_SR;
tmp = UART1_DR;
// Reset the UART registers to the reset values.
UART1_CR1 = 0;
UART1_CR2 = 0;
UART1_CR4 = 0;
UART1_CR3 = 0;
UART1_CR5 = 0;
UART1_GTR = 0;
UART1_PSCR = 0;
// Set up the port to 14400,n,8,2.
UART1_CR1_M = 0; // 8 Data bits.
UART1_CR1_PCEN = 0; // Disable parity.
UART1_CR3 = 0x20; // 2 stop bits
UART1_BRR2 = 0x07; // Set the baud rate registers to 14400
UART1_BRR1 = 0x45; // based upon a 16 MHz system clock.
// Disable the transmitter and receiver.
UART1_CR2_TEN = 0; // Disable transmit.
UART1_CR2_REN = 0; // Disable receive.
// Set the clock polarity, clock phase and last bit clock pulse.
UART1_CR3_CPOL = 0;
UART1_CR3_CPHA = 0;
UART1_CR3_LBCL = 0;
// Set the Receive Interrupt RM0016 p358,362
UART1_CR2_TIEN = 0; // Transmitter interrupt enable
UART1_CR2_TCIEN = 0; // Transmission complete interrupt enable
UART1_CR2_RIEN = 1; // Receiver interrupt enable
UART1_CR2_ILIEN = 0; // IDLE Line interrupt enable

// Turn on the UART transmit, receive and the UART clock.
UART1_CR2_TEN = 1;
UART1_CR2_REN = 1;
UART1_CR1_PIEN = 0;
UART1_CR4_LBDIEN = 0;
}
//-----------------------------
#pragma vector = UART1_R_RXNE_vector
__interrupt void UART1_IRQHandler(void)
{
byte recd;
recd = UART1_DR;
//send the byte to circular buffer;
}

关于c - STM8中断串行接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40354967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com