用的开发板式stm32f407zgt6,自己仿照视频写的代码,不知道哪里有问题,lcd显示的只有小数,没有整数部分!!!麻烦大家指导一下!!
#include "adc.h"
#include "stm32f4xx.h"
#include "delay.h"
void Adc_Init(void)
{
ADC_CommonInitTypeDef ADC_CommonInitADC;
GPIO_InitTypeDef GPIO_InitADC;
ADC_InitTypeDef ADC_InitADC;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitADC.GPIO_Pin=GPIO_Pin_5;
GPIO_InitADC.GPIO_Mode=GPIO_Mode_AN;
GPIO_InitADC.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitADC);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
ADC_CommonInitADC.ADC_Mode=ADC_Mode_Independent;
ADC_CommonInitADC.ADC_Prescaler=ADC_Prescaler_Div4;
ADC_CommonInitADC.ADC_DMAAccessMode=ADC_DMAAccessMode_Disabled;
ADC_CommonInitADC.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitADC);
ADC_InitADC.ADC_Resolution=ADC_Resolution_12b;
ADC_InitADC.ADC_ScanConvMode=DISABLE;
ADC_InitADC.ADC_ContinuousConvMode=DISABLE;
ADC_InitADC.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;
ADC_InitADC.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitADC.ADC_NbrOfConversion=1;
ADC_Init(ADC1,&ADC_InitADC);
ADC_Cmd(ADC1,ENABLE);
}
/*
ADC_Channel_1 || ADC_Channel_2|| ADC_Channel_3 ||ADC_Channel_4 ||ADC_Channel_5 ||ADC_Channel_6 || ADC_Channel_7 ||
ADC_Channel_8 || ADC_Channel_9 || ADC_Channel_10 || ADC_Channel_11 || ADC_Channel_12 ||ADC_Channel_13 ||
ADC_Channel_14 ||ADC_Channel_15 || ADC_Channel_16 || ADC_Channel_17 ||ADC_Channel_18
*/
u8 Get_ADCVal(u8 ch)
{
ADC_RegularChannelConfig(ADC1,ch,1,ADC_SampleTime_480Cycles);
ADC_SoftwareStartConv(ADC1);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==0);
return ADC_GetConversionValue(ADC1);
}
u16 Get_ADC_avergeVal(u8 ch,u8 time)
{
u32 sum=0;
u8 i;
for(i=0;i<time;i++)
{
sum+=Get_ADCVal(ch);
delay_ms(5);
}
return sum/time;
}
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "adc.h"
int main(void)
{
float temp;
u16 adcx;
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200); //3õê¼»ˉ′®¿ú2¨ìØÂêÎa115200
LED_Init(); //3õê¼»ˉLED
LCD_Init(); //3õê¼»ˉLCD FSMC½ó¿ú
Adc_Init();
POINT_COLOR=RED; //»-±êÑÕé«£ooìé«
LCD_ShowString(30,40,210,24,24,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"ADC TEST");
LCD_ShowString(30,130,200,16,16,"ADC1_CH5_VAL:");
LCD_ShowString(30,150,200,16,16,"ADC1_CH5_VOL:0.000V");
while(1)
{
adcx=Get_ADC_avergeVal(ADC_Channel_5,20);
LCD_ShowxNum(134,130,adcx,4,16,0);
temp=(float)adcx*(3.3/4096);
adcx=temp;//Õûêy2¿·Ö
LCD_ShowxNum(134,150,adcx,1,16,0);
temp-=adcx;
temp*=1000;
LCD_ShowxNum(150,150,temp,3,16,0x80);
LED0=!LED0;
delay_ms(300);
}
}
一周热门 更多>