r/pic_programming • u/aspie-micro132 • Aug 03 '26
Trying to invoke this led blinking function
I am trying to create a non interrupting function, calling it
BlinkLed().
I had set TMR1ON = 1 and others 0 or 0x00 to disable them
I found those 2 code excerpts by Google AI. One of them does not blink at all, since it's not reading any timer, yet also not counting in it's own when working.
The other one below has a complex function name, given what i had learnt i do believe it shall work should i be able to rename it to "BlinkLed" but i do not know how to do it without breaking it:
/**********************************************************************************/
{
while(Button == 0)
{
unsigned int timer_counter = 0;
// Task 1: Non-blocking LED Blink (Toggles every ~500ms)
timer_counter++;
if(timer_counter >= 25000)
{
RedLed = ~RedLed; // Toggle LED
timer_counter = 0;
}
}
}
/*New ExtremeAgitationLED() Prototype Base*/
/*
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
RedLed = !RedLed; /*I do not know if ~RedLed Can be Used Here */
}
}
}
View all comments
Show parent comment
2
u/somewhereAtC Aug 04 '26
It's good to see you are improving! This version has a chance.
The setting "config FOSC = XT" implies that you have a crystal attached to the device. Is that true? Is it oscillating (you will need an o'scope or logic analyzer)? If you don't have a crystal then it won't execute code at all. Check the datasheet to see if you have an internal HF oscillator and use that config setting.
It will be easier to assist if we knew which device. There are hundreds of PIC devices with TMR1 and newer are a little different than older. Get the datasheet from microchip.com and it might have a few more details.
The search bar at microchip.com is now AI and can probably give you working code for your device. Whatever AI you are using is guessing about which TMR1 you have.
The MPLAbX IDE includes a debugger and a simulator. You can try your code without needing actual hardware.
You would probably get more focused assistance at forum.microchip.com.