graciouspace
Newbie level 4
Hi i need to write an AVR code to program the Atmega 644. The input is an analogue signal(a continuous square wave) . I want an output which detects whether the input signal is high.
For this purpose I have written some code. I'm using an ADC to convert the analogue signal to a digital signal. Further I am using pin change interrupt to detect when the input signal is high. But my code is not working :sad::sad: . I'll be grateful if someone could help me with the correct code. thanks! I'm posting my code below:
For this purpose I have written some code. I'm using an ADC to convert the analogue signal to a digital signal. Further I am using pin change interrupt to detect when the input signal is high. But my code is not working :sad::sad: . I'll be grateful if someone could help me with the correct code. thanks! I'm posting my code below:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #include <stdlib.h> int main() { DDRC= 0b11111111; //PortC set as output port ADMUX |= 1<<MUX0; //ADC input set to PA0 ADMUX |= 1<<REFS0; //voltage reference taken from AREF pin ADMUX |=1<<ADLAR; //using 8 bit resolution ADCSRA |= 1<<ADEN; //turns on ADC ADCSRA |= 1<<ADPS1 | 1<<ADPS0; //Prescaler set at 16 ADCSRA |= 1<<ADIE; //ADC interrupt enable sei(); //enable global interrupts ADCSRA |= 1<<ADSC; //start AD conversion while(1) {} } ISR(ADC_vect) //interrupt service routine for ADC { //interrupts while loop when ADC conversion is finished switch(ADMUX) //switches between the two analogue inputs and outputs a pulse on Channel A and B { case 1: //That is, the output is high. This might have to be changed. PORTC = 0b00000001; //If the whistle is detected, PC0 goes to 1 for 30 seconds _delay_ms(30000); //And then goes back 0 PORTC = 0b00000000; break; default: PORTC= 0b00000000; break; } ADCSRA |= 1<<ADSC; //starts next AD conversion }
Last edited by a moderator: