📄 i2c.#3
字号:
#include <c8051f320.h>
#include "usb_api.h"
sbit SDA = P2^2 ;
sbit SCL = P2^3 ;
#define HIGH 1
#define LOW 0
//int rda5800_reg_tab[45]={
code const rda5800_reg_tab[]={
0x0000,
0x0000,
0xd881, //0x02
//0x6300,
0x8e00, //101.7M
0x4400, //0x04
0x06ff, //0x13ff, lilin, 06/22, 0x10f8, //0x05
0x0000,
0x00cd,
0x0096,
0x0020,
0x4163,
0x0806,
0x5800,
0x5800,
0x5800,
0x5800,
0x4c17, //lilin, 08/13, seek_single step, 0x4817,
0x20a2,
0x0000,
0x000f,
0x06de,
0xecc0,
0x0200,
0x5383,
0x95a4,
0xe848,
0x0500, //0x0500, lilin, 06/22, 0x0500,
0x00a4, //0x00a4, lilin, 06/22, 0x00a4,
0x889b,
0x0d84,
0x4f04,
0x8832,
0x7f71,
0x0660,
0x4010,
0x6002,
0x1808,
0x6458,
0x787f,
0x0100,
0xc040,
0xc020,
0x0024,
0x0400,
0x0020,//
};
void nop(unsigned int cnt)
{
unsigned int i;
for(i=0;i<cnt;i++);
}
void I2C_Start(void)
{
SDA=HIGH;
nop(1);
SCL = HIGH;
nop(4);
SDA = LOW;
nop(4);
SCL = LOW;
nop(1);
}
void I2C_Stop(void)
{
SDA = LOW;
nop(1);
SCL = HIGH;
nop(4);
SDA = HIGH;
nop(4);
}
void I2C_Write_Byte(unsigned char i2c_data)
{
unsigned char BitCnt;
bit status;
for(BitCnt = 0; BitCnt < 8; BitCnt++)
{
if(i2c_data &0x80)
SDA=1;
else
SDA=0;
i2c_data = i2c_data << 1;
nop(4);
SCL=HIGH;
nop(4);
SCL=LOW;
}
SDA=LOW;
nop(1);
SCL = HIGH;
nop(1);
nop(1);
status=SDA;
SCL = LOW;
nop(4);
}
unsigned char I2C_Read_Byte_ACK(void)
{
unsigned char BitCnt,i2c_data=0;
for(BitCnt= 0; BitCnt < 8; BitCnt++)
{
i2c_data = i2c_data << 1;
SDA=HIGH;
nop(1);
SCL=HIGH;
if(SDA==1)i2c_data++;
nop(4);
SCL=LOW;
}
SDA=LOW;//ack
nop(1);
SCL = HIGH;
nop(4);
SCL = LOW;
nop(1);
return i2c_data;
}
unsigned char I2C_Read_Byte_NAK(void)
{
unsigned char BitCnt,i2c_data=0;
for(BitCnt= 0; BitCnt < 8; BitCnt++)
{
i2c_data = i2c_data << 1;
SDA=HIGH;
nop(1);
SCL=HIGH;
if(SDA==1)i2c_data++;
nop(4);
SCL=LOW;
}
SDA=HIGH;//nak
nop(1);
SCL = HIGH;
nop(4);
SCL = LOW;
nop(1);
return i2c_data;
}
void Dab_Write_Register(int reg_addr,int reg_data)
{
unsigned char ValueH,ValueL,dev_addr;
I2C_Start();
//device address
dev_addr = 0xD6;
I2C_Write_Byte(dev_addr);
//write register address
ValueL = (reg_addr & 0xFF);
ValueH = ((reg_addr >> 8) & 0xFF);
I2C_Write_Byte(ValueH);
I2C_Write_Byte(ValueL);
//write register value
ValueL = (reg_data & 0xFF);
ValueH = ((reg_data >> 8) & 0xFF);
I2C_Write_Byte(ValueH);
I2C_Write_Byte(ValueL);
I2C_Stop();
}
void Dab_Read_Register(int reg_addr,int *ptrdata)
{
unsigned char ValueH,ValueL,dev_addr;
//unsigned char i;
I2C_Start();
//device address
dev_addr = 0xD6; //write
I2C_Write_Byte(dev_addr);
//write register address
ValueL = (reg_addr & 0xFF);
ValueH = ((reg_addr >> 8) & 0xFF);
I2C_Write_Byte(ValueH);
I2C_Write_Byte(ValueL);
I2C_Start();
//device address
dev_addr = 0xD7; //read
I2C_Write_Byte(dev_addr);
ValueH = I2C_Read_Byte_ACK();
ValueL = I2C_Read_Byte_NAK();
I2C_Stop();
*ptrdata = (((int)ValueH) << 8) | ValueL;
}
//add FM part;
void DabFM_Write_Register( unsigned char reg_addr, int reg_data)
{
unsigned char ValueH,ValueL,dev_addr;
unsigned char i;
if(reg_addr<2) return;
I2C_Start();
//device address
dev_addr = 0x20;
I2C_Write_Byte(dev_addr);
for(i=2;i<reg_addr;i++)
{
ValueL = ((rda5800_reg_tab[i]) & 0xFF);
ValueH = (((rda5800_reg_tab[i]) >> 8) & 0xFF);
I2C_Write_Byte(ValueH);//high byte first
I2C_Write_Byte(ValueL);
}
ValueL = (reg_data & 0xFF);
ValueH = ((reg_data >> 8) & 0xFF);
I2C_Write_Byte(ValueH);//high byte first
I2C_Write_Byte(ValueL);
I2C_Stop();
}
void DabFM_Read_Register( unsigned char reg_addr, int *ptrdata)
{
unsigned char ValueH,ValueL,dev_addr;
unsigned char i;
unsigned int temp;
I2C_Start();
//device address
dev_addr = 0x21;
I2C_Write_Byte(dev_addr);
if(reg_addr<0x0a)
{
for(i=0x0a;i<=0x3a;i++)
{
ValueH = I2C_Read_Byte_ACK();
ValueL = I2C_Read_Byte_ACK();
}
for(i=0x00;i<reg_addr;i++)
{
ValueH = I2C_Read_Byte_ACK();
ValueL = I2C_Read_Byte_ACK();
}
}
if(reg_addr>0x0a)
{
for(i=0x0a;i<reg_addr;i++)
{
ValueH = I2C_Read_Byte_ACK();
ValueL = I2C_Read_Byte_ACK();
}
}
ValueH = I2C_Read_Byte_ACK();
ValueL = I2C_Read_Byte_NAK();
I2C_Stop();
*ptrdata = (((int)ValueH) << 8) | ValueL;
}
void DabFM_Init(void)
{
int register_data;
DabFM_Write_Register(45,0x0020);
Dab_Write_Register(0x2000,0x1234);
Dab_Read_Register(0x2000,®ister_data);
DabFM_Read_Register(0x0a,®ister_data);
DabFM_Read_Register(0x0a,®ister_data);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -