I'm adding a clock to my program, but for some reason I cannot lower the value of my "sec" variable.
I have a millisecond timer that raises a flag every ms. So, every 1000 ms I clear the ms variable; this happens without a problem.
Now every 10 (for debug purposes) seconds I want to clear the sec
variable and everything stops.
I'm using a pic18f67k22.
I should just be able to assign 0 to it, and it is VERY frustrating. Every value below 10 crashes; values above 10 resume like nothing happened.
minimal reproducible example, I left out lcd code as it was just for me to see if it still happened with minimal code, left out delay because it's just a bunch of "nop"'s, fuses and pin.h do not appear relevant either, fuses might be but your pins would be different): main.c:
#include "fuses.h"
#include <stdbool.h>
#include <stdio.h>
#include <xc.h>
#include "clock.h"
#include "interrupt.h"
#include "LCDSerialTerminalMaster.h"
uint8_t prevSec = 0;
void main(void)
{
// disable comparators
CM1CONbits.CON = 0;
CM2CONbits.CON = 0;
CM3CONbits.CON = 0;
// disable analog input
ADCON0bits.ADON = false;
ANCON0 = 0;
ANCON1 = 0;
ANCON2 = 0;
INT_Init();
CLK_Init();
LCD_Init();
while(1)
{
if(CLK_GetSec() != prevSec)
{
prevSec = CLK_GetSec();
LCD_Write();
}
}
}
interrupt.c:
#include <stdbool.h>
#include "interrupt.h"
#include "clock.h"
#include "pin.h"
void INT_Init(void)
{
// Enable Interrupt Priority Vectors
IPEN = 1;
// RCI - high priority
UART1_RC_IP = 1;
UART2_RC_IP = 1;
UART1_TX_IP = 0;
UART2_TX_IP = 0;
MASTER_SYNC_IP = 1;
INT_Toggle(true);
}
void INT_Toggle(bool toggle)
{
INT_ToggleHigh(toggle);
INT_ToggleLow(toggle);
}
void INT_ToggleHigh(bool toggle)
{
INTCONbits.GIEH = toggle;
}
void INT_ToggleLow(bool toggle)
{
INTCONbits.GIEL = toggle;
}
void __interrupt() INT_InterruptManagerHigh(void)
{
if(PIR4bits.CCP4IF)
CLK_ISR();
}
clock.c
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <xc.h>
static uint8_t hour = 0;
static uint8_t min = 0;
static uint8_t sec = 0;
static volatile uint16_t ms = 0;
void CLK_Init(void)
{
T3CONbits.TMR3CS = 0b00; // Source = Fosc/4
T3CONbits.SOSCEN = 0b1; // SOSC disabled
T3CONbits.T3CKPS = 0b00; // Prescaler 1:1
CCP4CONbits.CCP4M = 0b1011; // Compare Mode: Special Event Trigger, reset TMR3 on CCP4 match
CCPTMRS1bits.C4TSEL = 0b01; // TMR3/TMR4
// set compare value to 5000
CCPR4L = 136;
CCPR4H = 19;
// Disable TMR3 interrupt.
PIE2bits.TMR3IE = 0;
// clear CCP4 interrupt.
PIR4bits.CCP4IF = 0;
// enable CCP4 interrupt.
PIE4bits.CCP4IE = 1;
IPR4bits.CCP4IP = 1;
// Start the Timer by writing to TMRxON bit.
T3CONbits.TMR3ON = 1;
}
void CLK_GetTime(char *buffer)
{
sprintf(buffer, "%02hhu:%02hhu:%02hhu", hour, min, sec);
}
uint8_t CLK_GetSec(void)
{
return sec;
}
void CLK_ISR(void)
{
PIR4bits.CCP4IF = 0;
ms++;
if(ms == 1000)
{
ms = 0;
sec++;
if(sec == 10)
{
sec = 0;
min++;
if(min == 60)
{
min = 0;
hour++;
if(hour == 24)
hour = 0;
}
}
}
}
the error happens in CLK_ISR, I can't assign a value lower than the value i'm comparing to, if I try sec-- the same thing happens.