How to i turn this into a digital clock for microcontroller pic16f1829 in Mplab?
ID: 2081930 • Letter: H
Question
How to i turn this into a digital clock for microcontroller pic16f1829 in Mplab?
void Init_LCD(void);
void PutCMD_LCD(char);
void PutDATA_LCD(char);
void Busy_LCD(void);
void Delay_S(int);
void Delay_S(int);
int main(int argc, char** argv) {
char Letter;
__delay_ms(30);
//Set up for Basic Clock
ANSELA = 0;
TRISA = 0;
ANSELB = 0;
TRISB = 0;
ANSELC = 0;
TRISC = 0;
OSCCON = 0X6A;
RA5 ^= 1;
Init_LCD();
while(1){
__delay_ms(300);
PutCMD_LCD (0xC0); //set to lower line on LCD display
Delay_S(20000);
Letter = 'D';
PutDATA_LCD(Letter);
Letter = 'r';
PutDATA_LCD(Letter);
Letter = ' ';
PutDATA_LCD(Letter);
Letter = 'M';
PutDATA_LCD(Letter);
Letter = 'o';
PutDATA_LCD(Letter);
Letter = 'r';
PutDATA_LCD(Letter);
Letter = 't';
PutDATA_LCD(Letter);
Letter = 'o';
PutDATA_LCD(Letter);
Letter = 'n';
PutDATA_LCD(Letter);
Delay_S(30000);
}
return ;
}
Explanation / Answer
code:
int ms;
int sec;
int min;
int hour;
unsigned char bfr[4];
void Init_LCD(void);
void PutCMD_LCD(char);
void PutDATA_LCD(char);
void Busy_LCD(void);
void Delay_S(int);
void Delay_S(int);
void Timer0_ISR interrupt (void);
int main(int argc, char** argv)
{
char Letter;
__delay_ms(30);
//Set up for Basic Clock
ANSELA = 0;
TRISA = 0;
ANSELB = 0;
TRISB = 0;
ANSELC = 0;
TRISC = 0;
OSCCON = 0X6A;
RA5 ^= 1;
// Here I considerd 500 kHz as Fosc.
// timer0 Uses 500/4 = 125 kHz as clock.
OPTION_REGbits.INTEDG = 1;
OPTION_REGbits.TMR0SE = 1;
OPTION_REGbits.PSA = 0;
OPTION_REGbits.PS = 0b101; // 64 prescaler
TMR0 = -2;
INTCONbits.GIE = 1;
INTCONbits.TMR0IE = 1;
Init_LCD();
__delay_ms(300);
PutCMD_LCD (0xC0); //set to lower line on LCD display
Delay_S(20000);
Letter = 'D';
PutDATA_LCD(Letter);
Letter = 'I';
PutDATA_LCD(Letter);
Letter = 'G';
PutDATA_LCD(Letter);
Letter = 'I`';
PutDATA_LCD(Letter);
Letter = 'T';
PutDATA_LCD(Letter);
Letter = 'A';
PutDATA_LCD(Letter);
Letter = 'L';
PutDATA_LCD(Letter);
Letter = ' ';
PutDATA_LCD(Letter);
Letter = 'C';
PutDATA_LCD(Letter);
Letter = 'L';
PutDATA_LCD(Letter);
Letter = 'C';
PutDATA_LCD(Letter);
Letter = 'K';
PutDATA_LCD(Letter);
Delay_S(30000);
while(1)
{
PutCMD_LCD (0xD3); //set to lower line on LCD display
bin_bcd(hour);
Letter = bfr[1]; PutDATA_LCD(Letter);
Letter = bfr[0]; PutDATA_LCD(Letter);
Letter = ':'; PutDATA_LCD(Letter);
bin_bcd(min);
Letter = bfr[1]; PutDATA_LCD(Letter);
Letter = bfr[0]; PutDATA_LCD(Letter);
Letter = ':'; PutDATA_LCD(Letter);
bin_bcd(sec);
Letter = bfr[1]; PutDATA_LCD(Letter);
Letter = bfr[0]; PutDATA_LCD(Letter);
}
return ;
}
void Timer0_ISR interrupt (void)
{
if(INTCONbits.TMR0IF)
{
// Every 1 ms this timer interrupt will be generated.
INTCONbits.TMR0IF = 0;
TMR0 = -2;
if(++ms > 999)
{
ms = 0;
if(sec > 59)
{
sec = 0;
if(min > 59)
{
min = 0;
if(hour > 23)
{
hour = 0;
}
}
}
}
}
}
void bin_bcd (int x)
{
int k = x;
bfr[0] = bfr [1] = bfr [2] = bfr [3] = 0;
while (k > 999)
{
k -= 999;
bfr[3]++;
}
while (k > 99)
{
k -= 99;
bfr[2]++;
}
while (k > 9)
{
k -= 9;
bfr[1]++;
}
bfr[0]++;
}
In code timer interrupt of 500 us for clock update.
The Fosc changes the prescaler and timer0 counter value most be also changed.
Binary to BCD converter is added for display of time.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.