【发布时间】:2018-04-03 15:19:28
【问题描述】:
我正在使用 STM32F030R8 来执行连接到 AD3485 的 RS485 通信。到目前为止,我已经能够让它传输得很好,但是当我设置一个中断处理程序时,似乎中断是由我的芯片自己的传输触发并停止所有进一步的传输。
我是否错过了在某处设置或清除标志?我在 RS485 初始化步骤中遗漏了什么吗?当我的传输不可避免地触发中断请求时,我是否应该清除标志?
下面我包含了一个非常精简但完整的代码版本,用于传输数据,以及我看到的来自我连接的 RS485 芯片的数据捕获到引脚 A9 A10 和 A12。
代码如下:
#include "stm32f0xx_conf.h"
#include <stm32f0xx_gpio.h>
#include <stm32f0xx_rcc.h>
#include <stm32f0xx_usart.h>
void SysTick_Handler(void) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 0xAC);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, 0x44);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, 0x04);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, 0x53);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
void RS485_Init(){
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_12; // Pins 9 (TX) 10 (RX) 12 (DE) are used
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate!
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this defines the output type as push pull mode (as opposed to open drain)
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // this activates the pullup resistors on the IO pins
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); //
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_1);
USART_InitStruct.USART_BaudRate = 38400 ; // the baudrate is set to the value we passed into this init function
USART_InitStruct.USART_WordLength = USART_WordLength_8b; // we want the data frame size to be 8 bits (standard)
USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)
USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver
USART_Init(USART1, &USART_InitStruct);
USART_HalfDuplexCmd(USART1, ENABLE);
USART_DECmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_SetDEAssertionTime(USART1, 0x4E2);
USART_SetDEDeassertionTime(USART1, 0x4E2);
USART_DEPolarityConfig(USART1, USART_DEPolarity_High);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct); //When I comment this out, I'm able to transmit normally, but without an ISR receiving bytes will be difficult
USART_Cmd(USART1, ENABLE);
}
//This function handles USART1 global interrupt request.
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
unsigned char USART_Temp_Byte;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_Temp_Byte = (unsigned char) USART_ReceiveData(USART1); //receive a char
}
}
int main(void)
{
RS485_Init();
SysTick_Config(SystemCoreClock/1000);
while(1);
}
这是我启用中断(第一次捕获)时的捕获,与我注释掉 NVIC_Init(&NVIC_InitStruct); 时的捕获。并且没有 ISR 触发:
【问题讨论】:
标签: c microcontroller stm32 rs485