摘要:本文提供了利用MAX7651评估板实现模/数转换的源代码和函数。它是MAX7651评估板提供的3个应用例程之一,能够进行写、编译和下载等简单的编程功能。
另请参考: 本文利用8051兼容的微控制器MAX7651实现模/数转换,并给出了程序代码。其中包括一些关键的写操作、编译和程序下载步骤,支持MAX7651EVKIT进行模数转换,并可读取转换结果。程序使用了MAX7651内部的12位集成ADC。
本例源代码利用Keil DK-51 IDE创建,需要从www.keil.com下载并安装Keil DK-51演示软件,以简化例程使用。Keil程序包含MAX7651基本的库文件,接下来,应该下载µVision2®项目文件和源代码:AN3083_uVision2_Project_Files.zip。
本例使用MAX7651EVKIT,当然,必要时也可以使用Keil µVision® IDE仿真器演示这些源代码,无需使用MAX7651EVKIT。软件安装请参考MAX7651EVKIT的快速入门使用手册,其中提供了关于Keil软件安装的详细信息。
注意:这里提供的例程是Maxim给出的公开源代码,可以免费复制,并根据具体应用进行修改。
主程序main()调用两个子函数,函数convert_all_channels()顺序对8个ADC输入通道进行转换;函数convert_channel()能够使用户自行选择转换通道并返回ADC转换结果。
为了启动一次转换,只需简单调用函数convert_all_channels()或convert_channel()。函数通过将所要求的转换通道写入MAX7651的ADCON寄存器执行转换操作,然后通过循环检测ADCON寄存器终止转换。转换结束后,将转换结果返回到调用函数内。主程序main()通过调用stdio.h内的printf()函数,通过串口将结果显示在显示器上。
另请参考: 本文利用8051兼容的微控制器MAX7651实现模/数转换,并给出了程序代码。其中包括一些关键的写操作、编译和程序下载步骤,支持MAX7651EVKIT进行模数转换,并可读取转换结果。程序使用了MAX7651内部的12位集成ADC。
本例源代码利用Keil DK-51 IDE创建,需要从www.keil.com下载并安装Keil DK-51演示软件,以简化例程使用。Keil程序包含MAX7651基本的库文件,接下来,应该下载µVision2®项目文件和源代码:AN3083_uVision2_Project_Files.zip。
本例使用MAX7651EVKIT,当然,必要时也可以使用Keil µVision® IDE仿真器演示这些源代码,无需使用MAX7651EVKIT。软件安装请参考MAX7651EVKIT的快速入门使用手册,其中提供了关于Keil软件安装的详细信息。
注意:这里提供的例程是Maxim给出的公开源代码,可以免费复制,并根据具体应用进行修改。
C程序说明
为了有效使用该例程,需要熟悉C语言编程。程序包括三部分:“ADC Test Function”主程序(main())和两个调用子函数:convert_all_channels()和convert_channel()。主程序首先对MAX7651的寄存器进行初始化,并提供一些标准的库函数。然后配置MAX7651,与PC的标准串口进行通信。主程序main()调用两个子函数,函数convert_all_channels()顺序对8个ADC输入通道进行转换;函数convert_channel()能够使用户自行选择转换通道并返回ADC转换结果。
为了启动一次转换,只需简单调用函数convert_all_channels()或convert_channel()。函数通过将所要求的转换通道写入MAX7651的ADCON寄存器执行转换操作,然后通过循环检测ADCON寄存器终止转换。转换结束后,将转换结果返回到调用函数内。主程序main()通过调用stdio.h内的printf()函数,通过串口将结果显示在显示器上。
/*------------------------------------------------------------------------- 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
评论
查看更多