小学生
最后登录1970-1-1
在线时间 小时
注册时间2017-2-28
|
项目需要使用USART读取CO2传感器的数据传到上位机,我先写了USART3与电脑的通信程序,没有用printf函数,接收和发送字符串都很正常,但是将主程序改成和传感器连接,程序就无法进入中断了,希望大家帮忙看一下程序有没有问题?(注:传感器本身是正常的)
附上代码:
void DEBUG_USART_IRQHandler(void)//中断处理函数
{
ReceiveBit3=1;
if(USART_GetITStatus(DEBUG_USART,USART_IT_RXNE)!=RESET)
{
ReceiveBit3=1;
USART_ITConfig(DEBUG_USART, USART_IT_RXNE, DISABLE);
temp_str[RecCount]=(char)USART_ReceiveData(DEBUG_USART);//接收到的字符存入一个全局变量
RecCount++;
//if(RecCount==6)
//{
//ReceiveBit3=1;
//}
USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE);
}
}
#include "./usart/bsp_debug_usart.h"
uint8_t ReceiveBit3;
char temp_ch;
char temp_str[7];
uint8_t RecCount;
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Debug_USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(DEBUG_USART_RX_GPIO_CLK|DEBUG_USART_TX_GPIO_CLK,ENABLE);
RCC_APB1PeriphClockCmd(DEBUG_USART_CLK, ENABLE);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_PIN ;
GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_PIN;
GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(DEBUG_USART_RX_GPIO_PORT,DEBUG_USART_RX_SOURCE,DEBUG_USART_RX_AF);
GPIO_PinAFConfig(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_SOURCE,DEBUG_USART_TX_AF);
USART_InitStructure.USART_BaudRate = DEBUG_USART_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_Rx | USART_Mode_Tx;
USART_Init(DEBUG_USART, &USART_InitStructure);
NVIC_Configuration();
USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE);
USART_Cmd(DEBUG_USART, ENABLE);
}
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
USART_SendData(pUSARTx,ch);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
#ifndef __DEBUG_USART_H
#define __DEBUG_USART_H
#include "stm32f4xx.h"
#include <stdio.h>
/*******************************************************/
#define DEBUG_USART USART3
#define DEBUG_USART_CLK RCC_APB1Periph_USART3
#define DEBUG_USART_BAUDRATE 9600 //´®¿Ú²¨ÌØÂÊ
#define DEBUG_USART_RX_GPIO_PORT GPIOB
#define DEBUG_USART_RX_GPIO_CLK RCC_AHB1Periph_GPIOB
#define DEBUG_USART_RX_PIN GPIO_Pin_11
#define DEBUG_USART_RX_AF GPIO_AF_USART3
#define DEBUG_USART_RX_SOURCE GPIO_PinSource11
#define DEBUG_USART_TX_GPIO_PORT GPIOB
#define DEBUG_USART_TX_GPIO_CLK RCC_AHB1Periph_GPIOB
#define DEBUG_USART_TX_PIN GPIO_Pin_10
#define DEBUG_USART_TX_AF GPIO_AF_USART2
#define DEBUG_USART_TX_SOURCE GPIO_PinSource10
#define DEBUG_USART_IRQHandler USART3_IRQHandler
#define DEBUG_USART_IRQ USART3_IRQn
/************************************************************/
extern uint8_t ReceiveBit3;
extern uint8_t RecCount;
extern char temp_ch;
extern char temp_str[7];
//extern uint8_t SendBit3;
void Debug_USART_Config(void);
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
void Usart_SendString( USART_TypeDef * pUSARTx, char *str);
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
#endif
#include "stm32f4xx.h"
#include "led.h"
#include "./usart/bsp_debug_usart.h"
char Humid[3]={0x48,0x0d,0x0a};
void Delay(__IO uint32_t count)
{
for( ;count!=0;count--)
{}
}
int main(void)
{
Debug_USART_Config();
LED_GPIO_Config();
RecCount=0;
ReceiveBit3=0;//计数位清零
Usart_SendByte(DEBUG_USART,Humid[0]);
Usart_SendByte(DEBUG_USART,Humid[1]);
Usart_SendByte(DEBUG_USART,Humid[2]);//向传感器发送指令,指令没有错误
LED3_TOGGLE;//为了检查LED灯是否可以正常亮起
Delay(0xffff);
while(1)
{
if(temp_str[0]==0x48)//如果受到正确数据,第一个字节是‘H’
{
//ReceiveBit3=0;
//RecCount=0;
LED1_TOGGLE;
}
if(ReceiveBit3==1)//这里设了一个标志位,只要进入中断就会更改,但是LED并没有换颜色,说明没有进中断
{
LED2_TOGGLE;
ReceiveBit3=0;
}
}
}
|
|