CH32V307 中斷實現(xiàn)串口發(fā)送 求助

CH32V307似乎沒有提供串口中斷發(fā)送的例程。

我自己寫了個環(huán)形緩沖,但是不知道為什么有穩(wěn)定性問題。 求助

#define?DEBUG_UART_BUF_SIZE?64
#define?DEBUG_UART_BUF_MASK?DEBUG_UART_BUF_SIZE-1

uint8_t?debug_uart_tx_buffer?[DEBUG_UART_BUF_SIZE];
//uint8_t?debug_uart_rx_buffer?[DEBUG_UART_BUF_SIZE];
volatile?size_t?debug_uart_tx_head;
volatile?size_t?debug_uart_tx_tail;
volatile?size_t?debug_uart_tx_fill;
typedef?enum?{?READY?=?0,SENDING?=?1}?UART_TX_Buf_Status;
volatile?UART_TX_Buf_Status??debug_uart_tx_status;


void?USART_Printf_Init(uint32_t?baudrate)
{
????GPIO_InitTypeDef??GPIO_InitStructure;
????USART_InitTypeDef?USART_InitStructure;
????NVIC_InitTypeDef??NVIC_InitStructure;

????RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1?|?RCC_APB2Periph_GPIOA,?ENABLE);

????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_9;
????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP;
????GPIO_Init(GPIOA,?&GPIO_InitStructure);

????USART_InitStructure.USART_BaudRate?=?baudrate;
????USART_InitStructure.USART_WordLength?=?USART_WordLength_8b;
????USART_InitStructure.USART_StopBits?=?USART_StopBits_1;
????USART_InitStructure.USART_Parity?=?USART_Parity_No;
????USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None;
????USART_InitStructure.USART_Mode?=?USART_Mode_Tx;?//Tx?only?as?we?are?doing?debug

????USART_Init(USART1,?&USART_InitStructure);
????USART_Cmd(USART1,?ENABLE);

????//USART_ITConfig(USART1,?USART_IT_TXE,?ENABLE);
????//Enable?the?TX?interrupt,?so?when?the?data?is?taken?to?the?shift?register?of?the
????//?peripheral,?we?can?put?in?the?next?byte.

????NVIC_InitStructure.NVIC_IRQChannel?=?USART1_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?14;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure);

????debug_uart_tx_head=0;
????debug_uart_tx_tail=0;
????debug_uart_tx_fill=0;
????debug_uart_tx_status?=?READY;
????
}


void?USART1_IRQHandler(void)?__attribute__((interrupt("WCH-Interrupt-fast")));

void?USART1_IRQHandler(void)
{
????if(USART_GetITStatus(USART1,?USART_IT_TXE)?==?SET)
????{
????????//Interrupt?handler?for?trasmit?
????????//USART1_send_char_from_buffer();
????????//USART_ITConfig(USART1,?USART_IT_TXE,?ENABLE);

????????debug_uart_tx_head=(debug_uart_tx_head+1)&DEBUG_UART_BUF_MASK;
????????debug_uart_tx_fill--;
????????
????????if?(debug_uart_tx_fill?==?0)
????????{
????????????//?If?there?is?no?pending?data,?stop?transmission.
????????????USART_ITConfig(USART1,?USART_IT_TXE,?DISABLE);
????????????debug_uart_tx_status?=?READY;?
????????}
????????else{
????????????//?If?there?is?still?pending?data,?transmit?the?next?byte.
????????????USART_SendData(USART1,?debug_uart_tx_buffer[debug_uart_tx_head]);
????????}
????}
}


void?USART1_putchar(char?c){
????while?(debug_uart_tx_fill?>=?DEBUG_UART_BUF_SIZE);
????debug_uart_tx_buffer[debug_uart_tx_tail]?=?c;
????debug_uart_tx_tail=(debug_uart_tx_tail+1)&DEBUG_UART_BUF_MASK;
????debug_uart_tx_fill++;
????if?(debug_uart_tx_status?==?READY)
????{
????????debug_uart_tx_status?=?SENDING;
????????USART_SendData(USART1,?debug_uart_tx_buffer[debug_uart_tx_head]);
????????USART_ITConfig(USART1,?USART_IT_TXE,?ENABLE);
????}
}

具體表現(xiàn)為前面二三十個字節(jié)的發(fā)送都沒問題,但是后來debug_uart_tx_head 跑到tail前面去了,導致亂碼。不知道是什么原因。

您好,若要使用串口發(fā)送中斷,可在發(fā)送數(shù)據時開啟TXE中斷,在發(fā)送完成后立即關閉TXE中斷。注意關于清除TXE中斷標志位,注意對USART_DATAR的寫操作,將該位清零,即先讀狀態(tài)寄存器在讀數(shù)據寄存器可對該位清零。使用環(huán)形緩沖區(qū),注意對頭和尾以及緩沖區(qū)長度大小的處理,若需要,可留一個郵箱,可給你發(fā)一個對環(huán)形緩沖區(qū)的講解處理,你可參考一下。



能不能幫忙給我一個環(huán)形緩沖區(qū)的講解,謝謝,我的郵箱 個人信息保護,已隱藏


您好,可以直接網上搜索環(huán)形緩沖區(qū),會有很多專業(yè)的講解和參考例程,可以參考一下,本人也是參考該類講解進行理解應用的


只有登錄才能回復,可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频