主函数:
/*********************&retvala&retval***************************\
* by threewater<threewater@up-tech.com> *
* 2004.06.18 *
* *
\**************&retval*********************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <fcntl.h>
#include "s3c2440-adc.h"
#ifndef U8
#define U8 unsigned char
#endif
#ifndef U16
#define U16 unsigned short
#endif
#ifndef U32
#define U32 unsigned long
#endif
#define ADC_DEV "/dev/s3c2440_adc"
#define FFT_LEN 16
U16 snsWtAdResult[FFT_LEN];
U16 snsWtValue[FFT_LEN];
U16 snsoilAdResult[FFT_LEN];
U16 snsoilValue[FFT_LEN];
U8 snsAdCnt=0;
U16 snsWt=0;
U16 snsoil=0;
typedef struct
{
U16 index;
U16 mask;
}snsCurveType;
/*
0 0.07 0.14 0.22 0.29 0.34 0.4 0.48 0.58 0.87 1.2 1.91 2.66 2.87 3.09 3.3
0 7 14 21 28 33 39 47 56 84 116 185 258 278 300 320
140 130 120 110 100 95 90 85 80 70 60 50 40 30 20 20
*/
snsCurveType snsWtCurve[]=
{
0, 140,
70, 130,
140, 120,
220, 110,
290, 100,
340, 95,
400, 90,
480, 85,
580, 80,
870, 70,
1200, 60,
1910, 50,
2660, 40,
2870, 30,
3090, 20,
3300, 20,
};
/*
0 0.22 0.44 0.66 0.88 1.1 1.32 1.54 1.76 1.98 2.2 2.42 2.64 2.86 3.08 3.3
0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240
0 0 179 241 338 421 503 600 703 814 938 1034 1034 1034 1034 1034
*/
snsCurveType snsoilCurve[]=
{
0, 0,
220, 0,
440, 179,
660, 241,
880, 338,
1100, 421,
1320, 503,
1540, 600,
1760, 703,
1980, 814,
2200, 938,
2420, 1034,
2640, 1034,
2860, 1034,
3080, 1034,
3300, 1034,
};
static int adc_fd = -1;
/*????????????±??????*/
void init(void)
{
U8 i;
for(i=0;i<FFT_LEN;i++)
snsWtAdResult[i]=0;
for(i=0;i<FFT_LEN;i++)
snsWtValue[i]=0;
for(i=0;i<FFT_LEN;i++)
snsoilValue[i]=0;
}
/*????????????ADC??????é*/
static int init_ADdevice(void)
{
if((adc_fd=open(ADC_DEV, O_RDWR))<0){
printf("Error opening %s adc device\n", ADC_DEV);
return -1;
}
}
/*
????????AD??????ù????
*/
static int GetADresult(int channel)
{
int PRESCALE=0XFF;
int data=ADC_WRITE(channel, PRESCALE);
write(adc_fd, &data, sizeof(data));
read(adc_fd, &data, sizeof(data));
return data;
}
static int stop=0;
static void* comMonitor(void* data)
{
getchar();
stop=1;
return NULL;
}
/*
????????????×é??ù????
????????????
*pdata ????×é????????
*/
U16 snsCalcAverage(U16 *pdata)
{
U32 sum=0;
U8 i=0;
for(i=0;i<FFT_LEN;i++)
sum+=*pdata++;
return (sum/FFT_LEN);
}
/*
????×é????????????????×????????????????á??ú
????????????
*pdata ????×é????????
*/
void snsPushUint(U16 *pdata){
U8 i;
for(i=0; i<(FFT_LEN-1); i++)
{
*pdata = *(pdata + 1);
pdata++;
}
}
/*
????????????×è??????????????÷????????????????????????
????????????
*pCurve ??????????÷??ú????????????
data AD??????ù????????????????????????
*/
U16 snsCalcRestSensor(snsCurveType *pCurve,U16 data)
{
U8 cnt_buf=15;
float buf1;
float buf2;
float buf3;
float buf4;
float anlog_buf;
snsCurveType *p_buf;
U8 i;
U16 index_lo,index_hi,msk_lo,msk_hi;
index_lo = pCurve->index;
index_hi = pCurve->index;
msk_lo = pCurve->mask;
msk_hi = pCurve->mask;
/*????????????????????????????????????????????????????*/
p_buf = pCurve+1;
for(i=0; i<cnt_buf; i++)
{
if((data >= pCurve->index) && (data <= p_buf->index))
{
index_lo = pCurve->index;
index_hi = p_buf->index;
msk_lo = pCurve->mask;
msk_hi = p_buf->mask;
break;
}
else
{
pCurve++;
p_buf = pCurve + 1;
}
}
if(msk_lo <= msk_hi)
{
buf1 = (float)index_hi * msk_lo;
buf2 = (float)(msk_hi - msk_lo)*data;
buf3 = (float)msk_hi * index_lo;
buf4 = (buf1 + buf2 - buf3);
return (U16)(buf4/(index_hi - index_lo));
}
else
{
buf1 = (float)index_hi * msk_lo;
buf2 = (float)msk_hi * index_lo;
buf3 = (float)(msk_lo - msk_hi)*data;
buf4 = (buf1 - buf2 - buf3);
return (U16)(buf4/((index_hi - index_lo)));
}
}
/*
????????·????????ú????????
*/
void snsProcessWt(void)
{
U16 average;
average=snsCalcAverage(snsWtAdResult);//????????????????????AD??????ù????
snsPushUint(snsWtValue);//????×????????????????????????ù??????????ú
snsWtValue[FFT_LEN-1] = average;//????×????????????ù????????????????????×é????
average = snsCalcAverage(snsWtValue);//??????????ù??????????ù??????????????????????????¨
snsWt = snsCalcRestSensor(snsWtCurve,average);//????????????????????
}
/*
????????·????????ú????????
*/
void snsProcessoil(void)
{
U16 average;
average=snsCalcAverage(snsoilAdResult);
snsPushUint(snsoilValue);
snsoilValue[FFT_LEN-1] = average;
average = snsCalcAverage(snsoilValue);
snsoil = snsCalcRestSensor(snsoilCurve,average);
}
/*
????????
*/
void snsDelay(U16 x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<x;j++);
}
/*
????????AD??????ù??????????????????????à??????????????????????????????????°????????
*/
void snsGetAdValue(void)
{
int i;
float d;
while(stop==0){
if(snsAdCnt<FFT_LEN){
//????????AD??????ù???? 哈哈哈
snsWtAdResult[snsAdCnt]=(U32)GetADresult(0)*3300/1024;
snsoilAdResult[snsAdCnt]=(U32)GetADresult(1)*3300/1024;
snsDelay(10);
snsAdCnt++;
}
else{
snsProcessWt();
snsProcessoil();
printf("\r");
printf(" %3d %3d \t",snsWt,snsoil);
snsAdCnt = 0;
}
}
}
int main(void)
{
pthread_t th_com;
void * retval;
init();
//set s3c2440 AD register and start AD
if(init_ADdevice()<0)
return -1;
/* Create the threads */
pthread_create(&th_com, NULL, comMonitor, 0);
printf("\nPress Enter key exit!\n");
snsGetAdValue();
/* Wait until producer and consumer finish. */
pthread_join(th_com, &retval);
printf("\n");
return 0;
}