嵌入式课程设计 —— STM32模数转换器ADC
程序员文章站
2022-04-01 23:38:06
...
主要代码:
/*ADC Çý¶¯*/
#include "sys.h"
/*
ADC ³õʼ»¯
¹¤×÷ʱÖÓ£¬×ª»»Ä£Ê½Îªµ¥´Îģʽ
·Ö±æÂÊλ 12λ
*/
void ADC_Init(void)
{
RCC->AHB1ENR |= (0x1<<0); //´ò¿ªPAµÄʱÖÓ
RCC->AHB2ENR |= (0x1<<8); //´ò¿ªADC1ʱÖÓ
//ÅäÖÃPA5 µÄ¹¤×÷ģʽ - Ä£ÄâÁ¿ÊäÈëģʽ
GPIO_Set(GPIOA,PIN5,GPIO_MODE_AIN,0,0,GPIO_PUPD_PU);
//¸´Î»ADC
RCC->APB2RSTR |= (0x1<<8);
RCC->APB2RSTR &= (~(0x1<<8));
ADC1->CR1 = 0;
ADC1->CR2 = 0;
//ÉèÖÃͨµÀ5µÄ²ÉÑùʱ¼äΪ480 ADCCLK
ADC1->SMPR2 |= (0x7<<15);
//ÉèÖÃADCת»»¹æÔòΪת»»1´Î
ADC1->SQR1 &= (~(0xf<<20));
//ÉèÖÃת¿îͨµÀ Ϊ µÚ5¸öÊäÈëͨµÀ
ADC1->SQR3 &= (~0x1f);
ADC1->SQR3 |= 0x5;
}
u16 GetAdc(void)
{
u16 ad_value = 0;
//Æô¶¯×ª»»¹æÔò
ADC1->CR2 |= (0x1<30);
//µÈ´ýת»»Íê³É
while((ADC1->SR & (0x1<<1)) == 0);
ad_value = ADC1->DR;
return ad_value;
}
下一篇: STM32笔记(十三)---ADC