📄 i2c.c
字号:
#include<string.h>
#include<reg51.h>
#include<intrins.h>
#include<absacc.h>
#include<math.h>
#include<CST.h>
#define DELAY_TIME 4
#define NOP _nop_(); _nop_(); _nop_(); _nop_();
#define uint unsigned int
#define uchar unsigned char
#define AddWr 0xD4
#define AddRd 0xD5
#define TRUE 1
#define FALSE 0
sfr CLKRL=0X97;
sfr CLKCON0=0X8F;
sfr BRL=0X9A;
sfr BDRCON=0X9B;
sfr RCAP2L=0xca;
sfr RCAP2H=0xCB;
sbit RCLK=0xcd;
sbit TCLK=0xcc;
sbit TR2=0xca;
sbit DQ=P1^0;
sbit jdq0=P1^1;
sbit jdq1=P1^2;
sbit jdq2=P1^3;
sbit jdq3=P1^4;
sbit SCL=P3^3;
sbit SDA=P3^4;
unsigned char pad[9],aPoint,aBuffer[32];
unsigned char code baud_table[8][2]=
{
{ 0x80, 0x01 }, /* 300 */
{ 0x60, 0x00 }, /* 1200 */
{ 0x30, 0x00 }, /* 2400 */
{ 0x0c, 0x00 }, /* 9600 */
{ 0x06, 0x00 }, /* 19K */
{ 0x03, 0x00 }, /* 38k */
{ 0x02, 0x00 }, /* 56k */
{ 0x01, 0x00 } /* 115k */
};
//unsigned char code ROMID[4][8]={{0x28,0xfd,0x96,0xa5,0x00,0x00,0x00,0x25},{0x28,0x3e,0x5d,0xa5,0x00,0x00,0x00,0xb1},
// {0x28,0x2c,0xa1,0xa5,0x00,0x00,0x00,0x03},{0x28,0x65,0x45,0xa5,0x00,0x00,0x00,0x25}};
unsigned char ROMID[4][8]={{0x28,0xfd,0x96,0xa5,0x00,0x00,0x00,0x25},{0x28,0x65,0x45,0xa5,0x00,0x00,0x00,0x25},
{0x28, 0x67,0x81,0xA5 ,0x00, 0x00, 0x00, 0x76}, {0x28,0x2c,0xa1,0xa5,0x00,0x00,0x00,0x03}};
///1-wire
void set_exp(int usec) ;
void set_exp1(void) ;
//????: void delay(int cnt)
//????:???????,???????????????????????????????11?0592M???,
//?????????16*cnt+24us????
void delay(int cnt)
{
int i;
for(i=0;i<cnt;i++);
}
//-------------------------------------------------
//????: unsigned char bus_reset(void)
//????:????????????,??????????????????
//???0????????????,??1?????????????????
unsigned char bus_reset(void)
{
unsigned char presence;
DQ=0; //?DQ?????
delay(29); // ?DQ???????????480us
DQ=1; // ??????????????
delay(3); //??????????
presence=DQ; //????????
delay(25); //???????????
return presence; //?????????????
}
//-------------------------------------------
//????: unsigned char read_bit(void)
//????:??????????????????????,?????????
//??????????15us,?????????delay()?????,?????
//for?????15us????
unsigned char read_bit(void)
{
unsigned char i;
DQ=0; //?DQ??????????
DQ=1; //??DQ???,?????????
for(i=0;i<3;i++); //??15us???,??????????
return DQ; //??????
}
//-------------------------------------------
//????: void write_bit(char bitvalue)
//????:???????????
void write_bit(char bitvalue)
{
DQ=0; //?DQ??????????
if(bitvalue==1)
{
DQ=1; //??????1,?DQ????????
}
delay(5); //???????
DQ=1; //??DQ???
}
//-------------------------------------------
//????:unsigned char read_byte(void)
//????:?????????????
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value=0; //????????0
for(i=0;i<8;i++) //????8????
{
if(read_bit()) //???????????1
{
value=value|(0x01<<i); //?????????????1
}
}
delay(6); //??????
return value;
}
//-------------------------------------------
//????: void write_byte(char bytevalue)
//????:???????????
void write_byte(char bytevalue)
{
unsigned char i;
unsigned char temp;
for(i=0;i<8;i++) //????8????
{
temp=bytevalue>>i; //??????????i?
temp=temp & 0x01; //????????i?
write_bit(temp); //?????????
}
delay(5); //??????
}
//------------------------------------------
//????:unsigned char send_matchRom(unsigned char DS18B20No)
//????:???????????ROM??,ROM ID????DS18B20No???????ROM???
unsigned char send_matchRom(unsigned char DS18B20No)
{
unsigned char i;
if (bus_reset()) return FALSE;
write_byte(0x55);
for(i=0;i<8;i++)
{
write_byte(ROMID[DS18B20No][i]);
}
return TRUE;
}
//---------------------------------------
//????:void write_scratchPad(unsigned char DTNo)
//????:????????DS18B20????????TH?TL????????
//TH=0x1f;
//TL=0x00;
//confgReg=0x7f;
void write_scratchPad(unsigned char DTNo)
{
send_matchRom(DTNo); //???????ROM??,????DTNo?????????
write_byte(0x4e);
write_byte(0x1f);
write_byte(0x00);
write_byte(0x7f);
}
//-------------------------------------
//????:void read_scratchPad(unsigned char DTNo)
//????:????????DS18B20?????????????????,
//????????????scratchPad[9]?
void read_scratchPad(unsigned char DTNo)
{
unsigned char i;
send_matchRom(DTNo); //???????ROM??,????DTNo?????????
write_byte(0xbe);
for(i=0;i<9;i++)
{
pad[i]=read_byte();
}
}
//IIC
void mDelay(uchar j)
{
uint i;
for ( ; j>0; j--)
{for (i=0; i<125;i--)
{;}
}
}
void Start(void)
{
SDA=1;
SCL=1;
NOP;
SDA=0;
NOP;
}
void Stop(void)
{
SDA=0;
SCL=1;
NOP;
SDA=1;
NOP;
}
void Ack(void)
{
SDA=0;
NOP;
SCL=1;
NOP;
SCL=0;
}
void NoAck(void)
{
SDA=1;
NOP;
SCL=1;
NOP;
SCL=0;
}
void Send(uchar rdata)
{
uchar BitCounter=8;
uchar temp;
//rdata=5;
do
{
temp=rdata;
SCL=0;
NOP;
if ((temp&0x80)==0x80)
SDA=1;
else
SDA=0;
NOP;
SCL=1;
temp=rdata<<1;
rdata=temp;
BitCounter--;
}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)
temp=temp|0x01;
else
temp=temp&0xfe;
if(BitCounter-1)
{
temp1=temp<<1;
temp=temp1;
}
BitCounter--;
}while(BitCounter);
return(temp);
}
void WrToRom(uchar rdata, uchar Address)
{
Start();
Send(AddWr);
Ack();
Send(Address);
Ack();
Send(rdata);
Ack();
Stop();
mDelay(10);
}
uchar RdFromRom(uchar Address)
{
uchar tdata;
Start();
Send(AddWr);
Ack();
Send(Address);
Ack();
Start();
Send(AddRd);
Ack();
tdata=Read();
SCL=0;
NoAck();
Stop();
return tdata;
}
unsigned char ReadRegister(unsigned char port,unsigned char reg )
{
unsigned int addr;
unsigned char a;
addr=RegRd+(port<<3)+reg;
addr=addr<<8;
a=XBYTE[addr];
return a;
}
void WriteRegister(unsigned char port,unsigned char reg,unsigned char value )
{
unsigned int addr;
addr=RegWr+(port<<3)+reg;
addr=addr<<8;
XBYTE[addr]= value;
}
unsigned char UART_PORT_CHECK(unsigned char UART_PORT)
{
unsigned char CHECK_DATA;
WriteRegister(UART_PORT,SCR,UART_PORT );
XBYTE[0x00]=0;
CHECK_DATA=ReadRegister(UART_PORT,SCR);
if(CHECK_DATA== UART_PORT)
return TRUE;
else
return FALSE;
}
int UART_PORT_Init(unsigned int UART_PORT,unsigned int baud,unsigned int parity,unsigned int tdata,unsigned int stop,unsigned int fifo,unsigned int trigger)
{
char lcr_byte,fifo_byte;
WriteRegister(UART_PORT,LCR,0x80);
WriteRegister(UART_PORT,DIVMSB,baud_table[baud][1]);
WriteRegister(UART_PORT,DIVLSB,baud_table[baud][0]);
lcr_byte = 0x00;
lcr_byte = tdata; /* Set the bit0 & bit1 for word length */
lcr_byte |= stop << 3; /* Set the bit2 for stop bit */
if(parity != _COM_NOPARITY_)
{
lcr_byte |= 1 << 4; /* Set the bit3 for parity */
if(parity == _COM_EVENPARITY_)
lcr_byte |= 1 << 5; /* Set the bit4 for EVEN parity */
}
WriteRegister(UART_PORT,LCR,lcr_byte);
if(fifo)
{
/* Have to first set the fifo enable */
fifo_byte = 0x01;
WriteRegister(UART_PORT,FCR,fifo_byte);
/* Now program the FIFO */
fifo_byte = 0x07; /* set bit0 - FIFO enable, Reset RCVR and XMIT FIFO */
fifo_byte |= trigger << 7; /* set bit6 and bit7 with the trigger level */
/* Program FCR */
WriteRegister(UART_PORT,FCR,fifo_byte);
WriteRegister(UART_PORT,IER,0x01);
return TRUE;
}
}
void MCU_init()
{
//com serial port (89ls51)
/*
SCON=0x50;
TMOD=0x21; //auto reload in mod 2 for timer 1,mod 1 for tiner 0
TH1=0xfd;
TL1=0xfd;
TR1=1; //start counter
ES=0; //open com serial port interrupt
*/
//(p89lv51rd2)
RCLK=1;
TCLK=1;
RCAP2H=0xff;
RCAP2L=0xdc;
SCON=0x52;
TR2=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -