?? bus.c
字號:
/************************************************
* *
* BUS.C: BUS control routines. *
* *
*************************************************/
#include <reg51.h>
#include <intrins.h>
#include "mascot.h"
#include "OSD.h"
#include "global.h"
#define USE_ASM 1
/*******************************************************************
* Send SCL pin high and wait for any clock stretching peripherals *
********************************************************************/
#define SCLHigh() { SCLPin = 1; while(!SCLPin); }
#define SCLHigh2() { SCLPin2 = 1; while(!SCLPin2); }
/********************************************************
* Grasp the I2C bus, send slave address and command *
*********************************************************/
void GoI2CMaster(unsigned char adr)
{
BusBusy = 1;
if (!SCLPin) SCLHigh();
NoAck = 0;
BusFault = 0;
if (SCLPin & SDAPin) {
SDAPin = 0;
_nop_();
SCLPin = 0;
_nop_();
SendI2CByte(adr);
} else
BusFault = 1;
}
/*********************************************
* Send one byte of data to an I2C device *
**********************************************/
void SendI2CByte(unsigned char val)
{
#if USE_ASM
ACC = val;
#pragma asm
MOV R7,#8
SendByte1:
RLC A
MOV SDAPin,C
NOP
SETB SCLPin
JNB SCLPin,$
NOP
CLR SCLPin
DJNZ R7,SendByte1
SETB SDAPin
NOP
SETB SCLPin
JNB SCLPin,$
JNB SDAPin,SendByte2
SETB NoAck
SendByte2:
CLR SCLPin
#pragma endasm
#else
unsigned char bmsk = 0x80;
while (bmsk) {
SDAPin = (val & bmsk) ? 0x01 : 0x00;
SCLHigh();
bmsk >>= 1;
SCLPin = 0;
}
SDAPin = 1;
SCLHigh();
if (SDAPin) NoAck = 1;
SCLPin = 0;
#endif
}
/*********************************************
* Receive byte of data from an I2C device *
**********************************************/
unsigned char RcvI2CByte(unsigned char cnt)
{
#if USE_ASM
B = cnt;
#pragma asm
MOV R7,#8
RcvByte1:
SETB SCLPin
JNB SCLPin,$
MOV C,SDAPin
RLC A
CLR SCLPin
DJNZ R7,RcvByte1
PUSH ACC
MOV A,B
CJNE A,#1,RcvByte2
SETB SDAPin
SJMP RcvByte3
RcvByte2:
CLR SDAPin
NOP
RcvByte3:
SETB SCLPin
JNB SCLPin,$
NOP
CLR SCLPin
NOP
SETB SDAPin
POP ACC
#pragma endasm
return ACC;
#else
unsigned char bval = 0, b;
for(b=0; b < 8; b++) {
SCLHigh();
bval <<= 1;
bval |= SDAPin;
SCLPin = 0;
}
SDAPin = (cnt==1) ? 1 : 0;
SCLHigh();
SCLPin = 0;
SDAPin = 1; //Clear acknowledge bit.
return bval;
#endif
}
/*************************************
* Send I2C STOP, release bus *
**************************************/
void SendI2CStop(void)
{
SDAPin = 0;
_nop_();
SCLHigh();
_nop_();
SDAPin = 1;
_nop_();
BusBusy = 0;
}
#if USE_I2C_2
/********************************************************
* Grasp the I2C bus, send slave address and command *
*********************************************************/
void GoI2CMaster2(unsigned char adr)
{
BusBusy = 1;
if (!SCLPin2) SCLHigh2();
NoAck = 0;
BusFault = 0;
if(SCLPin2 & SDAPin2) {
SDAPin2 = 0;
_nop_();
SCLPin2 = 0;
_nop_();
SendI2CByte2(adr);
} else
BusFault = 1;
}
/*********************************************
* Send one byte of data to an I2C device *
**********************************************/
void SendI2CByte2(unsigned char val)
{
#if USE_ASM
ACC = val;
#pragma asm
MOV R7,#8
SendByte1:
RLC A
MOV SDAPin2,C
SETB SCLPin2
JNB SCLPin2,$
CLR SCLPin2
DJNZ R7,SendByte1
SETB SDAPin2
SETB SCLPin2
JNB SCLPin2,$
JNB SDAPin2,SendByte2
SETB NoAck
SendByte2:
CLR SCLPin2
#pragma endasm
#else
unsigned char bmsk = 0x80;
while (bmsk) {
SDAPin2 = (val & bmsk) ? 0x01 : 0x00;
SCLHigh2();
bmsk >>= 1;
SCLPin2 = 0;
}
SDAPin2 = 1;
SCLHigh2();
if (SDAPin2) NoAck = 1;
SCLPin2 = 0;
#endif
}
/*********************************************
* Receive byte of data from an I2C device *
**********************************************/
unsigned char RcvI2CByte2(unsigned char cnt)
{
#if USE_ASM
B = cnt;
#pragma asm
MOV R7,#8
RcvByte1:
SETB SCLPin2
JNB SCLPin2,$
MOV C,SDAPin2
RLC A
CLR SCLPin2
DJNZ R7,RcvByte1
PUSH ACC
MOV A,B
CJNE A,#1,RcvByte2
SETB SDAPin2
SJMP RcvByte3
RcvByte2:
CLR SDAPin2
RcvByte3:
SETB SCLPin2
JNB SCLPin2,$
CLR SCLPin2
SETB SDAPin2
POP ACC
#pragma endasm
return ACC;
#else
unsigned char bval = 0, b;
for(b=0; b < 8; b++) {
SCLHigh2();
bval <<= 1;
bval |= SDAPin2;
SCLPin2 = 0;
}
SDAPin2 = (cnt==1) ? 1 : 0;
SCLHigh2();
SCLPin2 = 0;
SDAPin2 = 1; //Clear acknowledge bit.
return bval;
#endif
}
/*************************************
* Send I2C STOP, release bus *
**************************************/
void SendI2CStop2(void)
{
SDAPin2 = 0;
_nop_();
SCLHigh2();
_nop_();
SDAPin2 = 1;
_nop_();
BusBusy = 0;
}
#endif //USE_I2C_2
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -