簡體   English   中英

stm8 uart tx中斷問題

[英]stm8 uart tx interrupt issue

我正在通過中斷將 STM8S103F3 編程到 UART 上的 TX。 我知道在“發送數據寄存器空中斷”之后寫入 DR 將啟動另一個 TX,所以我的 ISR 中有這個。 但它只有在我的主循環旋轉等待中斷時才有效。 如果它在 nop 上旋轉,則僅發送第一個字符 - 就好像在 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();
    }
}

請參閱第 12.9.1 章的 stm8 參考手冊(我使用 CD00218714),您將看到 CPU 條件代碼寄存器的默認值(復位后)為 0x28 - 這意味着在啟動后您的 mcu 將在中斷級別 3 和所有軟件下工作中斷被禁用,只有 RESET 和 TRAP 可以工作。

根據程序手冊(我使用 CD00161709)指令 WFI 將中斷級別更改為 0 級,並且您的 USART 軟件中斷變得可用。

您需要插入asm("rim"); 就在初始化代碼之后(在CLK_CKDIVR = 0;行之后) - 這將使您的代碼可用於asm("nop"); 基於主循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM