Đồng hồ hiển thị lcd ( Dùng TMR1 )

/*
 * File:   dongho_timr2.c
 * Author: PhanHoangThinh
 *
 * Created on Ngày 10 tháng 7 n?m 2017, 12:45
 */


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF      // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
void Timer1_init(){
    TMR1=65125;
    TMR1H=TMR1/256;
    TMR1L=TMR1%256;
    TMR1IF=0;
    TMR1CS=1;T1OSCEN=1;
    GIE=PEIE=TMR1IE=0;
    T1CKPS0=0;T1CKPS1=1;
    T1SYNC=0;
    TMR1ON=1;
}

void delay_50ms(unsigned int ms)
{
    unsigned int i;
    for(i=0;i<=ms;i++){
    TMR1=65125;
    TMR1H=TMR1/256;
    TMR1L=TMR1%256;
    TMR1IF=0;
    while(TMR1IF==0);
    }
}
unsigned int phut=0;
int day=0;
int gio=0;
void main(void) {
    ANSEL=ANSELH=0;
    char s[10];
    lcd_init();
    Timer1_init();
    while(1){
        lcd_gotoxy(3,0);
        lcd_puts("Dong ho so");
        sprintf(s,"%u",(gio/10));
        lcd_gotoxy(5,1);
        lcd_puts(s);
        sprintf(s,"%u",(gio%10));
        lcd_gotoxy(6,1);
        lcd_puts(s);
        lcd_puts(":");
        sprintf(s,"%u",(phut/10));
        lcd_gotoxy(8,1);
        lcd_puts(s);
        sprintf(s,"%u",(phut%10));
        lcd_gotoxy(9,1);
        lcd_puts(s);
        lcd_puts(":");
        sprintf(s,"%u",(day/10));
        lcd_gotoxy(11,1);
        lcd_puts(s);
        sprintf(s,"%u",(day%10));
        lcd_gotoxy(12,1);
        lcd_puts(s);

            day++;
            delay_50ms(20);
            if(day==61)
            {
            phut++;
            day=0;
            }
            if(phut==61)
            {
               gio++;
               phut=0;
            }
            if(gio==24)
            {
                gio=0;
            }
    }
}


Previous
Next Post »