| Digital Power Starter Kit 3 Firmware dsPIC33C Boost Converter Voltage Mode Control Example |

init_timer1.c
1 /*
2 * File: init_timer1.c
3 * Author: M91406
4 *
5 * Created on July 8, 2019, 2:47 PM
6 */
7
8 #include <xc.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11
12 #include "config/hal.h"
13 #include "config/init/init_timer1.h"
14
22 /***********************************************************************************
23 * @fn uint16_t sysOsTimer_Initialize
24 * @brief Initializes the timer used as time-base for the task scheduler
25 * @return unsigned integer
26 * @return 0=failure
27 * @return 1=success
28 *
29 * @details
30 * The task scheduler runs on a specific, usually fixed time base specified
31 * in the state machine section of the hardware abstraction layer (e.g. 100us).
32 * This routine initializes a device timer exclusively used for that purpose
33 * which is not shared with other tasks/peripherals to ensure stable, deterministic
34 * behavior of the firmware. The timer does not use interrupts. The scheduler
35 * task execution period is controlled by polling on the timer overrun bit, allowing
36 * some more relaxed timing while not putting additional burden on the CPU.
37 *
38 **********************************************************************************/
39
40 volatile uint16_t sysOsTimer_Initialize (void)
41 {
42 volatile uint16_t retval=1;
43
44 T1CONbits.TON = 0; // Timer1 On: Stops 16-bit Timer1 during configuration
45 T1CONbits.TSIDL = 0; // Timer1 Stop in Idle Mode: Continues module operation in Idle mode
46 T1CONbits.TMWDIS = 0; // Asynchronous Timer1 Write Disable: Back-to-back writes are enabled in Asynchronous mode
47 T1CONbits.TMWIP = 0; // Asynchronous Timer1 Write in Progress: Write to the timer in Asynchronous mode is complete
48 T1CONbits.PRWIP = 0; // Asynchronous Period Write in Progress: Write to the Period register in Asynchronous mode is complete
49 T1CONbits.TECS = 0b11; // Timer1 Extended Clock Select: FRC clock
50 T1CONbits.TGATE = 0; // Timer1 Gated Time Accumulation Enable: Gated time accumulation is disabled when TCS = 0
51 T1CONbits.TCKPS = 0; // Timer1 Input Clock Prescale Select: 1:1
52 T1CONbits.TSYNC = 0; // Timer1 External Clock Input Synchronization Select: Does not synchronize the External Clock input
53 T1CONbits.TCS = 0; // Timer1 Clock Source Select: Internal peripheral clock
54
55 // Reset Timer Counter Register TMR to Zero;
56 TMR1 = 0x00;
57 //Period = 0.0001 s; Frequency = 100000000 Hz; PR 9999;
58 PR1 = MAIN_EXEC_PER;
59
60 // Reset interrupt and interrupt flag bit
61 _OSTIMER_IP = _OSTIMER_PRIORITY; // Set interrupt priority to DEFAULT
62 _OSTIMER_IF = 0; // Reset interrupt flag bit
63 _OSTIMER_IE = 0; // Disable Timer1 interrupt
64
65 return(retval);
66 }
67
68 /***********************************************************************************
69 * @fn uint16_t sysOsTimer_Enable
70 * @brief Enables the timer used as time-base for the task scheduler
71 * @return unsigned integer
72 * @return 0=failure
73 * @return 1=success
74 *
75 * @details
76 * Once the task scheduler time base has been initialized, this function can be
77 * used to enable the timer and start the scheduled task execution.
78 *
79 **********************************************************************************/
80
81 volatile uint16_t sysOsTimer_Enable (volatile bool interrupt_enable, volatile uint8_t interrupt_priority)
82 {
83 volatile uint16_t retval=1;
84
85 // Enable Timer1
86 _OSTIMER_IP= interrupt_priority; // Set interrupt priority to zero
87 _OSTIMER_IF = 0; // Reset interrupt flag bit
88 _OSTIMER_IE = interrupt_enable; // Enable/Disable Timer1 interrupt
89 T1CONbits.TON = 1; // Turn on Timer1
90 retval &= T1CONbits.TON; // Add timer enable bit to list of checked bits
91
92 return(retval);
93 }
94 // end of group os-timer-initialization
96
97 // end of file
sysOsTimer_Enable
volatile uint16_t sysOsTimer_Enable(volatile bool interrupt_enable, volatile uint8_t interrupt_priority)
Definition: init_timer1.c:81
_OSTIMER_IP
#define _OSTIMER_IP
interrupt priority register
Definition: dpsk3_hwdescr.h:175
_OSTIMER_IF
#define _OSTIMER_IF
interrupt flag bit
Definition: dpsk3_hwdescr.h:177
sysOsTimer_Initialize
volatile uint16_t sysOsTimer_Initialize(void)
Definition: init_timer1.c:40
_OSTIMER_IE
#define _OSTIMER_IE
interrupt enable bit
Definition: dpsk3_hwdescr.h:176
_OSTIMER_PRIORITY
#define _OSTIMER_PRIORITY
interrupt priority (1 ... 7, default = 2)
Definition: dpsk3_hwdescr.h:178
MAIN_EXEC_PER
#define MAIN_EXEC_PER
Global state-machine user-settings conversion macros.
Definition: dpsk3_hwdescr.h:160