我正在通过中断将STM8S103F3编程为UART上的TX。我知道在"Transmit data register empty interrupt“之后写入DR会启动另一个TX,所以我的ISR中有这个。但只有当我的主循环在等待中断时旋转时,它才能工作。如果它在nop上旋转,则只有第一个字符是TXed的-就好像在ISR中写入DR不会产生后续中断一样。
使用SDCC编译器。
sdcc -mstm8 -o build\uart.hex uart.c
#include <stdint.h>
#include <stdlib.h>
#include "stm8.h"
#define DEBUG_BUF_SIZE 10
char debugBuf[DEBUG_BUF_SIZE];
volatile unsigned char *debugPtr;
// UART Tx interrupt
void TX_complete(void) __interrupt(UART_TX_COMPLETE) {
if(*debugPtr != 0) {
UART1_DR = *debugPtr++;
}
}
void log(char *msg)
{
unsigned char i = 0;
UART1_CR2 &= ~UART_CR2_TIEN;
for(; msg[i] != 0 && i<DEBUG_BUF_SIZE-1; i++) {
debugBuf[i] = msg[i];
}
debugBuf[i] = 0;
debugPtr = debugBuf;
UART1_CR2 |= UART_CR2_TIEN;
// Write to DR will start tx
UART1_DR = *debugPtr++;
}
int main(void)
{
// UART 115K2 baud, interrupt driven tx
// UART1_CR1, UART_CR3 reset values are 8N1
UART1_BRR2 = 0x0B;
UART1_BRR1 = 0x08;
UART1_CR2 |= UART_CR2_TEN | UART_CR2_TIEN;
/* Set clock to full speed (16 Mhz) */
CLK_CKDIVR = 0;
log("Run\r\n");
while(1) {
// Only the first char is txed if nop is used
nop();
// But all chars txed if wfi is used
// wfi();
}
}发布于 2021-05-19 15:52:28
参见你的stm8参考手册(我使用CD00218714)在第12.9.1章你会看到CPU条件码寄存器的默认值(复位后)它是0x28 -这意味着在启动后你的微处理器将工作在中断级别3并且所有的软件中断都被禁用,只有复位和陷阱是可行的。
根据程序手册(我使用CD00161709)指令,WFI将中断级别更改为0,您的USART的软件中断就可以工作了。
您需要在初始化代码之后插入asm("rim"); (在CLK_CKDIVR = 0;行之后)-这将使您的代码能够与基于asm("nop");的主循环一起工作。
https://stackoverflow.com/questions/66396179
复制相似问题