59、ADC的工作原理和编写程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18077275/article/details/89421068

1、工作原理

2、adc.c

#include "../s3c2440_soc.h"

void adc_init(void)
{
    /* [15] : ECFLG,  1 = End of A/D conversion
     * [14] : PRSCEN, 1 = A/D converter prescaler enable
     * [13:6]: PRSCVL, adc clk = PCLK / (PRSCVL + 1)
     * [5:3] : SEL_MUX, 000 = AIN 0
     * [2]   : STDBM
     * [0]   : 1 = A/D conversion starts and this bit is cleared after the startup.
     */
    ADCCON = (1<<14) | (49<<6) | (0<<3);

    ADCDLY = 0xff;    
}

int adc_read_ain0(void)
{
    /* Æô¶¯ADC */
    ADCCON |= (1<<0);

    while (!(ADCCON & (1<<15)));  /* µÈ´ýADC½áÊø */

    return ADCDAT0 & 0x3ff;
}

3、adc_test.c


#include "adc.h"

void adc_test(void)
{
    int val;
    double vol;
    int m; /* 整数部分 */
    int n; /* 小数部分 */
    
    adc_init();

    while (1)
    {
        val = adc_read_ain0();
        vol = (double)val/1023*3.3;   /* 1023----3.3v */
        m = (int)vol;   /* 3.01, m = 3 */
        vol = vol - m;  /* 小数部分: 0.01 */
        n = vol * 1000;  /* 10 */

        /* 在串口上打印 */
        printf("vol: %d.%03dv\r", m, n);  /* 3.010v */

        /* 在LCD上打印 */
        //fb_print_string();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_18077275/article/details/89421068