I am trying to create a firmware for Pic16f877A, using mplab 6.15 on gnu/linux and pickit 3.
After finding out code examples in C language i could try and test parts to build a working piece of source code for it. However, i am finding myself at odds whenever i do wish to get actual work done.
I do wish to drive some leds on an off in sequences, sometimes turning on one, off other several times and holding them off some minutes to do it back. I could accomplish part of it using something called "fizz buzz" example and reading ticks from Timer 1's prescaler. My C and PIC knowledge is not yet the best.
The major problem i am struggling with is that, i can get 2 leds toggling one after the other, but if i try to get that within a FOR cycle, it either never stops if i do not add any code at FOR's end, or if i add code, it inmediatly jumps to it avoiding to toggle the leds altogether. If i code them off after for() they stay off, if i code them on after for, they stay offf.
The source code i am in trouble with is this one:
void MainProgramme(void)
{
/*Essential Variables Are Declared Here*/
Led3 = 0; //Seems It Has To Be Turned Explicitly Off Before Anything...
unsigned int tick = 0; //A Common Variable For Measuring Timer0 Ticks
unsigned int activations = 0; //This Is For Counting Movements.
unsigned int times = 0; //This Is For Counting SubFunction Executions.
unsigned int blinks = 0;// This Is For Programme Led Blink Timer 0 Overflow Counting.
unsigned int counter = 1;
/*The Function Actually Starts Here*/
while(Button1 == 0) //Checks If Button1 Is Depressed Aiming Towards Turning Off The Machine
{
while(Run == 0) //Checks If Run Button Is Depressed Toggling Between Work Or Pause
{
/******************************************************************************************************/
/*Main Led Stays Blinking At Around 2hz While The Procedure Takes Place If Not Paused*/
tick++;
// Check if Timer0 has overflowed
if(TMR0IF == 1)
{
TMR0IF = 0; // Reset the flag immediately
blinks++;
// 7 overflows ~ 458ms (close to 0.5s for 1Hz toggle)
if(blinks >= 266) {
MainLed = ~MainLed; // Toggle LED on RB0
blinks = 0; // Reset counter
}
/**********************************************************************************************************/
if(Led7 == 0)
{
//--------------------------------------------------------------
while(Switch2 == 0)
{
Blink2 = 0;
Blink1 = 0;
Led3 = 0;
// Timer 0 For Programme Led Blinking. IT WORKS, AVOID BREAKING.
// Check if Timer0 has overflowed
if(TMR0IF == 1)
{
TMR0IF = 0; // Reset the flag immediately
blinks++;
// 7 overflows ~ 458ms (close to 0.5s for 1Hz toggle)
if(blinks >= 266) {
MainLed = ~MainLed; // Toggle LED on RB0
blinks = 0; // Reset counter
}
} Led6 = 1;
} Led6 = 0;
} // Main Programme Led Blink Ends Here. AVOID TOUCHING THIS PART, IT WORKS.
//------------------------------------------------------------------------
if(Led7 == 1)
{
while(Switch1 == 0)
{
Blink2 = 0;
Blink1 = 0;
Led3 = 0;
// Timer 0 For Programme Led Blinking
// Check if Timer0 has overflowed
if(TMR0IF == 1)
{
TMR0IF = 0; // Reset the flag immediately
blinks++;
// 7 overflows ~ 458ms (close to 0.5s for 1Hz toggle)
if(blinks >= 266) {
MainLed = ~MainLed; // Toggle LED on RB0
blinks = 0; // Reset counter
}
}
Led6 = 1;
} Led6 = 0;
} // Main Programme Led Blink Ends Here. AVOID TOUCHING THIS PART, IT WORKS.
//------------------------------------------------------------------------------------------------
//Timer 1 For Blink Led Timings
while(Switch1 == 1 || Switch2 == 1)
{
/************************************************************************/
/*Programme Led Seems To Be Blinked Locally*/
// Check if Timer0 has overflowed
if(TMR0IF == 1)
{
TMR0IF = 0; // Reset the flag immediately
blinks++;
// 7 overflows ~ 458ms (close to 0.5s for 1Hz toggle)
if(blinks >= 198) {
MainLed = ~MainLed; // Toggle LED on RB0
blinks = 0; // Reset counter
}
}
/*************************************************************************/
//This Is Timer 1 Setting leds Timings.Timer 0 Sets Programme Led Blinking
if (PIR1bits.TMR1IF) { // Check if Timer1 overflowed
PIR1bits.TMR1IF = 0; // Clear flag
TMR1H = 0x00; // Reload timer
TMR1L = 0x00;
counter++;
if (counter > 200) counter = 1; // Loop 1 to 100
}
if((MainStrenghtRedLed == 0) && (MainStrenghtGreenLed == 1))
{
for(times = 10; times >= 0; times --)
{
if (counter % 3 == 0)
{
times = times + 1;
Blink1 = 0;
Blink2 = 1;
} else if (counter % 5 == 0)
{
Blink1 = 1;
Blink2 = 0;
} else if (counter % 7 == 0)
{
Blink1 = 0;
Blink2 = 1;
}
else if (counter % 9 == 0)
{
Blink1 = 1;
Blink2 = 0;
}
else if (counter % 11 == 0)
{
Blink1 = 0;
Blink2 = 1;
}
else if (counter % 13 == 0)
{
Blink1 = 1;
Blink2 = 0;
}
else if (counter % 15 == 0)
{
Blink1 = 0;
Blink2 = 1;
activations--;
}
} //It's suposed to perform this FizzBuzz routine a couple of times and stop, then again for some times.
Problem is: it never stops. If i do add any code after the For() it actually jumps to it.
}
}Led6 = 1; //End Of While
/*******************************************************/
}
/*******************************************************/
}
}__delay_ms(100);
PowerOff();
}