Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] problem in my code ??

Status
Not open for further replies.

sayf alawneh

Junior Member level 3
Joined
Aug 15, 2014
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
256
hello guys am doing a 24 hours digital clock consists of 6 multiplexed seven segments CC the problem is in the push buttons
i use 2 push buttons to incease the minutes (m1) and the other to increase the hours (h1)
but they dont obey my code for example when m1 reaches 9 it stops increasing and shows strange numbers the same for h1 given that my code is working fine when i dont use those 2 push buttons
digital clock.PNG
here is my code

Code:
char x=0,y=0,s1=0,s2=0,m1=0,m2=0,h1=0,h2=0;
unsigned short mask(unsigned short num){
switch(num){
case 0:return 0x3f;
case 1:return 0x06;
case 2:return 0x5B;
case 3:return 0x4F;
case 4:return 0x66;
case 5:return 0x6D;
case 6:return 0x7D;
case 7:return 0x07;
case 8:return 0x7F;
case 9:return 0x6F;
}
}
void Interrupt(){ //initializing timer 1 interrupt every 5 ms 
  if (TMR1IF_bit){
TMR1IF_bit = 0;
TMR1H         = 0xD8;
TMR1L         = 0xF0;
if(y==0){PORTD=1;PORTB=mask(s1);}//s1 and s2 are for seconds
if(y==1){PORTD=2;PORTB=mask(s2);}
if(y==2){PORTD=4;PORTB=mask(m1);}//m1 and m2 are for minutes
if(y==3){PORTD=8;PORTB=mask(m2);}
if(y==4){PORTD=16;PORTB=mask(h1);}h1 and h2 are for hours
if(y==5){PORTD=32;PORTB=mask(h2);}
y++; //increment y every 5 ms (eye delay)
x++;//increment x every 5 ms so when x is 200 we can get a 1 second delay to increment s1 counter
if(y==6)y=0;
if(x==200){s1++;x=0;} 
}
}
void main() {
T1CON= 0x01;//  
TMR1IF_bit=0;//  
TMR1H=0xD8;//         <~~~ setting the timer 1 interrupt for 5 ms 
TMR1L=0xF0;//    
TMR1IE_bit=1;//   
INTCON=0xC0;//
PORTB=0;
TRISB=0; // port b as output for the 7 segments
TRISD=0; // port d as output for the transistors
TRISC=255;// port c as input for the switches 
for(;;){ 
if(s1==10){s1=0;s2++;
if(s2==6){s2=0;m1++;
if(m1==10){m1=0;m2++;
if(m2==6){m2=0;h1++;
if(h1==10){h1=0;h2++;
if(h2==2&&h1==4){h2=0;h1=0;m2=0;m1=0;s2=0;s1=0; if the clock is 24 make all digits 0 
}}}}}}
if(RC0_BIT==0){m1++;// if the button connected to rc0 is pushed increment 1 for the m1 digit
while(rc0_bit==0){}}// dont increment while the button is pushed 
if(RC1_bit==0){h1++;// if the button connected to rc1 is pushed increment 1 for the h1 digit
while(rc0_bit==0){}}// dont increment while the button is pushed 

}
}
 
Last edited by a moderator:

Use a total of 4 buttons. One will be Set button. If this is pressed then a flag is set to 1. If this button is pressed again then different SSDs are selected and the selection of SSDs cycle on pressing the button. 2 buttons will be used for incrementing and decrementing the value of selected SSD. The 4th button is to get out of SET mode and to enter RUN mode. As you are using PIC then you can use one ADC pin to interface the 4 buttons and use ADC for key press detection.
 

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
if(RC0_BIT==0)
{
  if(++m1 > 9)
  {
    m1 = 0;
    if(++m2 > 5)
    m2 = 0;
  }
  delay_ms(50); // in practical circuit the push buttons will have key bounce
  while(rc0_bit==0);
}



Do the same for hour increment.
 

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
if(RC0_BIT==0)
{
  if(++m1 > 9)
  {
    m1 = 0;
    if(++m2 > 5)
    m2 = 0;
  }
  delay_ms(50); // in practical circuit the push buttons will have key bounce
  while(rc0_bit==0);
}



Do the same for hour increment.
hmmm i think u forgot the m1++, ill try this and give u a reply any ways and before trying why should i re do the conditions for the push buttons dont this part of the code do the same thing ? ??
code:

Code dot - [expand]
1
2
3
4
5
6
if(s1==10){s1=0;s2++;
if(s2==6){s2=0;m1++;}
if(m1==10){m1=0;m2++;
if(m2==6){m2=0;h1++;
if(h1==10){h1=0;h2++;
if(h2==2&&h1==4){h2=0;h1=0;m2=0;m1=0;s2=0;s1=0;}}}}}}

 
Last edited by a moderator:

you will check the minute and hour only when second is 10, but that can be edited any time. also there should be >= instead of ==.

try the code i given in place of push button code, it will work fine.
 
now i understand ur code the m1++ is not needed
sorry ,
any ways it worked fine for the minutes but when i tried it for hours it completed incrementing even after reaching 24
here is the code i used

code:

Code dot - [expand]
1
2
3
4
if(RC1_bit==0){if(++h1 > 9){ h1 = 0;if(++h2 > 2 && ++h1 > 4){h2=0;h1=0;}}
delay_ms(100);
while(rc1_bit==0){};
}



i dont know where is the problem here
 
Last edited by a moderator:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(RC1_bit==0)
{
  if(++h1 > 9)
  {
    h1 = 0;
    h2++;
  }
  if(h1 >= 4 && h2 >= 2)
  {
    h1 = 0;
    h2 = 0;
  }
  delay_ms(100);
  while(rc1_bit==0);
}

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top