The E0C33208 includes a clock timer capable of counting up to 64K days in units of 1/128 seconds. Here, we will explain how to generate an alarm interrupt exactly one minute later using this clock timer. The example program used here can be found in cc33\sample\drv33208\ct.
●
The one-minute alarm interrupt
Vector table [vector.c]/* vector table */
const unsigned long vector[] = {
(unsigned long)boot, // 0 0
|
|
(unsigned long)dummy, // 252 63 (unsigned long)dummy, // 256 64
(unsigned long)int_ct, // 260 65 (1)
(unsigned long)dummy, // 264 66
| };
(1) Setting the vector table
Register the interrupt handling routine int_c as the clock timer interrupt vector.
Initial settings [drv_ct.c]
#include "..\include\common.h"
#include "..\include\ct.h"
#include "..\include\int.h"
/* Prototype */
void init_ct(void);
void int_ct(void);
extern void save_psr(void);
extern void restore_psr(void);
/*******************************************************************************
* init_ct
* Type : void * Ret val : none * Argument :void
* Function :Initialize clock timer to use real time clock.
*******************************************************************************/
void init_ct(void) {
/* Save PSR and disable all interrupt */
save_psr(); (1)
/* Set clock timer interrupt disable on interrupt controller */
*(volatile unsigned char *)INT_EADE_ECTM_EP4_ADDR = INT_ENABLE_DIS;
// Set clock timer interrupt disable /* Stop clock timer */
*(volatile unsigned char *)CT_TCRUN_ADDR &= 0xfe; (2) /* Reset clock timer */
*(volatile unsigned char *)CT_TCRUN_ADDR |= CT_TCRST_RST;
/* Set clock timer data (1999.01.01 21:05) */
*(volatile unsigned char *)CT_TCHD_ADDR = 0x05; (3) // Minute data (5 minutes)
*(volatile unsigned char *)CT_TCDD_ADDR = 0x15;
// Hour data (21 hours)
*(volatile unsigned char *)CT_TCNDL_ADDR = 0xd7;
// Year-month-day low byte data (3287 days)
*(volatile unsigned char *)CT_TCNDH_ADDR = 0x0c;
// Year-month-day high byte data (3287 days) /* Set clock timer comparison data */
*(volatile unsigned char *)CT_TCCH_ADDR = 0x06; (4) // Minute comparison data (6 minutes)
*(volatile unsigned char *)CT_TCCD_ADDR = 0x0;
// Hour comparison data (0 hour)
*(volatile unsigned char *)CT_TCCN_ADDR = 0x0;
// Day comparison data (0 day)
/* Set clock timer interrupt factor control flag */
*(volatile unsigned char *)CT_TCAF_ADDR (5)
= CT_TCISE_NONE | CT_TCASE_M | CT_TCIF_RST | CT_TCAF_RST;
/* Set clock timer interrupt priority level 3 on interrupt controller */
*(volatile unsigned char *)INT_PCTM_ADDR = INT_PRIL_LVL3; (6) /* Reset clock timer interrupt factor flag on interrupt controller */
*(volatile unsigned char *)INT_FADE_FCTM_FP4_ADDR = INT_FCTM;
// Reset clock timer interrupt factor flag
/* Set clock timer interrupt enable on interrupt controller */
*(volatile unsigned char *)INT_EADE_ECTM_EP4_ADDR = INT_ECTM;
// Set clock timer interrupt enable /* Restore PSR */
restore_psr(); (7)
}
(1) Disabling interrupts
Save PSR and mask interrupts with IE.
/* Save PSR and disable all interrupt */
save_psr();
Using the interrupt controller, disable the clock timer interrupt.
/* Set clock timer interrupt disable on interrupt controller */
*(volatile unsigned char *)INT_EADE_ECTM_EP4_ADDR = INT_ENABLE_DIS;
// Set clock timer interrupt disable (2) Resetting the clock timer
After stopping the clock timer, reset the counter.
/* Stop clock timer */
*(volatile unsigned char *)CT_TCRUN_ADDR &= 0xfe;
/* Reset clock timer */
*(volatile unsigned char *)CT_TCRUN_ADDR |= CT_TCRST_RST;
(3) Setting the date and time
Set the date and time to 21:05, January 1, 1999. The 3287 days set in the day counter are calculated using January 1, 1990 as the starting point.
/* Set clock timer data (1999.01.01 21:05) */
*(volatile unsigned char *)CT_TCHD_ADDR = 0x05;
// Minute data (5 minutes)
*(volatile unsigned char *)CT_TCDD_ADDR = 0x15;
// Hour data (21 hours)
*(volatile unsigned char *)CT_TCNDL_ADDR = 0xd7;
// Year-month-day low byte data (3287 days)
*(volatile unsigned char *)CT_TCNDH_ADDR = 0x0c;
// Year-month-day high byte data (3287 days) (4) Setting an alarm
Here, we set 6 minutes as comparison data and set the alarm interrupt to occur in one minute.
/* Set clock timer comparison data */
*(volatile unsigned char *)CT_TCCH_ADDR = 0x06;
// Minute comparison data (6 minutes)
*(volatile unsigned char *)CT_TCCD_ADDR = 0x0;
// Hour comparison data (0 hour)
*(volatile unsigned char *)CT_TCCN_ADDR = 0x0;
// Day comparison data (0 day)
(5) Settings for alarm interrupt
Enable only the minutes alarm interrupt. Clear the interrupt fuctor generation and alarm fuctor generation flags.
/* Set clock timer interrupt factor control flag */
*(volatile unsigned char *)CT_TCAF_ADDR
= CT_TCISE_NONE | CT_TCASE_M | CT_TCIF_RST | CT_TCAF_RST;
These steps set the internal functions of the clock timer, not the interrupt controller. This control register must always be reset before use, since its initial value cannot be guaranteed.
(6) Setting the interrupt controller Set the interrupt level to 3.
/* Set clock timer interrupt priority level 3 on interrupt controller */
*(volatile unsigned char *)INT_PCTM_ADDR = INT_PRIL_LVL3;
Clear the clock timer interrupt factor flag.
/* Reset clock timer interrupt factor flag on interrupt controller */
*(volatile unsigned char *)INT_FADE_FCTM_FP4_ADDR = INT_FCTM;
// Reset clock timer interrupt factor flag Enable the clock timer interrupt.
/* Set clock timer interrupt enable on interrupt controller */
*(volatile unsigned char *)INT_EADE_ECTM_EP4_ADDR = INT_ECTM;
// Set clock timer interrupt enable
Note that the clock timer interrupt has no IDMA request flag and can function only as an interrupt to the CPU.
(7) Return processing
Restore PSR and enable interrupts.
/* Restore PSR */
restore_psr();
Interrupt processing [drv_ct.c]
/*******************************************************************************
* int_ct
* Type : void * Ret val : none * Argument :void
* Function :Clock timer interrupt function.
*******************************************************************************/
void int_ct(void) {
extern volatile int ctint_flg;
INT_BEGIN; (1)
ctint_flg = TRUE; // Clock timer interrupt flag on (2)
*(volatile unsigned char *)INT_FADE_FCTM_FP4_ADDR = INT_FCTM; (3) // Reset clock timer interrupt factor flag
INT_END; (1)
}
(1) Saving and restoring registers
Use INT_BEGIN and INT_END (defined in common.h) to save and restore registers at the begin-ning and end of the interrupt handling routine.
#define INT_BEGIN asm("pushn %r15")
#define INT_END asm("popn %r15\n reti") (2) Setting an interrupt-generated confirmation flag
Set a flag notifying the host routine that an interrupt has been generated.
ctint_flg = TRUE; // Clock timer interrupt flag on (3) Resetting the cause of the interrupt flag
Clear the interrupt factor flag.
*(volatile unsigned char *)INT_FADE_FCTM_FP4_ADDR = INT_FCTM;
// Reset clock timer interrupt factor flag
Application section [demo_ct.c]
|
/* Initialize clock timer */
write_str("*** Initialize clock timer and start ***\n");
write_str(" Today date and time (1999.01.01 21:05)\n");
write_str(" Set minute alarm interrupt enable (6 minutes)\n");
init_ct(); (1)
/* Run clock timer */
write_str("*** Run clock timer ***\n");
*(volatile unsigned char *)CT_TCRUN_ADDR |= 0x01; (2) /* Initialize clock timer interrupt flag */
ctint_flg = FALSE;
write_str("*** Wait 1 minute ***\n");
write_str("\n");
while (1) { (3)
if (ctint_flg == TRUE) { break;
} }
/* Stop clock timer */
write_str("*** Stop clock timer ***\n");
*(volatile unsigned char *)CT_TCRUN_ADDR &= 0xfe; (4)
| (1) Initial settings
Call the above-mentioned init_ct() and initialize the clock timer and interrupt settings.
(2) Starting the clock timer
Start the clock timer and clear the interrupt-generated confirmation flag.
*(volatile unsigned char *)CT_TCRUN_ADDR |= 0x01;
/* Initialize clock timer interrupt flag */
ctint_flg = FALSE;
(3) Wait for alarm interrupt
When the alarm time arrives, the above-mentioned interrupt handling routine int_ct() is called.
When processing is complete, the flag ctint_flg is set. Loop the program until this flag is set to 1.
An alarm interrupt occurs one minute after the clock timer starts.
while (1) {
if (ctint_flg == TRUE) { break;
} }
(4) Stopping the clock timer
After the interrupt occurs, stop the clock timer.
*(volatile unsigned char *)CT_TCRUN_ADDR &= 0xfe;