📄 bc7281.c
字号:
#include "def.h"
void write728x(unsigned char reg_add, unsigned char write_data);
void showscreen(void);
void readBC7281(void);
const unsigned char zf[8]={0xce,0x8c,0x92,0xc6,0x91,0x80,0xf1,0x90};
// ***********************************************************
// * 向 BC728X 发送一个字节 *
// ***********************************************************
void send_byte(unsigned char send_byte)
{
unsigned char bit_counter;
unsigned char time,temp;
clrClkBC;//clk=0; // 产生一 clk 脉冲
time=10;
while(time>0)time--;
setClkBC;//clk=1;
time=10;
while(time>0)time--;
time=200;
temp=P1IN&0x20;
while ((temp==0x20)&&(time>0)){time--;temp=P1IN&0x20;}// 等待 BC728X 输出 dat 低电平响应
time=4;
while(time>0)time--;
clrClkBC;//clk=0; // 再次输出一 clk 脉冲
time=10;
while(time>0)time--;
setClkBC;//clk=1;
time=10;
while(time>0)time--;
time=150;
while((time>0)&&((~P1IN)&0x20==0x20))time--;//while (!(~P1IN&0x20)); // 等待 BC728X 进入接收状态
P1DIR|=0x20;
time=10;
while(time>0)time--;
for (bit_counter=0;bit_counter<8;bit_counter++)
{ // 发送 8 个比特
if ((send_byte&0x80)==0)
{
clrDatBC;//dat=0;// 如果待发bit为0, 置 dat 为 0
}
else
{
setDatBC;//dat=1; // 反之置为 1
}
send_byte=send_byte*2; // send_byte 左移一位
time=10;
while(time>0)time--;
clrClkBC;//clk=0; // 输出一 clk 脉冲
time=10;
while(time>0)time--;
setClkBC;//clk=1;
time=10;
while(time>0)time--; // 短暂延时
}
setDatBC;//dat=1;
P1DIR&=~0x20;
time=10;
while(time>0)time--;
// 恢复 dat 为高电平
}
// ************************************************************
// * 从 BC728X 接收一个字节 *
// ************************************************************
unsigned char receive_byte(void)
{
unsigned char bit_counter, in_byte;
unsigned char time;
clrClkBC;//clk=0; // 发出一 clk 脉冲
time=20;
while(time>0)time--;
setClkBC;//clk=1;
time=70;
while((P1IN&0x20)&&(time>0))time--; // 等待 BC728X 响应
time=20;
while(time>0)time--;
clrClkBC;//clk=0; // 收到响应, 再发一脉冲等待接收数据
time=20;
while(time>0)time--;
setClkBC;//clk=1;
for (bit_counter=0;bit_counter<8;bit_counter++)
{ // 接收8个bit
time=20;
while (time>0)time--; // 短暂延时
in_byte=in_byte*2; // in_byte 左移一位
if (P1IN&0x20) // 如果 dat 为'1'
{
in_byte=in_byte|0x01; // bit0=1
}
clrClkBC;//clk=0; // 输出一 clk 脉冲
time=20;
while(time>0)time--;
setClkBC;//clk=1;
time=20;
while(time>0)time--;
}
time=20;
while(time>0)time--;
return(in_byte);
}
// ***********************************************************
// * 写入BC728X, 第一个参数为目标寄存器地址, 第二个参数为要写入的数据 *
// ***********************************************************
void write728x(unsigned char reg_add, unsigned char write_data)
{
send_byte(reg_add); // 发送寄存器地址
send_byte(write_data); // 发送数据字节
}
void writzi(unsigned char addr, unsigned char number)
{
unsigned char temp;
if((addr>0x0f)||(number>0x0f))return;
temp=addr<<4;
temp|=number;
send_byte(0x15); // 发送寄存器地址
send_byte(temp); // 发送数据字节
}
void shownum(unsigned char dany, unsigned int number)
{
unsigned char addtemp,temp,i;
unsigned int numtemp;
numtemp=number;
addtemp=dany*4-1;
temp=addtemp;
for(i=0;i<4;i++)
{
send_byte(temp); // 发送寄存器地址
send_byte(0xff); // 发送数据字节
temp--;
}
while(numtemp>0)
{
temp=numtemp%10;
writzi(addtemp,temp);
addtemp--;
numtemp=numtemp/10;
}
if(number<10)
{
temp=dany*4-2;
writzi(temp,0);
}
temp=dany*32-9;
send_byte(0x18); // 发送寄存器地址
send_byte(temp); // 发送数据字节
}
// **********************************************************
// * 读出 BC728X 内部寄存器的值, 调用参数为寄存器地址 *
// **********************************************************
unsigned char read728x(unsigned char reg_add)
{
send_byte(reg_add|0x80); // 发送读指令(bit7=1)
return(receive_byte()); // 接收数据字节并返回
}
void readBC7281(void)
{
unsigned char temp;
send_byte(0x13|0x80); // 发送读指令(bit7=1)
temp=receive_byte(); // 接收数据字节并返回
show.test=temp;
show.lowstate=0;
savemsg(KEPPRESS,temp);
}
void showscreen(void)
{
static unsigned char num;
num++;
if(show.page==1)
{
shownum(1,sa866ae.freset);
write728x(0,0x8c); // p
}
else
{
if(num<4)
{
shownum(1,sa866ae.freset);
write728x(0,0x8c); // p
}
else
{
write728x(1,0xff); // nop
write728x(3,0xff); // nop
write728x(2,zf[work.workstatic]); // E
write728x(0,0x86); // E
if(num>8)num=0;
}
}
shownum(2,adc.gongdu);
shownum(4,adc.Vadc);
shownum(3,adc.Iadc);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -