Chào các bạn trong bài đăng này mình sẽ hướng dẫn các bạn làm đồng hồ số hiển thị led LCD 16x02 sử dụng PIC16F877A và trình biên dịch Mplab XC8 đê viết code.
Ảnh mô phỏng protues.
- Đây là code file main.c .
#include "Includes.h"
// Configuration word for PIC16F877A
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON
& LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);
// Main Function
void main(void)
{
InitLCD(); // Initialize LCD in 4bit mode
Init1msecTimerInterrupt(); // Start 1 msec timer
while(1)
{
if( msCounter == 0 ) // msCounter becomes zero after exact one sec
{
// Displays time in HH:MM:SS format
DisplayTimeToLCD(hrCounter, minCounter, secCounter);
}
UpdateTimeCounters(); // Update sec, min, hours counters
}
}
- Đây là code file timer.c .
#include "Includes.h"
// define digital clock variables
unsigned int msCounter = 0;
unsigned int secCounter = 0;
unsigned int minCounter = 0;
unsigned int hrCounter = 0;
void InitTimer0(void)
{
// Timer0 is 8bit timer, select T0CS and PSA to be zero
OPTION_REG &= 0xC1; // Make Prescalar 1:4
T0IE = 1; // Enable Timer0 interrupt
GIE = 1; // Enable global interrupts
}
void Init1msecTimerInterrupt(void)
{
InitTimer0(); // Intialize timer0 to genrate 1msec interrupts
}
void UpdateTimeCounters(void)
{
if (msCounter==1000)
{
secCounter++;
msCounter=0;
}
if(secCounter==60)
{
minCounter++;
secCounter=0;
}
if(minCounter==60)
{
hrCounter++;
minCounter=0;
}
if(hrCounter==24)
{
hrCounter = 0;
}
}
Link download project.
http://adf.ly/11512871/digit-clock-xc8
Ảnh mô phỏng protues.
- Đây là code file main.c .
#include "Includes.h"
// Configuration word for PIC16F877A
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON
& LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);
// Main Function
void main(void)
{
InitLCD(); // Initialize LCD in 4bit mode
Init1msecTimerInterrupt(); // Start 1 msec timer
while(1)
{
if( msCounter == 0 ) // msCounter becomes zero after exact one sec
{
// Displays time in HH:MM:SS format
DisplayTimeToLCD(hrCounter, minCounter, secCounter);
}
UpdateTimeCounters(); // Update sec, min, hours counters
}
}
- Đây là code file timer.c .
#include "Includes.h"
// define digital clock variables
unsigned int msCounter = 0;
unsigned int secCounter = 0;
unsigned int minCounter = 0;
unsigned int hrCounter = 0;
void InitTimer0(void)
{
// Timer0 is 8bit timer, select T0CS and PSA to be zero
OPTION_REG &= 0xC1; // Make Prescalar 1:4
T0IE = 1; // Enable Timer0 interrupt
GIE = 1; // Enable global interrupts
}
void Init1msecTimerInterrupt(void)
{
InitTimer0(); // Intialize timer0 to genrate 1msec interrupts
}
void UpdateTimeCounters(void)
{
if (msCounter==1000)
{
secCounter++;
msCounter=0;
}
if(secCounter==60)
{
minCounter++;
secCounter=0;
}
if(minCounter==60)
{
hrCounter++;
minCounter=0;
}
if(hrCounter==24)
{
hrCounter = 0;
}
}
Link download project.
http://adf.ly/11512871/digit-clock-xc8
EmoticonEmoticon