首页 > 代码库 > C8051F850 ADC Polling Example

C8051F850 ADC Polling Example

有项目需要用到Silabs 的C8051F850. 其实最主要是看中了价格,以及其工作温度。因此需要开始学习这颗片子。如下是最简单的ADC例程:

对系统时钟进行初始化,对IO进行初始化等。P1.0为LED输出,P1.2为ADC输入。ADC采用VDD和GND为参考源。并使用Polling的方式,而不是中断的方式。

void C8051F850_IO_Initial(void)
{
    //Disable Watchdog with key sequence
    WDTCN = 0xDE;
    WDTCN = 0xAD;
    
    //Using internal 24.5MHz RC Oscillator, and no Division
    CLKSEL = CLKSEL_CLKSL__HFOSC | CLKSEL_CLKDIV__SYSCLK_DIV_1;
    
    //GPIO initial
    P0MDOUT = P0MDOUT_B0__OPEN_DRAIN | P0MDOUT_B1__OPEN_DRAIN | P0MDOUT_B2__OPEN_DRAIN
		 | P0MDOUT_B3__OPEN_DRAIN | P0MDOUT_B4__PUSH_PULL | P0MDOUT_B5__OPEN_DRAIN
		 | P0MDOUT_B6__OPEN_DRAIN | P0MDOUT_B7__OPEN_DRAIN;
    
    //P1.0 LED output, P1.2 ADC input
    P1MDOUT = P1MDOUT_B0__PUSH_PULL | P1MDOUT_B1__OPEN_DRAIN | P1MDOUT_B2__OPEN_DRAIN
		 | P1MDOUT_B3__OPEN_DRAIN | P1MDOUT_B4__OPEN_DRAIN | P1MDOUT_B5__OPEN_DRAIN
		 | P1MDOUT_B6__OPEN_DRAIN | P1MDOUT_B7__OPEN_DRAIN;
    
    //P1.2 Alalog ADC input
    P1MDIN = P1MDIN_B0__DIGITAL | P1MDIN_B1__DIGITAL | P1MDIN_B2__ANALOG
		 | P1MDIN_B3__DIGITAL | P1MDIN_B4__DIGITAL | P1MDIN_B5__DIGITAL
		 | P1MDIN_B6__DIGITAL | P1MDIN_B7__DIGITAL;
    
    P1SKIP = P1SKIP_B0__NOT_SKIPPED | P1SKIP_B1__NOT_SKIPPED | P1SKIP_B2__SKIPPED
		 | P1SKIP_B3__NOT_SKIPPED | P1SKIP_B4__NOT_SKIPPED | P1SKIP_B5__NOT_SKIPPED
		 | P1SKIP_B6__NOT_SKIPPED | P1SKIP_B7__NOT_SKIPPED;
    
    //CrossBar initial
    XBR0 = XBR0_URT0E__ENABLED | XBR0_SPI0E__DISABLED | XBR0_SMB0E__DISABLED
		 | XBR0_CP0E__DISABLED | XBR0_CP0AE__DISABLED | XBR0_CP1E__DISABLED
		 | XBR0_CP1AE__DISABLED | XBR0_SYSCKE__DISABLED;
    
    XBR2 = XBR2_WEAKPUD__PULL_UPS_ENABLED | XBR2_XBARE__ENABLED;
    
    //ADC initial
    ADC0MX = ADC0MX_ADC0MX__ADC0P10;
    
    ADC0CF = (1 << ADC0CF_ADSC__SHIFT) | ADC0CF_AD8BE__NORMAL | ADC0CF_ADGN__GAIN_1
		 | ADC0CF_ADTM__TRACK_NORMAL;
    
    //ADC enable and start by writing ADBUSY
    ADC0CN0 &= ~ADC0CN0_ADCM__FMASK;
    ADC0CN0 |= ADC0CN0_ADEN__ENABLED
		 | ADC0CN0_ADCM__ADBUSY;
    
    //Voltage reference control
    REF0CN = REF0CN_REFSL__VDD_PIN | REF0CN_IREFLVL__2P4 | REF0CN_GNDSL__GND_PIN
		 | REF0CN_TEMPE__TEMP_DISABLED;
    
    //Interrupt, All interrupt disabled
    EIE1 = EIE1_EADC0__DISABLED | EIE1_ECP0__DISABLED | EIE1_ECP1__DISABLED
		 | EIE1_EMAT__DISABLED | EIE1_EPCA0__DISABLED | EIE1_ESMB0__DISABLED
		 | EIE1_ET3__DISABLED | EIE1_EWADC0__DISABLED;
}

在主函数中有如下的调用:

void main( void )
{
    C8051F850_IO_Initial();
    
    while(1)
    {
        unsigned long Delay = 60000;
        while(Delay--);
        
        ADC0CN0_bit.AD0BUSY = 1; //ADC start by Soft trigger 
        while(ADC0CN0_bit.AD0INT == 0); //Wait for ADC complate
        ADC0CN0_bit.AD0INT = 0; //Clear ADC Flag
        
        ADCValue = http://www.mamicode.com/ADC0H;>


以上代码,基于IAR for 8051编写。



C8051F850 ADC Polling Example