程序运行输出一直为0,请教给位大神,帮忙看一下原因,O(∩_∩)O谢谢!
usart.c 文件
#include "bsp_usart3.h"
#include <stdarg.h>
//配置USART3接收中断
sta
tic void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*
* 函数名:USART1_Config
* 描述 :USART1 GPIO 配置,工作模式配置
* 输入 :无
* 输出 : 无
* 调用 :外部调用
*/
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 //TX初始化
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上啦输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化
USART_InitStructure.USART_BaudRate = 2400; //波特率2400
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(USART1, &USART_InitStructure); //usart1初始化
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
/*
* 函数名:USART3_Config
* 描述 :USART3 GPIO 配置,工作模式配置
* 输入 :无
* 输出 : 无
* 调用 :外部调用
*/
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
// GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 //TX初始化
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure); //RX初始化
USART_InitStructure.USART_BaudRate = 2400; //波特率2400
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(USART3, &USART_InitStructure); //usart3初始化
NVIC_Configuration();
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
}
/***************** 发送一个字符 **********************/
static void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch )
{
/* 发送一个字节数据到USART1 */
USART_SendData(pUSARTx,ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
/***************** 指定长度的发送字符串 **********************/
void Usart_SendStr_length( USART_TypeDef * pUSARTx, uint8_t *str,uint32_t strlen )
{
unsigned int k=0;
do
{
Usart_SendByte( pUSARTx, *(str + k) );
k++;
} while(k < strlen);
}
/***************** 发送字符串 **********************/
void Usart_SendString( USART_TypeDef * pUSARTx, uint8_t *str)
{
unsigned int k=0;
do
{
Usart_SendByte( pUSARTx, *(str + k) );
k++;
} while(*(str + k)!=' ');
}
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到USART1 */
USART_SendData(USART1, (uint8_t) ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
/******************* (C) COPYRIGHT 2012 WildFire Team *****END OF FILE************/
main.c 文件
/* * 接线方式 PM2.5 TXD -----------> PB11
**/
#include "
STM32f10x.h"
#include "bsp_usart3.h"
#define N 7 //数据位长度
int i=0;
int n;
float pm;
int pmshow;
unsigned char cal[7]; //定义接收数组
unsigned char sum;
void senddata(unsigned char dat) //数据处理函数
{
if(dat==170) //首位是否为0xAA
{
i=0;
cal
=dat;
}
else
{
i=i+1;
cal=dat;
if(i==6)
{
sum= cal[1]+cal[2]+cal[3]+cal[4];
if(sum == cal[5] && cal[6]==255)
{
pm=(int)((cal[1]*256+cal[2])/(1.024));
pmshow=pm;
}
}
}
}
int main(void)
{
USART3_Config();
USART1_Config();
printf( "这是一个PM2.5数值实时显示实验
" );
for(;;);
}
void USART3_IRQHandler(void)
{
unsigned char ch;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
ch=USART_ReceiveData(USART3);
senddata(ch);
printf("PM2.5的值为%d
",pmshow);
}
}
一周热门 更多>