本帖最后由 x20130927 于 2017-9-22 21:15 编辑
#include "adc.h"
#include "dma.h"
#include "lcd.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
u16 ADCData[50];
void main()
{
u16 adcx=0;
char i=0;
float temp;
Stm32_Clock_Init(336,8,2,7);
delay_init(168);
uart_init(84,115200);
LCD_Init();
MYDMA_Config(DMA2_Stream0, 0,(u32)&ADC1->DR, (u32)ADCData,50);
Adc_Init();
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"ADC TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2014/5/6");
POINT_COLOR=BLUE;
LCD_ShowString(30,130,200,16,16,"ADC1_CH5_VAL:");
LCD_ShowString(30,150,200,16,16,"ADC1_CH5_VOL:0.000V");
while(1)
{
for(i=0;i<50;i++)
{
adcx=adcx+ADCData;
}
adcx=adcx/50;
LCD_ShowxNum(134,130,adcx,4,16,0);
temp=(float)adcx*(3.3/4096);
adcx=temp;
LCD_ShowxNum(134,150,adcx,1,16,0);
temp-=adcx;
temp*=1000;
LCD_ShowxNum(150,150,temp,3,16,0X80);
}
}
#include "adc.h"
#include "delay.h"
void Adc_Init(void)
{
RCC->APB2ENR|=1<<8;
RCC->AHB1ENR|=1<<0;
GPIO_Set(GPIOA,PIN5,GPIO_MODE_AIN,0,0,GPIO_PUPD_PU);
RCC->APB2RSTR|=1<<8;
RCC->APB2RSTR&=~(1<<8);
ADC->CCR=3<<16;
ADC->CCR=~(3<<14);
ADC1->CR1=0;
ADC1->CR2=0;
ADC1->CR2 |= 1<<9;
ADC1->CR2 |= 1<<8;
ADC1->CR1|=0<<24;
ADC1->CR1|=0<<8;
ADC1->CR1&=~(1<<5);
ADC1->CR2|=1<<1;
ADC1->CR2&=~(1<<11);
ADC1->CR2|=0<<28;
ADC1->SQR1&=~(0XF<<20);
ADC1->SQR1|=0<<20;
ADC1->SMPR2&=~(7<<(3*5));
ADC1->SMPR2|=7<<(3*5);
ADC1->CR2|=1<<0;
}
u16 Get_Adc(u8 ch)
{
//éèÖÃ×a»»DòáD
ADC1->SQR3&=0XFFFFFFE0;
ADC1->SQR3|=ch;
ADC1->CR2|=1<<30;
while(!(ADC1->SR&1<<1));
return ADC1->DR;
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
#include "dma.h"
#include "delay.h"
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u8 chx,u32 par,u32 mar,u16 ndtr)
{
DMA_TypeDef *DMAx;
u8 streamx;
if((u32)DMA_Streamx>(u32)DMA2)
{
DMAx=DMA2;
RCC->AHB1ENR|=1<<22;
}else
{
DMAx=DMA1;
RCC->AHB1ENR|=1<<21;
}
while(DMA_Streamx->CR&0X01);//μè′yDMA¿éÅäÖÃ
streamx=(((u32)DMA_Streamx-(u32)DMAx)-0X10)/0X18;
if(streamx>=6)DMAx->HIFCR|=0X3D<<(6*(streamx-6)+16);
else if(streamx>=4)DMAx->HIFCR|=0X3D<<6*(streamx-4);
else if(streamx>=2)DMAx->LIFCR|=0X3D<<(6*(streamx-2)+16);
else DMAx->LIFCR|=0X3D<<6*streamx;
DMA_Streamx->PAR=par;
DMA_Streamx->M0AR=mar;
DMA_Streamx->NDTR=ndtr;
DMA_Streamx->CR=0;
DMA_Streamx->CR&=~(3<<6);
DMA_Streamx->CR|=1<<8;
DMA_Streamx->CR|=0<<9;
DMA_Streamx->CR|=1<<10;
DMA_Streamx->CR|=1<<11;
DMA_Streamx->CR|=1<<13;
DMA_Streamx->CR|=1<<16;
DMA_Streamx->CR|=0<<21;
DMA_Streamx->CR|=0<<23;
DMA_Streamx->CR|=(u32)chx<<25;
DMA_Streamx->FCR&=0xffffff40;
DMA_Streamx->FCR|=0x07;
DMA_Streamx->CR|=1<<0;
}
void MYDMA_Enable(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
{
DMA_Streamx->CR&=~(1<<0);
while(DMA_Streamx->CR&0X1);
DMA_Streamx->NDTR=ndtr;
DMA_Streamx->CR|=1<<0;
}
这程序AD值一直为0,求求各位大神帮我指点一下
一周热门 更多>