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

📄 82x54ad_test.bak

📁 8051 AD Converter demo source code
💻 BAK
字号:
// Megawin MPC82X54  A/D Converter Demo code
// Edited by Peter_Lin                     Date:12-20-2005
#include "C:\Keil\C51\INC\Megawin\reg82xx.h"
unsigned int get_channel_value(unsigned char);
unsigned char key_sel, key_status;
void init_ad(void);
void erase_IAP(unsigned int);
void write_IAP(unsigned int, unsigned char);
unsigned char read_IAP(unsigned int);
sbit test_T2=   P2^6;


unsigned int get_channel_value(unsigned char channel)
{
	
	ADCTL = channel ;
	ADCTL &= 0x07 ;  // clear ADCI
    ADCTL |= 0x80 ;  // enable A/D Converter
    ADCTL |= 0x08 ;  // set ADCS to start A/D conversion
        while (1) {
            ACC = ADCTL ;
            if (ACC^4) break ;
        }
    ADCTL &= 0x67;   // Stop A/D Converter .and. ADCI
	return( ((unsigned int) ADCV << 2) | (ADCVL & 3) );
               }


void init_ad(void)
    {
           AUXR  |= 0x10;       // Enable "ENADCI" bit, Select the ADC function block to generate interrupt to the MCU.
           IE    |= 0x20;       // Enable "EA" & "ESPI" Bit, Select the A/D function.
           IP    |= 0x20;       // Enable "PSPI"  Bit, Set  priority for A/D inturrept higher.
           IPH   |= 0x20;       // Enable "PSPIH"  Bit, Set  priority for A/D inturrept higher.
               
          }

void erase_IAP(unsigned int iap_address)
  {
    IFADRL = iap_address & 0xFF;           //get low bytes adress                                                           ;                  // MPC89X52 IAP Start address 0x2000 ~0x2BFF
	IFADRH = (iap_address >> 8) & 0x1F;    //get high byte address              
	IFMT = 0x03 ;                          // command page-erase
	ISPCR = 0x84 ;                         // supposed 6MHz X's oscillator applied
	SCMD = 0x46 ;                          //Trigger IAP memory
	SCMD = 0xB9 ;                          //
         }


void write_IAP(unsigned int iap_address, unsigned char value)
   {
                                                               ;                  //Byte writing
	IFADRH = (iap_address >> 8) & 0x1F;   // get high byte address              
	IFADRL = iap_address & 0xFF;          // get low bytes adress
	IFD = value ;                         // Flash data buffer
	IFMT = 0x02 ;                         // command byte-writing
	ISPCR = 0x84 ;                        // supposed 6MHz X's oscillator applied
	SCMD = 0x46 ;
	SCMD = 0xB9 ;
}


unsigned char read_IAP(unsigned int iap_address)
   {
                                                               ;                  //Byte writing
	IFADRH = (iap_address >> 8) & 0x1F;   // get high byte address              
	IFADRL = iap_address & 0xFF;          // get low bytes adress
	IFMT = 0x01;                          // command byte-reading
	ISPCR = 0x84;                         // supposed 6MHz X's oscillator applied
	SCMD = 0x46;
	SCMD = 0xB9;
    return(IFD);                          //return Flash data
}


void main(void)
{
    unsigned int ad_result;
	unsigned int iap_addr;
data unsigned int  time; //delay

              P1M0 = 0x03; //P1 bit 0 set to input only
              P1M1 = 0x00; // P1M0 Bit0 set to "1"/P1M1 bit0 set to "0"
             iap_addr=0x1000;
             init_ad();
             erase_IAP(iap_addr);
    while(1)
              {
             ad_result=get_channel_value((unsigned char) 0x01); //select channel 1
            // write_IAP(iap_addr,ad_result & 0x03);    
            // write_IAP(iap_addr+1,ad_result >> 4 & 0x08); 
             ad_result=read_IAP(iap_addr); 
             if (ad_result > 200) { 
             for(time=0;time<0x1000;time++) 
               { }  
              test_T2 ^=1; }

                         }                   
                }     



            
            

   






⌨️ 快捷键说明

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