Using the Analog Comparator (ACMP) for the QE Microcontrollers
QE128 Quick Reference User Guide, Rev. 1.0
Freescale Semiconductor
7-3
are registers used for power saving consumption, here the bus clock to peripherals can be enabled or
disabled. In this example only the bus clock to the ACMP module is active. The clocks to the other
peripherals are disabled.
void MCU_Init(void) {
SOPT1 = 0x23; // Watchdog disable. Stop Mode Enable. Background Pin enable. RESET pin enable
SCGC1 = 0x00; // Disable Bus clock to unused peripherals
SCGC2 = 0x08; // Bus Clock to ACMP module is enabled
}
This is the General Purpose Input/Output configuration. These code lines configure the direction for the
PTE port. The eight LEDs from the EVB are connected to the PTE port, and only the PTE0 is configured
as output in order to drive a LED.
void GPIO_Init(void) {
PTEDD = 0x01; // Configure PTE port as output
PTED = 0x00; // Put 0's in PTE port
}
This is the initialization code for the analog comparator used for the QE128 MCU. This application uses
the ACMP2 module. The internal bandgap is selected and this voltage is compared with the PTC7 pin
voltage.
void ACMP_Init (void) {
ACMP2SC = 0xC3; // ACMP module enable. Internal reference selected. Comparator
// output rising or falling edge
}
This is the main function, above are the described called functions, and all the interrupts are enabled. After
this the analog comparator interrupt can be serviced.
void main(void) {
MCU_Init(); // Function that initializes the MCU
GPIO_Init(); // Function that initializes the Ports of the MCU
ACMP_Init() // Function that initializes the KBI module
EnableInterrupts; // enable interrupts
ACMP2SC_ACIE = 1; // enable the interrupt from the ACMP
for(;;) {
} // loop forever
// please make sure that you never leave this function
}
NOTE
This is the analog comparator interrupt service routine. Every time an
interrupt is detected, this routine toggles a LED (PTE0). The
VectorNumber_Vacmpx can be replaced by the interrupt vector number, this
depends if the MCU is S08 or V1. Using this example makes the code fully
compatible for either MCU.
void interrupt VectorNumber_Vacmpx ACMP_ISR(void) {
// ACMP vector address = 20 (S08)
// ACMP vector address = 82 (V1)
ACMP2SC_ACF = 1; // Clear ACMP flag
PTED_PTED0 ^= 1; // Toggles PTE0
}