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

📄 hal_adc.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
字号:
/************************************************************************/
/*                                                                      */
/*    Copyright (C) 2006 Oki Electric Industry Co., LTD.                */
/*                                                                      */
/*    System Name    :  ML675050 series                                 */
/*    Module Name    :  ML675050 ADC HAL program                        */
/*    File   Name    :  hal_adc.c                                       */
/*    Date           :  2005/12/19 initial version                      */
/*                                                                      */
/************************************************************************/

#include <string.h>
#include "common.h"
#if defined(__arm)
#include "ml675050.h"
#else
#include "ml675050sim.h"
#endif
#include "hal_common.h"
#include "hal_interrupt.h"
#include "hal_adc.h"


/******************************/
/*     Private defines        */
/******************************/
/*--------- variable ---------*/
static volatile uint16_t adc_mode;
static volatile uint16_t adc_channel;
static volatile int8_t adc_result[0x8 - ADCON0_ADSNM4_7][3];

/*--------- function ---------*/
static void _adc_handler(void);          /* The handler of adc. */
static void get_adc_result(uint16_t channel, uint32_t addr);

/******************************/
/*     Public defines         */
/******************************/
FP adc_handler = _adc_handler;


/************************************************************************/
/*                                                                      */
/*  Function Name   : HALAdc_Init                                       */
/*  Input           : adc_mode 1: scan  0: select.                      */
/*                    channel Select ADC channel.                       */
/*  Output          : int16_t HAL_OK(1)                                 */
/*                            HAL_PARAM_ERROR(-2)                       */
/*                                                                      */
/*  Note : Initialize adc.                                              */
/*                                                                      */
/************************************************************************/
int16_t HALAdc_Init(uint16_t mode, uint16_t channel) {
    int16_t rtnVal = HAL_OK;

    adc_mode = mode;
    adc_channel = channel;

    /* CPUCLK/8 */
    OkiCLib_write16(ADCON2, ADCON2_ACKSEL);

    /* Scan mode */
    if (mode == 0) {
        OkiCLib_write16(ADCON0, (uint16_t)(channel | ADCON0_SCNC));
        /* Enable IRQ. */
        OkiCLib_set16bit(ADINT, ADINT_ADSNIE);
        /* Clear IRQ status. */
        OkiCLib_clr16bit(ADINT, ADINT_INTSN);
    }
    /* Select mode */
    else if (mode == 1) {
        OkiCLib_write16(ADCON1, channel);
        /* Enable IRQ. */
        OkiCLib_set16bit(ADINT, ADINT_ADSTIE);
        /* Clear IRQ status. */
        OkiCLib_clr16bit(ADINT, ADINT_INTST);
    }
    else {
        rtnVal = HAL_PARAM_ERROR;
    }

    return rtnVal;
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : HALAdc_Start                                      */
/*  Input           : start 1: start  0: stop.                          */
/*                    channel Select ADC channel.                       */
/*  Output          : int16_t HAL_OK(1)                                 */
/*                            HAL_PARAM_ERROR(-2)                       */
/*                                                                      */
/*  Note : Start or stop adc.                                           */
/*                                                                      */
/************************************************************************/
int16_t HALAdc_Start(uint16_t start) {
    int16_t rtnVal = HAL_OK;

    /* Scan mode */
    if (adc_mode == 0) {
        if (start == 1) {
            OkiCLib_set16bit(ADCON0, ADCON0_ADRUN);
        }
        else if (start == 0) {
            OkiCLib_clr16bit(ADCON0, ADCON0_ADRUN);
        }
        else {
            rtnVal = HAL_PARAM_ERROR;
        }
    }
    /* Select mode */
    else {
        if (start == 1) {
            OkiCLib_set16bit(ADCON1, ADCON1_STS);
        }
        else if (start == 0) {
            OkiCLib_clr16bit(ADCON1, ADCON1_STS);
        }
        else {
            rtnVal = HAL_PARAM_ERROR;
        }
    }

    return rtnVal;
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : HALAdc_RxData                                     */
/*  Input           : buff Buffer of ADC result.                        */
/*  Output          : int16_t HAL_OK(1)                                 */
/*                            HAL_PARAM_ERROR(-2)                       */
/*                                                                      */
/*  Note : Copy ADC result to buff.                                     */
/*                                                                      */
/************************************************************************/
int16_t HALAdc_RxData(int8_t *buff) {
    int16_t rtnVal = HAL_OK;
    static uint16_t i = 0;

    if (buff == NULL) {
        return HAL_PARAM_ERROR;
    }

    if (i >= (0x8 - ADCON0_ADSNM4_7)) {
        i = 0x0;
    }
    /* Scan mode */
    strcpy(buff, (int8_t *)adc_result[i]);
    i++;
    
    return rtnVal;
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : _adc_handler                                      */
/*  Input           : void                                              */
/*  Output          : void                                              */
/*                                                                      */
/*  Note : ADC handler.                                                 */
/*                                                                      */
/************************************************************************/
static void _adc_handler(void) {
    uint32_t addr[8];
    uint16_t i;
    
    addr[0] = ADR0;
    addr[1] = ADR1;
    addr[2] = ADR2;
    addr[3] = ADR3;
    addr[4] = ADR4;
    addr[5] = ADR5;
    addr[6] = ADR6;
    addr[7] = ADR7;
    
    /* stop A/D  */
    OkiCLib_write16(ADCON0, 0x0);

    /* Scan mode */
    if (adc_mode == 0) {
        for (i = adc_channel; i <= 0x7; i++) {
            get_adc_result(i, addr[i]);
        }
        OkiCLib_clr16bit(ADINT, ADINT_INTSN);   /* interruption factor are cleared */
    }
    /* Select mode */
    else {
        i = adc_channel;
        get_adc_result(i, addr[i]);
        OkiCLib_clr16bit(ADINT, ADINT_INTST);   /* interruption factor are cleared */
    }

    CALL_BACK_TABLE[INT_ADC](CALLBACK_STATE_NON);
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : get_adc_result                                    */
/*  Input           : channel ADC channel.                              */
/*                    addr AD result register address.                  */
/*  Output          : void                                              */
/*                                                                      */
/*  Note : Read ADC result and change to character.                     */
/*                                                                      */
/************************************************************************/
static void get_adc_result(uint16_t channel, uint32_t addr) {
    uint16_t ad_result;
    
    /* Read result from AD result register. */
    ad_result = OkiCLib_read16(addr);
    
    switch (channel) {
    case 0:     /* NOT USED */
        break;
    case 1:     /* NOT USED */
        break;
    case 2:     /* NOT USED */
        break;
    case 3:     /* NOT USED */
        break;
    case 4:     /* Channel 4 */
        if (ad_result <= 0x02b7) {        /* "*" */
            adc_result[4 - adc_channel][0] = '*';
            adc_result[4 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x02b8) && (ad_result <= 0x06bd)) {   /* "0" */
            adc_result[4 - adc_channel][0] = '0';
            adc_result[4 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x06be) && (ad_result <= 0x08eb)) {   /* "#" */
            adc_result[4 - adc_channel][0] = '#';
            adc_result[4 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x08ec) && (ad_result <= 0x0a2e)) {   /* "D" */
            adc_result[4 - adc_channel][0] = 'D';
            adc_result[4 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x0a2f) && (ad_result <= 0x0d0a)) {   /* "F4" */
            adc_result[4 - adc_channel][0] = 'F';
            adc_result[4 - adc_channel][1] = '4';
        }
        else {
            ;
        }
        break;
    case 5:     /* Channel 5 */
        if (ad_result <= 0x02b7) {        /* "7" */
            adc_result[5 - adc_channel][0] = '7';
            adc_result[5 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x02b8) && (ad_result <= 0x06bd)) {   /* "8" */
            adc_result[5 - adc_channel][0] = '8';
            adc_result[5 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x06be) && (ad_result <= 0x08eb)) {   /* "9" */
            adc_result[5 - adc_channel][0] = '9';
            adc_result[5 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x08ec) && (ad_result <= 0x0a2e)) {   /* "C" */
            adc_result[5 - adc_channel][0] = 'C';
            adc_result[5 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x0a2f) && (ad_result <= 0x0d0a)) {   /* "F3" */
            adc_result[5 - adc_channel][0] = 'F';
            adc_result[5 - adc_channel][1] = '3';
        }
        else {
            ;
        }
        break;
    case 6:     /* Channel 6 */
        if (ad_result <= 0x02b7) {        /* "4" */
            adc_result[6 - adc_channel][0] = '4';
            adc_result[6 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x02b8) && (ad_result <= 0x06bd)) {   /* "5" */
            adc_result[6 - adc_channel][0] = '5';
            adc_result[6 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x06be) && (ad_result <= 0x08eb)) {   /* "6" */
            adc_result[6 - adc_channel][0] = '6';
            adc_result[6 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x08ec) && (ad_result <= 0x0a2e)) {   /* "B" */
            adc_result[6 - adc_channel][0] = 'B';
            adc_result[6 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x0a2f) && (ad_result <= 0x0d0a)) {   /* "F2" */
            adc_result[6 - adc_channel][0] = 'F';
            adc_result[6 - adc_channel][1] = '2';
        }
        else {
            ;
        }
        break;
    case 7:     /* Channel 7 */
        if (ad_result <= 0x02b7) {        /* "1" */
            adc_result[7 - adc_channel][0] = '1';
            adc_result[7 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x02b8) && (ad_result <= 0x06bd)) {   /* "2" */
            adc_result[7 - adc_channel][0] = '2';
            adc_result[7 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x06be) && (ad_result <= 0x08eb)) {   /* "3" */
            adc_result[7 - adc_channel][0] = '3';
            adc_result[7 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x08ec) && (ad_result <= 0x0a2e)) {   /* "A" */
            adc_result[7 - adc_channel][0] = 'A';
            adc_result[7 - adc_channel][1] = '\0';
        }
        else if ((ad_result >= 0x0a2f) && (ad_result <= 0x0d0a)) {   /* "F1" */
            adc_result[7 - adc_channel][0] = 'F';
            adc_result[7 - adc_channel][1] = '1';
        }
        else {
            ;
        }
        break;
	default :
        break;
    }
}

⌨️ 快捷键说明

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