/*發送資料子程式,Data為要求發送的資料*/
void Send(uchar Data)
{
uchar BitCounter=8;
uchar temp;
do
{
temp=Data; //將待發送資料暫存temp
Scl=0;
Nop();
if((temp&0x80)==0x80) //將讀到的資料&0x80
Sda=1;
else
Sda=0;
Scl=1;
temp=Data<<1; //資料左移
Data=https://bbs.csdn.net/topics/temp; //資料左移后重新賦值Data
BitCounter--; //該變數減到0時,資料也就傳送完成了
}
while(BitCounter); //判斷是否傳送完成
Scl=0;
}
/*讀一位元組的資料,并回傳該位元組值*/
uchar Read(void)
{
uchar temp=0;
uchar temp1=0;
uchar BitCounter=8;
Sda=1;
do
{
Scl=0;
Nop();
Scl=1;
Nop();
if(Sda) //資料位是否為1
temp=temp|0x01; //為1 temp的最低位為1(|0x01,就是將最低位變為1)
else //如果為0
temp=temp&0xfe; //temp最低位為0(&0xfe(11111110)最低位就是0)
if(BitCounter-1) //BitCounter減1后是否為真
{
temp1=temp<<1; //temp左移
temp=temp1;
}
BitCounter--; //BitCounter減到0時,資料就接收完了
}
while(BitCounter); //判斷是否接收完成
return(temp);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/228609.html
標籤:單片機/工控
上一篇:航順MCU
