问题描述
限时送ChatGPT账号..我正在通过中断将 STM8S103F3 编程到 UART 上的 TX.我了解在传输数据寄存器空中断"后写入 DR将启动另一个 TX,所以我在 ISR 中有这个.但它只有在我的主循环旋转等待中断时才有效.如果它在 nop 上旋转,则只发送第一个字符 - 好像在 ISR 中写入 DR 不会产生后续中断.
I am programming a STM8S103F3 to TX on UART via interrupt. I understand a write to DR after "Transmit data register empty interrupt" will start another TX, so I have this in my ISR. But it only works if my main loop spins on wait for interrupt. If it spins on nop only the first char is TXed - as though the write to DR within the ISR does not generate a subsequent interrupt.
使用 SDCC 编译器.
Using SDCC compiler.
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();
}
}
推荐答案
请参阅您的 stm8 参考手册(我使用 CD00218714)第 12.9.1 章您将看到 CPU 条件代码寄存器的默认值(复位后)为 0x28 -这意味着刚启动您的 mcu 将在中断级别 3 下工作并且所有软件中断都被禁用,只有 RESET 和 TRAP 可以工作.
See your reference manual for stm8 (I use CD00218714) at chapter 12.9.1 you will see default value (after reset) of CPU condition code register it's 0x28 - this mean that just after start your mcu will work at interrupt level 3 and all software interrupt are disabled, only RESET and TRAP will workable.
根据程序手册(我用的是CD00161709)指令WFI将中断级别改为0级,你的USART软件中断就可以工作了.
According to program manual (I use CD00161709) instruction WFI change interrupt level to level 0 and your software interrupt of USART become workable.
您需要在初始化代码之后插入 asm("rim");
(在 CLK_CKDIVR = 0;
行之后) - 这将使您的代码可以使用 asm("nop");
基于主循环.
You need to insert asm("rim");
just after initialization code (after line CLK_CKDIVR = 0;
) - this will make your code workable with asm("nop");
based main loop.
这篇关于stm8 uart tx中断问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论