|
1. 我的USART3的程序是借用USART1的. USART1是能接收中断;
2. USART3查询发送和接收都是OK的, 证明USART3的时钟和初始化设置是没有问题的.
3. 我的USART3初始化设置:
void USART3_Configuration(void)
{
/* USART3 configuration ------------------------------------------------------*/
/* USART3 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
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_Rx | USART_Mode_Tx;
/* Configure USART3 */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART3 Receive interrupts */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //接收寄存器不为空中断使能是使能的.
/* Enable the USART3 */
USART_Cmd(USART3, ENABLE);
}
4. IO口初始化, 这里我只针对USART3部分.
/* Configure USART3 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = USART3_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART3_GPIO, &GPIO_InitStructure);
/* Configure USART3 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART3_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART3_GPIO, &GPIO_InitStructure);
USART3_RxPin , USART3_TxPin 这里有定义:
#define USART3_GPIO GPIOB
#define USART3_CLK RCC_APB1Periph_USART3
#define USART3_GPIO_CLK RCC_APB2Periph_GPIOB
#define USART3_RxPin GPIO_Pin_11
#define USART3_TxPin GPIO_Pin_10
5. 中断初始化:
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the DMA1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Enable the DMA1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the DMA1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART3 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Enable the TIM2 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
6. 中断服务函数:
void USART3_IRQHandler(void)
{
static uint8_t temp1 = 0;
if(!temp1)
{
temp1 = 1;
GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_RESET);
}
else
{
temp1 = 0;
GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_SET);
}
}
7. 启动代码是用的这个文件 startup_stm32f10x_hd.s
DCD USART2_IRQHandler ; USART2
DCD USART3_IRQHandler ; USART3
DCD EXTI15_10_IRQHandler ; EXTI Line 15..10
|
|