📄 tlc2543.c
字号:
/*-------ADCtlc2543 to ARM Gport-----*/
#ifndef CURRENT
#define CURRENT 50
#define VOLT 350
#endif
//GPF5 4 7 8
// DIN CS CLK DOUT
// 59 30 57 56
//EXTINT2 EOC
// 36
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#define AD_SIZE 12
#ifndef CHN0
#define CHN0 0x00//ADV
#define CHN1 0x10//ADI
#endif
extern float f[8];
float Filter(int a[16]);
void Ad_Init(void);
float Read_Ad(unsigned char chn)
{
int i,k;
int temp,save_chn;
//int temp_sum[16];
save_chn=chn;
rPDATF &=0x17f;//CLK=L;
rPDATF &=0x17f;//CLK=L;
rPDATF |=0x40;//CS=H;
rPDATF |=0x40;//CS=H;
rPDATF &=~0x10;//CS=L;
Delay(30);
for(i=0;i<AD_SIZE;i++) //输入本次通道,去掉上次结果
{
//rPDATF =((chn&0x80)>>2)|0x1cf;//0x1cf; //here din=0x00 for test;
if((chn&0x80)!=0) {rPDATF |= 0x020;}
else {rPDATF &= 0x1cf;}
rPDATF |=0x80; //CLK=H;
rPDATF |=0x80; //CLK=H;
Delay(10);
rPDATF &=0x17f; //CLK=L;
rPDATF &=0x17f; //CLK=L;
Delay(10);
chn = chn<<1;
}
rPDATF |= 0x10; //CS=H
/////sample 16 times, by average filter
Delay(30);
//for(k=0;k<32;k++)
{
temp=0;
//Delay(1000);
rPDATF &=0x17f; //CLK=L;
//rPDATF &=0x17f; //CLK=L;
rPDATF |=0x40; //CS=H;
//rPDATF |=0x40; //CS=H;
rPDATF &=~0x10; //CS=L;
Delay(30);
chn = save_chn;
for(i=0;i<AD_SIZE;i++)
{
//rPDATF =((chn&0x80)>>2)|0x1cf;//0x1cf=0;
if((chn&0x80)!=0) {rPDATF |= 0x020;}
else {rPDATF &= 0x1cf;}
temp <<=1;
if((rPDATF&0x100)==0x100){
temp +=1;
//Uart_Printf("%d-",i);
}
//Delay(5);
rPDATF|=0x80; //CLK=H;
//rPDATF|=0x80; //CLK=H;
Delay(50);
rPDATF&=0x17f; //CLK=L;
//rPDATF&=0x17f; //CLK=L;
//Delay(10);
chn=chn<<1;
}
}
rPDATF |=0x10;//CS=H;
/*if(save_chn==CHN0)
{
//Uart_Printf("\n chn0=VOLT*Filter(temp_sum)/4096= %f \n",VOLT*Filter(temp_sum)/4096);
// *(f+4) = Filter(temp_sum);
Uart_Printf(" CHN0===%d- ",temp);
//return VOLT*Filter(temp_sum)/4096;
}
else if(save_chn==CHN1)
{
//Uart_Printf("\n chn1= %f \n",Filter(temp_sum));
// *(f+3) = CURRENT*Filter(temp_sum)/4096;
//Uart_Printf("---------------%f----------\n",*(f+3));
Uart_Printf(" CHN1===%d- ",temp);
//return CURRENT*Filter(temp_sum)/4096;
}*/
return temp;
}
void Ad_Init(void)
{
rPCONF&=0x0000ff; //GPF5,4,7 OUT; F8 IN
rPCONF|=0x012500;
//rPCONG|=0x30; //int_init(); PG2=extint2
//rPCONG&=~(0x30); //G2 input
rPUPF&=0x0ff; //GPF8 PULL UP
//rPUPF =0xff;
//rPUPG&=0xfc; //GPG2 UP
//Uart_Printf("[External Interrupt3 Init(enable) & Test Started!!]\n");
}
/*
float Filter(int a[16])
{
int i,j,k,t;
float temp_sum=0;
i=j=k=t=0;
for(i=0;i<16-1;i++)
{
k=i;
for(j=i+1;j<16;j++)
if(a[j]>a[k]) k=j;
if(k!=i)
{
t=a[i];a[i]=a[k];a[k]=t;
}
}
for(i=2;i<14;i++)
temp_sum += (float)a[i];
return temp_sum/12;
}
float Filter(int a[16])
{
int i,k;
float temp_sum=0;
i=k=0;
for(i=0;i<16;i++)
temp_sum += a[i];
//temp_sum /=16;
for(i=0;i<16;i++)
if(a[i]<temp_sum*0.707||a[i]>temp_sum*1.293)
a[i] = 0;
for(i=0;i<16;i++)
{
temp_sum += a[i];
if(a[i]==0) k++;
}
return temp_sum/k;
} */
/*
void ADC_EINT2(void)
{
unsigned char chn;
//Uart_Printf(" ad begin\n");
Read_Ad(chn);
rI_ISPC=BIT_EINT2; //clear EXTINT2 pending_bit
//Uart_Printf("ad contverted\n");
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -