由某个单排序器件总线协议:
初始化时序
读时序
写1时序
写0时序
从机要实现ACT_INIT,READ和WRITE几个动作,另外当主机初始化后写入0x99,从机就回复8个byte数据;
#define DQ_PIN (xx)
#define DQ_OUT //(xx)设置输出模式
#define DQ_IN //(xx)设置输入模式
#define tRSTL 480//-
#define tRSTH 480//-
#define tPDH 15//-60
#define tPDL 60//-240
#define tSLOT 60//60-120
#define tLOW1 1//1-15
#define tREC 1//1-
#define tLOW0 60//60-120<tSLOT
#define tRELEASE 0//0-45
#define tLOWR 1//1-15
#define tRDV 15
#define tSU 1//<1
void ACT_INIT(){
DQ_OUT;
delay_us(tPDH);
DQ_PIN=0;
delay_us(tPDL);
delay_us(tPDL);
DQ_PIN=1;
}
void WRITE_BYTES(const unsigned char * src, unsigned char len){
unsigned char i = 0;
DQ_IN;
while(len>0){
data=*src;
for(i=0;i<8;i++){
while( DQ_PIN == 1 );
DQ_OUT;
DQ_PIN = ( data&0x1 );
data>>=1;
delay_us(tRDV);//max tLOWR
delay_us(tRDV);//
DQ_PIN=1;
DQ_IN;
if( DQ_PIN == 0 ){
while( DQ_PIN == 0 );
ACT_INIT();
return;
}
}
src++;
len--;
}
}
const unsigned char datas[] = {
0x99,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,
};
void 主函数()
{
unsigned int lowTime;
unsigned char cmd = 0, Count = 0;
while(1)
{
DQ_IN;
while( DQ_PIN == 1 );
lowTime = 0;
while( DQ_PIN == 0 ){
lowTime++;
delay_us(1);
}
if( lowTime > tRSTL )
{
Count = 0;
ACT_INIT();
continue;
}
Count++;
cmd>>=1;
if( lowTime < tLOW1 ){
cmd |= 0x80 ;
}
if( Count == 8 ){
Count = 0;
if( cmd == datas[0] ){
WRITE_BYTES( datas + 1, 8 );
}
}
}
}