📄 comm.bak
字号:
/* ---------------------------------------------------------- */
/* */
/* IC卡加油机键盘通讯子模块 */
/* 负责与PSAM卡、用户卡、加油机主板通讯 */
/* 最后修改日期:2002年4月21日 */
/* */
/* ---------------------------------------------------------- */
#include "D:\comm_d\comm.h"
void send(unsigned char offset,unsigned char len)
{
register unsigned char counter=0;
bit tem;
//ES=0;
DIRECTION=1; // 置数据传送方向为发送
is_send=1; // 置发送标志
PT0=1; // 置定时器0中断为高优先级
ET0=1; // 允许定时器0中断
while(counter<len) // 未发完len字节
{
TL0=0x84; // 定时器0工作在模式2,晶振为14.31818M,定时溢出频率为9600(实际为9622)
TH0=0x97; // 用仿真器时用0x97
TR0=1; // 启动定时器0
com_cur=0; // 当前发送起始位
com_tem=send_buf[offset+counter]; // 从外部读入要发送的字节
com_tem1=0xe;
ACC=com_tem;
com_tem1|=P; // 取校验位
com_tem1=com_tem1<<1;
tem=com_tem&0x80; // 取d7
com_tem1|=tem; // 把d7放在com_tem1的D0位
com_tem=com_tem<<1; // com_tem存起始位(0)+d0~d6
while(com_cur<13); // 等待本字节发送完成
counter++;
TR0=0; // 关定时器0
}
ET0=0; // 禁止定时器0中断
dog();
//comm_status=0;
//ES=1;
}
unsigned char recv(unsigned char offset,unsigned char len)
{
register unsigned char timer=0;
//ES=0;
DIRECTION=0; // 8888
is_send=0; // 置接收标志
IE0=0; // 清外部中断0触发标志
IT0=1; // 置为沿触发
PX0=1; // 置外部中断0为高优先级
EX0=1; // 允许外部中断0
is_over=0;
is_err=0;
com_tem1=offset; // 初始化接收指针
com_limit=offset+len;
while(!is_over && timer<100) // 等待接收结束,2s超时
{
com_delay();
dog();
timer++;
}
EX0=0; // 禁止外部中断0
ET0=0; // 禁止定时器0中断
TR0=0; // 关定时器0
//comm_status=0;
dog();
//ES=1;
if(is_err)return 1; // 校验位错
else if(is_over)return 0; // 成功返回
else return 2; // 超时
}
void com_delay()
{
register unsigned int i;
for(i=0;i<3409;i++)if(is_over)break;
}
/* 中断服务程序 */
void ic_timer0() interrupt 1 // 定时器0
{
if(is_send) // 发送
{
if(com_cur<8) // 发送com_tem字节
{
com_sda=com_tem&0x1; // 发送最低位
com_tem=com_tem>>1; // 右移1位
}
else // 发送com_tem1
{
com_sda=com_tem1&0x1;
com_tem1=com_tem1>>1;
}
com_cur++;
}
else // 接收
{
if(com_cur<8) // 收数据位
{
com_tem=com_tem>>1;
if(com_sda)com_tem|=0x80;
}
else if(com_cur==8) // 收校验位
{
ACC=com_tem;
if(P!=com_sda) // 校验错
{
is_err=1;
is_over=1;
}
}
else // 停止位
{
rekk[com_tem1]=com_tem;
com_tem1++;
if(com_tem1<com_limit) // 未收完
{
ET0=0; // 禁止定时器0中断
TR0=0; // 关定时器0
IE0=0;
EX0=1; // 允许外部中断0
}
else is_over=1; // 收完
}
com_cur++;
}
}
void ic_int0() interrupt 0 // 外部中断0
{
EX0=0; // 禁止外部中断0
TL0=0x74; // 第一次定时为1.5个9600BAUD
TH0=0x84;
TF0=0; // 清定时器0中断触发标志
PT0=1; // 置定时器0中断为高优先级
ET0=1; // 允许定时器0中断
com_tem=0;
com_cur=0;
TR0=1; // 启动定时器0
}
void key_dlu()
{
unsigned int k;
for(k=0;k<2400;k++);
}
unsigned int get_CRC(unsigned char xdata *buffer,unsigned int length)
{
register unsigned int i,j;
unsigned char data tem[2];
bit lsb0,lsb1;
//ES=0;
tem[0]=0; // 初始化CRC寄存器为0
tem[1]=0;
for(i=0;i<length;i++)
{
tem[1]^=buffer[i]; // 输入数据与CRC寄存器低字节异或
for(j=0;j<8;j++)
{
lsb0=tem[0]&1; // 取高字节的最低位
tem[0]=tem[0]>>1;
lsb1=tem[1]&1; // 取低字节的最低位
tem[1]=tem[1]>>1;
if(lsb0)tem[1]|=0x80;
if(lsb1) // 最低位为1,CRC寄存器与0xa001异或
{
tem[0]^=0xa0;
tem[1]^=0x01;
}
}
}
//comm_status=0;
//ES=1;
return(*(unsigned int *)tem);
}
unsigned long bcdtohex(unsigned char xdata *bcd,unsigned char xdata bcd_num)
{
unsigned long xdata tem;
register unsigned char i;
tem=0;
//ZK_CS=1;
for(i=bcd_num;i>0;i--)
{
// tem=tem*100+(bcd[bcd_num-i]/16)*10+bcd[bcd_num-i]%16;
tem=tem*100;
tem=tem+(bcd[bcd_num-i]/16)*10;
tem=tem+bcd[bcd_num-i]%16;
//dog();
}
return tem;
}
unsigned char * hextobcd(unsigned long xdata hexb)
{
//unsigned long xdata hex_temp=0;
unsigned char xdata char_temp[4]={0,0,0,0};
// hex_temp=hexb;
//ZK_CS=1;
char_temp[1]=hexb/100000;
char_temp[1]<<=4;
hexb=hexb-hexb/100000*100000;
char_temp[1]=char_temp[1]|hexb/10000;
hexb=hexb-hexb/10000*10000;
char_temp[2]=hexb/1000;
char_temp[2]<<=4;
hexb=hexb-hexb/1000*1000;
char_temp[2]=char_temp[2]|hexb/100;
hexb=hexb-hexb/100*100;
char_temp[3]=hexb/10;
char_temp[3]<<=4;
hexb=hexb-hexb/10*10;
char_temp[3]=char_temp[3]|hexb;
return char_temp;
}
/*******************************************************************
向有子地址器件读取多字节数据函数
函数原型: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);
功能: 从启动总线到发送地址,子地址,读数据,结束总线的全过程,从器件
地址sla,子地址suba,读出的内容放入s指向的存储区,读no个字节。
如果返回1表示操作成功,否则操作有误。
注意: 使用前必须已结束总线。
********************************************************************/
unsigned char IRcvStr(unsigned char sla,unsigned char suba,unsigned char no)
{
unsigned char i;
unsigned char *s;
Start_I2c(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(ack==0)return(0);
SendByte(suba); /*发送器件子地址*/
if(ack==0)return(0);
Start_I2c();
SendByte(sla+1);
if(ack==0)return(0);
for(i=0;i<no-1;i++)
{
flash[i]=RcvByte(); /*发送数据*/
Ack_I2c(0); /*发送就答位*/
//s++;
}
flash[i]=RcvByte();
Ack_I2c(1); /*发送非应位*/
Stop_I2c(); /*结束总线*/
return(1);
}
/*******************************************************************
向有子地址器件发送多字节数据函数
函数原型: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);
功能: 从启动总线到发送地址,子地址,数据,结束总线的全过程,从器件
地址sla,子地址suba,发送内容是s指向的内容,发送no个字节。
如果返回1表示操作成功,否则操作有误。
注意: 使用前必须已结束总线。
********************************************************************/
unsigned char ISendStr(unsigned char sla,unsigned char suba,unsigned char no)
{
unsigned char i;
//unsigned char *s;
Start_I2c(); /*启动总线*/
SendByte(sla); /*发送器件地址*/
if(ack==0)return(0);
SendByte(suba); /*发送器件子地址*/
if(ack==0)return(0);
for(i=0;i<no;i++)
{
SendByte(flash[i]); /*发送数据*/
if(ack==0)return(0);
//s++;
}
Stop_I2c(); /*结束总线*/
return(1);
}
/********************************************************************
应答子函数
原型: void Ack_I2c(bit a);
功能:主控器进行应答信号,(可以是应答或非应答信号)
********************************************************************/
void Ack_I2c(bit a)
{
if(a==0) SDA=0; /*在此发出应答或非应答信号 */
else SDA=1;
_nop_();
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_(); /*时钟低电平周期大于4μs*/
_nop_();
_nop_();
_nop_();
SCL=0; /*清时钟线,钳住I2C总线以便继续接收*/
_nop_();
_nop_();
}
/*******************************************************************
起动总线函数
函数原型: void Start_I2c();
功能: 启动I2C总线,即发送I2C起始条件.
********************************************************************/
void Start_I2c()
{
SDA=1; /*发送起始条件的数据信号*/
_nop_();
SCL=1;
_nop_(); /*起始条件建立时间大于4.7us,延时*/
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0; /*发送起始信号*/
_nop_(); /* 起始条件锁定时间大于4μs*/
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0; /*钳住I2C总线,准备发送或接收数据 */
_nop_();
_nop_();
}
/*******************************************************************
结束总线函数
函数原型: void Stop_I2c();
功能: 结束I2C总线,即发送I2C结束条件.
********************************************************************/
void Stop_I2c()
{
SDA=0; /*发送结束条件的数据信号*/
_nop_(); /*发送结束条件的时钟信号*/
SCL=1; /*结束条件建立时间大于4μs*/
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=1; /*发送I2C总线结束信号*/
_nop_();
_nop_();
_nop_();
_nop_();
}
/*******************************************************************
字节数据传送函数
函数原型: void SendByte(uchar c);
功能: 将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对
此状态位进行操作.(不应答或非应答都使ack=0 假)
发送数据正常,ack=1; ack=0表示被控器无应答或损坏。
********************************************************************/
void SendByte(unsigned char c)
{
unsigned char BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) /*要传送的数据长度为8位*/
{
if((c<<BitCnt)&0x80)SDA=1; /*判断发送位*/
else SDA=0;
SCL=1; /*置时钟线为高,通知被控器开始接收数据位*/
_nop_();
_nop_(); /*保证时钟高电平周期大于4μs*/
_nop_();
_nop_();
_nop_();
dlu();
SCL=0;
}
_nop_();
_nop_();
SDA=1; /*8位发送完后释放数据线,准备接收应答位*/
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
if(SDA==1)ack=0;
else ack=1; /*判断是否接收到应答信号*/
SCL=0;
_nop_();
_nop_();
}
/*******************************************************************
字节数据传送函数
函数原型: uchar RcvByte();
功能: 用来接收从器件传来的数据,并判断总线错误(不发应答信号),
发完后请用应答函数。
********************************************************************/
unsigned char RcvByte()
{
unsigned char retc;
unsigned char BitCnt;
retc=0;
SDA=1; /*置数据线为输入方式*/
for(BitCnt=0;BitCnt<8;BitCnt++)
{
_nop_();
SCL=0; /*置时钟线为低,准备接收数据位*/
_nop_();
_nop_(); /*时钟低电平周期大于4.7μs*/
_nop_();
_nop_();
_nop_();
SCL=1; /*置时钟线为高使数据线上数据有效*/
_nop_();
_nop_();
retc=retc<<1;
if(SDA==1)retc=retc+1; /*读数据位,接收的数据位放入retc中 */
_nop_();
_nop_();
}
SCL=0;
_nop_();
_nop_();
return(retc);
}
void dlu()
{
_nop_();
_nop_();
_nop_();
_nop_();
}
void gettime()
{
//unsigned char i;
//unsigned char con[7];
IRcvStr(0xa2,0x02,7);
second=flash[0]&0x7f;
minutes=flash[1]&0x7f;
hour=flash[2]&0x3f;
day=flash[3]&0x3f;
month=flash[5]&0x1f;
year=flash[6];
}
void settime()
{
//unsigned char qs[9];
flash[0]=0;
flash[1]=0;
flash[2]=second;
flash[3]=minutes;
flash[4]=hour;
flash[5]=day;
flash[6]=0;
flash[7]=month;
flash[8]=year;
ISendStr(0xa2,0x00,9);
}
unsigned int dw_zk(unsigned int qw)
{
union
{
unsigned int int_z;
unsigned char ch_z[2];
} xdata zk;
union
{
unsigned long int_z1;
unsigned int ch_z1[2];
} xdata zk1;
unsigned long w1;
zk.int_z=qw;
w1=94*(zk.ch_z[0]-0xa1)+(zk.ch_z[1]-0xa1);
zk1.int_z1=w1*32;
zk.int_z=zk1.ch_z1[0];
P34=1;
XBYTE[0xc011]=zk.ch_z[1];
return zk1.ch_z1[1];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -