查看VR1链接的丝印:XadcAIN3
设置相关寄存器
使用的是通道3,要设置相应的通道寄存器
#include "exynos_4412.h"
int main()
{
unsigned int AdcValue = 0;
/*将ADC的精度设置成 12bit*/
ADCCON = ADCCON | (1 << 16);
/*使能ADC的分频器*/
ADCCON = ADCCON | (1 << 14);
/*设置ADC的分频值, ADC的时钟频率 = PLCK/(19+1) = 5MHZ
* ADC的转换频率 = 5MHZ / 5 = 1MHZ*/
ADCCON = ADCCON & (~(0xFF << 6)) | (19 << 6);
/*关闭待机模式,使能正常模式*/
ADCCON = ADCCON & (~(1 << 2));
/*关闭读使能通过转换AD*/
ADCCON = ADCCON & (~(1 << 1));
/*选择转换通道 3通道*/
ADCMUX = 3;
while(1) {
/*开始转换*/
ADCCON = ADCCON | 1;
/*等待转换完成,因为转换需要时间*/
while(!(ADCCON & (1 << 15)));
/*读取转换结果*/
AdcValue = ADCDAT & 0xFFF;
/*将结果转换成实际的电压值 mv 1800(mv) / (2^12-1) = 0.44*/
AdcValue = AdcValue * 0.44;
printf("AdcValue = %dmv\n", AdcValue);
}
return 0;
}
ADC实现小作业
1.编程实现通过LED状态显示当前电压范围
注:
电压在1501mv~1800mv时,LED2、LED3、LED4、LED5点亮
电压在1001mv~1500mv时,LED2、LED3、LED4点亮
电压在501mv~1000mv时,LED2、LED3点亮
电压在0mv~500mv时,LED2闪烁
#include "exynos_4412.h"
/* 点亮LED2 */
#define LED2_ON (GPX2.DAT = GPX2.DAT | (1 << 7))
/* 熄灭LED2 */
#define LED2_OFF (GPX2.DAT = GPX2.DAT & (~(1 << 7)))
/* 点亮LED3 */
#define LED3_ON (GPX1.DAT = GPX1.DAT | 1)
/* 熄灭LED3 */
#define LED3_OFF (GPX1.DAT = GPX1.DAT & (~ 1))
/* 点亮LED4 */
#define LED4_ON (GPF3.DAT = GPF3.DAT | (1 << 4))
/* 熄灭LED4 */
#define LED4_OFF (GPF3.DAT= GPF3.DAT & (~(1 << 4)))
/* 点亮LED5 */
#define LED5_ON (GPF3.DAT = GPF3.DAT | (1 << 5))
/* 熄灭LED5 */
#define LED5_OFF (GPF3.DAT = GPF3.DAT & (~ (1 << 5)))
/*循环延时*/
void Delay(unsigned int Time) {
while(Time--);
}
/*初始化呢ADCCON寄存器*/
void ADC_CON(void) {
/*将ADC的精度设置成 12bit*/
ADCCON = ADCCON | (1 << 16);
/*使能ADC的分频器*/
ADCCON = ADCCON | (1 << 14);
/*设置ADC的分频值, ADC的时钟频率 = PLCK/(19+1) = 5MHZ
* ADC的转换频率 = 5MHZ / 5 = 1MHZ*/
ADCCON = ADCCON & (~(0xFF << 6)) | (19 << 6);
/*关闭待机模式,使能正常模式*/
ADCCON = ADCCON & (~(1 << 2));
/*关闭读使能通过转换AD*/
ADCCON = ADCCON & (~(1 << 1));
/*选择转换通道 3通道*/
ADCMUX = 3;
}
int main()
{
unsigned int AdcValue = 0;
ADC_CON();
GPX2.CON = GPX2.CON & (~(0xF << 28)) | (0x1 << 28);
GPX1.CON = GPX1.CON & (~(0xF)) | (0x1);
GPF3.CON = GPF3.CON & (~(0xFF << 16)) | (0x11 << 16);
while(1) {
/*开始转换*/
ADCCON = ADCCON | 1;
/*等待转换完成,因为转换需要时间*/
while(!(ADCCON & (1 << 15)));
/*读取转换结果*/
AdcValue = ADCDAT & 0xFFF;
/*将结果转换成实际的电压值 mv 1800(mv) / (2^12-1) = 0.44*/
AdcValue = AdcValue * 0.44;
/*打印电压值*/
printf("AdcValue = %dmv\n", AdcValue);
if(AdcValue >= 1501 && AdcValue <= 1800) {
/*LED2 LED3 LED4 LED5 点亮*/
LED2_ON;
LED3_ON;
LED4_ON;
LED5_ON;
} else if (AdcValue >= 1001 && AdcValue <= 1500) {
/*LED2 LED3 LED4 点亮*/
LED5_OFF;
LED2_ON;
LED3_ON;
LED4_ON;
} else if (AdcValue >= 501 && AdcValue <= 1000) {
/*LED2 LED3 点亮*/
LED4_OFF;
LED5_OFF;
LED2_ON;
LED3_ON;
} else if (AdcValue >= 0 && AdcValue <= 500) {
/*LED2 闪烁*/
LED3_OFF;
LED4_OFF;
LED5_OFF;
LED2_ON;
Delay(1000000);
LED2_OFF;
Delay(1000000);
}
}
return 0;
}