Page MenuHomec4science

timer.c
No OneTemporary

File Metadata

Created
Sun, Sep 1, 17:56
#include <p33FJ128GP802.h>
#include "timer.h"
#include "spi_led.h"
extern uint16 led_outputs;
unsigned int systime;
/* Timer 1 is used as system timer */
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
systime ++;
// update rgb led status (pwm-functionality)
UPDATE_RGB_LED(systime);
// start new ADC conversion
AD1CON1bits.SAMP = 0;
IFS0bits.T1IF = 0; // Clear Timer 1 Interrupt Flag
}
/**
* start timer 1 as main clock, for systime and PWM for the leds.
* The interrupt goes off FSYST times per second
*/
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
/* 1x prescaler */
PR1 = (FCY/FSYST -1); //timer goes off every PR1 clock cycles. So FSYST times per second.
T1CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
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
}

Event Timeline