📄 main.c
字号:
/**
* Copyright (c) 2004, Freescale Semiconductor
* Freescale Willy Note
*
* File name : main.c
* Project name: HC08 ADC Willy Note
*
* Author : Luis Reynoso
* Department : RTAC Americas
*
* Description : This is a descriptive example of the ADC module in the
* MC98HC908QY4 MCU.
* The Example will turn on a LED when PTA5/OSC1/AD3 is
* greater than 2.5V.
*
* History :
* 29/06/2004 : Release. (A19257)
*/
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC68HC908QY4.h> /* include peripheral declarations */
/* Macro Definitions */
#define START_CONVERSION(Channel) ADSCR = 0x40|(Channel);
/*
* 0b010XXXXX
* ||||||||__
* |||||||___|
* ||||||____|--> Selected Channel
* |||||_____|
* ||||______|
* |||_______ Single ADC conversion
* ||________ ADC interrupt Enabled
* |_________ Unimplemented
*/
/* Functions prototypes */
void MCUInit (void);
void ADCInit (void);
/* Global Variables */
volatile unsigned char result; /* Result of the ADC conversion */
void main(void) {
MCUInit();
DDRA = 0x02; /* Set PTA1 as output */
ADCInit();
EnableInterrupts;
START_CONVERSION(0x03); /*
* Start Conversion for Ch3 (See #define)
* Since Internal OSC is used, AD3 is free
*/
for(;;)
; /* Empty Body */
}
/*
* ADCIsr: Interrupt Service routine for the ADC.
* It will turn on PTA1 if ADC result is greater than 128(2.5V).
*
* Parameters: None
*
* Return : None
*/
void interrupt 16 ADCIsr (void){
result = ADR; /* Read result and acknowledge interrupt */
PTA_PTA1 = (result >> 7); /* Turn on PTA1 if result > 128 */
START_CONVERSION(0x03); /* Re-Start Conversion for Ch3 */
return;
}
/*
* ADCInit: Initialization of the ADC Module.
* For the QY4 it will configure the ADC clock.
*
* Parameters: None
*
* Return : None
*/
void ADCInit (void)
{
ADICLK = 0x40; /*
* 0b01000000
* ||||||||__
* |||||||___|
* ||||||____|
* |||||_____|--> Unimplemented
* ||||______|
* |||_______|
* ||________\__ ADC Clock Rate = BusClock/4
* |_________/
* ADC Clock Rate = BusClock/4 = 3.2Mhz/4 =0.8Mhz
* The ADC clock frequency should be set between
* fADIC(MIN) = 0.5Mhz and fADIC(MAX) = 1.048Mhz
*/
return;
}
/*
* MCUInit: Initialization of the MCU.
* It will configure the QY4 to use the internal oscillator.
*
* Parameters: None
*
* Return : None
*/
void MCUInit (void)
{
CONFIG1 = 0x39; /*
* 0b00111001
* ||||||||__ COP Module Disabled
* |||||||___ STOP is illegal
* ||||||____ Stop recovery after 4096 cycles
* |||||_____ LVI operate in 5-V mode
* ||||______ LVI module disabled
* |||_______ LVI resets disabled
* ||________ LVI disabled in stop mode
* |_________ COP reset long cycle
*/
CONFIG2 = 0x01; /*
* 0b00000001
* ||||||||__ Reset function active in pin
* |||||||___ Reserved
* ||||||____ Reserved
* |||||____
* ||||_____> Internal Oscilator
* |||_______ Reserved
* ||________ IRQ inactive in pin
* |_________ IRQ pullup connected
*/
OSCTRIM = 0x75; /* Trim OSC with value in Flash (0xFFC0) */
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -