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.

Days settings problem (PIC based LCD clock )

Status
Not open for further replies.
I was running it on ISIS and it was working just fine.... I disabled the interrupts and I could alter all three values...

I have converted it to XC8, but still should be the same on your compiler...
OK how about the TMR0 interrupt ??? , i need to keep that interrupt working all the time on my application . you say i should disable all the interrupts , am a bit confused here to be honest , i have two interrupts to use one is self-active (TMR0) & one is activated by a button .
before i implemented the TMR0 interrupt everything was wrking fine but after that , i got that s1 overflow , isn't the problem in the TMR0 interrupt only ..?
You can take a look at the changes ive done to my code up page
 
If you keep the clock interrupts running while adjusting the clock, then the clock changes by the interrupt will interfere with the human adjusting the clock. This is, at best, annoying to the person who adjusts the clock.

The easiest way to deal with that is to have an "edit" flag. You set it when adjusting and clear it when adjustment is done. When this flag is true, the interrupt code doesn't adjust the clock (or only adjusts the seconds without carrying into minutes).

You can also avoid the adjustments if you install a GPS chip or atomic clock radio to set the time automatically.
 
When adjusting the time, you need to stop the timer interrupt otherwise it will always be screwed up. You re-enable the interrupt again when you are exiting your adjustment routine. Like most clocks, when you set the time, the seconds are zeroed, so you set the clock accurate to the whole minute. So when you adjust the time, disable tmr0 interrupt, zero the seconds, adjust your hours and minutes with your buttons, clear your tmr0 interrupt flag, re-enable your tmr0 interrupt and exit your routine :)
 
What about the seconds overflow when arriving at the days adjust ???

You need to go intelligently about this. For example, I want to adjust the hour for daytime savings (I really do not want to, but I don't have much choice), and while I'm doing so seconds are overflowing. If you don't carry this into minutes, I will have to adjust minutes manually. What happens if minutes overflow when I'm doing that? Should it be carried into hours? If you're adjusting for daytime they definitely should, but if you're simply setting the clock - absolutely no way. So, you need to decide what is good for most common patterns of use. It may get complicated if you want to stay user-friendly.
 
Ok i have made changes ad got ride of that annoying overflow , but i cant adjust days , i can only move by one step , monday > tuesday ; when i release the button , day goes back to monday .

Here is the changes i made :


void interrupt(){
if(intcon.t0if){
t++;}
if(t==15){

s1++;


t=0;
}
intcon.t0if=0;
if(intcon.intf){
r=~r;
}
intcon.intf=0;

}
void DisplayD()
{

if(j==1){Lcd_out(1,11,"MON");}
if(j==2){Lcd_out(1,11,"TUE");}
if(j==3){Lcd_out(1,11,"WED");}
if(j==4){Lcd_out(1,11,"THU");}
if(j==5){Lcd_out(1,11,"FRI");}
if(j==6){Lcd_out(1,11,"SAT");}
if(j==7){Lcd_out(1,11,"SUN");}
if(j==8){j=1;}
}
void Display (){
Lcd_chr(1,8,s1);
Lcd_chr(1,7,s2);
Lcd_chr(1,6,p);
Lcd_chr(1,5,m1);
Lcd_chr(1,4,m2);
Lcd_chr(1,3,p );
Lcd_chr(1,2,h1);
Lcd_chr(1,1,h2);
DisplayD();
}
void main() {
INTCON=0xB0;
OPTION_REG=0x17;
TRISB.b0=1;
PORTB.b0=0;
TRISA.b0=1;
PORTA.b0=0;
r=0;
tmr0=0;
Lcd_Init();
Lcd_cmd(_LCD_CURSOR_OFF);

Display ();
s1=s2=m1=m2=h1=h2=48;
p=58;
j=1;

for( ; ; ) {

Display ();



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 (h1==52&&h2==50){s1=s2=m1=m2=h1=h2=48;j++;}



//CLOCK ADJUST
while(r==1){

//minutes

Lcd_cmd(_lcd_clear);delay_ms (500);Display (); delay_ms (500);Lcd_cmd(_lcd_clear);delay_ms(500);Display();delay_ms(500);
while(porta.b0==0){
intcon.t0if=0;
Display();
m1++;
if(m1==58){m1=48;m2++;}
if(m2==54){m1=48;m2=48;}
delay_ms(100);
}
//Hours
Lcd_cmd(_lcd_clear);delay_ms (500);Display (); delay_ms (500);Lcd_cmd(_lcd_clear);delay_ms(500);Display();delay_ms(500);
while(porta.b0==0){
intcon.t0if=0;
Display();
h1++;
if(h1==58){h1=48;h2++;}
if(h2==50&&h1==52){h1=48;h2=48;}
delay_ms(200);
}
//Days
Lcd_cmd(_lcd_clear);delay_ms (500);Display (); delay_ms (500);Lcd_cmd(_lcd_clear);delay_ms(500);Display();delay_ms(500);
while(porta.b0==0){
intcon.t0if=0;
Display ();
j++;
if(j==8){j=1;}
delay_ms(500);

}
r=0;

}
}
}
 
Last edited:
LOL , of course i re-compile .
But really i can't find where the problem is in my code

That's strange. The "==" bug would reset the day to Monday every time DisplayD() runs, but after you fixed it, I cannot see anything else.
 
ok am back guys iam the original poster of this thread am back with my current account BGAmodzX . i hope that ian rogers can do a revision of the problem i had back then.

I am ready to get back at it now that am fully recovering from my mental illness.

Thanks.
 
I am ready to get back at it now that am fully recovering from my mental illness.
Long road.... My wife had a breakdown just over a year ago... Takes a bit to get your life back on track... I'll try not to stress you out!

I'll scroll through my files and see if I can find you original coding ( I know I can get it here, but I want the modified one I did.)
 
Long road.... My wife had a breakdown just over a year ago... Takes a bit to get your life back on track... I'll try not to stress you out!

I'll scroll through my files and see if I can find you original coding ( I know I can get it here, but I want the modified one I did.)
take your time bro
 
Didn't find my code.. But I remember what I did... This is in MikroC The problem was a) disable interrupts whilst changing time and B) for some reason you had this line... if(j=8) j=1; in the display routine... It had to go as it always made day = 1...

C:
char s1,s2,m1,m2,h1,h2,p,j;
char t;
bit r;
sbit LCD_RS at RB6_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D4 at RB2_bit;

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

void interrupt()
     {
     if(intcon.t0if){
     t++;}
     if(t==15){
     s1++ ;
  if(s1==58){s1= 48;}
  t=0;
  }
  intcon.t0if=0;
  if(intcon.intf){
 r=~r;
 }
 intcon.intf=0;

   }
   void DisplayD()
 {
 if(j==1){Lcd_out(1,11,"MON");}
 if(j==2){Lcd_out(1,11,"TUE");}
 if(j==3){Lcd_out(1,11,"WED");}
 if(j==4){Lcd_out(1,11,"THU");}
 if(j==5){Lcd_out(1,11,"FRI");}
 if(j==6){Lcd_out(1,11,"SAT");}
 if(j==7){Lcd_out(1,11,"SUN");}
 }

 void Display (){
 Lcd_chr(1,8,s1);
 Lcd_chr(1,7,s2);
 Lcd_chr(1,6,p);
 Lcd_chr(1,5,m1);
 Lcd_chr(1,4,m2);
 Lcd_chr(1,3,p );
 Lcd_chr(1,2,h1);
 Lcd_chr(1,1,h2);
 DisplayD();
 }
 void main() {
 INTCON=0xB0;
 OPTION_REG=0x17;
 TRISB.b0=1;
 PORTB.b0=0;
 TRISA.b0=1;
 PORTA.b0=0;
 r=0;
 tmr0=0;
 Lcd_Init();
 Lcd_cmd(_LCD_CURSOR_OFF);

 Display ();
 s1=s2=m1=m2=h1=h2=48;
 p=58;
 j=1;

 for(;;){

 Display ();

 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 (h1==52&&h2==50){s1=s2=m1=m2=h1=h2=48;j++;}



  //CLOCK ADJUST
 while(r==1){
  INTCON=0x0;
 //minutes

 Lcd_cmd(_lcd_clear);delay_ms (500);Display (); delay_ms (500);Lcd_cmd(_lcd_clear);delay_ms(500);Display();delay_ms(500);
 while(porta.b0==0){
 Display();
 m1++;
 if(m1==58){m1=48;m2++;}
 if(m2==54){m1=48;m2=48;}
 delay_ms(500);
 }
 //Hours
 Lcd_cmd(_lcd_clear);delay_ms (500);Display (); delay_ms (500);Lcd_cmd(_lcd_clear);delay_ms(500);Display();delay_ms(500);
 while(porta.b0==0){
 Display();
 h1++;
 if(h1==58){h1=48;h2++;}
 if(h2==50&&h1==52){h1=48;h2=48;}
 delay_ms(500);
 }
 //Days
 Lcd_cmd(_lcd_clear);delay_ms (500);Display (); delay_ms (500);Lcd_cmd(_lcd_clear);delay_ms(500);Display();delay_ms(500);
 while(porta.b0==0){

 Display ();

 j++;
 if(j==8){j=1;}
 delay_ms(500);
 }
 r=0;
 INTCON=0xB0;
 }
 }
 }
I think this will work as expected.... NOTE*** I have changed the timing slightly on the adjust as it was too fast to control.
 
ok i will test your code as soon as i install isis.
didnt touch my laptop for about 4 years nor did electronics so i guess i should revise everything again.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top