#include "SM39R16A2.h"
#include "ad.h"
#define d_ADCnEN 0x40
#define d_ADC_CLK_Sel d_ADC_CLK_DIV2
#define d_ADCR 0x00
double n_data,z;
double n_ADCD10 ;
double z_ADCD10 ;
void ADCstart(unsigned char n_ADC_CH)
{
ADCC2 = (0x80|d_ADCR|n_ADC_CH); //ADC start conversion
}
void ADCInit(unsigned char n_ADCnEN, unsigned char n_ADC_CLK)
{
ADCC1 = n_ADCnEN; //Set ADC channel
ADCCS = n_ADC_CLK; //Select ADC clock
IEADC = 1; //Enable ADC interrupt.
EA = 1;
}
double ADC_Read10()//10-bit ADC
{
n_ADCD10 = (ADCDH<<2); //10-bit ADC
return n_ADCD10;
}
void ADCInterrupt(void) interrupt d_ADC_Vector // ADC Interrupt Vector Address 0053h => interrupt 10
{
n_data = ADC_Read10(); //Read ADC
}
void ADC_stop(void)
{
ADCC2 = 0x00; //ADC stop conversion
}
double ADC(void)
{
if (d_ADCnEN & 0x01)
{
ADCstart(d_ADC_CH0_IN);
while(!ADCIF);
ADC_stop();
}
if (d_ADCnEN & 0x02)
{
ADCstart(d_ADC_CH1_IN);
while(!ADCIF);
ADC_stop();
}
if (d_ADCnEN & 0x04)
{
ADCstart(d_ADC_CH2_IN);
while(!ADCIF);
ADC_stop();
}
if (d_ADCnEN & 0x08)
{
ADCstart(d_ADC_CH3_IN);
while(!ADCIF);
ADC_stop();
}
if (d_ADCnEN & 0x10)
{
ADCstart(d_ADC_CH4_IN);
while(!ADCIF);
ADC_stop();
}
if (d_ADCnEN & 0x20)
{
ADCstart(d_ADC_CH5_IN);
while(!ADCIF);
ADC_stop();
}
if (d_ADCnEN & 0x40)
{
ADCstart(d_ADC_CH6_IN);
while(!ADCIF);
ADC_stop();
}
if (d_ADCnEN & 0x80)
{
ADCstart(d_ADC_CH7_IN);
while(!ADCIF);
ADC_stop();
}
return n_data;
}
/*--------------------------------------------------------------------------
Header file for ADC.
Modify on 2010/07/1.
--------------------------------------------------------------------------*/
#ifndef __ADC_H__
#define __ADC_H__
// Example Code
/*
void main(void)
{
ADCInit(d_ADCnEN, d_ADC_CLK_Sel);
ADC();
ADC_stop();
while(1);
}
*/
void ADCstart(unsigned char n_ADC_CH);
void ADCInit(unsigned char n_ADCnEN, unsigned char n_ADC_CLK);
double ADC_Read10();
void ADC_stop(void);
double ADC(void);
//===============================
//DEFINITIONs
//===============================
#define d_ADC_CLK_DIV2 0x00
#define d_ADC_CLK_DIV4 0x01
#define d_ADC_CLK_DIV6 0x02
#define d_ADC_CLK_DIV8 0X03
#define d_ADC_CLK_DIV10 0x04
#define d_ADC_CLK_DIV12 0x05
#define d_ADC_CLK_DIV14 0x06
#define d_ADC_CLK_DIV16 0x07
#define d_ADC_CLK_DIV18 0x08
#define d_ADC_CLK_DIV20 0x09
#define d_ADC_CLK_DIV22 0x0A
#define d_ADC_CLK_DIV24 0x0B
#define d_ADC_CLK_DIV26 0x0C
#define d_ADC_CLK_DIV28 0x0D
#define d_ADC_CLK_DIV30 0x0E
#define d_ADC_CLK_DIV32 0x0F
#define d_ADC_CLK_DIV34 0x10
#define d_ADC_CLK_DIV36 0x11
#define d_ADC_CLK_DIV38 0x12
#define d_ADC_CLK_DIV40 0x13
#define d_ADC_CLK_DIV42 0x14
#define d_ADC_CLK_DIV44 0x15
#define d_ADC_CLK_DIV46 0x16
#define d_ADC_CLK_DIV48 0x17
#define d_ADC_CLK_DIV50 0x18
#define d_ADC_CLK_DIV52 0x19
#define d_ADC_CLK_DIV54 0x1A
#define d_ADC_CLK_DIV56 0x1B
#define d_ADC_CLK_DIV58 0x1C
#define d_ADC_CLK_DIV60 0x1D
#define d_ADC_CLK_DIV62 0x1E
#define d_ADC_CLK_DIV64 0x1F
#define d_ADC_CH0_IN 0
#define d_ADC_CH1_IN 1
#define d_ADC_CH2_IN 2
#define d_ADC_CH3_IN 3
#define d_ADC_CH4_IN 4
#define d_ADC_CH5_IN 5
#define d_ADC_CH6_IN 6
#define d_ADC_CH7_IN 7
#endif
为什么检测出来的adc不稳定,变化很大,波动,是片子的问题,我觉得硬件是没问题的 |