⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 摩托罗拉08单片机AD转换程序 使用CodeWarrior开发
💻 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 
 *               MC98HC908EY16 MCU.
 *               The Example will turn two LEDS when PTB4/AD4 is 
 *               greater than 2.5V
 *
 * History     :
 * 29/06/2004  : Release. (A19257)
 */

#include <hidef.h>           /* for EnableInterrupts macro */
#include <MC68HC908EY16.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 short result;     /* Result of the ADC in 10-bits */
volatile unsigned char res_l,res_h; /* Lower & Upper bytes of ADC */

void main(void) {

    MCUInit();
    DDRB = 0x03;             /* Set PTB0 & PTB1 as output2 */
    ADCInit(); 
    EnableInterrupts;
    START_CONVERSION(0x04);	 /* Start Conversion for Ch4 (See #define) */
    for(;;) 
        ; /* Empty Body */
}

/*
 * ADCIsr: Interrupt Service routine for the ADC.
 * It will turn on PTB0 if ADC result is greater than 512(2.5V).
 *
 * Parameters: None
 *
 * Return : None
 */
void interrupt 14 ADCIsr (void){

    res_h = ADRH;             /* Read result and acknowledge interrupt */
    res_l = ADRL;
    result = (res_h << 8) + (res_l); /* Using 16 bits variable */
    PTB_PTB0 = (result >> 9); /* Turn on PTB0 if result >= 512 */
    PTB_PTB1 = (res_h >> 1);  /* Same as above but using PTB1 and upper byte */
    START_CONVERSION(0x04);   /* Re-Start Conversion for Ch4 */

    return;
}

/*
 * ADCInit: Initialization of the ADC Module.
 * For the EY16 it will configure the ADC clock, and the mode
 *
 * Parameters: None
 *
 * Return : None
 */
void ADCInit (void)
{

    ADCLK = 0x84;           /*  
                             *  0b10000100
                             *    ||||||||__ Unimplemented
                             *	  |||||||___ Reserved  
                             *	  ||||||____
                             *	  |||||_____> Right Justified Mode
                             *	  ||||______ CGMXCLK is ADC clock input 
                             *	  |||_______ 
                             *	  ||________\__	ADC Clock Rate = ADCClock/16 
                             *	  |_________/ 
                             *   ADC Clock Rate = ADCClock / 16 
                             *                  = CGMXCLK  / 16
                             *                  = 9.8304Mhz/ 16 
                             *                  = 0.6144MHz
                             *  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 EY16 to use the External 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 = 0x19;	        /*  
                             *  0b00011001
                             *	  ||||||||__ SS pull-up enabled
                             *	  |||||||___ OSC disabled in STOP
                             *	  ||||||____ Disable division by 128 for TBM
                             *	  |||||_____ PTC4/OSC1 enabled as clock input
                             *	  ||||______ Fast external crystal operation 
                             *	  |||_______ PTC3/OSC2 is I/O pin
                             *	  ||________ CGMXCLK is source of ESCI
                             *	  |_________ Reserved
                             */  
    do {
        ICGCR = 0x13;            
    } while (ICGCR != 0x13); /* Enable the external oscillator */

    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -