📄 i2c_avr.c
字号:
/*
#include <io.h>
#include <interrupt.h> // include for Intrrupt Vector
#include <sig-avr.h>
#include <pgmspace.h>
#include <string.h>
#include "mytypes.h"
#include "uart.h"
*/ // <<<<--- ////This is 8051 define_...C///////
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/pgmspace.h>
#include <compat/deprecated.h>
#include <string.h>
#include "mytypes.h"
///*******************
#define QSFUN_LIB // note that this define !
#include "QS_Func.h"
#include "QSxxxxH2_EQ.h"
#include "uart.h"
#include "Qs51_Reg.h"
#define FREQUENCYTABLE
#include "SampleRateTbl.h"
///*/
/*********************************************************
******************* I2c_8051.c *************************
********* SDA -> P1_0 SCK -> P1_1 *****************
*********************************************************/
//#include <macros.h>
//#include <sfr_defs.h>
//#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
//#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(x, y) (x |= (1 << y)) /*置位寄器x的第y位*/
#define clr(x, y) (x &= ~(1 <<y )) /*清零寄器x的第y位*/
#define CLK_PORT_DDR DDRC
#define CLK_PORT PORTC
#define SDA_PORT_DDR DDRC
#define SDA_PORT PORTC
#define SCK 6
#define SDA 7
//extern void Delay(U16 nDel);
//#ifdef DEBUG_MODE
extern U08 sDebugBuf[30];
extern U08 sHex[17];
//#endif
void Start_Con(void) // Start Condition
{
sbi(CLK_PORT_DDR,SCK);//SCK :Output Mode
sbi(SDA_PORT_DDR,SDA);//SDA :Output Mode
//SCK = 1; // SCK High
sbi(CLK_PORT,SCK);//SCK :SCK High
//SDA = 1; // SDA High
sbi(SDA_PORT,SDA);//SDA :SDA High
Delay(50);
//SDA = 0; // SDA Low
cbi(SDA_PORT,SDA);//SDA :SDA Low
Delay(10);
//SCK = 0; // SCK Low
cbi(CLK_PORT,SCK);//SCK :SDA Low
Delay(10);
}
void Stop_Con(void) // Stop Condition
{
sbi(CLK_PORT_DDR,SCK);//SCK :Output Mode
sbi(SDA_PORT_DDR,SDA);//SDA :Output Mode
//SDA = 0; // SDA Low
cbi(SDA_PORT,SDA);//SDA :SDA Low
//SCK = 0; // SCK Low
cbi(CLK_PORT,SCK);//SCK :SCK Low
// Delay_Loop(20); //眠啊
Delay(2);
//SCK = 1; // SCK High
sbi(CLK_PORT,SCK);//SCK :SCK High
Delay(10);
//SDA = 1; // SDA High
sbi(SDA_PORT,SDA);//SDA :SDA High
Delay(50);
// Delay_Loop(20); //眠啊
}
void Restart_Con(void) // Restart Condition
{
Stop_Con();
Delay(250); // 2ms Delay
}
void I2c_DataOut(U08 wr_data)
{
U08 cnt, Temp;
sbi(CLK_PORT_DDR,SCK);//SCK :Output Mode
sbi(SDA_PORT_DDR,SDA);//SDA :Output Mode
for(cnt=0;cnt<8;cnt++)
{
Temp = wr_data << cnt;
if(0x80&Temp)
//SDA = 1;
sbi(SDA_PORT,SDA);//SDA :SDA High
else
//SDA = 0;
cbi(SDA_PORT,SDA);//SDA :SDA Low
Delay(2);
//SCK = 1;
sbi(CLK_PORT,SCK);//SCK :SCK High
Delay(10);
//SCK = 0;
cbi(CLK_PORT,SCK);//SCK :SCK Low
Delay(10);
}
}
U08 I2c_DataIn(void)
{
U08 cnt, rd_data = 0x00;
sbi(CLK_PORT_DDR,SCK);//SCK :Output Mode
cbi(SDA_PORT_DDR,SDA);//SDA :Input Mode
//SDA = 1; // Open drain on
sbi(SDA_PORT,SDA);//SDA :SDA High
// TRISC4 = 1;
for(cnt=0;cnt<8;cnt++)
{
rd_data <<= 1;
//SCK = 1;
sbi(CLK_PORT,SCK);//SCK :SCK High
// Delay_Loop(20); //眠啊
if(PINC&(1<<SDA)) rd_data |= 0x01;
else rd_data &= 0xFE;
//SCK = 0;
cbi(CLK_PORT,SCK);//SCK :SCK Low
// Delay_Loop(20); //眠啊
}
// TRISC4 = 0; // RC4 output
return(rd_data);
}
U08 AckFlag(void)
{
U16 time_out;
U08 Ack = 1;
sbi(CLK_PORT_DDR,SCK);//SCK :Output Mode
cbi(SDA_PORT_DDR,SDA);//SDA :Input Mode
//SCK = 1;
sbi(CLK_PORT,SCK);//SCK :SCK High
Delay(5);
//Ack = SDA; // ACK
Ack = PINC&(1<<SDA);
Delay(5);
//SCK = 0; // SCK Low
cbi(CLK_PORT,SCK);//SCK :SCK Low
Delay(10);
//sbi(SDA_PORT,SDA);//SDA :SDA High
return Ack;
}
void I2c_Write(U08 control, U08 addr, U08 wr_data)
{
//Re_Write:
Start_Con();
I2c_DataOut(control); // device address and R/W
if(AckFlag()==0)
{
#ifdef DEBUG_MODE_OFF
strcpy(sDebugBuf, "MSG ACK Error\r\n");
PRINT(sDebugBuf);
#endif
//Restart_Con();
//goto Re_Write;
}
Delay(50);
I2c_DataOut(addr); // address
if(AckFlag()==0)
{
#ifdef DEBUG_MODE_OFF
strcpy(sDebugBuf, "MSG ACK Error\r\n");
PRINT(sDebugBuf);
#endif
//Restart_Con();
//goto Re_Write;
}
Delay(50);
I2c_DataOut(wr_data); // data
if(AckFlag()==0)
{
#ifdef DEBUG_MODE_OFF
strcpy(sDebugBuf, "MSG ACK Error\r\n");
PRINT(sDebugBuf);
#endif
//Restart_Con();
//goto Re_Write;
}
Delay(50);
Stop_Con();
}
/*BYTE I2c_Read(BYTE control, BYTE addr)
{
BYTE rd_data;
Re_Read:
Start_Con();
I2c_DataOut(control); // device address and R/W
if(AckFlag()==0)
{
Restart_Con();
goto Re_Read;
}
I2c_DataOut(addr); // address
if(AckFlag()==0)
{
Restart_Con();
goto Re_Read;
}
Start_Con();
I2c_DataOut(control+1); // device address and R/W
if(AckFlag()==0)
{
Restart_Con();
goto Re_Read;
}
rd_data = I2c_DataIn(); // data read
SDA = 1; // --+- Not ack
SCK = 1; // |
SDA = 1; // |
SDA = 1; // |
// Delay_Loop(100); //眠啊
SCK = 0; // |
// Delay_Loop(100); //眠啊
Stop_Con();
return(rd_data);
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -