Sorry for posting something for a non-atmega 168 here but this is the forum I most frequent. I have a friend who is using an atmega 32 for his electrical engineering senior project. He is having some trouble with his code so I figured I'd ask here for any obvious mistakes.
The code is meant to light one of three leds and/or sound a piezo and activate a vib motor depending on the value of an analog sensor on port A0 and how long it has been below a threshold voltage(greater than .5s, greater than 1.5s). Also, no external crystal seems to have been used based on the schematic he sent me. Any help at all would be appreciated.
Here's the code:
#include<mega32.h>
#include<delay.h>
#define GREEN PORTB.0 // Output port designations
#define YELLOW PORTB.1
#define RED PORTB.2
#define BUZZER PORTB.3
#define MOTOR PORTB.4
#define START PINC.0 // Input pin designations
#define RESET PINC.1
unsigned char buzzer_on; // Declaration of variables
unsigned char adc_data = 0xff;
// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
// Read the 8 most significant bits
// of the AD conversion result
adc_data=ADCH;
ADCSRA=0xDE; //start new conversion
}
interrupt [TIM0_OVF ] void ISR_TIMER0 (void)
{
TCNT0 = 241; //init count
if(buzzer_on!=0) //if buzzer on star tbuzzer at 1khz frec
BUZZER = ~BUZZER;
}
void main (void)
{
int i;
// Declaration of variables
char reset,state;
PORTA=0x00;
// Clear all ports
DDRA=0x00;
// Clear all memory
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: AVCC pin
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=0x60;
ADCSRA=0xDE;
DDRB = 0x1F; //outpus// Sets ports to output
DDRC = 0x00; //inputs// Sets pin inputs
/***init Timer0***/
TCCR0 = 0x04; //prescaler 256
TCNT0 = 241; //init count
TIMSK |= 0x01; //unmask timer 0 overflow interrupt
SREG |= 0x80;
PORTB = 0x00;
reset = 0;
while(1)
{
YELLOW = RED = buzzer_on = MOTOR = 0;
// Sets all to 0 (Green is go!)
GREEN = 1;
state = 0;
do{
while(adc_data > 230); //wait eye to lose if not green led on
delay_ms(110); //test if is not a tab
if(adc_data > 230) //in eye still closed go to green
state = 1;
else
state = 0;
}while(state == 0);
// Eye closes, interrupts green
GREEN = 0; //gren led off
YELLOW = buzzer_on = 1; //buzzer on
reset = 0;
i=0;
do{
delay_us(999);
if(!RESET)
{
while(!RESET); //wait reset
reset = 1; //indicate has been a reset
}
i++;
}while(!reset && i <100);
// reset routine
if(reset != 1 ) //if reset don't wait
{
YELLOW = 0;
RED = MOTOR = 1;
while(RESET); //wait reset
while(!RESET);
}
}
}