ad.c

来自「ATmega128和ads7864实现ATP闭环控制」· C语言 代码 · 共 361 行

C
361 行
字号
#include "public.h"

void AD_Init(void)
{
	SETBIT(AD_RST_OUT, AD_RST);
	CLEARBIT(AD_RST_OUT, AD_RST);
	NOP();
	NOP();
	SETBIT(AD_RST_OUT, AD_RST);
	NOP();
	NOP();
}

void AD_GetData(u16* pusOutBuff)
{
	u8 uctemp[2];
	//u16 usADData[4]={0,0,0,0};
	pusOutBuff[0]=0;
	pusOutBuff[1]=0;
	pusOutBuff[2]=0;
	pusOutBuff[3]=0;

	
	//hold channel A and channel B
	PORTD=PORTD&0x9F;	//bring HoldA,HoldB low
	PORTD=PORTD|0xE0;	//bring HoldA,HoldB high

	//PORTD=PORTD&0xDF;	//bring HoldA,HoldB low
	//PORTD=PORTD|0xE0;	//bring HoldA,HoldB high
	
	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is finished
	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is finished
	CLEARBIT(AD_CS_OUT, AD_CS);//select the ads7864

	SETBIT(AD_RD_OUT, AD_RD);
	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE3;	//A2A1A0:000	channel A0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	pusOutBuff[0]=(uctemp[1]&0x0F)<<8;
	pusOutBuff[0]=pusOutBuff[0]|uctemp[0];

	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE7;	//A2A1A0:001	channel A1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	pusOutBuff[1]=(uctemp[1]&0x0F)<<8;
	pusOutBuff[1]=pusOutBuff[1]|uctemp[0];


	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEB;	//A2A1A0:010	channel B0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	pusOutBuff[2]=(uctemp[1]&0x0F)<<8;
	pusOutBuff[2]=pusOutBuff[2]|uctemp[0];

	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEF;	//A2A1A0:011	channel B1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	pusOutBuff[3]=(uctemp[1]&0x0F)<<8;
	pusOutBuff[3]=pusOutBuff[3]|uctemp[0];

	SETBIT(AD_CS_OUT, AD_CS);//unselect the ads7864

}


void ulAD_GetData(u32* pulOutBuff)
{
	u8 uctemp[2];
	s16 sstemp[4];
	
	//sstemp[0]=0;
	//sstemp[1]=0;
	//sstemp[2]=0;
	//sstemp[3]=0;

	
	//hold channel A and channel B
	PORTD=PORTD&0x9F;	//bring HoldA,HoldB low
	PORTD=PORTD|0xE0;	//bring HoldA,HoldB high

	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is finished
	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is finished
	CLEARBIT(AD_CS_OUT, AD_CS);//select the ads7864

	SETBIT(AD_RD_OUT, AD_RD);
	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE3;	//A2A1A0:000	channel A0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[0]=(uctemp[1]&0x0F)<<8;
	sstemp[0]=sstemp[0]|uctemp[0];
	sstemp[0]=sstemp[0]<<4;

	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE7;	//A2A1A0:001	channel A1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[1]=(uctemp[1]&0x0F)<<8;
	sstemp[1]=sstemp[1]|uctemp[0];
	sstemp[1]=sstemp[1]<<4;

	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEB;	//A2A1A0:010	channel B0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[2]=(uctemp[1]&0x0F)<<8;
	sstemp[2]=sstemp[2]|uctemp[0];
	sstemp[2]=sstemp[2]<<4;
	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEF;	//A2A1A0:011	channel B1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[3]=(uctemp[1]&0x0F)<<8;
	sstemp[3]=sstemp[3]|uctemp[0];
	sstemp[3]=sstemp[3]<<4;

	SETBIT(AD_CS_OUT, AD_CS);//unselect the ads7864

	pulOutBuff[0]=sstemp[0]+32768;
	pulOutBuff[1]=sstemp[1]+32768;
	pulOutBuff[2]=sstemp[2]+32768;
	pulOutBuff[3]=sstemp[3]+32768;

}


/*
void usAD_GetData(u16* pusOutBuff)
{
	u8 uctemp[2];
	s16 sstemp[4];
	
	//sstemp[0]=0;
	//sstemp[1]=0;
	//sstemp[2]=0;
	//sstemp[3]=0;

	
	//hold channel A and channel B
	PORTD=PORTD&0x9F;	//bring HoldA,HoldB low
	PORTD=PORTD|0xE0;	//bring HoldA,HoldB high

	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is finished
	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is finished
	CLEARBIT(AD_CS_OUT, AD_CS);//select the ads7864

	SETBIT(AD_RD_OUT, AD_RD);
	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE3;	//A2A1A0:000	channel A0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[0]=(uctemp[1]&0x0F)<<8;
	sstemp[0]=sstemp[0]|uctemp[0];
	sstemp[0]=sstemp[0]<<4;

	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE7;	//A2A1A0:001	channel A1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[1]=(uctemp[1]&0x0F)<<8;
	sstemp[1]=sstemp[1]|uctemp[0];
	sstemp[1]=sstemp[1]<<4;

	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEB;	//A2A1A0:010	channel B0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[2]=(uctemp[1]&0x0F)<<8;
	sstemp[2]=sstemp[2]|uctemp[0];
	sstemp[2]=sstemp[2]<<4;
	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEF;	//A2A1A0:011	channel B1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[3]=(uctemp[1]&0x0F)<<8;
	sstemp[3]=sstemp[3]|uctemp[0];
	sstemp[3]=sstemp[3]<<4;

	SETBIT(AD_CS_OUT, AD_CS);//unselect the ads7864

	pusOutBuff[0]=sstemp[0]+32768;
	pusOutBuff[1]=sstemp[1]+32768;
	pusOutBuff[2]=sstemp[2]+32768;
	pusOutBuff[3]=sstemp[3]+32768;

}
*/

void usAD_GetData(u16* pusOutBuff)
{
	u8 uctemp[2];
	s16 sstemp[4];
	
	//hold channel A and channel B
	PORTD=PORTD&0x9F;	//bring HoldA,HoldB low
	PORTD=PORTD|0xE0;	//bring HoldA,HoldB high

	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until first convertion is finished
	while(CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is started
	while(0x00==CHECKBIT(AD_BUSY_IN, AD_BUSY));//wait until second convertion is finished
	CLEARBIT(AD_CS_OUT, AD_CS);//select the ads7864

	SETBIT(AD_RD_OUT, AD_RD);
	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE3;	//A2A1A0:000	channel A0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[2]=(uctemp[1]&0x0F)<<8;
	sstemp[2]=sstemp[2]|uctemp[0];
	sstemp[2]=sstemp[2]<<4;

	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xE7;	//A2A1A0:001	channel A1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[3]=(uctemp[1]&0x0F)<<8;
	sstemp[3]=sstemp[3]|uctemp[0];
	sstemp[3]=sstemp[3]<<4;

	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEB;	//A2A1A0:010	channel B0
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[0]=(uctemp[1]&0x0F)<<8;
	sstemp[0]=sstemp[0]|uctemp[0];
	sstemp[0]=sstemp[0]<<4;
	
	PORTD=PORTD|0x0C;
	PORTD=PORTD&0xEF;	//A2A1A0:011	channel B1
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[0]=PINC;		//first byte
	SETBIT(AD_RD_OUT, AD_RD);
	CLEARBIT(AD_RD_OUT, AD_RD);
	NOP();
	uctemp[1]=PINC;		//second byte
	SETBIT(AD_RD_OUT, AD_RD);
	sstemp[1]=(uctemp[1]&0x0F)<<8;
	sstemp[1]=sstemp[1]|uctemp[0];
	sstemp[1]=sstemp[1]<<4;

	SETBIT(AD_CS_OUT, AD_CS);//unselect the ads7864

	pusOutBuff[0]=sstemp[0]+32768;
	pusOutBuff[1]=sstemp[1]+32768;
	pusOutBuff[2]=sstemp[2]+32768;
	pusOutBuff[3]=sstemp[3]+32768;

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?