Skip to content

Instantly share code, notes, and snippets.

@arn-ob
Last active July 30, 2018 04:54
Show Gist options
  • Select an option

  • Save arn-ob/2a85b25a4f5d435b0c7e1f7ede54cb91 to your computer and use it in GitHub Desktop.

Select an option

Save arn-ob/2a85b25a4f5d435b0c7e1f7ede54cb91 to your computer and use it in GitHub Desktop.
Atmega32 chip programming with ADC
/*
* LDRCode.c
*
* Created: 7/30/2018 09:47:05 AM
* Author : Arnob
*/
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
void ADC_Init()
{
DDRA=0x0; /* Make ADC port as input */
ADCSRA = 0x87; /* Enable ADC, fr/128 */
ADMUX = 0x40; /* Vref: Avcc, ADC channel: 0 */
}
int ADC_Read(char channel)
{
int Ain,AinLow;
ADMUX=ADMUX|(channel & 0x0f); /* Set input channel to read */
ADCSRA |= (1<<ADSC); /* Start conversion */
while((ADCSRA&(1<<ADIF))==0); /* Monitor end of conversion interrupt */
_delay_us(10);
AinLow = (int)ADCL; /* Read lower byte*/
Ain = (int)ADCH*256; /* Read higher 2 bits and
Multiply with weight */
Ain = Ain + AinLow;
return(Ain); /* Return digital value*/
}
int main(void)
{
ADC_Init();
int value;
DDRD = 0xFF;
while(1)
{
value=ADC_Read(0);
if (value > 900){
PORTD = 0xFF;
_delay_ms(220);
_delay_ms(220);
}else{
PORTD = 0x00;
_delay_ms(220);
_delay_ms(220);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment