我用STC89C52来驱动ADS1256时,发现需要用到SPI协议,单片机需要用IO口模拟SPI总线与ADS1256通信,但是我不知道如何通过总线来初始化ADS1256的寄存器,希望大家能给我提供一些代码支持,C51为内核的
Zehua Xu:
我写了一个代码,还是不行,求各位精通这方面的工程师指教一下,感激不尽
#include<reg52.h>#include<stdio.h>#include<intrins.h>
#define uchar unsigned char#define ulong unsigned long#define uint unsigned int
sbit ***=P1^0;
sbit mosi=P1^1;
sbit miso=P1^2;
sbit ss=P1^3;
sbit dula=P2^6;
sbit wela=P2^7;
sbit rst=P1^4;
uchar code table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};
/////////////////////////////////////////void nop(void){ ;}/////////////////////////////////////////void delayms(uint xms){ uint i,j; for(i=xms;i>0;i–) for(j=110;j>0;j–);}
void delayus(uchar a) //ͦʱ1uS{ uint i=0,j=0; for(i=0;i<a;i++) for(j=0;j<50;j++);}///////////////////////////////////////////////////uchar data_receive(void) //ÊÕ×Ö½ÚÒ»Êý¾Ý{ uchar i,dat=0,temp; bit bt; ***=1; delayms(10); miso=1; ss=0; nop(); nop(); for(i=0;i<8;i++) { ***=1; nop(); nop(); ***=0; delayms(10); nop(); nop(); bt=miso; if(bt) temp=0x01; else temp=0x00; dat=(dat<<1); dat=(dat|temp); } ss=1; ***=1; delayms(10); return dat;}////////////////////////////////////////////////////void dat_transmit(uchar dat) //·¢Ò»×Ö½ÚÊý¾Ý{ uchar i,datbuf; datbuf=dat; ss=0; //while(ss!=0); for(i=0;i<8;i++) { ***=0; delayms(10); if(datbuf&0x80) miso=1; else miso=0; datbuf=(datbuf<<1); ***=1; delayms(10); } }
////////////////////////////////////////////void display (uchar qianwang,uchar baiwang,uchar shiwang,uchar wang,uchar qian,uchar bai,uchar shi,uchar ge){ dula=1; P0=table[qianwang]; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; P0=0xff; delayms(5);
dula=1; P0=table[baiwang]; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; P0=0xff; delayms(5);
dula=1; P0=table[shiwang]; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; P0=0xff; delayms(5);
dula=1; P0=table[wang]; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; P0=0xff; delayms(5);
dula=1; P0=table[qian]; dula=0; P0=0xff; wela=1; P0=0xef; wela=0; P0=0xff; delayms(5);
dula=1; P0=table[bai]; dula=0; P0=0xff; wela=1; P0=0xdf; wela=0; P0=0xff; delayms(5);
dula=1; P0=table[shi]; dula=0; P0=0xff; wela=1; P0=0xbf; wela=0; P0=0xff; delayms(5);
dula=1; P0=table[ge]; dula=0; P0=0xff; wela=1; P0=0x7f; wela=0; P0=0xff; delayms(5);}
///////////////////////////////////////////////////void ads_reg(uchar reg,uchar dat1) //д¼Ä´æÆ÷ºÍµØÖ·Êý¾Ý{ ss=0; dat_transmit(0x50+reg); dat_transmit(0x00); dat_transmit(dat1); ss=1; delayus(1); }///////////////////////////////////////void ads1256_into(void){ rst=0;
nop();
rst=1;
ads_reg(00,0xf0);//STATUS
ads_reg(01,0×10);//MUX
ads_reg(02,00);//ADCOM
ads_reg(03,0xf0);//DTATE
ads_reg(04,0x0f);//IO
}
/////////////////////////void main(){ uchar a,A1=0,A2=0,A3=0,A4=0,A5=0,A6=0,A7=0,A8=0,adval=0; ads1256_into(); while(1) { adval=data_receive(); P0=0xff;
for(a=10;a>0;a–) { display(A1,A2,A3,A4,A5,A6,A7,A8); }
A1=adval/10000000; A2=adval%10000000/1000000; A3=adval%1000000/100000; A4=adval%100000/10000; A5=adval%10000/1000; A6=adval%1000/100; A7=adval%100/10; A8=adval%10; } }
Mickey Zhang:
回复 Zehua Xu:
1. 建议您通过示波器观察SPI时序,是否如datasheet Figure 1所示。
2. 您可以下载如下链接下的ADS1255-7 Design Calculator,来帮助您寄存器配置:
www.ti.com/…/toolssoftware