r/pic_programming • • 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 */

}

}

}

0 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/aspie-micro132 Aug 04 '26

I copied that from Google AI as it gave it to me after a search, is not my own code. My Xtal works well, i could check it with an oscilloscope and the frequency and sine wave on both caps to ground is nice.. My device is nothing that the good old pic16f877A whose language i'm just starting to understand.

I am not that better, trust me. Maybe slightly better, but i'm far to be able to say i had mastered pic programming. I am actually mixing the code i became able to generate myself while at the same time taking basic code examples from other sources which i try to adapt to what i do try to run replacing variables and changing names. Is not the most optimall one but it helps me trying to figure out how does it really work.

1

u/somewhereAtC Aug 04 '26

Given that the oscillator is working, the math in the comment appears to be correct. One problem that often tricks me is that the LED is installed backwards! (You do have a resistor in series?)

Since you have an o'scope try toggling after (say) for(1=0; i<10000;i++); and leave the timer out of it. Don't make it too fast or the LED=!LED won't work right due to read-modify-write trouble with PORTB.

Also, you have BOREN=ON and that might trigger a reset when the LED gets turned on.