Can anyone help me in my Microcontroller Clock Work? - Page 1
Forum section: electronics / general

author: Maurinho
author_rank: Newbie
timestamp: June 10, 2022, 01:40:31 am
content: HeyI have a job to do based on Microcontrollers. The objective is to make a clock in the Proteus program based on the PIC18F45K22 Microcontroller and the programming is done in the MicroC program. The watch must be able to give the hours and minutes, in addition to being able to change the hours and minutes according to the user's wishes.Could anyone help me with this work? I'll do the work, I just need help. I'm willing to pay for the help. Thanks.

author: jpanhalt
author_rank: Super Contributor
timestamp: June 10, 2022, 05:24:02 am
content: Quote from: Maurinho on June 10, 2022, 01:40:31 amWelcome to the forum.As this appears to be a school assignment, what work have you done already? For such a clock you can use almost any of the timers. Timer 1 is often used as its registers are effectively 16-bit. So, with a 32,768 Hz crystal as its independent clock, setting the high bit will give a rollover/interrupt every second (2^16 = 65536, divide 2 = 32768). Once there, there are simple algorithms to convert seconds to time in H:M:S.

author: Ian.M
author_rank: Super Contributor
timestamp: June 10, 2022, 05:39:03 am
content: To clarify, Timer 1, with its prescaler disabled, and a 32768 Hz crystal on the T1OSC pins has a natural (free-running) rollover period of two seconds, so if you need an interrupt once per second, the ISR must immediately set bit 7 of TMR1H (with T1CON:RD16 cleared so you can modify the high byte of the timer directly), to halve the remaining time to the next rollover interrupt.

author: jpanhalt
author_rank: Super Contributor
timestamp: June 10, 2022, 05:57:53 am
content: Why do you need a 16-bit write to set TMR1H,7? If done at a rollover while the timer runs continuously, that bit will be clear unless your ISR for doing that is really slow.

author: Ian.M
author_rank: Super Contributor
timestamp: June 10, 2022, 06:08:10 am
content: You don't need, nor want, a 16 bit write, which is why I specified "with T1CON:RD16 cleared". However as 65536 Hz crystals are fairly rare, and 32768 Hz watch crystals are 'jellybean' parts, its desirable to halve the natural period of Timer 1, by setting TMR1H:7 directly in 8 bit mode, to get a 1 Hz timekeeping 'tick'.Of course, the O.P. may well not have a watch crystal on the T1OSC pins, so may have to resort to other methods, e.g. decimating a more frequent timer interrupt to derive a 1 Hz 'tick' from the (hopefully crystal controlled) Fosc clock source.Personally, I favor a free running Timer 0, usually with prescaler chosen so its interrupt rate is suitable for display multiplexing, and keeping track of the fractional seconds in its ISR using fixed point maths, 'bumping' the seconds count and subtracting one second from the fractional seconds accumulator whenever it exceeds one whole second, but ad the O.P. isn't very experienced yet they may well do better to use a CCP module in special event compare mode, to reset Timer 1 at a lower non-power-of-two count to get a CCP interrupt rate that is convenient to decimate to a 1 Hz timekeeping 'tick' using only integer maths.

author: jpanhalt
author_rank: Super Contributor
timestamp: June 10, 2022, 06:09:41 am
content: Sorry about that, I simply misread it as "set."

author: tepalia02
author_rank: Regular Contributor
timestamp: June 10, 2022, 06:41:20 am
content: Here is a similar project :https://www.hackster.io/Razbot/digital-clock-with-pic18f45k22-mcu-timer-module-d6a61f

author: Maurinho
author_rank: Newbie
timestamp: June 10, 2022, 10:56:35 am
content: Hey, yesterday I was trying some things.I can put the numbers on the display, but I can't put 2 different numbers, I don't know why. I'm using timer0.Heres my code://Configuração do TIMER0void InitTimer0(){T0CON = 0xC2;TMR0L = 0x06;GIE_bit = 1;TMR0IE_bit = 1;}//Interrupção do TIMER0void Interrupt(){if (TMR0IF_bit){TMR0IF_bit = 0;TMR0L = 0x06;while(1){PORTC = 0b00000111;PORTD = 0x3F;PORTC = 0b00001011;PORTD = 0x06;}}}void main() {//PortasTRISC = 0b00000000;TRISD = 0b00000000;TRISB.F1 = 1;TRISB.F2 = 1;TRISB.F3 = 1;InitTimer0();ANSELC = 0;ANSELD = 0;ANSELB = 0;

author: jpanhalt
author_rank: Super Contributor
timestamp: June 10, 2022, 11:11:53 am
content: Quote from: Maurinho on June 10, 2022, 10:56:35 amTimer0 must be running quite fast if the system clock is in MHz. Why did you pick timer0 in 8-bit mode?Nevertheless, what numbersare you putting on the screen? What do you mean by "different numbers?" For example, when the count changes, can you put the changed values on the screen? Are you converting to H:M:S as requested in the assignment or just putting a raw count. Pictures and more code will probably help.

author: Maurinho
author_rank: Newbie
timestamp: June 10, 2022, 12:06:26 pm
content: Now I can put numbers. I used a delay to put 4 numbers at the same time in the display. Like the pictures shows.And I didnt do any programming yet to calculate the time so the numbers can change. I don't know how to do it.This is my programm now://Variáveisint milhar=0x00;int centena=0x00;int dezena=0x00;int unidade=0x00;//Configuração do TIMER0void InitTimer0(){T0CON = 0xC2;TMR0L = 0x06;GIE_bit = 1;TMR0IE_bit = 1;}//Interrupção do TIMER0void Interrupt(){if (TMR0IF_bit){TMR0IF_bit = 0;TMR0L = 0x06;while(1){unidade = 0x3F;PORTC = 0b00000111;PORTD = unidade;delay_ms(1);PORTC = 0b00001011;PORTD = 0x06;delay_ms(1);PORTC = 0b00001101;PORTD = 0x5B;delay_ms(1);PORTC = 0b00001110;PORTD = 0x4F;delay_ms(1);PORTC = 0b00001111;PORTD = 0x4F;}}}void main() {//PortasTRISC = 0b00000000;TRISD = 0b00000000;TRISB.F1 = 1;TRISB.F2 = 1;TRISB.F3 = 1;InitTimer0();ANSELC = 0;ANSELD = 0;ANSELB = 0;

author: ledtester
author_rank: Super Contributor
timestamp: June 10, 2022, 02:52:16 pm
content: You might find this video helpful:That channel also has other videos related to multiplexing led displays with a PIC.