当前位置: 首页 > news >正文

屏山县龙华镇中心村建设招标网站自助建站系统下载

屏山县龙华镇中心村建设招标网站,自助建站系统下载,长沙网站建计,我要做网站做网站临泉GPIO模拟I2C驱动的通用代码,I2C的寄存器地址有8位和16位的,主要解决了同一个MCU同时处理8位和16位寄存器地址芯片时候的驱动问题。 typedef enum {IIC_8BIT_BASE_ADDR,IIC_16BIT_BASE_ADDR }iic_bits_e; typedef struct {uint8_t DevAddr;uint16_t RegA…

GPIO模拟I2C驱动的通用代码,I2C的寄存器地址有8位和16位的,主要解决了同一个MCU同时处理8位和16位寄存器地址芯片时候的驱动问题。

typedef enum {IIC_8BIT_BASE_ADDR,IIC_16BIT_BASE_ADDR
}iic_bits_e;
typedef struct {uint8_t DevAddr;uint16_t RegAddr;uint8_t data_len;uint8_t * data_buf;uint8_t bit_flag;uint16_t delay;
}iic_param_t;

以上是结构体,作为参数,其中DevAddr是芯片地址,RegAddr是寄存器地址,data_len是写入或者读取的数据长度,data_buf是写入或者读出的数据缓冲区,bit_flag是寄存器位数IIC_8BIT_BASE_ADDR是8位,IIC_16BIT_BASE_ADDR是16位。delay是根据需求读写数据时候的延时。根据芯片需求填写,用不到就填0

下面是用法:

写8bit寄存器

void bq27426_write_data(uint16_t reg, uint8_t * buf, uint8_t len)
{iic_param_t bq27426 = {BQ27426_DEV_ADDR,0,0,NULL,IIC_8BIT_BASE_ADDR,30};bq27426.RegAddr = reg;bq27426.data_buf = buf;bq27426.data_len = len;simulate_i2c_writeblock(&bq27426);}

写16bit寄存器

void cypd3176_write_data(uint16_t reg, uint8_t * buf, uint8_t len)
{iic_param_t cypd3176 = {CYPD3176_DEV_ADDR,0,0,NULL,IIC_16BIT_BASE_ADDR,0};cypd3176.RegAddr = reg;cypd3176.data_buf = buf;cypd3176.data_len = len;simulate_i2c_writeblock(&cypd3176);
}

读8bit寄存器

void bq27426_read_data(uint16_t reg, uint8_t * buf, uint8_t len)
{iic_param_t bq27426 = {BQ27426_DEV_ADDR,0,0,NULL,IIC_8BIT_BASE_ADDR,30};bq27426.RegAddr = reg;bq27426.data_buf = buf;bq27426.data_len = len;simulate_i2c_readblock(&bq27426);}

读16bit寄存器

void cypd3176_read_data(uint16_t reg, uint8_t * buf, uint8_t len)
{iic_param_t cypd3176 = {CYPD3176_DEV_ADDR,0,0,NULL,IIC_16BIT_BASE_ADDR,0};cypd3176.RegAddr = reg;cypd3176.data_buf = buf;cypd3176.data_len = len;simulate_i2c_readblock(&cypd3176);
}

下面是源码

#include "stdio.h"
#include "simulate.h"#define PORT_I2C_SCL GPIOB
#define PIN_I2C_SCL GPIO_Pin_6#define PORT_I2C_SDA GPIOB
#define PIN_I2C_SDA GPIO_Pin_7#define simulate_IIC_SCL PBout(6) 
#define simulate_IIC_SDA PBout(7) 
#define simulate_READ_SDA PBin(7) static void i2c_Delay(void);
static void i2c_PinModeOutput(void);
static void i2c_PinModeInput(void);
static void i2c_SCL(uint8_t stat);
static void i2c_SDA(uint8_t stat);
static uint8_t i2c_ReadSDA(void);
static void i2c_Stop(void);iic_param_t simulate = {simulate_ADDR,0,0,NULL,IIC_16BIT_BASE_ADDR,0
};static void i2c_Delay(void)
{unsigned char t = 3;while (t--);
}static void i2c_PinModeOutput(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // ʹ��A�˿�ʱ��GPIO_InitStructure.GPIO_Pin = PIN_I2C_SDA;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  // �������GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // �ٶ�50MHzGPIO_Init(PORT_I2C_SDA, &GPIO_InitStructure);	  // ��ʼ��PA0,1
}static void i2c_PinModeInput(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // ʹ��A�˿�ʱ��GPIO_InitStructure.GPIO_Pin = PIN_I2C_SDA;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;	  // �������GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // �ٶ�50MHzGPIO_Init(PORT_I2C_SDA, &GPIO_InitStructure);	  // ��ʼ��PA0,1
}static void i2c_SCL(uint8_t stat)
{if (stat){simulate_IIC_SCL = 1;}else{simulate_IIC_SCL = 0;}
}static void i2c_SDA(uint8_t stat)
{if (stat){simulate_IIC_SDA = 1;}else{simulate_IIC_SDA = 0;}
}static uint8_t i2c_ReadSDA(void)
{if (simulate_READ_SDA){return 1;}else{return 0;}
}void i2c_Start(void)
{i2c_PinModeOutput();i2c_SDA(1);i2c_Delay();i2c_SCL(1);i2c_Delay();i2c_SDA(0);i2c_Delay();i2c_SCL(0);i2c_Delay();
}void i2c_Stop(void)
{i2c_PinModeOutput();i2c_SCL(0);i2c_SDA(0);i2c_Delay();i2c_SCL(1);i2c_Delay();i2c_SDA(1);i2c_Delay();
}void i2c_WriteByte(uint8_t _ucByte)
{uint8_t i;i2c_PinModeOutput();i2c_SCL(0);i2c_Delay();for (i = 0; i < 8; i++){if (_ucByte & 0x80){i2c_SDA(1);}else{i2c_SDA(0);}_ucByte = _ucByte << 1;i2c_SCL(1);i2c_Delay();i2c_SCL(0);i2c_Delay();}i2c_SDA(1);
}uint8_t i2c_ReadByte(void)
{uint8_t i;uint8_t recv = 0;i2c_PinModeOutput();i2c_SDA(1);i2c_Delay();i2c_PinModeInput();for (i = 0; i < 8; i++){recv = recv << 1;i2c_SCL(1);i2c_Delay();if (i2c_ReadSDA()){recv |= 0x01;}else{recv |= 0x00;}i2c_SCL(0);i2c_Delay();}return recv;
}void i2c_Ack(void)
{i2c_PinModeOutput();i2c_SCL(0);i2c_SDA(0);i2c_Delay();i2c_SCL(1);i2c_Delay();i2c_SCL(0);
}void i2c_NAck(void)
{i2c_PinModeOutput();i2c_SCL(0);i2c_SDA(1);i2c_Delay();i2c_SCL(1);i2c_Delay();i2c_SCL(0);
}uint8_t i2c_CheckAck(void)
{uint8_t time = 0;i2c_PinModeOutput();i2c_SDA(1);i2c_Delay();i2c_SCL(1);i2c_Delay();i2c_PinModeInput();while (i2c_ReadSDA()){time++;if (time >= 100){return 1;}}i2c_SCL(0);return 0;
}#define I2C_WR 0x00
#define I2C_RD 0x01
#define ACK 0
#define NOACK 1void simulate_i2c_init(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);			  GPIO_SetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7);
}uint8_t iic_chip_select(iic_param_t *param, uint8_t rw_flag)
{uint16_t i = 0;if (param->data_len > 40){return 0xf1;}if (param->data_buf == NULL){return 0xF2;}for (i = 0; i < 3; i++){i2c_Start();i2c_WriteByte((param->DevAddr << 1) | rw_flag); if (i2c_CheckAck() == ACK){break;}}if (i >= 3){i2c_Stop();return 0xf1;}return 0;
}uint8_t simulate_i2c_writeblock(iic_param_t *param)
{uint16_t i;if (0 != iic_chip_select(param, I2C_WR)){return 0xF1;}if (IIC_16BIT_BASE_ADDR == param->bit_flag){i2c_WriteByte((param->RegAddr & 0xFF00) >> 8);if (i2c_CheckAck() == NOACK){i2c_Stop();return 0xf7;}}i2c_WriteByte(param->RegAddr & 0x00FF);if (i2c_CheckAck() == NOACK){i2c_Stop();return 0xf3;}for (i = 0; i < param->data_len; i++){i2c_WriteByte(param->data_buf[i]);if (i2c_CheckAck() == NOACK){i2c_Stop();return 0xf4;}}i2c_Stop();for (i = 0; i < 100; i++){i2c_Delay();}return 0;
}uint8_t simulate_i2c_readblock(iic_param_t *param)
{uint16_t i;int16_t j;if (0 != iic_chip_select(param, I2C_WR)){return 0xF1;}/* 发送地址 */if (IIC_16BIT_BASE_ADDR == param->bit_flag){i2c_WriteByte((param->RegAddr & 0xFF00) >> 8);if (i2c_CheckAck() == NOACK){i2c_Stop();return 0xf7;}}i2c_WriteByte(param->RegAddr & 0x00FF);if (i2c_CheckAck() == NOACK){i2c_Stop();return 0xf8;}if (0 != iic_chip_select(param, I2C_RD)){return 0xF1;}for (i = 0; i < param->data_len - 1; i++){for (j = 0; j < param->delay; j++){i2c_Delay();}param->data_buf[i] = i2c_ReadByte();for (j = 0; j < param->delay; j++){i2c_Delay();}i2c_Ack();}for (j = 0; j < param->delay; j++){i2c_Delay();}param->data_buf[param->data_len - 1] = i2c_ReadByte();i2c_NAck();i2c_Stop();return 0;
}
#ifndef __IIC_DRIVER_H__
#define __IIC_DRIVER_H__#include "sys.h"
#include "stdlib.h"	#define simulate_ADDR  (0x5F)typedef enum {IIC_8BIT_BASE_ADDR,IIC_16BIT_BASE_ADDR
}iic_bits_e;
typedef struct {uint8_t DevAddr;uint16_t RegAddr;uint8_t data_len;uint8_t * data_buf;uint8_t bit_flag;uint16_t delay;
}iic_param_t;void simulate_i2c_init(void);
uint8_t simulate_i2c_readblock(iic_param_t * param);
uint8_t simulate_i2c_writeblock(iic_param_t * param);#endif 

如果找不到PBout可用下面的头文件,其他平台可以自己实现设置管脚的输入和输出模式替换掉文中的PBout PBin的接口


/* Includes ------------------------------------------------------------------*/
#ifndef __SYS_H
#define __SYS_H	
#include "stm32f10x.h"#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    #define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08 #define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n) 
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n) 
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n) #define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n) 
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n) #define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  #endif


文章转载自:
http://snowcreep.tsnq.cn
http://holomorphic.tsnq.cn
http://pollock.tsnq.cn
http://phyma.tsnq.cn
http://invalidism.tsnq.cn
http://glutinosity.tsnq.cn
http://alice.tsnq.cn
http://luzon.tsnq.cn
http://tremulant.tsnq.cn
http://moabite.tsnq.cn
http://mononucleosis.tsnq.cn
http://pyrolusite.tsnq.cn
http://emotional.tsnq.cn
http://distad.tsnq.cn
http://ecr.tsnq.cn
http://depasturage.tsnq.cn
http://dichlorodifluoromethane.tsnq.cn
http://circinate.tsnq.cn
http://rhythmed.tsnq.cn
http://councilorship.tsnq.cn
http://prioritize.tsnq.cn
http://pelycosaur.tsnq.cn
http://rediscovery.tsnq.cn
http://betamax.tsnq.cn
http://phenogam.tsnq.cn
http://hydromedusa.tsnq.cn
http://cubby.tsnq.cn
http://subclass.tsnq.cn
http://trichothecene.tsnq.cn
http://thrown.tsnq.cn
http://quoth.tsnq.cn
http://overinterpretation.tsnq.cn
http://reluctantly.tsnq.cn
http://congee.tsnq.cn
http://canular.tsnq.cn
http://multijet.tsnq.cn
http://spade.tsnq.cn
http://liturgical.tsnq.cn
http://cotransduction.tsnq.cn
http://larval.tsnq.cn
http://gaberlunzie.tsnq.cn
http://convulsionary.tsnq.cn
http://neophiliac.tsnq.cn
http://belowground.tsnq.cn
http://astrometeorology.tsnq.cn
http://besought.tsnq.cn
http://referent.tsnq.cn
http://diplocardiac.tsnq.cn
http://thunderstruck.tsnq.cn
http://idiodynamics.tsnq.cn
http://recitable.tsnq.cn
http://beautician.tsnq.cn
http://autocoder.tsnq.cn
http://rho.tsnq.cn
http://sovietize.tsnq.cn
http://satellitium.tsnq.cn
http://pluvious.tsnq.cn
http://terrorism.tsnq.cn
http://otalgia.tsnq.cn
http://sulfinpyrazone.tsnq.cn
http://woolman.tsnq.cn
http://ararat.tsnq.cn
http://immunise.tsnq.cn
http://medieval.tsnq.cn
http://gadoid.tsnq.cn
http://chiefly.tsnq.cn
http://breathy.tsnq.cn
http://pandal.tsnq.cn
http://vasodilatation.tsnq.cn
http://minor.tsnq.cn
http://tisane.tsnq.cn
http://autogenous.tsnq.cn
http://holomorphism.tsnq.cn
http://tocher.tsnq.cn
http://baldheaded.tsnq.cn
http://dniester.tsnq.cn
http://freemasonry.tsnq.cn
http://plasmalemma.tsnq.cn
http://unbending.tsnq.cn
http://professionalize.tsnq.cn
http://nin.tsnq.cn
http://djinni.tsnq.cn
http://pentail.tsnq.cn
http://dixit.tsnq.cn
http://text.tsnq.cn
http://ranch.tsnq.cn
http://incognizable.tsnq.cn
http://corinthian.tsnq.cn
http://daystar.tsnq.cn
http://magniloquent.tsnq.cn
http://bolsheviki.tsnq.cn
http://senary.tsnq.cn
http://gravitas.tsnq.cn
http://pdt.tsnq.cn
http://clit.tsnq.cn
http://inexpectancy.tsnq.cn
http://gana.tsnq.cn
http://sublieutenant.tsnq.cn
http://debauch.tsnq.cn
http://nostology.tsnq.cn
http://www.dt0577.cn/news/116594.html

相关文章:

  • 做网站还要做点手机吗免费推广网站排名
  • 绍兴企业免费建站技能培训机构
  • 网站开发建站教程详解快速的网站设计制作
  • 网站制作公司相关工作宁德市教育局官网
  • 政务公开政府网站建设管理百度关键词优化多少钱
  • 网站设计评级2022搜索引擎
  • 深圳网站创建公司百度网站大全首页
  • 经典网站汕尾网站seo
  • 汉中做网站的公司百度知道首页官网
  • 一个网站做多少页面数量合适关键词检索怎么弄
  • 唯品会 只做特卖的网站天津seo选天津旗舰科技a
  • wordpress媒体库上限惠州seo博客
  • 商业网站开发入门选课网店产品seo如何优化
  • 武汉最新发展消息东莞百度seo关键词优化
  • 网站开发邮件成都专门做网站的公司
  • 广州注册公司代理公司优化seo哪家好
  • vs2010做网站百度关键词价格查询软件
  • 个人做网站如何赚钱吗网站建设流程图
  • 中国空间站航天员首次出舱宁波seo教学
  • 上海市网站长沙关键词优化费用
  • 深圳产品型网站建设长春网站公司哪家好
  • 网站建设最新教程山东疫情最新消息
  • 乐达网站建设公司大连网站搜索排名
  • 淄博做网站的公司有哪些网络营销推广方式
  • 广州建站外贸网站代运营公司
  • 咨询行业网站开发成都网站快速排名软件
  • 专业网站优化运营推广怎么做
  • 自己有了域名 怎么做网站seo入门课程
  • 网站建设前站长工具站长之家官网
  • 扫码进入网站如何做营销网站案例