📄 51机ad574.c
字号:
#include<reg51.h>
unsigned char xdata start_ad574 _at_ 0x6000;
unsigned char xdata high8 _at_ 0x6002;
unsigned char xdata low4 _at_ 0x6003;
//*** 函数定义 ***
void delay(unsigned char); //短暂延时
void write728x(unsigned char, unsigned char); // 写入到BC728x
unsigned char read728x(unsigned char); // 从BC728x读出
void send_byte(unsigned char); //发送一个字节
unsigned char receive_byte(void); //接收一个字节
void chushihua_BC7281();
void bit_display(unsigned char,unsigned char);
//*** 变量及I/O定义 ***
unsigned char key_number;
unsigned int tmr;
sbit dat=P1^0; // dat 连接于 P1.0
sbit key=P1^1; // key 连接于 P1.1
sbit clk=P1^2; // clk 连接于 P1.2
sbit P1_2=P1^3;
//*** 主程序 ***
main()
{
unsigned int i;
unsigned char a;
unsigned char b;
unsigned int c;
unsigned char d;
unsigned char e;
float f=2.048;
chushihua_BC7281();
for(;;)
{
P1_2=1; // STS状态转换引脚
start_ad574=0;
while(P1_2);
a=high8;
// b=low4;
b=(low4&0xf0)/16;
c=a*16+b;
if(c>2047)
{
c=(c&0x07ff)/f;
d=(c/1000)*16+(c%1000)/100;
e=(((c%1000)%100)/10)*16+(((c%1000)%100)%10);
write728x(0x14,0x4f);
write728x(0x18,0x2f);
bit_display(6,d);
bit_display(7,e);
bit_display(8,e);
}
else
{
c=(c^0x07ff)/f;
d=(c/1000)*16+(c%1000)/100;
e=(((c%1000)%100)/10)*16+(((c%1000)%100)%10);
write728x(0x14,0x4f);
write728x(0x18,0x2f);
bit_display(6,d);
bit_display(7,e);
bit_display(8,e);
}
for(i=0;i<30000;i++)
{
}
}
}
//******************************************************************
//* 初始化 BC7281 *
//******************************************************************
void chushihua_BC7281()
{
for (tmr=0;tmr<0xffff;tmr++); // 等待BC728x完成复位
write728x(0x12,0x80); // 初始化BC728x 为164模式,不反相
}
//******************************************************************
//* 按位显示 By BC7281 *
//******************************************************************
void bit_display(unsigned char bt,unsigned char date)
{
switch(bt)
{
case 1 :write728x(0x15,0x00+(date&0xf0)/16);break;
case 2 :write728x(0x15,0x10+(date&0x0f));break;
case 3 :write728x(0x15,0x20+(date&0xf0)/16);break;
case 4 :write728x(0x15,0x30+(date&0x0f));break;
case 5 :write728x(0x15,0x40+(date&0xf0)/16);break;
case 6 :write728x(0x15,0x50+(date&0x0f));break;
case 7 :write728x(0x15,0x60+(date&0xf0)/16);break;
case 8 :write728x(0x15,0x70+(date&0x0f));break;
case 9 :write728x(0x15,0x80+(date&0xf0)/16);break;
case 10 :write728x(0x15,0x90+(date&0x0f));break;
case 11 :write728x(0x15,0xa0+(date&0xf0)/16);break;
case 12 :write728x(0x15,0xb0+(date&0x0f));break;
case 13 :write728x(0x15,0xc0+(date&0xf0)/16);break;
case 14 :write728x(0x15,0xd0+(date&0x0f));break;
case 15 :write728x(0x15,0xe0+(date&0xf0)/16);break;
case 16 :write728x(0x15,0xf0+(date&0x0f));break;
}
}
// *****************************************************************
// *写入BC728X,第一个参数为目标寄存器地址,第二个参数为要写入的数据 *
// *****************************************************************
void write728x(unsigned char reg_add, unsigned char write_data)
{
send_byte(reg_add); //发送寄存器地址
send_byte(write_data); //发送数据字节
}
// **********************************************************
// * 读出BC728X内部寄存器的值,调用参数为寄存器地址 *
// **********************************************************
unsigned char read728x(unsigned char reg_add)
{
send_byte(reg_add|0x80); // 发送读指令(bit7=1)
return(receive_byte()); //接收数据并返回
}
// ***********************************************************
// * 向BC728X发送一个字节 *
// ***********************************************************
void send_byte(unsigned char send_byte)
{
unsigned char bit_counter;
clk=0; // 产生一个clk脉冲
clk=1;
do
{
clk=0; // 发送clk脉冲,直至dat为低电平
clk=1;
} while (dat);
clk=0; // 15us之内再输出一个clk脉冲
clk=1;
while (!dat); // 等待BC728X进入接收状态
for (bit_counter=0;bit_counter<8;bit_counter++)
{ // 发送8个比特
if ((send_byte&0x80)==0)
{
dat=0; // 如果待发bit为0, 置dat为0
}
else
{
dat=1; // 反之为1
}
send_byte=send_byte*2; // send_byte左移一位
clk=0; // 输出一个clk脉冲
clk=1;
delay(1); //短暂延时
}
dat=1; // 恢复dat为高电平
delay(2); //指令间设置一个微小延时
}
// ************************************************************
// * 从BC728X接受一个字节 *
// ************************************************************
unsigned char receive_byte(void)
{
unsigned char bit_counter, in_byte;
clk=0; // 只发送单一的clk脉冲
clk=1;
while (dat); // 等待BC728X响应dat低电平
clk=0; // 收到响应,在发一个脉冲等待接收数据
clk=1;
for (bit_counter=0;bit_counter<8;bit_counter++)
{ // 接收8个bit
delay(1); // 短暂延时
in_byte=in_byte*2; // in_byte左移一位
if (dat) // 如果dat为'1'
{
in_byte=in_byte|0x01; // bit0=1
}
clk=0; // 输出一个clk脉冲
clk=1;
}
delay(2); //指令间设置一个微小延时
return(in_byte);
}
// ***********************************************************
// *短暂延时程序,延时时间与time成正比,范围是几个uS到几百个uS *
// ***********************************************************
void delay(unsigned char time)
{
while (time!=0)
{
time--;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -