SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By o_0
#119396
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.)
Code: Select all
void dataFromMouse(void)
{   
[color=#FF0000]     DDRB=0xFF;
     PORTB=0x00;
     ADCSRA=0b11000111;[/color]

    /////////////////// X /////////////////////     

    ADMUX =  0b01100000; //read voltage on A.0 for x axis
[color=#FF0000]    ADCSRA=0b11000111;
    //[/color]ADCSRA.6 = 1;

    //ADCSRA.6 bit will be clear after the conversion is done.
    while(ADCSRA.6 == 1){}

    xADC = ADCH;    //read the ADC value
[color=#FF0000]   if(ADCH!=0){ PORTB.0=1;}
   if(ADCH==0){ PORTB.0=1;}[/color]
    /////////////////// Y /////////////////////           
    ADMUX =  0b01100001; //read voltage on A.1 for y axis
[color=#FF0000]    ADCSRA=0b11000111;
    //ADCSRA.6 = 1;[/color]

    //ADCSRA.6 bit will be clear after the conversion is done.
    while(ADCSRA.6 == 1){}

    yADC = ADCH;    //read the ADC value
[color=#FF0000]    if(ADCH!=0){ PORTB.1=1;}
    if(ADCH==0){ PORTB.1=1;}[/color]
    /////////////////// Z /////////////////////           
    ADMUX =  0b01100010; //read voltage on A.2 for z axis
[color=#FF0000]    ADCSRA=0b11000111;
    //[/color]ADCSRA.6 = 1;

    //ADCSRA.6 bit will be clear after the conversion is done.
    while(ADCSRA.6 == 1){}

    zADC = ADCH;    //read the ADC value
[color=red]   if(ADCH!=0){ PORTB.2=1;}
   if(ADCH==0){ PORTB.2=1;}[/color]
    //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); 

} 
You do not have the required permissions to view the files attached to this post.
User avatar
By itikhonov
#119500
Code: Select all
ADCSRA=0b11000111;
Here you enable ADC and start conversion all at once. Not sure it's legal. May be you should enable ADC first without starting conversion:

ADCSRA=0b10000111;

And then start conversion separately by uncommenting
Code: Select all
ADCSRA.6 = 1;