我有一块STM32开发板,一本《例说STM32》。
我参考书里的ADC例程,写了个程序,书里的是PA1(通道1)
我改为PC0,通道10。下载程序发现程序只执行一次,采样的AD值也对。
我本来意图是连续采样,显示了延时250ms,继续采。
可是采一次显示就不变了。
程序如下,大神看看,哪里有错?谢谢!
void Adc_Init(void)
{
RCC->APB2ENR|=1<<4; //ʹÄÜPORTC¿ÚʱÖÓ
GPIOC->CRL&=0XFFFFFFF0; //PC0 anologÊäÈë
RCC->APB2ENR|=1<<9; //ADC1ʱÖÓʹÄÜ
RCC->APB2RSTR|=1<<9; //ADC1¸´Î»
RCC->APB2RSTR&=~(1<<9); //¸´Î»½áÊø
RCC->CFGR&=~(3<<14); //·ÖƵÒò×ÓÇåÁã
RCC->CFGR|=2<<14; //SYSCLK/DIV2=12M ADCʱÖÓÉèÖÃΪ12MHz£¬ADC×î´óʱÖÓ²»Äܳ¬¹ý14MHz
ADC1->CR1&=0XF0FFFF; //¹¤×÷ģʽÇåÁã
ADC1->CR1|=0<<16; //¶ÀÁ¢¹¤×÷ģʽ
ADC1->CR1&=~(1<<8); //·ÇɨÃèģʽ
ADC1->CR2&=~(1<<1); //µ¥´Îת»»Ä£Ê½
ADC1->CR2&=~(7<<17);
ADC1->CR2|=7<<17; //Èí¼þ¿ØÖÆת»»
ADC1->CR2|=1<<20; //±ØÐëʹÓÃÍⲿ´¥·¢(SWSTART) ±ØÐëʹÓÃÒ»¸öʼþÀ´´¥·¢
ADC1->CR2&=~(1<<11); //ÓÒ¶ÔÆë
ADC1->SQR1&=~(0XF<<20);
ADC1->SQR1|=0<<20; //Ò»¸öת»»ÔÚ¹æÔòÐòÁÐÖÐÒ²¾ÍÊÇֻת»»¹æÔòÐòÁÐ1
ADC1->SMPR1&=~7;//ͨµÀ10²ÉÑùʱ¼äÇå¿Õ
ADC1->SMPR1|=7;//ͨµÀ10 239.5ÖÜÆÚ£¬Ìá¸ß²ÉÑùʱ¼ä¿ÉÒÔÌá¸ß¾«È·¶È
ADC1->CR2|=1<<0; //¿ªÆôADת»»Æ÷
ADC1->CR2|=1<<3; //ʹÄܸ´Î»Ð£×¼
while(ADC1->CR2&1<<3); //µÈ´ýУ׼½áÊø
ADC1->CR2|=1<<2; //¿ªÆôADУ׼
while(ADC1->CR2&1<<2); //µÈ´ýУ׼½áÊø
}
u16 Get_Adc(u8 ch)
{
ADC1->SQR3&=0XFFFFFFE0; //¹æÔòÐòÁÐ1ͨµÀch
ADC1->SQR3|=ch;
ADC1->CR2|=1<<22; //Æô¶¯¹æÔòת»»Í¨µÀ
while(!(ADC1->SR&1<<1)); //µÈ´ýת»»½áÊø
return ADC1->DR; //·µ»ØADCÖµ
}
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;
}
int main(void)
{
u16 adcx;float temp;
u8 x=0;
u8 lcd_id[12];
Stm32_Clock_Init(9); //72MHz
uart_init(72,9600);
delay(1000);
LED_Init();
KEY_Init();
LCD_Init();
Adc_Init();
POINT_COLOR=RED;
sprintf((char *)lcd_id,"LCD ID:%04X",lcddev.id);
LCD_ShowString(60,50,200,16,16,"Mini STM32");
LCD_ShowString(60,70,200,16,16,"ADC TEST");
LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(60,110,200,16,16,"2014/3/9");
POINT_COLOR=BLUE;
LCD_ShowString(60,130,200,16,16,"ADC_CH0_VAL:");
LCD_ShowString(60,150,200,16,16,"ADC_CH0_VOL:0.000V");
while(1)
{
adcx=Get_Adc_Average(ADC_CH10,10);
LCD_Show2Num(156,130,adcx,4,16,0);
temp=(float)adcx*(3.3/4096);
adcx=temp;
LCD_Show2Num(156,150,adcx,1,16,0);
temp-=adcx;
temp*=1000;
LCD_Show2Num(172,150,temp,3,16,0X80);
delay_ms(250);
}
}
|
|