Hi,
I am building a digital alarm clock using PIC18f2420 and I it is recommended from me to use C language. I wrote the code for dispalying the hours,minute and seconds and it works successfuly. Now I need to modify the code to set the time using switches in this way(using port B ):
if RB0(switch 0 in the circuit)=1 >>the second display will stop and I will use RB2(switch 2) to controle incrmenting and decremining the seconds.. the same is with minute and hours using another switches and RB..
I tried this code on my circuit but it didn't work.. ca any body help me please
#include <pic18.h>
#define Start_State 0
#define check_State 1
#define inc_State 2
#define dec_State 3
volatile unsigned char hours, mins, secs,State,int_flag ;
int counter;
unsigned char value1 ;
unsigned char a,y,x,x1,y1,b, v2;
void interrupt timer_isr(void) {
if (TMR2IF){
TMR2IF= 0;
counter = counter+ 1;
if ( counter == 800){
counter = 0;
secs++;
if (secs == 60){
secs =0;
mins++;
if (mins == 60){
mins =0;
hours++;
if ( hours == 24)
hours = 0;
}
}
}
}
}
void interrupt sw_isr(void){
if ( INT0IF){
INT0IF= 0;
int_flag= 1;
switch(State){
case Start_State:
if (RB0){
State= check_State;
}
break;
case check_State:
v2= RB2;
if(v2)
{
State= inc_State;
}
else
{
State= dec_State;
}
break;
case inc_State:
secs++;
State= Start_State;
break;
case dec_State:
secs--;
State= Start_State;
break;
}
}
}
void main(void){
// Configure timer2
TOUTPS3=1; TOUTPS2= 0; TOUTPS1=0; TOUTPS0= 1; // postscale values
T2CKPS1= 0; T2CKPS0= 1; // prescale values
PR2= 250;
// enable timer intrrupt
TMR2ON= 1; // turn on timer 2
PEIE=1; GIE= 1; IPEN=0;
TMR2IF= 0;// clear timer 2 intrrupt flag
TMR2IE= 1; // to allow timer 2 to intrrupt
// enable the intrrupt of the switch for sec INT0
RBPU= 0;
INTEDG0= 0;
INT0IF= 0;
INT0IE= 1;
// setting of port B as an output port
TRISB = 0x05; // set RB0 and RB2 as input others are output
PORTB = 0x00; // clear port B
TRISC=0X00;
PORTC=0X00;
RB5=0X0;
RB6=0X0;
RB7=0X00;
while (1)
{
a=secs;
x=a/10;
y=a%10;
b=mins;
x1=b/10;
y1=b%10;
value1=x;
PORTC=value1;
RB5=0X00;
RB6=0X00;
RB7=0X01;
value1=y;
RB7=0X00;
PORTC=value1;
RB5=0X01;
RB6=0X00;
RB7=0X01;
RB7=0X00;
value1=x1;
PORTC=value1;
RB5=0X00;
RB6=0X01;
RB7=0X01;
RB7=0X00;
value1=y1;
PORTC=value1;
RB5=0X01;
RB6=0X01;
RB7=0X01;
RB7=0X00;
}
}
I am building a digital alarm clock using PIC18f2420 and I it is recommended from me to use C language. I wrote the code for dispalying the hours,minute and seconds and it works successfuly. Now I need to modify the code to set the time using switches in this way(using port B ):
if RB0(switch 0 in the circuit)=1 >>the second display will stop and I will use RB2(switch 2) to controle incrmenting and decremining the seconds.. the same is with minute and hours using another switches and RB..
I tried this code on my circuit but it didn't work.. ca any body help me please
#include <pic18.h>
#define Start_State 0
#define check_State 1
#define inc_State 2
#define dec_State 3
volatile unsigned char hours, mins, secs,State,int_flag ;
int counter;
unsigned char value1 ;
unsigned char a,y,x,x1,y1,b, v2;
void interrupt timer_isr(void) {
if (TMR2IF){
TMR2IF= 0;
counter = counter+ 1;
if ( counter == 800){
counter = 0;
secs++;
if (secs == 60){
secs =0;
mins++;
if (mins == 60){
mins =0;
hours++;
if ( hours == 24)
hours = 0;
}
}
}
}
}
void interrupt sw_isr(void){
if ( INT0IF){
INT0IF= 0;
int_flag= 1;
switch(State){
case Start_State:
if (RB0){
State= check_State;
}
break;
case check_State:
v2= RB2;
if(v2)
{
State= inc_State;
}
else
{
State= dec_State;
}
break;
case inc_State:
secs++;
State= Start_State;
break;
case dec_State:
secs--;
State= Start_State;
break;
}
}
}
void main(void){
// Configure timer2
TOUTPS3=1; TOUTPS2= 0; TOUTPS1=0; TOUTPS0= 1; // postscale values
T2CKPS1= 0; T2CKPS0= 1; // prescale values
PR2= 250;
// enable timer intrrupt
TMR2ON= 1; // turn on timer 2
PEIE=1; GIE= 1; IPEN=0;
TMR2IF= 0;// clear timer 2 intrrupt flag
TMR2IE= 1; // to allow timer 2 to intrrupt
// enable the intrrupt of the switch for sec INT0
RBPU= 0;
INTEDG0= 0;
INT0IF= 0;
INT0IE= 1;
// setting of port B as an output port
TRISB = 0x05; // set RB0 and RB2 as input others are output
PORTB = 0x00; // clear port B
TRISC=0X00;
PORTC=0X00;
RB5=0X0;
RB6=0X0;
RB7=0X00;
while (1)
{
a=secs;
x=a/10;
y=a%10;
b=mins;
x1=b/10;
y1=b%10;
value1=x;
PORTC=value1;
RB5=0X00;
RB6=0X00;
RB7=0X01;
value1=y;
RB7=0X00;
PORTC=value1;
RB5=0X01;
RB6=0X00;
RB7=0X01;
RB7=0X00;
value1=x1;
PORTC=value1;
RB5=0X00;
RB6=0X01;
RB7=0X01;
RB7=0X00;
value1=y1;
PORTC=value1;
RB5=0X01;
RB6=0X01;
RB7=0X01;
RB7=0X00;
}
}
Last edited: