📄 main.c
字号:
/*******************************************************************
** Filename : main.c
** Project : adc compare example
** Processor : MC9S08QG8
** Version : $v
** Location : h:\projects\freescale\adc comp\
** Author : Jim McLean
** Company : Highlander Technologies
** Date : 11/04/2005, 8:33 AM :)
** Notes : HC9S08QG Quick Start Example
** : This example can be used on the DEMO9S08QG8 board
**
*******************************************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC9S08QG8.h> /* include peripheral declarations */
int ADC_val_H;
int ADC_val_L;
void main(void)
{
// set up adc
ADCSC1_ADCH = 0x1f; // all channels selected disables the ADC during config
ADCCFG_ADICLK = 0; // clock source, 00=bus clock, 11 = async. clock
ADCCFG_MODE = 0b10; // conversion mode, 00 = 8 bit, 10 = 10 bit mode
ADCCFG_ADLSMP = 1; // sample time, 1 = long sample time
ADCCFG_ADIV = 1; // clock divide, 1 = input clock/2
ADCCFG_ADLPC = 1; // low power config, 1 = low power
ADCSC2_ADTRG = 0; // trigger select, 0 = software trigger
ADCSC2_ACFE = 1; // compare function, 1 = enable compare function
ADCSC2_ACFGT = 1; // 1 = compare function greater than
ADCSC1_ADCO = 1; // enable single conversion, 1 is continuous conversion
ADCSC1_AIEN = 1; // enable ADC interrupt
APCTL1_ADPC0 = 1; // disable I/O control on pin
ADCCVL = 0x80; // set a compare value
ADCCVH = 0x01; // set a compare value
// set up output port for an indicator
PTBDD_PTBDD7 = 1; //set PTB.7 as an output
PTBD_PTBD7 = 0;
// set up kbi
PTAPE_PTAPE2 = 1; // enable pullup for kb2
KBIPE_KBIPE2 =1; // enable kb 2
KBISC_KBACK = 1; // clear kb interrupts
KBISC_KBIE = 1; // enable kbi interrupts
EnableInterrupts; // enable interrupts
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
}// end main
//KBI ISR
// the key interrupt is used to start and ADC conversion
// and enter wait mode until the Input > Compare value
interrupt 18 void KBI_ISR(void)
{
KBISC_KBACK = 1; // clear kb interrupt
ADCSC1_ADCH = 0;
}
interrupt 19 void ADC_ISR(void)
{
ADC_val_H = ADCRH;
ADC_val_L = ADCRL;
PTBD_PTBD7 = ~PTBD_PTBD7; // toggle PTB.7 pin
ADCSC1_ADCH = 0x1f; // disable the ADC
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -