📄 max127.c
字号:
#include "intrins.h"
#include "At89x51.h"
/////////////////////////////////////
//采用P1口作控制
//11.0592的晶体
////////////////////////////////////
//I2C总线的数据和时钟
#define SCL P1_7
#define SDA P1_6
//////////////////////////////////////
//7219的各控制线
#define CLK7219 P2_7
#define DIN7219 P2_6
#define CS7219 P2_5
///////////////////////////////////////
max7219(unsigned int dat)
{
char i;
CS7219=0;
_nop_();
for(i=0;i<16;i++)
{
DIN7219=(dat>>(15-i))&0x1;
CLK7219=1;
_nop_();
CLK7219=0;
};
CS7219=1;
}
/////////////////////////////////////////////////////////
///Max127 12位AD操作程序
/////////////////////////////////////////////////////////
void I2cWriteByte(unsigned char wdata)
{
char i;
for(i=0;i<8;i++)
{
SDA=((wdata>>(7-i))&0x1);
SCL=1;
_nop_();
SCL=0;
}
}
unsigned char I2cReadByte(void)
{
unsigned char result,i;
bit getdata;
result=0;
for(i=0;i<8;i++)
{
SDA=1;
SCL=1;
getdata=SDA;
SCL=0;
result|=getdata;
if(i!=7)
result<<=1;
}
return result;
}
//////////////////////////////////////////////
//address: 0 1 0 1 A2 A1 A0
///ctr : Start SEL2 SEL1 SEL0 RING BIP PD1 PD0
//fail return 0xffff
unsigned int max127(unsigned char address,unsigned char ctr)
{
bit ack;
unsigned int result;
unsigned char hbyte,lbyte;
address<<=1;
//////////////////////////////////////////////
//开始转换
SDA=0; //启动
_nop_();
SCL=0;
I2cWriteByte(address&0xfe); //发送地址写
SDA=1;
SCL=1;
ack=SDA;
SCL=0;
if(ack) //应答slave to master
{
SDA=0;
SCL=1;
SDA=1;
result=0xffff;
return result;
}
I2cWriteByte(ctr); //发送控制字
SDA=1;
SCL=1;
ack=SDA;
SCL=0;
if(ack) //应答slave to master
{
SDA=0;
SCL=1;
SDA=1;
result=0xffff;
return result;
}
SDA=0;
SCL=1;
SDA=1; //stop
_nop_();
_nop_();
////////////////////////////////////////////////////
///开始读数据
SDA=0; //启动
_nop_();
SCL=0;
I2cWriteByte(address|0x1); //发送地址读
SDA=1;
SCL=1;
_nop_();
SCL=0;
if(ack) //应答slave to master
{
SDA=0;
SCL=1;
SDA=1;
result=0xffff;
return result;
}
hbyte=I2cReadByte();
SDA=0; //应答mater to slave
SCL=1;
_nop_();
SCL=0;
lbyte=I2cReadByte();
SDA=1; ////应答master to slave
SCL=1;
_nop_();
SCL=0;
SDA=0;
SCL=1;
SDA=1; //stop
/////////////////////////////////////
//处理数据
result=0;
result|=hbyte;
result<<=8;
result|=lbyte;
result>>=4;
return result;
}
main()
{
int i;
unsigned char j,k;
unsigned int addata,disp;
SCL=1;
SDA=1;
CLK7219=0;
for(i=0;i<31000;i++);
///////////////////////////////////////////////////////
//7219初始化
max7219(0x0C01); //shutdown register format
max7219(0x09FF); //decode mode code B 7-0
max7219(0x0A0F); //intensity register format
max7219(0x0B07); //sacan limit register format
max7219(0x0F00); //display test register format
////////////////////////////////////////////////////////
for(k=0;k<8;k++)
{
disp=k+1;
disp<<=8;
max7219(disp);
}
/////////////////////////////////////////////////
while(1)
{
for(j=0;j<8;j++)
{
disp=0x0800|(j&0xf);
max7219(disp);
//address: 0 1 0 1 A2 A1 A0
///ctr : Start SEL2 SEL1 SEL0 RING BIP PD1 PD0
addata=max127(0x28,0x80|((j&0x7)<<4));
for(k=0;k<4;k++)
{
disp=k+1;
disp<<=8;
disp|=addata%10;
addata/=10;
max7219(disp); //转换成十进制显示
}
P1_0=~P1_0;
for(k=0;k<4;k++)
{
for(i=0;i<31000;i++);
for(i=0;i<31000;i++);
for(i=0;i<31000;i++);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -