I have attached code (for Codevision) and a schematic for an accelerometer mouse with PS/2 interface. It does not produce mouse movement on screen (data out and clock out just stay on +5V all the time) so I’m trying to debug it piece by piece. The following function gets accelerometer input, and processes it through the ADC of Atmega32a. The parts in red are what I have changed for testing purposes. As you can see, the LEDs should light no matter what, but they don’t. The uC is brand new. What’s wrong with my ADC?
(LEDs will light if I just put PORTB.0=1, etc instead of the if statement. The color tags are not in the actual code, they are just there to indicate the parts that were changed for testing purposes. Also, elsewhere in the code the sleep enable bit was set, MCUCR=0b10000000, but I also commented this out for testing purposes. I’m using a 12 MHz crystal.)
void dataFromMouse(void)
{
DDRB=0xFF;
PORTB=0x00;
ADCSRA=0b11000111;
/////////////////// X /////////////////////
ADMUX = 0b01100000; //read voltage on A.0 for x axis
ADCSRA=0b11000111;
//ADCSRA.6 = 1;
//ADCSRA.6 bit will be clear after the conversion is done.
while(ADCSRA.6 == 1){}
xADC = ADCH; //read the ADC value
if(ADCH!=0){ PORTB.0=1;}
if(ADCH==0){ PORTB.0=1;}
/////////////////// Y /////////////////////
ADMUX = 0b01100001; //read voltage on A.1 for y axis
ADCSRA=0b11000111;
//ADCSRA.6 = 1;
//ADCSRA.6 bit will be clear after the conversion is done.
while(ADCSRA.6 == 1){}
yADC = ADCH; //read the ADC value
if(ADCH!=0){ PORTB.1=1;}
if(ADCH==0){ PORTB.1=1;}
/////////////////// Z /////////////////////
ADMUX = 0b01100010; //read voltage on A.2 for z axis
ADCSRA=0b11000111;
//ADCSRA.6 = 1;
//ADCSRA.6 bit will be clear after the conversion is done.
while(ADCSRA.6 == 1){}
zADC = ADCH; //read the ADC value
if(ADCH!=0){ PORTB.2=1;}
if(ADCH==0){ PORTB.2=1;}
//We need to record the init reading of ADC for computing relative position
//the initial position of the mouse should be positioned on a flat surface.
if(xCal == 0)
{
xCal = xADC;
yCal = yADC;
zCal = zADC;
}
//displayAxis(xADC, yADC, zADC);
}