⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ads1110.txt

📁 AVR基础试验程序
💻 TXT
字号:
//这些函数用于操作ADS1110。程序工作原理与spi是一样的。与spi不同的是启动i2c
//开始读取采集到的数据是由定时器中断完成的。a/d芯片完成一次采集大约需要60多ms
//因此定时器中断必须大于这个值。
//***************FileName:Compare.C***************//
//***************ICCAVR V6.30编译*****************//

#include <iom16v.h> 
#include <macros.h>
#define YES    0
#define SLA_W   0x9A
#define SLA_R   0x9B
#define I2cIdle_yes  Flag&=~0x02 
#define I2cIdle_no   Flag|=0x02
#define ADInputNew_yes  Flag&=~0x08 
#define ADInputNew_no    Flag|=0x08
#define ADInputNew     ( Flag&0x08) 
#define I2CCONFIG     0x0c       //连续转换,15sps
#define uchar unsigned char
#define uint unsigned int
uint k;
//数码管字型表,对应0,1,2,3,4,5,6,7,8,9//
uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Data[4]={0,0,0,0};       //显示初始值:0 0 0 0
void DelayMs(uint i )           //Ms级延时,参数i为延时时间
{uint j;
 for(;i!=0;i--)
  {for(j=1142;j!=0;j--) {;}}     //原来8000,改为1142//
}

void Display(uchar *p)         //动态显示函数,参数p为待显示的数组名
{uint i,sel=0xff7f;           
 for(i=0;i<4;i++)
  {PORTC=sel;                  //选通最右边的数码管
   PORTA=Table[p[i]];          //送字型码
   DelayMs(2);                 //显示延时    
   sel=sel>>1;                 //移位以显示前一位
  }
}
unsigned char Flag,Key;
struct i2cdata
{
	unsigned char s;
 	int ReceiveData;
}I2cData;
void StartI2c(void)
{
	TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN)|(1<<TWIE);
 	I2cData.s=1;
 	I2cIdle_no;
}

void error(unsigned char err)
{//unsigned char er[]={"错误:"};
 
 //LcdLineDis(0x91,"0错误:");
}

//这个中断用于开始一次a/d数据读取
//#pragma interrupt_handler timer1_compa_isr:7
//void timer1_compa_isr(void)
//{
	//StartI2c();
//}
void  port_init()
{PORTA=0Xff;    //all out
 DDRA=0Xff; 
 PORTC=0Xff;
 DDRC=0Xff; 
}

#pragma interrupt_handler twi_isr:18
void twi_isr(void)
{
	switch(I2cData.s)
  {
   	case 0:
   		StartI2c();           
		  break;
    case 1:
    	if((TWSR&0xf8)!=0x08)error(0x08);	       
	      TWDR=SLA_R;
		  TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWIE);
		  I2cData.s++;
		  break;
    case 2:
    	if((TWSR&0xf8)!=0x40)error(0x40);	              
		  TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWIE)|(1<<TWEA);
		  I2cData.s++;
		  break;
		case 3:
			if((TWSR&0xf8)!=0x50)error(0x50);
	      I2cData.ReceiveData=TWDR;
		  I2cData.ReceiveData=I2cData.ReceiveData*256;	       
		  TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWIE);
		  I2cData.s++;
		  break;
    case 4:
    	if((TWSR&0xf8)!=0x58)error(0x58);
	      I2cData.ReceiveData=TWDR+I2cData.ReceiveData;
		  k=I2cData.ReceiveData;
		  TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
		  I2cData.s=0;
		  ADInputNew_yes;											//标志已经采集到了一个新数据
		  I2cIdle_yes;		   
		  break;
    default :
    	I2cData.s=0;	       
	    break;
   }
}

void i2c_init(void)
{
	TWCR= 0X00; //disable twi
 	TWBR= 0x0B; //set bit rate
 	TWSR= 0x00; //set prescale 
}

//ADS1110芯片初始化。对这个芯片工作方式的一些配置。配置为单次转化模式,是必须的。
void ADS1110_init(void)
{
	TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
 	while(!(TWCR&(1<<TWINT)));
 	if((TWSR&0XF8)!=0X08)error(0X08);
 	TWDR=SLA_W;
 	TWCR=(1<<TWINT)|(1<<TWEN);	
 	while(!(TWCR&(1<<TWINT)));
 	if((TWSR&0XF8)!=0X18)error(0X18);	
 	TWDR=I2CCONFIG;
 	TWCR=(1<<TWINT)|(1<<TWEN);	
 	while(!(TWCR&(1<<TWINT)));
 	if((TWSR&0XF8)!=0X28)error(0X28);	
 	TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);	
}
void Process(uint i,uchar *p)
{p[0]=i/1000;
 i=i%1000;
 p[1]=i/100;
 i=i%100;
 p[2]=i/10;
 i=i%10;
 p[3]=i;
}
main(void)
{
	port_init();
	i2c_init();
	ADS1110_init();
	while(1)
	{
	
	StartI2c();
	
	
	SEI();
	
	//if(ADInputNew == YES)
	//{
		//ADInputNew_no;    
		//data = I2cData.ReceiveData;	//读取接收到的数据
	//}	
	k=k/16;
	Process(k,Data);
   Display(Data);
  
}
}

⌨️ 快捷键说明

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