另請參考: 本文利用8051兼容的微控制器MAX7651實現(xiàn)模/數(shù)轉(zhuǎn)換,并給出了程序代碼。其中包括一些關(guān)鍵的寫操作、編譯和程序下載步驟,支持MAX7651EVKIT進行模數(shù)轉(zhuǎn)換,并可讀取轉(zhuǎn)換結(jié)果。程序使用了MAX7651內(nèi)部的12位集成ADC。
本例源代碼利用Keil DK-51 IDE創(chuàng)建,需要從www.keil.com下載并安裝Keil DK-51演示軟件,以簡化例程使用。Keil程序包含MAX7651基本的庫文件,接下來,應(yīng)該下載μVision2?項目文件和源代碼:AN3083_uVision2_Project_Files.zip。
本例使用MAX7651EVKIT,當然,必要時也可以使用Keil μVision? IDE仿真器演示這些源代碼,無需使用MAX7651EVKIT。軟件安裝請參考MAX7651EVKIT的快速入門使用手冊,其中提供了關(guān)于Keil軟件安裝的詳細信息。
注意:這里提供的例程是Maxim給出的公開源代碼,可以免費復(fù)制,并根據(jù)具體應(yīng)用進行修改。
C程序說明
為了有效使用該例程,需要熟悉C語言編程。程序包括三部分:“ADC Test Function”主程序(main())和兩個調(diào)用子函數(shù):convert_all_channels()和convert_channel()。主程序首先對MAX7651的寄存器進行初始化,并提供一些標準的庫函數(shù)。然后配置MAX7651,與PC的標準串口進行通信。主程序main()調(diào)用兩個子函數(shù),函數(shù)convert_all_channels()順序?qū)?個ADC輸入通道進行轉(zhuǎn)換;函數(shù)convert_channel()能夠使用戶自行選擇轉(zhuǎn)換通道并返回ADC轉(zhuǎn)換結(jié)果。
為了啟動一次轉(zhuǎn)換,只需簡單調(diào)用函數(shù)convert_all_channels()或convert_channel()。函數(shù)通過將所要求的轉(zhuǎn)換通道寫入MAX7651的ADCON寄存器執(zhí)行轉(zhuǎn)換操作,然后通過循環(huán)檢測ADCON寄存器終止轉(zhuǎn)換。轉(zhuǎn)換結(jié)束后,將轉(zhuǎn)換結(jié)果返回到調(diào)用函數(shù)內(nèi)。主程序main()通過調(diào)用stdio.h內(nèi)的printf()函數(shù),通過串口將結(jié)果顯示在顯示器上。
/*------------------------------------------------------------------------- ADC Test Function (main.c) Copyright: Maxim Integrated Products Target: MAX7651 Date: Feb 26, 2004 Author: Maxim Integrated Products Description: This program will convert 8 channels using the MAX7651 and send the results to Serial Port 0 --------------------------------------------------------------------------*/ #include//include MAX7651 register definitions #include //Standard I/O, Print() function. #define NUMBER_OF_CHANNELS 8 //Convert 8 channels void convert_all_channels(int* buffer); //Function declaration int convert_channel(int adc_ch); //Function declaration void main(void) //Begin Main() { int Adc_Results[NUMBER_OF_CHANNELS]; //Array to store conversion result int i; //for loop counter convert_all_channels(Adc_Results); //Convert all channels, Store results in Adc_Results #ifndef MONITOR51 //Setup Serial Port if not using Keil Monitor SCON = 0x50; //9600 Baud, 8N1, Xon, Xoff TMOD |= 0x20; TH1 = 0xFA; TR1 = 1; TI = 1; PCON |= 0x80; #endif for (i=0; i < NUMBER_OF_CHANNELS; i++) //Display contents to Adc_Results { printf ("CH %d:%x ",i,Adc_Results[i]); //print the hex } printf(" channel 0 %x", convert_channel(0)); //Convert a single channel and display printf(" channel 1 %x", convert_channel(1)); printf(" channel 2 %x", convert_channel(2)); printf(" channel 3 %x", convert_channel(3)); printf(" channel 4 %x", convert_channel(4)); printf(" channel 5 %x", convert_channel(5)); printf(" channel 6 %x", convert_channel(6)); printf(" channel 7 %x", convert_channel(7)); while(1); //End Program, Start infinite loop since there is no OS } /*------------------------------------------------------------------------- Function: convert_all_channels Copyright: Maxim Integrated Products Target: MAX7651 Date: Feb 26, 2004 Author: Maxim Integrated Products Usage: The function will return 8 conversion results to an array. Parameters: p_buffer, pointer to an 8 location array stores the conversion results Return: Values are returned to the calling function using the function parameters /*------------------------------------------------------------------------- Setup ADC in the MAX7651 --------------------------------------------------------------------------*/ sfr ADCON = 0xC5; //Define address of MAX7651 ADCON sfr ADDAT1 = 0xC3; //Define address of MAX7651 ADDAT1 (8MSBs) sfr ADDAT0 = 0xC2; //Define address of MAX7651 ADDAT0 (4LSBs) void convert_all_channels(int* buffer); --------------------------------------------------------------------------*/ #define NUMBER_OF_CHANNELS 8 void convert_all_channels(int* p_buffer) //pointer Buffer to return { int adc_ch; int conv_val; /*----------------------------------------------- Convert all ADC channels -----------------------------------------------*/ for (adc_ch = 0; adc_ch < NUMBER_OF_CHANNELS; adc_ch++) //for ADC channels 1 to 7 { /*----------------------------------------------- Start a conversion and wait for it to complete. -----------------------------------------------*/ ADCON = adc_ch; //Start conversion and select channel while ((ADCON & 0x80) == 0); //wait for conversion to complete conv_val = (ADDAT0 >> 4) | (ADDAT1 << 4); //Format the data in 12 bit format *(p_buffer+adc_ch) = conv_val; //Write result back to calling function } //End For } //End function convert_all_channels() /*------------------------------------------------------------------------- Function: convert_channel Copyright: Maxim Integrated Products Target: MAX7651 Date: Feb 26, 2004 Author: Maxim Integrated Products Usage: The function will convert and return the result of a Channel. Parameters: adc_ch, Select ADC channel to be converted. Channels 0-7 = single ended channel 0-7. Channels 8-11 = differential channel pairs {CH0,1}, {CH2,3}, {CH4,5}, {CH6,7} Channel 12 = differential reference measurement {REF+,REF-} Return: Function returns Integer with the conversion result Function Declaration: int convert_channel(int adc_ch); --------------------------------------------------------------------------*/ /*------------------------------------------------------------------------- Setup ADC in the MAX7651 --------------------------------------------------------------------------*/ sfr ADCON = 0xC5; //Define address of MAX7651 ADCON sfr ADDAT1 = 0xC3; //Define address of MAX7651 ADDAT1 (8MSBs) sfr ADDAT0 = 0xC2; //Define address of MAX7651 ADDAT0 (4LSBs) int convert_channel(int adc_ch) { int conv_val; if (ADCON <0 || ADCON >12){ //Check for valid channel return (-2048); //Using -FS for the error code } ADCON = adc_ch; //Select channel and Start conversion while ((ADCON & 0x80) == 0); //Wait for the conversion to complete conv_val = (ADDAT0 >> 4) | (ADDAT1 << 4); //Format the data in 12 bit format return (conv_val); //Return result back to calling function } //End function convert_chan
評論
查看更多