I'm trying to get a grasp of creating delays with interrupt and timers.
Below is an example of what I have so far, basically in the while loop I'm trying to replace the built in __delay_ms[x] function with interrupts, but have not been successful in understanding how to implement that. If I moved the if...else from the while loop into the T4 ISR then it works fine. It will also work if I did:
Below is an example of what I have so far, basically in the while loop I'm trying to replace the built in __delay_ms[x] function with interrupts, but have not been successful in understanding how to implement that. If I moved the if...else from the while loop into the T4 ISR then it works fine. It will also work if I did:
Code:
while(1)
{
if(ton >= 4)
{
LATAbits.LATA1 = ~LATAbits.LATA1; //Toggle LED [this works]
ton = 0;
}
}
Code:
/*
* File: main.c
* Author: DRkidd22
* Created on August 16, 2019, 12:17 AM
*/
#include <xc.h>
#include "device_config.h"
volatile unsigned int tick_count = 0x00;
volatile unsigned int ton = 0;
void interrupt T4Interrupt (void)
{
if(PIR4bits.TMR4IF) //if timer flag is set
{
PIR4bits.TMR4IF = 0; //Clear the interrupt flag
++tick_count; //[25ms increments]
if(tick_count >= 4)
{
ton++; //[100ms increments]
tick_count = 0;
}
}
}
void main() {
//INTERRUPT CONTROL REGISTER
INTCONbits.GIE = 1; //Enable Global Interrupts
INTCONbits.PEIE = 1; //Enable peripheral interrupts
//PERIPHERAL INTERRUPT REQUEST REGISTER 4
PIR4bits.TMR4IF = 0; //Clear IF flag before enabling the interrupt.
//PERIPHERAL INTERRUPT ENABLE REGISTER 4
PIE4bits.TMR4IE = 1; //Enable timer4 interrupts
//Configure the timer
//CLOCK SELECTION REGISTER
T4CLKCONbits.CS = 0x05; //T4CS MFINTOSC 31.25khz
//TIMER4 HARDWARE LIMIT CONTROL REGISTER
T4HLTbits.MODE = 0x00; //T4MODE Software control
T4HLTbits.CKSYNC = 0x00; //T4CKSYNC Not Synchronized
T4HLTbits.CKPOL = 0x00; //T4CKPOL Rising Edge;
T4HLTbits.PSYNC = 0x00; //T4PSYNC Not Synchronized
//T4RST EXTERNAL RESET SIGNAL SELECTION REGISTER
T4RSTbits.RSEL = 0x00; //T4RSEL selected by T4CKIPPS pin
T4PR = 0x01; //Timer4 Module Period Register [25ms period]
T4TMR = 0x00; //Holding Register for the 8-bit TMR4 Register
//TIMER 4 CONTROL REGISTER
T4CONbits.CKPS = 0x06; //T4CKPS 1:64, Prescale Select bits
T4CONbits.OUTPS = 0x0B; //T4OUTPS 1:12 Postscale
T4CONbits.ON = 0x01; //TIMER On
SYSTEM_Init();
//RA0 is the output LED D2
//RA5 is the input button S2 (pulled hi)
//Configure pin direction
TRISAbits.TRISA0 = 0; //Set RA0 as output
TRISAbits.TRISA1 = 0; //Set RA1 as output
TRISAbits.TRISA2 = 0; //Set RA2 as output
TRISAbits.TRISA3 = 0; //Set RA3 as output
TRISAbits.TRISA5 = 1; //Set RA5 as input
//Configure Analog pins to Digital
ANSELA = 0x00;
ANSELB = 0x00;
ANSELC = 0x00;
LATA = 0;
while(1)
{
if(ton >= 4)
{
LATAbits.LATA1 = 1;
ton = 0; //reset counter
}
else
{
LATAbits.LATA1 = 0;
}
}
}
//64/31.25kHz = 0.002048
//2.048 * 34 = .069632
//.069632 * 12 = .835584
//31.25kHz / 64 = 488
//488Hz / 12 = 41
//1/41 = ~25ms period