New programmer simple problem I don't understand in C

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
David Halliday
Posts: 9
Joined: Tue Jan 10, 2006 4:36 pm
Location: Hampshire
Contact:

New programmer simple problem I don't understand in C

Post by David Halliday »

:oops:

Hi I'm a very new to programming microcontroller and have been trying to set RA4 as an input and as the switch is hit RB1 shall light and after 4 pulses RB2 should light and RB1 stop and after 10 input pulses it should reset. But for some reason it does not do this.
Also RB7 AND RB6 remain on constantly why. If you change the bounce time it cause RB0 to light also why ?


Why do Ra0-ra3 work as input switches are they by default analog and have to be told to be digital ?

Any help greatly recieved as i've fallen at the first hurdle !

Dave

[

unsigned char count=0;
unsigned char oldv, newv ;




unsigned char key ( void ) // Routine from Matrix teaching disk
{
unsigned char count=0;
unsigned char oldv, newv ;
oldv = input_pin_port_b(4);
while ( count<20 )
{
newv = input_pin_port_b(4);
if ( oldv==newv )
{
count++ ;
}
else
{
count=0 ;
oldv=newv ;
}
}
return oldv ;
}





void main ()
{
int x=0; // Set count

set_tris_a (0xff) ; /* set all of PORTB for input */
set_tris_b (0x00) ; /* set bit 0 of PORTA for output */

while(1)
{

while (key()==0) // when switch not depress no LED light
{
output_low_port_b(1);
output_low_port_b(2);
}

while(key()==1)
{
x=x+1;

if (x==10) // Once x=10 reset 'X'
{
x=0;
}
}


while((key()==1)&&(x<4)) //Light LED 2 for first 4 presses
{
output_high_port_b(2);
}


while((key()==1)&&(x>4)) // After 4 press light led 1
{
output_high_port_b(1);
}


}

}









][/code]

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Hi Dave,

No need for the 'embarassed' icon - everyone has to start somewhere!

The 'key' routine seems to be returning the state of B4 rather than A4 - could this be the source of your problem?

The other problem with your code is the program flow through the "while(1)" loop in the main function. In words, here's what it seems to do:

(1) while the key is not pressed, B1 & B2 = 0 - the program will stay in this loop while the key is not pressed (it will only leave it when the key has been pressed).

(2) while the key is pressed, increment x (x will increase many times while the key is pressed) - the program will stay in this loop whil the key is pressed (it will only leave this loop when the key has been released).

(3) & (4) The other while statements will probably never be active because your program will only reach these statements once the key as been released (it is held in (2) until then).

A better program structure may be as follows:

Code: Select all

set_port_b(0x00); //clear port B

x = 0;

while (1)
{
    //wait until key has been pressed:
    while (key() == 0);
    
    //key has been pressed, so increment our variable
    x = x + 1;
    
    //at x==1, B1 should come on:
    if (x == 1)
    {
        output_high_port_b(1);
    }

    //at x==4, B2 should come on and B1 go off:
    if (x == 4)
    {
        output_low_port_b(1);
        output_high_port_b(2);
    }
    
    //at x==10, it should reset
    if (x == 10)
    {
        set_port_b(0x00); //clear port B
        x = 0;
    }

    //wait until key has been released:
    while (key() == 1);
}
I've not tried this, so it may not work - but it may help to get you going.

Post Reply