Hello Everyone
(Please excuse if this is in a wrong section)
So I have strted my journey with C programming for PIC.
I got stuck when I want to display a 0 when counting down from 9 in a while loop
I have an array of numbers that I send to display ports of my MCU and when I count from 0 to 9 it is all good but not when counting down.
I think the problem is somewhere in here:
Code:
if (digit<0) digit=9;
but I do not know how to correct it.
I want to display 0 and then start from 9 again.
Full code:
Code:
unsigned char const SEGMENT_MAP[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void main(void) {
TRISC = 0; //Set PortC to all outputs
TRISBbits.RB6=1; //Set Port B7 as an input
OSCCON = 0X76; //SET fOR INTERNAL OSCCILATOR
char digit=9;
while(1){
if (SW1) //Check if switch SW1 is closed
{
for(int c=0; c<=20; c++)__delay_ms(5); //debouncing wait for 100ms
if (SW1) //Check if switch SW1 is still closed
{
PORTC = (SEGMENT_MAP[digit]);
for (int count=0; count<20; count++) __delay_ms(50); //Delay 1 second
digit--;
if (digit<0) digit=9;
}
}
else
{
PORTC = (SEGMENT_MAP[digit]);
}
}
}
Thank you all for your help and your expertise