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

📄 82x54ad_test.txt

📁 8051 AD Converter demo source code
💻 TXT
字号:
// Megawin MPC82X52  A/D Converter Demo code
// Edited by Peter_Lin                        Date:12-20-2005
#include "C:\Keil\C51\INC\Megawin\reg82xx.h"
unsigned  get_channel_value(unsigned char);
unsigned char key_sel, key_status, on_off;
void erase_IAP(unsigned int);
void write_IAP(unsigned int, unsigned char);
unsigned char read_IAP(unsigned int);
unsi
void main(void)
{
                unisgned char ad_result;
	init_ad(void);
                unsigned int iap_address_H;
                Channel_sel((unsigned char) 0x00));
                erase_IAP(iap_address);
                get_channel_value((unsigned char) 0x01); 
                write_IAP(iap_adress,get_channel_value((unsigned char) 0x01) & 0x03);    
                write_IAP(iap_adress,get_channel_value((unsigned char) 0x01) >> 4 & 0x08);                        
                }     

unsigned char get_channel_value(unsigned char on_off)
{
	if (on_off == 1)
	ADCON = channel ;
	ADCON &= 0xEF ;  // clear ADCI
        ADCON |= 0x08 ;  // set ADCS to start A/D conversion
        while (1) {
            ACC = ADCON ;
            if (ACC^4) break ;
        }
	return( ((unsigned) ADCV << 2) | (ADCVL & 3) );
}


void init_ad(void)
    {
               AUXR |= 0x10;                                  // Enable "ENDCI" bit, Select the ADC function block to generate interrupt to the MCU.
               IE         |= 0xA0;                                // 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 Channel_sel(unsigned char channel)
    {
            ADCTL &= 0x80;                               // Set turn on A/D  block.     bit 7
            ADCTL  |= 0x00;                                 // AD converter speed control     bit 5 ~ bit 6     
                                       ;                                 // "0x00" by 840 cycle, "0x20" by 630 cycle, "0x40" by 420 cycle, "0x60" by 210 cycle.
            channel  &= 0xF8;                               // bit 0 ~ bit 2 select channel 0 ~ 7
            ADCTL  |= channel;                             // Set P1.0 ~ P1.7 as the AD input
                      }


void erase_IAP(unsigned int iap_address)
{
                                                               ;                  // MPC89X52 IAP Start address 0x2000 ~0x2BFF
	IFADRH = iap_address >> 8;                  //get high byte address              
	IFADRL = iap_address & 0xFF;             //get low bytes adress
	IFMT = 0x03 ;                                          // command page-erase
	ISPCR = 0x80 ;                                        // supposed 40MHz 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;                  //get high byte address              
	IFADRL = iap_address & 0xFF;             //get low bytes adress
	IFD = value ;                                            //Flash data buffer
	IFMT = 0x02 ;                                         // command byte-writing
	ISPCR = 0x80 ;                                       // supposed 40MHz X's oscillator applied
	SCMD = 0x46 ;
	SCMD = 0xB9 ;
}


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


            
            

   






⌨️ 快捷键说明

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