Page MenuHomec4science

timer.c
No OneTemporary

File Metadata

Created
Sun, Nov 3, 14:50
#include "timer.h"
#include "p33FJ128MC804.h"
#include "dma_sbcp.h"
#include "dma_spi.h"
#include "config.h"
#include "motorcontrol.h"
#define NTOTPULSE (FCY/FSYST -1)
#define NTOTPULSE_SBCP (FCY/FSBCPT -1)
#define NTOTPULSE_SPI_ME_READING_LATENCY (FCY/FSPI_ME_READING_LATENCY - 1)
volatile unsigned int systime;
extern motor M1, M2;
/* Timer 1 is used as system timer */
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
systime ++;
// timing critical part of motor control
MOTORCONTROL_UPDATE_POSITION_AND_SPEED((&M1), POS1CNT);
MOTORCONTROL_UPDATE_POSITION_AND_SPEED((&M2), POS2CNT);
IFS0bits.T1IF = 0; // Clear Timer 1 Interrupt Flag
}
/* Timer 2 is used to create the delay to communicate over the SPI1 bus */
void __attribute__((__interrupt__, no_auto_psv)) _T2Interrupt(void)
{
/* Interrupt Service Routine code goes here */
TIMER_2_STOP_AND_CLEAR;
DMA_SPI_ME_START_ACTUAL_READING;
IFS0bits.T2IF = 0; // Clear Timer 2 Interrupt Flag
}
/* Timer 3 is used to create the timeout for bus receiver part of the SBCP protocol */
void __attribute__((__interrupt__, no_auto_psv)) _T4Interrupt(void)
{
/* Interrupt Service Routine code goes here */
TIMER_4_STOP_AND_CLEAR;
dma_sbcp.bus_rx_error |= SBCP_COM_ERROR_CODE_BUS_TIMEOUT;
IFS1bits.T4IF = 0; // Clear Timer 3 Interrupt Flag
}
void timer_init()
{
// Timer 1 is currently implemented in motorcontrol.c
/* Set timer 1 as main clock */
T1CONbits.TON = 0; // Disable Timer
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TGATE = 0; // Disable Gated Timer mode
#if NTOTPULSE < 65535
/* 1x prescaler */
PR1 = NTOTPULSE;
T1CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
#elif NTOTPULSE < (8*65535)
/* 8x prescaler */
PR1 = NTOTPULSE/8;
T1CONbits.TCKPS = 0b01; // Select 1:8 Prescaler
#else
#error "FSYST - Check the values and maybe use a greater prescaler value"
#endif
TMR1 = 0x00; // Clear timer register
IPC0bits.T1IP = 0x06; // Set Timer1 Interrupt Priority Level to 6 = very high priority
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Start Timer
systime = 0;
// T2CONbits.T32 = 0; // Select 16 bit mode
/* Set timer 2 for SBCP timeout */
TIMER_2_STOP; // Disable Timer
T2CONbits.TCS = 0; // Select internal instruction cycle clock
T2CONbits.TGATE = 0; // Disable Gated Timer mode
#if NTOTPULSE_SPI_ME_READING_LATENCY < 65535
/* 1x prescaler */
PR2 = NTOTPULSE_SPI_ME_READING_LATENCY;
T2CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
#elif NTOTPULSE_SPI_ME_READING_LATENCY < (8*65535)
/* 8x prescaler */
PR2 = NTOTPULSE_SPI_ME_READING_LATENCY/8;
T2CONbits.TCKPS = 0b01; // Select 1:8 Prescaler
#else
#error "F_SPI_ME_REQADING_LATENCY - Check the values and maybe use a greater prescaler value"
#endif
TMR2 = 0x00; // Clear timer register
IPC1bits.T2IP = 0x01; // Set Timer2 Interrupt Priority Level to 1 = low priority
IFS0bits.T2IF = 0; // Clear Timer2 Interrupt Flag
IEC0bits.T2IE = 1; // Enable Timer2 interrupt
// We do not activate timer 2 but keep it stopped until it is needed
/* Set timer 3 for SBCP timeout */
TIMER_4_STOP; // Disable Timer
T4CONbits.T32 = 0; // Select 16 bit mode
T4CONbits.TCS = 0; // Select internal instruction cycle clock
T4CONbits.TGATE = 0; // Disable Gated Timer mode
#if NTOTPULSE_SBCP < 65535
/* 1x prescaler */
PR4 = NTOTPULSE_SBCP;
T4CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
#elif NTOTPULSE_SBCP < (8*65535)
/* 8x prescaler */
PR4 = NTOTPULSE_SBCP/8;
T4CONbits.TCKPS = 0b01; // Select 1:8 Prescaler
#else
#error "F_SBCP_T - Check the values and maybe use a greater prescaler value"
#endif
TMR4 = 0x00; // Clear timer register
IPC6bits.T4IP = 0x01; // Set Timer 3 Interrupt Priority Level to 1 = low priority
IFS1bits.T4IF = 0; // Clear Timer 3 Interrupt Flag
IEC1bits.T4IE = 1; // Enable Timer 3 interrupt
// We do not activate timer 3 but keep it stopped until it is needed
}

Event Timeline