Activity 3. Real time clock
In this activity we want to develop a real time clock. The system will work as follows: Initially the indication will be 12:00. The indicator will be done in two phases. For one second the hour will be displayed and for one second the minutes will be displayed. In order to be able to separate the indication between hours and minutes, when the indication on the two rightmost indicators is the hour, the leftmost indicator should show the “Ω”, while when the indication is the minutes, the leftmost indicator should have the indication “Π”.
(75 minutes)
Activity 3
Step 1. The circuit is drawn in the Proteus Design Suite.

Step 2. Timer0 configurations.

The Timer0 overflow interrupt method will be used to measure the time. Specifically, 5ms will be used as a time base, that is, an interruption from Timer0 will occur every 5ms. When 200 interruptions have passed, 1 second will have elapsed.
Let's assume that: Fclock=48MHz και Prescaler=1
To calculate what value timer0 should take we need to solve the equation:

Where y is the initial value that Timer0 should have.
Step 3. The program in C language is written.
Write in CCS C Compiler the program in C language
#include <main.h>
#byte PORTB =0xF81
#byte PORTC =0xF82
#byte PORTD =0xF83
// Variable definitions
int8 des=0;
//variable to select one of the three displays
int8 seconds=0;
int8 minute=0;
int8 hour=12;
int8 counter=200;
//variable to count interrupts
int1 flag=0;
//choose to display hours or minutes
//Table of codes to display in a 7-segment display
int8 table[16] = { 0b00111111, //0
0b00000110, //1
0b01011011, //2
0b01001111, //3
0b01100110, //4
0b01101101, //5
0b01111101, //6
0b00000111, //7
0b01111111, //8
0b01101111, //9
0b01101011, //Ω
0b00110111, //Π
};
int8 dig[3] = {1,2,4};
//Table for driving a single display from PORTC
//PORTC applies 5V to the base of only 1 of the 3 transistors
// Function declaration
void timer0_int(void);
void init (void);
void main()
{
init();
while (1){ }
}
// Interrupt Service Routine
#INT_TIMER0 HIGH
void timer0_int(void){
int16 mon,dec,eka;
//variable to display digits in the 7-segment displays
set_timer0(5536);
//Timer0 initial value to have interrupts occur every 5ms
counter--;
//Counter is decremented by 1 and reset every
//200 * 5 msec = 1 sec
if (counter == 0){
seconds++;
counter = 200;
flag^=1;
if (seconds > 59){
seconds = 0;
minute++;
if (minute > 59){
minute = 0;
hour++;
}
if (hour >24){
hour = 0;
}
}
}
if (flag == 0){
//if flag=0 then the minutes are displayed
dec = (int8)minute / 10;
mon = minute - dec * 10;
eka = 11;
}
if (flag == 1){
//if flag=1 then the hours are displayed
dec = (int8)hour / 10;
mon = hour - dec * 10;
eka = 10;
}
des = ++des%3;
/* This variable takes the values sequentially
0, 1, 2, 0, 1, 2, 0, 1, …
so that they are selected from the dig[des] array sequentially
the values … 0000 0001, 0000 0010, 0000 0100 …
and to activate the displays in order */
PORTC = dig[des];
if (des==0){
PORTB = table[mon];
}
if (des==1){
PORTB = table[dec];
}
if (des==2){
PORTB = table[eka];
}
}
// Initialization routine
void init (void){
set_tris_b(0x00);
set_tris_c(0x00);
PORTB = 0;
PORTC = 0;
counter = 200;
seconds = 0;
minute =0;
hour = 12;
des =0;
flag = 0;
SETUP_TIMER_0(T0_INTERNAL | T0_DIV_1);
set_timer0(5536);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
}
Step 4. Compile the program in C in order to create the program in the microcontroller machine code (hex file).
Step 5. Load to the microcontroller the hex file (program in machine code) that was created from the CCS C Compiler.