12 years, 5 months ago.

something really strange

int main() 
{
int keuze;


while(1) 
    {
    pc.printf ("\n\r wat wil je doen, a: startmeting, b: caliberen)");
    pc.scanf("%c", &keuze);
    switch (keuze) 
        {
        case 'a': startmeting(); break;
        case 'b': calibreren (); break;
        default: pc.printf ("foute invoer, probeer opnieuw");
        }    
    }
}

void startmeting()          //we zetten het ctrl register (0) op 1 zodat er een meting uitgevoerd wordt
                            //deze 1 wordt na de meting automatisch terug gereset
                            
                            //put ctrl register on 1, afther measure automatically on 0
{


    unsigned int colorData[4];
    char sendstartsample[2]={0,1};  //sturen eerst 0(register) en dan een 1 (weg te schrijven waarde)
                                    
                                    //first 0 (register) then 1 (value register)
   i2c.write(addr, sendstartsample, 2, 0);     //0 en 1 worden naar slave geschreven
    while(leesregister(0x00) != '0');          //WHEN I INCLUDE THIS FUNCTION IT DOESNT WORK ANYMORE
    pc.printf("\n\r A");
}


void calibreren()
{
pc.printf("\n\r B");
}

char leesregister(char adres)           //we get the adres of a register and we want to send back the value in that register
{
char terugstuurwaarde;              //this is the name of the variable we want to send back
i2c.write(addr, &adres, 1, 0);
i2c.read(addr, &terugstuurwaarde, 1, 0);
return terugstuurwaarde;
}

so my problem is, when i include the leesregister function, my case structure always respons to the default value(so i dont 'enter' the startmeting function when i push a), when i leave this function out of startmeting, i can enter the startmeting function when i push a. i really dont understand why this extra function 'leesregister' has anything to do with entering the startmeting function thnx in advance for all the help

i noticed that from the moment i try to read something from my slave in function'startmeting' it goes wrong and i can not enter 'startmeting' and casestructure always go to default

posted by Adriaan Van Steendam 16 Mar 2013

I don't really see yet why it would go to the default one, but what might be a problem is your while condition: You lock it there until register 0x00 is equal to '0', however that means it waits until the register is equal to the ASCII value that represents '0'. You should just use 0 if it needs to be equal to 0.

posted by Erik - 16 Mar 2013

I don't know either but if the switch takes the default route it indicates that 'keuze' is not 'a' or 'b'. Maybe a printf("%04X", keuze); can help you there.

posted by Ad van der Weiden 18 Mar 2013

1 Answer

12 years, 5 months ago.

Try changing the type of keuze to a char.

...nvm, there was a wrong suggestion here.

posted by Igor Skochinsky 16 Mar 2013