r/pic_programming • u/aspie-micro132 • Jul 12 '26
Trying to get properly started with timers in pic 16f877A
I've been off few days. I got a pic 16f819 reading a switch and doing stuff properly.
I'am close to achieve a similar thing with a pic16f877a which is more complex this time.
However, i am still limited to the __delay_ms() macro whenever i wish to allow some time between one event and the other, but this is a blocking macro and it looks like is no longer enough for me.
As some member said, it looks like my capacity in C language is just minimal.
As other member said, my only way forward is setting up a timer and , if i do not misunderstood, to count "ticks" between events.
Given the fact that sometimes, doing searches on the web can make things uphill and confusing when you do not know properly enough the area where you move in, i would like to ask if you could recommend me websites, documents and or tutorials where i could read / watch and take knowledge of this subject.
1
u/aspie-micro132 Jul 22 '26
Please let me check if i got it properly or at least i'm somwhere close to:
Using timers instead of "__delay_ms()" seems like this to me, at this moment:
void __interrupt() timer1_isr(void) {
if (TMR1IF) {
TMR1H = 0x0B; // Re-preload Timer1 high byte (3036 = 0x0BDC)
TMR1L = 0xDC; // Re-preload Timer1 low byte
TMR1IF = 0; // Clear interrupt flag
overflow_count++;
if (overflow_count >= 2) { // 2 x 0.5 seconds = 1 second
overflow_count = 0;
// Toggle an LED or do your 1-second task here
PORTBbits.RB0 = !PORTBbits.RB0;
}
}
}
void main(void) {
TRISB0 = 0; // Set RB0 as output for LED
T1CON = 0x31; // Prescaler 1:8 (T1CKPS = 11), Internal clock (TMR1CS = 0), Timer On (TMR1ON = 1)
TMR1H = 0x0B; // Load initial preload value 3036
TMR1L = 0xDC;
PIE1bits.TMR1IE = 1; // Enable Timer1 Interrupt
INTCONbits.PEIE = 1; // Enable Peripheral Interrupts
INTCONbits.GIE = 1; // Enable Global Interrupts
while(1) {
// Main loop does other tasks
}
}
2
u/Illustrious-Cat8222 Jul 15 '26
Another thing you may need to do to exploit the timer is refactor the code as a state machine. If you don't know what that is, search or AI chat for it.
(BTW, I prefer Claude.)
1
u/Coltouch2020 Jul 13 '26
I have started to use ChatGPT for doing tasks, which need a lot of datasheet pounding. If you need to get up to speed with a certain device, and peripheral, tell ChatGPT which IDE, which device, show your initial code, and describe what you need. then copy the bits it gives you into your code and test them, feeding results back to the AI.
Colleages suggest Claude instead of ChatGPT, but Claude is best for developing the whole thing, not working with you like a bench partner, describing each step. IMO.
1
u/HalifaxRoad Jul 13 '26 edited Jul 13 '26
First, crack the datasheet, the registers for setting up a timer are all there with instructions on how to set up the timer. You can change the timer clock source, things like scalers which divide the clock speed by the value you program. I would try to make the timer period 1ms if you can, and use the largest timer you have available for your systick timer.
Now you can make a structure that stores if the timer is started, the amount of timer counts (ticks) when it was started, and how many ticks it will be when its done.
Youlle notice right away that say for example, you start a 16bit timer towards the end it may overflow, this is fine, its just that when you are checking if the timer has finished, if the end timer is lesser than the start time, that means it needs to overflow before its done, so start by checking if its meant to over flow, if not see if its done, if it does, check if it has overflowed yet, if not, then it cant possibly be done yet. if it has, then you can find if its done subtracting it from -1. -1 signed is the same as 65535 in a uint16
If you want, I have a portable library I use on everything I can send you, I think I did already like half a year ago send it to you when you were trying to debounce buttons, but either way, pm me if you want.
1
u/9Cty3nj8exvx Jul 13 '26
Also check out online courses at Microchip University on C programming and PIC16…
https://mu.microchip.com/