ADC实验

将模拟信号转换为数字信号

PLCK为100MHz,ADC需要使用分频器降下时钟源,不能超过5MHz

#include"exynos_4412.h"
void Init(void)
{

	//LED2 的28-31位变为0001,其余不变
	GPX2.CON = GPX2.CON & (~(0xF<<28))|(0x1<<28);
	//LED3 的di [0-3]位变为0001,其余不变
	GPX1.CON = GPX1.CON & (~(0xF<<28))|(0x1);
	//LED5 的20位变为1,其余不变
	GPF3.CON = GPF3.CON & (~(0xF<<20))|(0x1<<20);
	//LED4 的20位变为1,其余不变
	GPF3.CON = GPF3.CON & (~(0xF<<16))|(0x1<<16);
}
void Delay(unsigned int Time)
{
while(Time--);
}

void Led2_on(void){
	GPX2.DAT = GPX2.DAT | (1<<7);
}
void Led2_off(void){
	GPX2.DAT = GPX2.DAT & (~(1<<7));
}

void Led3_on(void){
	GPX1.DAT = GPX2.DAT | 1;
}
void Led3_off(void){
	GPX1.DAT = GPX1.DAT & (~1);
}


void Led4_on(void){
	GPF3.DAT = GPF3.DAT | (1<<4);
}
void Led4_off(void){
	GPF3.DAT = GPF3.DAT & (~(1<<4));
}


void Led5_on(void){
	GPF3.DAT = GPF3.DAT | (1<<5);
}
void Led5_off(void){
	GPF3.DAT = GPF3.DAT & (~(1<<5));
}

int main()
{
	Init();

	unsigned int Adcvalue;
	//选择ADC的转换精度设置为12bit
	ADCCON = ADCCON | (1<<16);
	//打开分频器
	ADCCON = ADCCON |(1<<14);
	//设置ADC的分频值19-255
	ADCCON = ADCCON &(~(0xFF<<6))|(255<<6);
	//ADC时钟频率=PLCK/(19+1)=5MHz ADC的转换频率=5MHz/5=1MHz

	//关闭待机模式,使能正常模式
	ADCCON = ADCCON& (~(1<<2));
	//关闭通过读使能AD转换-第一位设为0
	ADCCON = ADCCON&(~(1<<1));

	//选择转换通道3
	ADCMUX = 3;

	while(1)
	{
		/*开始转换*/
		ADCCON = ADCCON |1;
		//等待转换完成
		while(!( ADCCON&(1<<15)));
		//读取转换结果,只读低12位的值
		Adcvalue = ADCDAT& 0xFFF;
		/*将结果转换为实际的电压值,单位 mv*/
		Adcvalue = Adcvalue * 0.44;
		printf("Adcvalue = %dmv\n",Adcvalue);
		if(Adcvalue<501)
		{
			Led2_on();
			Delay(1000000);
			Led2_off();
			Delay(1000000);
			Led2_on();
			
			Led3_off();
			Led4_off();
			Led5_off();
		}
		else if(Adcvalue<1001)
		{
			Led2_on();
			Led3_on();
			Led4_off();
			Led5_off();

		}
		else if (Adcvalue<1501)
		{

			Led2_on();
			Led3_on();
			Led4_on();
			Led5_off();
		}
		else{

			Led2_on();
			Led3_on();
			Led4_on();
			Led5_on();
		}
	}

		return 0;
	}

你可能感兴趣的:(单片机,嵌入式硬件)