2019-07-21 03:58发布
edmund1234 发表于 2019-3-23 15:26 你的Resultvolt, main在用, 中断函数也在用, 能不出问题么
edmund1234 发表于 2019-3-23 15:43 在USART2_IRQHandler里面设一断点看有没有停下来
1599064432 发表于 2019-3-23 15:46 停下来了,而且我刚刚又发现一个问题,我用的是GPIOA和GPIOC,我看寄存器为什么是A和B有变化?
最多设置5个标签!
#include "stdio.h"
#include "math.h"
#include "timer.h"
#include "usart.h"
uint32_t Resultvolt;
void NTCtp_config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
SysTick_Config_Init();
NTCtp_gpio_config();
NTCtp_usart_config();
NTCtp_nvic_config();
usart_release_gpio_int();
adc_config();
}
void NTCtp_gpio_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //采集电压
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //串口2
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
}
void NTCtp_usart_config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; /*波特率*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*流控*/
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*数据位,8*/
USART_InitStructure.USART_Parity = USART_Parity_No; /*校验位,n*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*停止位,1*/
USART_Init(USART2,&USART_InitStructure);
USART_ClearFlag(USART2,USART_FLAG_TC); //清除发送完成标志位
USART_Cmd(USART2,ENABLE); //使能串口3
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
void NTCtp_nvic_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int fputc(int ch, FILE *f) //IAR重定向printf函数
{
//while (USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET )
while (USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET )
{
}
USART_SendData(USART3,(uint8_t) ch);
return ch;
}
void adc_config(void)
{
ADC_DeInit(ADC1); //初始化
ADC_InitTypeDef ADC_InitStructure;
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //转换使能
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位分辨率
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; //浏览方向
ADC_Init(ADC1,&ADC_InitStructure);
ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_239_5Cycles); //PA3 通道3 USART2
ADC_GetCalibrationFactor(ADC1); //校验
ADC_Cmd(ADC1,ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); //等待就绪
ADC_StartOfConversion(ADC1); //启动ADC
}
uint32_t ADC_Check(void)
{
uint8_t i;
for(i=0; i<2; i++)
{
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
Resultvolt = (uint32_t) ADC_GetConversionValue(ADC1);
}
Resultvolt = (Resultvolt*3300)/4096; //1000倍
return Resultvolt;
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //判断读寄存器是否有数据
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除发送完成标志位
Resultvolt = USART_ReceiveData(USART2);
}
}
int main()
{
static uint32_t led_timer = 0;
static uint32_t usart_timer = 0;
SysTick_Config_Init();
led_gpio_init();
NTCtp_config();
while(1)
{
if (tick1ms - led_timer >= 500)
{
led_timer = tick1ms;
//GPIO_ResetBits(GPIOB,GPIO_Pin_7);
GPIOB->ODR ^= GPIO_Pin_7;
}
if (tick1ms - usart_timer >= 500)
{
usart_timer = tick1ms;
ADC_Check(); //采集电压
printf("电压是%d ",Resultvolt);
}
}
}
这边是部分代码
我又重新定义了一个变量,为什么还没有串口还没有接收到数据
停下来了,而且我刚刚又发现一个问题,我用的是GPIOA和GPIOC,我看寄存器为什么是A和B有变化?
说老实的, 我看不明白你的代码, 你又打印ADC的采样值, 又接收了放在同样的打印函数的参数里
所以帮不上忙呢
一周热门 更多>