微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32之i2C硬件驱动ht16c22

stm32之i2C硬件驱动ht16c22

时间:12-03 来源:互联网 点击:
使用硬件i2c,驱动ht16c22,程序不稳定是肯定的,还是贴出来吧,项目中最好不要用。

文件i2c.h内容:

#ifndef _STM32F103_I2C_H_
#define _STM32F103_I2C_H_
#include "stm32f10x.h"
#define SlaveAddress 0x7e //0x7e
//#define ModeSet 0b10001100 //80Hz,turn on sys and LCD bias,1/3 bias
#define ModeSet 0x8C //80Hz,turn on sys and LCD bias,1/3 bias
//#define VlcdAdjust 0b01111111 //Vlcd=0.6VDD,Seg Pin,enable internal voltage
#define VlcdAdjust 0x70 //0x7F --Vlcd=0.6VDD,Seg Pin,enable internal voltage
//#define Blink 0b11000000 //关闭闪烁
#define Blink 0xC0 //关闭闪烁

void LCD_I2C_config(void);
void I2C_Write(unsigned int device_id, unsigned int register_id, unsigned int data);
unsigned char I2C_Read(unsigned int device_id, unsigned int register_id);

void ht16c22_init(void );

unsigned char I2C_ht16c22_Read(unsigned int pBuffer, unsigned int ReadAddr);
void I2C_ht16c22_Write(unsigned int device_id, unsigned int register_id);

#endif

i2c.c文件内容为:

#include "xx.h"

static void I2C_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);

/*config I2C2 SCL PB10*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);
/*config I2C2 SDA PB11 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void LCD_I2C_config(void)
{
I2C_InitTypeDef I2C_InitStructure;
I2C_Config();
I2C_DeInit(I2C2);

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9;//I2C_DutyCycle_2//I2C_DutyCycle_16_9
I2C_InitStructure.I2C_OwnAddress1 = 0x0A; //0x0A//0xA0
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_ClockSpeed = 10000;//10KHZ的速率
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

I2C_Init(I2C2, &I2C_InitStructure);

I2C_Cmd(I2C2, ENABLE);
}

int I2C_FLAG_BUSY_write = 0;
int I2C_EVENT_MASTER_MODE_SELECT_write = 0 ;
int I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED_write = 0 ;
int I2C_EVENT_MASTER_BYTE_TRANSMITTED_write = 0 ;

void I2C_Write(unsigned int device_id, unsigned int register_id, unsigned int data)
{
int i = 0;
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_GenerateSTART(I2C2, ENABLE);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_Send7bitAddress(I2C2, device_id, I2C_Direction_Transmitter);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_SendData(I2C2, register_id);

while(! I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_SendData(I2C2, data);

while(! I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_GenerateSTOP(I2C2, ENABLE);
}

void ht16c22_init()
{
LCD_I2C_config();
I2C_ht16c22_Write(SlaveAddress,ModeSet);
I2C_ht16c22_Write(SlaveAddress,VlcdAdjust);
I2C_ht16c22_Write(SlaveAddress,Blink);

I2C_Write(SlaveAddress,0x00,0x00);
I2C_Write(SlaveAddress,0x01,0x00);
I2C_Write(SlaveAddress,0x02,0x00);
I2C_Write(SlaveAddress,0x03,0x00);
I2C_Write(SlaveAddress,0x04,0x00);
I2C_Write(SlaveAddress,0x05,0x00);
I2C_Write(SlaveAddress,0x06,0x00);
I2C_Write(SlaveAddress,0x07,0x00);
I2C_Write(SlaveAddress,0x08,0x00);
I2C_Write(SlaveAddress,0x09,0x00);
I2C_Write(SlaveAddress,0x0A,0x00);
I2C_Write(SlaveAddress,0x0B,0x00);
I2C_Write(SlaveAddress,0x0C,0x00);
I2C_Write(SlaveAddress,0x0D,0x00);
I2C_Write(SlaveAddress,0x0E,0x00);
I2C_Write(SlaveAddress,0x0F,0x00);
I2C_Write(SlaveAddress,0x10,0x00);
I2C_Write(SlaveAddress,0x11,0x00);
I2C_Write(SlaveAddress,0x12,0x00);
I2C_Write(SlaveAddress,0x13,0x00);
I2C_Write(SlaveAddress,0x14,0x00);
I2C_Write(SlaveAddress,0x15,0x00);
I2C_Write(SlaveAddress,0x16,0x00);
I2C_Write(SlaveAddress,0x17,0x00);
I2C_Write(SlaveAddress,0x17,0x00);
I2C_Write(SlaveAddress,0x18,0x00);
I2C_Write(SlaveAddress,0x19,0x00);
I2C_Write(SlaveAddress,0x1A,0x00);
I2C_Write(SlaveAddress,0x1B,0x00);
I2C_Write(SlaveAddress,0x1C,0x00);
I2C_Write(SlaveAddress,0x1D,0x00);
I2C_Write(SlaveAddress,0x1E,0x00);
I2C_Write(SlaveAddress,0x1F,0x00);
I2C_Write(SlaveAddress,0x20,0x00);
I2C_Write(SlaveAddress,0x21,0x00);

// I2C_Write(SlaveAddress,0x00,0xFF);
// I2C_Write(SlaveAddress,0x01,0xFF);
// I2C_Write(SlaveAddress,0x02,0xFF);
// I2C_Write(SlaveAddress,0x03,0xFF);
// I2C_Write(SlaveAddress,0x04,0xFF);
// I2C_Write(SlaveAddress,0x05,0xFF);
// I2C_Write(SlaveAddress,0x06,0xFF);
// I2C_Write(SlaveAddress,0x07,0xFF);
// I2C_Write(SlaveAddress,0x08,0xFF);
// I2C_Write(SlaveAddress,0x09,0xFF);
// I2C_Write(SlaveAddress,0x0A,0xFF);
// I2C_Write(SlaveAddress,0x0B,0xFF);
// I2C_Write(SlaveAddress,0x0C,0xFF);
// I2C_Write(SlaveAddress,0x0D,0xFF);
// I2C_Write(SlaveAddress,0x0E,0xFF);
// I2C_Write(SlaveAddress,0x0F,0xFF);
// I2C_Write(SlaveAddress,0x10,0xFF);
// I2C_Write(SlaveAddress,0x11,0xFF);
// I2C_Write(SlaveAddress,0x12,0xFF);
// I2C_Write(SlaveAddress,0x13,0xFF);
// I2C_Write(SlaveAddress,0x14,0xFF);
// I2C_Write(SlaveAddress,0x15,0xFF);
// I2C_Write(SlaveAddress,0x16,0xFF);
// I2C_Write(SlaveAddress,0x17,0xFF);
// I2C_Write(SlaveAddress,0x17,0xFF);
// I2C_Write(SlaveAddress,0x18,0xFF);
// I2C_Write(SlaveAddress,0x19,0xFF);
// I2C_Write(SlaveAddress,0x1A,0xFF);
// I2C_Write(SlaveAddress,0x1B,0xFF);
// I2C_Write(SlaveAddress,0x1C,0x00);
// I2C_Write(SlaveAddress,0x1D,0x00);
// I2C_Write(SlaveAddress,0x1E,0x00);
// I2C_Write(SlaveAddress,0x1F,0x00);
// I2C_Write(SlaveAddress,0x20,0x00);
// I2C_Write(SlaveAddress,0x21,0x00
// I2C_Write(SlaveAddress,0x05,0x0F);
// temp = I2C_ht16c22_Read(SlaveAddress,0x05);
// I2C_Write(SlaveAddress,0x05,(0x10) | temp);

// temp = I2C_ht16c22_Read(SlaveAddress,0x05);
// printf("temp = %d\r\n" ,temp);
// I2C_Write(SlaveAddress,0x05,0xE0 | temp);
//
// temp = I2C_ht16c22_Read(SlaveAddress,0x06);
// I2C_Write(SlaveAddress,0x06,0x0F | temp);

// I2C_ht16c22_Write(SlaveAddress,0x80,0x03,0xff);

// I2C_Write(SlaveAddress,0x0A,0xFF);
// I2C_Write(SlaveAddress,0x00,0xFF);
// I2C_Write(SlaveAddress,0x01,0xFF);
// I2C_Write(SlaveAddress,0x02,0xFF);
//
// I2C_Write(SlaveAddress,0x03,0xFF);
// I2C_Write(SlaveAddress,0x04,0xFF);
// I2C_Write(SlaveAddress,0x05,0xFF);
// I2C_Write(SlaveAddress,0x06,0xFF);
//
// I2C_Write(SlaveAddress,0x07,0xFF);
// I2C_Write(SlaveAddress,0x08,0xFF);
// I2C_Write(SlaveAddress,0x09,0xFF);
//
// I2C_Write(SlaveAddress,0x0A,0xFF);
// I2C_Write(SlaveAddress,0x0B,0xFF);
// I2C_Write(SlaveAddress,0x0C,0xFF);
// I2C_Write(SlaveAddress,0x0D,0xFF);
//
//
// I2C_Write(SlaveAddress,0x0E,0xFF);
// I2C_Write(SlaveAddress,0x0F,0xFF);
// I2C_Write(SlaveAddress,0x10,0xFF);
// I2C_Write(SlaveAddress,0x11,0xFF);
//
// I2C_Write(SlaveAddress,0x12,0xFF);
// I2C_Write(SlaveAddress,0x13,0xFF);
// I2C_Write(SlaveAddress,0x14,0xFF);
// I2C_Write(SlaveAddress,0x15,0xFF);

}

void I2C_ht16c22_Write(unsigned int device_id, unsigned int register_id)
{

while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)); // Added by Najoua 27/08/2008

I2C_GenerateSTART(I2C2, ENABLE);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

I2C_Send7bitAddress(I2C2, device_id, I2C_Direction_Transmitter);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

I2C_SendData(I2C2, register_id);

while(! I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
I2C_GenerateSTOP(I2C2, ENABLE);
}

int I2C_FLAG_BUSY_read = 0;
int I2C_EVENT_MASTER_MODE_SELECT_read_1 = 0 ;
int I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED_read_1 = 0 ;
int I2C_EVENT_MASTER_BYTE_TRANSMITTED_read = 0 ;
int I2C_EVENT_MASTER_MODE_SELECT_read_2 = 0 ;
int I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED_read_2 = 0 ;
int I2C_EVENT_MASTER_BYTE_RECEIVED_read = 0;

unsigned char I2C_ht16c22_Read(unsigned int pBuffer, unsigned int ReadAddr)
{
unsigned char temp;
int i = 0;

while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}
I2C_GenerateSTART(I2C2, ENABLE);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_Send7bitAddress(I2C2, pBuffer, I2C_Direction_Transmitter);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_Cmd(I2C2, ENABLE);

I2C_SendData(I2C2, ReadAddr);

/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_GenerateSTART(I2C2, ENABLE);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}

I2C_Send7bitAddress(I2C2, pBuffer, I2C_Direction_Receiver);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
;
}

I2C_AcknowledgeConfig(I2C2, DISABLE);
I2C_GenerateSTOP(I2C2, ENABLE);
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)))
{
if(i == 65536)
{
debug("----i2c dead--------\r\n");
i = 0;
break;
}
;
}
temp = I2C_ReceiveData(I2C2);
I2C_AcknowledgeConfig(I2C2, ENABLE);

return temp;

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top