Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Small problem in MikroC ( LCD clock)

Status
Not open for further replies.

BGAmodz

Member
Hello every one here in the board .

During the past days , i have been learning how to set up programs to run them on Microcontrolers like the PICxxFxx .
Now i am working on making a small ajustable electronic clock using the PIC16F84A but i have some minor problems ; for example i have a variable s1 that goes from 0 to 9 and goes back to 0 in a loop ; but on my case if the variable goes from 0 to 9 , i have 012345679:1...........
i got ':' in place of 0 .

i should get ....901.............

here is the program :

Code:
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D7 at RB6_bit;
sbit LCD_D6 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D4 at RB3_bit;


sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB6_bit;
sbit LCD_D6_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB3_bit;


char s1,s2,m1,m2,h1,h2,p,j;



void main() {
Lcd_Init();
Lcd_cmd(_lcd_cursor_off);
s1=s2=m1=m2=h1=h2=48;
p=58;
lcd_chr(1,6,p);  lcd_chr(1,3,p);
lcd_chr(1,8,s1); lcd_chr(1,7,s2);
lcd_chr(1,5,m1); lcd_chr(1,4,m2);
lcd_chr(1,2,h1); lcd_chr(1,1,h2);
for(;;){

s1++;
delay_ms(1000);
lcd_chr(1,6,p);  lcd_chr(1,3,p);
lcd_chr(1,8,s1); lcd_chr(1,7,s2);
lcd_chr(1,5,m1); lcd_chr(1,4,m2);
lcd_chr(1,2,h1); lcd_chr(1,1,h2);
if (s1==58){s1=48;s2++;}
if (s2==54){s1=s2=48;m1++;}
if (m1==58){s1=s2=m1=48;m2++;}
if (m2==54){s1=s2=m1=m2=48;h1++;}
if (h1==58){s1=s2=m1=m2=h1=48;h2++;}
if (h2==50){s1=s2=m1=m2=h1=h2=48;}

}
}
 
ASCII 48 is the character '0'
ASCII 49 is the character '1'
...
ASCII 57 is the character '9'
ASCII 58 is the character ':'

At the moment you check if s1==58
When you check for that value, you have already displayed it, because the IF statement is after the lcd commands.
Then you set s1=48, which is the character '0', but, at the beginning of the loop you add 1 to the value. So the next character displayed is '1'.
 
Beat me to it :)

Your logic is wrong, as Mister T mentioned, you are setting/ manipulating your variables after you have already displayed them. I also don't think that you are handling your rollover from 23:59 properly.

My apologies, after re-reading it, and thinking about it for a minute I realised that you are displaying a 12hr format. I'm so used to working in 24hr format I didn't even notice!
 
Last edited:
Try:
C:
for(;;) {
    delay_ms(1000);

    s1++;

    if (s1==58){s1=48;s2++;}
    if (s2==54){s1=s2=48;m1++;}
    if (m1==58){s1=s2=m1=48;m2++;}
    if (m2==54){s1=s2=m1=m2=48;h1++;}
    if (h1==58){s1=s2=m1=m2=h1=48;h2++;}
    if (h2==50){s1=s2=m1=m2=h1=h2=48;}

    lcd_chr(1,6,p);  lcd_chr(1,3,p);
    lcd_chr(1,8,s1); lcd_chr(1,7,s2);
    lcd_chr(1,5,m1); lcd_chr(1,4,m2);
    lcd_chr(1,2,h1); lcd_chr(1,1,h2);
}

EDIT: This should at least fix the problem with "s1". I did not track the other variables.. there may be problems with them (or not).
 
Last edited:
ASCII 48 is the character '0'
ASCII 49 is the character '1'
...
ASCII 57 is the character '9'
ASCII 58 is the character ':'

At the moment you check if s1==58
When you check for that value, you have already displayed it, because the IF statement is after the lcd commands.
Then you set s1=48, which is the character '0', but, at the beginning of the loop you add 1 to the value. So the next character displayed is '1'.
As you can see on the program i have s1 incremented from 0 and forward , when this variable reaches 10 it should be transformed to 0 then 1 and so on.
 
Yes, we got the problem.
Did you try my solution? Move the LCD commands after the IF statements. I think it should work.
 
Posts #2 and #3 explains it.. I don't know how to explain it better than that. Just carefully think about what the code does.. step-by-step.
 
Hey thanks for the help , now i understand , i should have put the s++ right before the conditions .
 
Yes. That is very good style. Always validate values immediately after you change them.
 
Status
Not open for further replies.

Latest threads

Back
Top