📄 bq_smbus.c
字号:
/* I2C test program that writes & reads data to an I2C EEPROM device. */
/*
此程序用于SMBUS通讯测试,配置可参考I2C.H
软件I2C通讯,使用RA1==>SDA,RA0==>SCL
单片机使用PIC16F716
晶振4M
*/
#include <pic.h>
#include "delay.h"
#include "i2c.h"
#include "delay.c"
#include "i2c.c"
#include "i2c_2.c"
#define ROM1 0x16 /* smbus */
#define ROM 0xA0 /* I2C EEPROM */
#define XTAL_FREQ 4MHZ
#define I2C_MODULE 0
uchar code1[]={0x06,0x06,0x5a,0xff,0x80,0x00,0x00,0x00,0xff};
uchar code2[]={0x5a,0xff,0x5a,0xff};
uchar code3[]={0x80,0x00,0x5a,0xff};
uchar code4[]={0x00,0x00,0x5a,0xff};
void flashled(void) { /* flash a led on RB0 */
while(1) {
RB0 = 1;
DelayMs(200);
RB0 = 0;
DelayMs(200);
}
}
void WriteByte(unsigned char addr, unsigned char byte) {
i2c_WriteTo(ROM);
if (i2c_PutByte(addr)==I2C_ERROR)
flashled();
if (i2c_PutByte(byte)==I2C_ERROR)
flashled();
}
/*================================================================
读取BQ寄存器里面数据函数-word
函数原型: unsigned int ReadByte_smbus(unsigned char addr),读取指定地址BQ
器件里数据。器件的ROM1码已经设定
返回读取的数据表示操作成功,否则操作有误。
=================================================================*/
unsigned int ReadByte_smbus(unsigned char addr) {
unsigned int data=0;
unsigned char data1,data2;
i2c_WriteTo(ROM1); //i2c_Open((address), I2C_read); i2c_read=0;
if (i2c_PutByte(addr)==I2C_ERROR) //send adress and sendack
flashled();
i2c_ReadFrom(ROM1); //i2c_Open((address), I2C_WRITE) i2c_write=1;
data1=i2c_GetByte(I2C_MORE); //i2c_more==>true sendack: more byte to send
data2=i2c_GetByte(I2C_LAST); //i2c_last==>false nosendack:no more byte to send
data|=data1;
data<<=8;
data|=data2;
return(data);
}
/*================================================================
写BQ寄存器里面数据函数-
函数原型
=================================================================*/
void write_word_smbus(unsigned char addr,unsigned char datal,unsigned char datah) {
unsigned int data=0;
unsigned char data1,data2;
i2c_WriteTo(ROM1); //i2c_Open((address), I2C_WRITE) i2c_write=1;
if (i2c_PutByte(addr)==I2C_ERROR) //send adress and sendack
flashled();
if (i2c_PutByte(datal)==I2C_ERROR) //send adress and sendack
flashled();
if (i2c_PutByte(datah)==I2C_ERROR) //send adress and sendack
flashled();
i2c_Stop();
}
/*================================================================
写i2c寄存器里面数据函数-
函数原型
=================================================================*/
void write_word_i2c(unsigned char addr,unsigned char datal,unsigned char datah) {
unsigned int data=0;
unsigned char data1,data2;
i2c_WriteTo(ROM); //i2c_Open((address), I2C_WRITE) i2c_write=1;
if (i2c_PutByte(addr)==I2C_ERROR) //send adress and sendack
flashled();
if (i2c_PutByte(datal)==I2C_ERROR) //send adress and sendack
flashled();
if (i2c_PutByte(datah)==I2C_ERROR) //send adress and sendack
flashled();
i2c_Stop();
}
////////********************************//////////////
int ReadWord(uchar addr){
uchar data1,data2;
int data;
start_i2c();
send_byte(0x16); //发送器件地址,即DEVICE ADDRESS。
if (ack==0) i2c_error(); //如果24LC02无应答。则进入I2C ERROR错误指示。
send_byte(addr); //发送字地址,即WORD ADDRESS。D口显示数组。
if (ack==0) i2c_error();
start_i2c(); //重新启动总线。
send_byte(0x17); //发送读命令和器件地址DEVICE ADDRESS。
if (ack==0) i2c_error();
data2=receive_byte();
i2c_SendAcknowledge(1);
data1=receive_byte();
stop_i2c();
//data<<=8;
//data|=data1;
return(data1);
}
//////////////////////////////////////////////
static int remain,remain1;
void main(void) {
unsigned char count,val;
//const unsigned char *add;
//add=0x20;
//TRISB=0; /* use a led on RB0 - set as output */
//PORTB=0;
//RB0=0;
PORTA=0;
ADCON1=0X06;
TRISA=0x0; /* use a led on RB0 - set as output */
RA2=0;
remain=0x0;
/* initialize i2c */
/*
#ifdef I2C_MODULE
//SSPMode(MASTER_MODE);
//SSPEN = 1;
CKP = 1;
#else
SCL_DIR = I2C_OUTPUT;
SDA_DIR = I2C_OUTPUT;
SDA = 0;
SCL = 0;
#endif
*/
SCL_DIR = I2C_OUTPUT;
SDA_DIR = I2C_OUTPUT;
SDA = 0;
SCL = 0;
while(1) {
RA2=0;
remain=0;
remain1=0;
count=0x1;
DelayMs(200);
remain1=ReadByte_smbus(0x02); //测试OK,能正确读取BQ器件的数据
DelayMs(20);
// I_send_word(0x16,0x4f,0x5a,0xff);
// I_send_word(0x16,0x4f,0x80,0x00);
// I_send_word(0x16,0x4f,0x00,0x00);
// I_send_word(0x16,0x00,0x06,0x06);
// I_send_str(0x16,0x0,code1,2);
write_word_smbus(0x0,0x06,0x06);
DelayMs(10);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
write_word_i2c(0x02,0x6d,0x29);
write_word_i2c(0x03,0x75,0xaf);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(200);
DelayMs(20);
remain1=ReadByte_smbus(0x02); //测试OK,能正确读取BQ器件的数据
remain1=ReadByte_EE(0x4); //测试OK,能正确读取BQ器件的数据
remain1=ReadByte_EE(0x5);
remain1=ReadByte_EE(0x6);
remain1=ReadByte_EE(0x7);
remain1=ReadByte_EE(0x8);
I_send_str(0xa0,0x0,code,5); //测试OK,能正确发送数据给BQ器件
DelayMs(20);
I_send_word(0x16,0x4f,0x5a,0xff);
I_send_word(0x16,0x4f,0x80,0x00);
I_send_word(0x16,0x4f,0x00,0x00);
DelayMs(20);
remain1=ReadByte_smbus(0x15); //OK
remain1=ReadByte_smbus(0x15); //OK
DelayMs(200);
/*===================以下直接写EEPROM
RA2=1;
I_send_str(0xa0,0x00,code,5); //OK
remain=0;
remain1=0;
count=0x1;
DelayMs(200);
remain1=ReadByte_EE(0x4); //OK
DelayMs(200);
==================================*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -