この例ではMAX7651EVKITを使用しています。また一方、Keil µVision IDE Simulatorを使用することによってMAX7651EVKITなしでもこのソースコードを動作させることが可能です。Keil社製ソフトウェアのインストールの詳細については、MAX7651EVKITのクイックスタートマニュアルを参照してください。
このソースコード例を有効に活用するためには、ある程度Cプログラミング言語に精通していることが前提になります。このコードは、「ADC Test Function」と呼ばれるmain()プログラムと、convert_all_channels()およびconvert_channel()という2個の関数の、3つの部分で構成されています。メインプログラムは、最初にMAX7651のレジスタ位置によるプログラムの初期化と標準ライブラリ(stdio.h)関数のインクルードを行っています。次に、標準シリアルポートを使用してPCとの通信を行うためのMAX7651の設定を行っています。
/*-------------------------------------------------------------------------
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 <reg51.h>//include MAX7651 register definitions
#include <stdio.h>//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
µVision2はKeil Corporationの登録商標です。
µVisionはKeil, An ARM Companyの登録商標です。