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

📄 adc_sample.c

📁 oki测试uart,pwm,a/d的程序
💻 C
字号:
/**************************************************************************/
/*                                                                        */
/*     Copyright (C) 2001 Oki Electric Industry Co.,LTD.                  */
/*                                                                        */
/*     System Name  :  ML674000                                           */
/*     Module Name  :  AD Converter sample program                        */
/*     File   Name  :  adc_sample.c                                       */
/*     Revision     :  01.00                                              */
/*     Date         :  2002/02/13                                         */
/*                     initial version                                    */
/*                                                                        */
/**************************************************************************/
#include   "ML674000.h"
#include   "common.h"
#include   "irq.h"
#include   "common.h"

/* constants */
#define    ROUND                10
#define    CHANNEL              7
#define    AD_DATA_1            0
#define    AD_DATA_2            1
#define    AD_DATA_3            2
#define    AD_DATA_4            3
#define    AD_DATA_5            4
#define    AD_DATA_6            5
#define    AD_DATA_7            6

/* functions */
void    init_irq(void);          /* initialize IRQ */
void    end_process(void);       /* ADC end process */
void    init_adc(void);          /* initialize ADC */
void    adc_handler(void);       /* ADC interrupt handler */
void    reg_irq_handler(void);   /* registration of IRQ handler */
void    led_on(UHWORD);          /* LED on */

/* global variable */
volatile  int  ADC_Mode;         /* 0:select mode , 1:scan mode */
volatile  int  sampling_count;   /* the number of times of conversion */
volatile  int  ADC_end_flag;     /* AD conversion end flag */
volatile  UHWORD  select_buff[ROUND];          /* the data changed in select mode are stored */
volatile  UHWORD  scan_buff[ROUND][CHANNEL];   /* the data changed in scan mode are stored */
volatile  UHWORD  scan_result[CHANNEL];        /* the sum total data of scan mode are stored */
volatile  UHWORD  select_result;               /* the sum total data of select mode is stored */

/****************************************************************************/
/*  Entry point                                                             */
/*  Function : main                                                         */
/*      Parameters                                                          */
/*          Input  :  Nothing                                               */
/*          Output :  0                                                     */
/****************************************************************************/
int main(void)
{
    int  i,j;
    int  channel_number;
    
    init_irq();                 /* initialize  IRQ */

    reg_irq_handler();          /* registration of an ADC interrupt handler */

    init_adc();                 /* initial setting of ADC */
    
    put_hvalue(GPPMB,0x00ff);   /* initialize LED */
    
    led_on(LED_START_PATTERN);  /* turn on upper square LED */
    
    irq_en();                   /* enable IRQ */
    
    
    /* initialize variables */
    for (i = 0; i < ROUND; i++){
        select_buff[i] = 0;
    }
    
    for (i = 0; i < ROUND; i++){
        for (j = 0; j < CHANNEL; j++)
            scan_buff[i][j] = 0;
    }
    
    select_result       =  0;
    
    for (i = 0; i < CHANNEL; i++){
        scan_result[i]  =  0;
    }
    
    
    /******************
       select mode 
    ******************/
    ADC_Mode = 0;
    for (sampling_count = 0; sampling_count < ROUND; sampling_count++){
        
        /* initialize AD conversion end flag */
        ADC_end_flag  =  0;
        
        /* AD conversion start */
        set_hbit(ADCON1,ADCON1_STS);
    
        while (ADC_end_flag != 1)
                ;
                
        /* sum total */
        select_result += select_buff[sampling_count];
    }
    
    /* average */
    select_result /= ROUND;
    
    
    /******************
         scan mode 
    ******************/
    ADC_Mode = 1;
   
    for (sampling_count = 0; sampling_count < ROUND; sampling_count++){
        
        /* initialize AD conversion end flag */
        ADC_end_flag   =  0;
        
        /* AD conversion start */
        set_hbit(ADCON0, ADCON0_SCNC | ADCON0_ADRUN| ADCON0_CH1_7);
    
        while (ADC_end_flag != 1)
                ;
                
        /* sum total */
        for(channel_number = 0; channel_number < CHANNEL; channel_number++)
            scan_result[channel_number] += scan_buff[sampling_count][channel_number];
            
    }
    
    /* average */
    for (channel_number = 0; channel_number < CHANNEL; channel_number++){
        scan_result[channel_number] /= ROUND;
    }
    
    
    end_process();              /* ADC end process */ 
    
    irq_dis();                  /* disable IRQ */
    
    led_on(LED_NORMAL_END_PATTERN);      /* turn on lower square LED */
    
    return 0;
}

/****************************************************************************/
/*  initialize ADC                                                          */
/*  Function : init_adc                                                     */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void init_adc(void) 
{
    set_hbit(ADCON2,ADCON2_CLK8);                 /* CCLK:33MHz -> ACKSEK:240nS */
    set_hbit(ADINT,ADINT_ADSTIE | ADINT_ADSNIE);  /* enable irq of selectmode & scanmode */

    return;
}

/****************************************************************************/
/*  ADC end process                                                         */
/*  Function : end_process                                                  */
/*        Parameters                                                        */
/*            Input  :  Nothing                                             */
/*            Output :  Nothing                                             */
/****************************************************************************/
void end_process(void)
{
    put_hvalue(ADINT,0x0);    /* disable ADC interrupt */
    
    return;
}

/****************************************************************************/
/*  ADC Interruption processing                                             */
/*  Function : adc_handler                                                  */
/*        Parameters                                                        */
/*            Input  :  Nothing                                             */
/*            Output :  Nothing                                             */
/****************************************************************************/
void adc_handler(void)
{

                                     
    if(ADC_Mode == 0){
        select_buff[sampling_count] = get_hvalue(ADR0);
        set_hbit(ADINT,ADINT_INTST);  /* interruption factor are cleared */
    }

    else if(ADC_Mode == 1){

        put_hvalue(ADCON0,0x0);       /* stop A/D  */

        scan_buff[sampling_count][AD_DATA_1] = get_hvalue(ADR1);
        scan_buff[sampling_count][AD_DATA_2] = get_hvalue(ADR2);
        scan_buff[sampling_count][AD_DATA_3] = get_hvalue(ADR3);
        scan_buff[sampling_count][AD_DATA_4] = get_hvalue(ADR4);
        scan_buff[sampling_count][AD_DATA_5] = get_hvalue(ADR5);
        scan_buff[sampling_count][AD_DATA_6] = get_hvalue(ADR6);
        scan_buff[sampling_count][AD_DATA_7] = get_hvalue(ADR7);

        set_hbit(ADINT,ADINT_INTSN);  /* interruption factor are cleared */
       
    }

    ADC_end_flag = 1;                 /* AD conversion completion */
    
    return;
}

/****************************************************************************/
/*  Registration of IRQ Handler                                             */
/*  Function : reg_irq_handler                                              */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/*  Note : Initialization of IRQ needs to be performed before this process. */
/****************************************************************************/
void reg_irq_handler(void)
{
    /* register IRQ handler into IRQ handler table */
    IRQ_HANDLER_TABLE[INT_AD] = adc_handler;  /* ADC Interrupt Number 11 */


    set_wbit(ILC1, ILC1_ILR11 & ILC1_INT_LV7 ); /* Interruption level of
                       ||          ||             IRQ number 11 is set as 7
                   IRQ number   interrypt level  */
    return;
}

void __gccmain()
{
}

⌨️ 快捷键说明

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