TXT

AD

By Luis Lopez,2014-07-08 06:53
33 views 0
AD

//ÓÃÕâ?öµ?Æ??ú?à?Ì?ì?âÓÐÎÞÎ?Ð?µçÑ?Öµ??Óõ?ADC??ÄÜ??Ö???P1.0ÉèΪ?ì?

    â?Ú??ÈôÓÐÄÇ?öÐ?µÄµçÑ???Ê?P2.0ºÍP2.1Ç?ÍÆÍìÊä?ö?ßµçÎ?

    typedef unsigned char BYTE; typedef unsigned int WORD; #include "reg51.h"

    #include "intrins.h"

/*Declare SFR associated with the ADC */

    sfr ADC_CONTR = 0xBC; sfr ADC_RES = 0xBD; sfr ADC_LOW2 = 0xBE; sfr P1ASF = 0x9D; sfr P2M0 = 0x96;

    sfr P2M1 = 0x95;

/*Define ADC operation const for ADC_CONTR*/

    #define ADC_POWER 0x80 #define ADC_FLAG 0x10 #define ADC_START 0x08 #define ADC_SPEEDLL 0x00 #define ADC_SPEEDL 0x20 #define ADC_SPEEDH 0x40 #define ADC_SPEEDHH 0x60

//ÑÓÊ?

    void Delay(WORD n)

    {

     WORD x;

     while (n--)

     {

     x = 5000;

     while (x--);

     }

    }

//?õÊ?AD?Ä?æÆ?

    void InitADC()

    {

     P1ASF = 0xff;

     ADC_RES = 0;

     ADC_CONTR = ADC_POWER | ADC_SPEEDLL;

     Delay(2);

    }

//ADת??

    BYTE GetADCResult(BYTE ch)

    {

     ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;

     _nop_();

     _nop_();

     _nop_();

     _nop_();

     while (!(ADC_CONTR & ADC_FLAG));

     ADC_CONTR &= ~ADC_FLAG;

     return ADC_RES;

    }

void main()

    { BYTE temp;

     P2M1=0; P2M0=0x03; //P2.0ºÍP2.1Ç?ÍÆÍì

     InitADC();

     while (1)

     {

     temp=GetADCResult(0);//ת??P1.0

     if(temp>1)

     P2|=0x03; //P1.0ÓеçÑ? P2.0ºÍP2.1Êä?ö?ß

     else

     P2&=0xFC; //P1.0ÎÞµçÑ? P2.0ºÍP2.1Êä?öµÍ

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com