RTC code

Code:
#include <p18f458.h>

#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config DEBUG = OFF
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF

#define data PORTB
#define en PORTCbits.RC1
#define rs PORTCbits.RC2
#define rw PORTCbits.RC3


void delay(unsigned int);
void lcddata(unsigned char value);
void lcdcmd(unsigned char value);

void chk_isr(void);
void inc (void);
void minute(void);


unsigned char count,mins,hours;

#pragma code Hi_Prio_int=0x008
void Hi_Prio_int(void)
{
_asm
GOTO chk_isr
_endasm
}

# pragma code

#pragma interrupt chk_isr
void chk_isr(void)
{
	 if(INTCONbits.TMR0IF==1)
	 
	 inc();
}


void inc (void)
{	
	count=count+1;

	TMR0H = 0x0B;
	TMR0L = 0xDC;
	INTCONbits.TMR0IF=0;
	if (count>=60)
	
	minute();
}	
void minute(void)	
	{count=0;
		mins++;
		
	if(mins>=60)
	{
	mins=0;
	hours++;
	if(hours>12)
	{hours=0;
	}
	
	}
		
	}
	
#pragma code
void main(void)

{
	
TRISB=0;
TRISC=0;
// Timer0 Registers:' 16-Bit Mode; Prescaler=1:16; TMRH Preset=B; TMRL Preset=DC; Freq=1.00Hz; Period=1.00 s
T0CONbits.TMR0ON = 1 ; // Timer0 On/Off Control bit:1=Enables Timer0 / 0=Stops Timer0
T0CONbits.T08BIT = 0 ; // Timer0 8-bit/16-bit Control bit: 1=8-bit timer/counter / 0=16-bit timer/counter
T0CONbits.T0CS   = 0 ; // TMR0 Clock Source Select bit: 0=Internal Clock (CLKO) / 1=Transition on T0CKI pin
T0CONbits.T0SE   = 0 ; // TMR0 Source Edge Select bit: 0=low/high / 1=high/low
T0CONbits.PSA    = 0 ; // Prescaler Assignment bit: 0=Prescaler is assigned; 1=NOT assigned/bypassed
T0CONbits.T0PS2  = 0 ; // bits 2-0  PS2:PS0: Prescaler Select bits
T0CONbits.T0PS1  = 1 ; //
T0CONbits.T0PS0  = 1 ; //
TMR0H = 0x0B     ; // preset for Timer0 MSB register
TMR0L = 0xDC     ; // preset for Timer0 LSB register
lcdcmd(0x38);
delay(1);
lcdcmd(0x0C);
delay(1);
lcdcmd(0x01);
delay(1);
// enabling intterupts


INTCONbits.TMR0IE=1;
INTCONbits.PEIE=1;
INTCONbits.GIE=1;


while(1)
{	
lcdcmd(0x84);
delay(1);

//delay(1);
lcddata('M');
delay(1);
lcddata('e');
delay(1);
lcddata('h');
delay(1);
lcddata('t');
delay(1);
lcddata('a');
delay(1);
lcddata('b');
delay(1);
lcdcmd(0xC3);
delay(1);
lcddata((hours/10)%10 + '0');
delay(1);
lcddata(hours%10 + '0');
delay(1);
lcdcmd(0xC5);
lcddata(':');

lcdcmd(0xC6);
delay(1);
lcddata((mins/10)%10 + '0');
delay(1);
lcddata(mins%10 + '0');
delay(1);
lcdcmd(0xC8);
lcddata(':');
lcdcmd(0xC9);
delay(1);
lcddata((count/10)%10 + '0');
delay(1);
lcddata(count%10 + '0');
delay(1);

}

}
/*--------------------------*/
void lcdcmd(unsigned char value)

{
data=value;
rs=0;
rw=0;
en=1;
delay(1);
en=0;

}
void lcddata(unsigned char value)
{
data=value;
rs=1;
rw=0;
en=1;
delay(1);
en=0;

}

void delay(unsigned int x)
{
unsigned char a;
unsigned int b;
for(a=0;a<x;a++)
for(b=0;b<165;b++);

}

/*T0CON=0x08;
TMR0H=0x35;
TMR0L=0x00;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;*/

Blog entry information

Author
ect_09
Views
1,119
Last update

More entries in General

Share this entry

Top