本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
- #include <AVR/io.h>
- #include <avr/twi.h>
- #include <avr/signal.h>
- #define fosc 7372800
- #define sla 0xa0
- #define slav 0xa2
- #define TW_WRITE 0
- #define TW_READ 1
- #define max 3
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- void Twi_Init( void )
- {
- TWBR = 0x20;//设置波特率
- TWSR &= 0xfc;//设置预分频因子,则SCL频率为92160Hz
- }
- /*----------------------------------------------------------------------
- 功 能:将读出的时间数据的无关位屏蔽掉
- 说 明:hym8563时钟寄存器的有些无关位的状态是不定的,所以应该将无效位屏蔽掉
- ----------------------------------------------------------------------*/
- void Datajust(void)
- {
- trdata[0]=trdata[0]&0x7f;
- trdata[1]=trdata[1]&0x7f;
- trdata[2]=trdata[2]&0x3f;
- trdata[3]=trdata[3]&0x3f;
- trdata[4]=trdata[4]&0x07;
- trdata[5]=trdata[5]&0x1f;
- trdata[6]=trdata[6]&0xff;
- }
- uchar FM24C256_Write( uint address,uchar write_data )
- {
- uchar twst,n=0x0,rv=0x1;
- restart:
- if(n++>=max)
- goto error;
- begin:
- TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);//发送开始信号
- while((TWCR & _BV(TWINT))==0x0);//等待发送完毕
- switch((twst = TW_STATUS))
- {
- case TW_REP_START://有可能出现
- case TW_START:
- break;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = sla | TW_WRITE;//发送SLA+W
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_SLA_ACK:
- break;
- case TW_MT_SLA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = address>>8;//传输地址高8位
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- case TW_MT_DATA_NACK:
- goto quit;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = address;//传输地址低8位
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- case TW_MT_DATA_NACK:
- goto quit;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = write_data;
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- default:
- goto error;
- }
- quit:
- TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);//发送停止信号
- return rv;
- error:
- rv=0x0;
- goto quit;
- }
- uchar FM24C256_Read( uint address )
- {
- uchar twst,n=0x0,rv=0x1;
- restart:
- if(n++>=max)
- goto error;
- begin:
- TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);//发送开始信号
- while((TWCR & _BV(TWINT))==0x0);//等待发送完毕
- switch((twst = TW_STATUS))
- {
- case TW_REP_START://有可能出现
- case TW_START:
- break;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = sla | TW_WRITE;//发送SLA+W
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_SLA_ACK:
- break;
- case TW_MT_SLA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = address>>8;//传输地址高8位
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- case TW_MT_DATA_NACK:
- goto quit;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = address;//传输地址低8位
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- case TW_MT_DATA_NACK:
- goto quit;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);//发送(重新)开始信号
- while((TWCR & _BV(TWINT))==0x0);//等待发送完毕
- switch((twst = TW_STATUS))
- {
- case TW_REP_START://正确的状态
- break;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = sla | TW_READ;//发送SLA+R
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MR_SLA_ACK:
- break;
- case TW_MR_SLA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MR_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWCR = _BV(TWINT) | _BV(TWEN) ;//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MR_DATA_NACK:
- rv = TWDR;
- break;
- default:
- goto error;
- }
- quit:
- TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);//发送停止信号
- return rv;
- error:
- rv=0x0;
- goto quit;
- }
- uchar SetHym8563(uchar suba,uchar *s,uchar no )
- {
- uchar twst,i,n=0x0,rv=0x1;
- restart:
- if(n++>=max)
- goto error;
- begin:
- TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);//发送开始信号
- while((TWCR & _BV(TWINT))==0x0);//等待发送完毕
- switch((twst = TW_STATUS))
- {
- case TW_REP_START://有可能出现
- case TW_START:
- break;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = slav | TW_WRITE;//发送SLAV+W
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_SLA_ACK:
- break;
- case TW_MT_SLA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = suba | TW_WRITE;//发送SUBA+W
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- case TW_MT_DATA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- for(i=0;i<no;i++)
- {
- TWDR = *s | TW_WRITE;
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- case TW_MT_DATA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- s++;
- }
- quit:
- TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);//发送停止信号
- return rv;
- error:
- rv=0x0;
- goto quit;
- }
- uchar GetHym8563(uchar suba,uchar *s,uchar no)
- {
- uchar twst,i,n=0x0,rv=0x1;
- restart:
- if(n++>=max)
- goto error;
- begin:
- TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);//发送开始信号
- while((TWCR & _BV(TWINT))==0x0);//等待发送完毕
- switch((twst = TW_STATUS))
- {
- case TW_REP_START://有可能出现
- case TW_START:
- break;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = slav | TW_WRITE;//发送SLAV+W
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_SLA_ACK:
- break;
- case TW_MT_SLA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = suba | TW_WRITE;//发送SUBA+W
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MT_DATA_ACK:
- break;
- case TW_MT_DATA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);//发送(重新)开始信号
- while((TWCR & _BV(TWINT))==0x0);//等待发送完毕
- switch((twst = TW_STATUS))
- {
- case TW_REP_START://正确的状态
- break;
- case TW_MT_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- TWDR = slav | TW_READ;//发送SLAV+R
- TWCR = _BV(TWINT) | _BV(TWEN);//清TWINT,开始传输
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MR_SLA_ACK:
- break;
- case TW_MR_SLA_NACK://器件忙,有可能处于内部写周期
- goto restart;
- case TW_MR_ARB_LOST:
- goto begin;
- default:
- goto error;
- }
- for(i=0;i<no;i++)
- {
- if(i==(no-1))
- {
- TWCR = _BV(TWINT) | _BV(TWEN) ;//清TWINT,开始传输(无应答)
- }
- else
- {
- TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWEA) ;//清TWINT,开始传输(有应答)
- }
- while((TWCR & _BV(TWINT))==0x0);//等待传输完毕
- switch((twst = TW_STATUS))
- {
- case TW_MR_DATA_NACK:
- case TW_MR_DATA_ACK:
- *s = TWDR;
- s++;
- break;
- default:
- goto error;
- }
- }
- quit:
- TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);//发送停止信号
- return rv;
- error:
- rv=0x0;
- goto quit;
- }
复制代码 |