MAX44009 driver for STM32 use software IIC
当前程序运行环境:STM32F103C8T6
IIC IO端口:PB10,PB11
ADDR端口连接至GND
IIC驱动文件:myiic.c
#include "myiic.h"
#include "delay.h"
//初始化IIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能GPIOB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
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_10 | GPIO_Pin_11); //PB6,PB7 输出高,空闲状态
}
//产生IIC起始信号
void IIC_Start(void)
{
SDA_OUT(); //sda线输出
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
}
void IIC_Stop(void)
{
SDA_OUT();//sda线输出
IIC_SDA=0;
IIC_SCL=1;
delay_us(4);
IIC_SDA=1;//发送I2C总线结束信号
delay_us(4);
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA设置为输入
IIC_SDA=1;delay_us(1);
IIC_SCL=1;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0;//时钟输出0
return 0;
}
//产生ACK应答
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//不产生ACK应答
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;//拉低时钟开始数据传输
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(2); //对TEA5767这三个延时都是必须的
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
delay_us(2);
}
}
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(2);
}
if (!ack)
IIC_NAck();//发送nACK
else
IIC_Ack(); //发送ACK
return receive;
}
IIC驱动文件:myiic.h
#ifndef __MYIIC_H
#define __MYIIC_H
#include "sys.h"
//IO方向设置
#define SDA_IN() {GPIOB->CRH &= 0xFFFF0FFF;GPIOB->CRH |= 0x00008000;} //PB11输入模式
#define SDA_OUT() {GPIOB->CRH &= 0xFFFF0FFF;GPIOB->CRH |= 0x00003000;} //PB11输出模式
//IO操作函数
#define IIC_SCL PBout(10) //SCL
#define IIC_SDA PBout(11) //SDA
#define READ_SDA PBin(11) //输入SDA
//IIC所有操作函数
void IIC_Init(void); //初始化IIC的IO口
void IIC_Start(void); //发送IIC开始信号
void IIC_Stop(void); //发送IIC停止信号
void IIC_Send_Byte(u8 txd); //IIC发送一个字节
u8 IIC_Read_Byte(unsigned char ack);//IIC读取一个字节
u8 IIC_Wait_Ack(void); //IIC等待ACK信号
void IIC_Ack(void); //IIC发送ACK信号
void IIC_NAck(void); //IIC不发送ACK信号
void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data);
u8 IIC_Read_One_Byte(u8 daddr,u8 addr);
#endif
MAX44009驱动文件:MAX44009.c
#include "MAX44009.h"
#include "myiic.h"
#include "delay.h"
void MAX44009_Init(void)
{
IIC_Init(); //IIC初始化
MAX44009_WriteOneByte(MAX44009_REG_CFG, 0x03); // Default mode: measurements performed every 800ms
// Auto range: CDR and integration time are automatically determined by
// the internal autoranging circuitry of the IC
MAX44009_WriteOneByte(MAX44009_REG_INTE, MAX44009_INT_DISABLE);//关闭中断
MAX44009_WriteOneByte(MAX44009_REG_THT, MAX44009_IT_800ms);//积分时间800ms
MAX44009_WriteOneByte(MAX44009_REG_THU, 0xff);
MAX44009_WriteOneByte(MAX44009_REG_THL, 0x00);
MAX44009_WriteOneByte(0x02,0x80);//连续转换、手动模式、积分时间6.25MS
}
//在MAX44009指定地址读出一个数据
//ReadAddr:开始读数的地址
//返回值 :读到的数据
u8 MAX44009_ReadOneByte(u8 ReadAddr)
{
u8 temp = 0;
IIC_Start();
IIC_Send_Byte(MAX44009_ADDR);
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(MAX44009_ADDR+1); //进入接收模式
IIC_Wait_Ack();
temp = IIC_Read_Byte(0);
IIC_Stop(); //产生一个停止条件
return temp;
}
//在MAX44009指定地址写入一个数据
//WriteAddr :写入数据的目的地址
//DataToWrite:要写入的数据
void MAX44009_WriteOneByte(u8 WriteAddr, u8 DataToWrite)
{
IIC_Start();
IIC_Send_Byte(MAX44009_ADDR);
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr);
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite); //发送字节
IIC_Wait_Ack();
IIC_Stop(); //产生一个停止条件
}
//在MAX44009里面读取流明值
float MAX44009_ReadLux(void)
{
u8 exponent=0, mantissa=0;
u8 High_Lux = 0, Low_Lux = 0;
float result=0;
High_Lux=MAX44009_ReadOneByte(0x03);
Low_Lux=MAX44009_ReadOneByte(0x04);
exponent = (High_Lux & 0xF0) >> 4;
mantissa = (High_Lux & 0x0F) << 4;
mantissa += Low_Lux & 0x0F;
result=(float)(((0x00000001 << exponent) * (float)mantissa) * 0.045);
return result;
}
//在MAX44009里面的指定地址开始写入长度为Len的数据
//该函数用于写入16bit或者32bit的数据.
//WriteAddr :开始写入的地址
//DataToWrite:数据数组首地址
//Len :要写入数据的长度2,4
void MAX44009_WriteLenByte(u16 WriteAddr, u32 DataToWrite, u8 Len)
{
u8 t;
for (t = 0; t < Len; t++)
{
MAX44009_WriteOneByte(WriteAddr + t, (DataToWrite >> (8 * t)) & 0xff);
}
}
//在MAX44009里面的指定地址开始读出长度为Len的数据
//该函数用于读出16bit或者32bit的数据.
//ReadAddr :开始读出的地址
//返回值 :数据
//Len :要读出数据的长度2,4
u32 MAX44009_ReadLenByte(u16 ReadAddr, u8 Len)
{
u8 t;
u32 temp = 0;
for (t = 0; t < Len; t++)
{
temp <<= 8;
temp += MAX44009_ReadOneByte(ReadAddr + Len - t - 1);
}
return temp;
}
//在MAX44009里面的指定地址开始读出指定个数的数据
//ReadAddr :开始读出的地址
//pBuffer :数据数组首地址
//NumToRead:要读出数据的个数
void MAX44009_Read(u16 ReadAddr, u8 *pBuffer, u16 NumToRead)
{
while (NumToRead)
{
*pBuffer++ = MAX44009_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
//在MAX44009里面的指定地址开始写入指定个数的数据
//WriteAddr :开始写入的地址
//pBuffer :数据数组首地址
//NumToWrite:要写入数据的个数
void MAX44009_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite)
{
while (NumToWrite--)
{
MAX44009_WriteOneByte(WriteAddr, *pBuffer);
WriteAddr++;
pBuffer++;
}
}
MAX44009驱动文件:MAX44009.h
#ifndef __MAX44009_H
#define __MAX44009_H
#include "sys.h"
// Accuracy of the Lux value calculation
// 0: reduced accuracy (slightly faster)
// 1: maximum accuracy (slightly slower)
#define MAX44009_ACCURACY 1
// MAX44009 HAL
#define MAX44009_I2C_PORT I2C1 // I2C port where the MAX44009 connected
// Possible I2C device addresses
#define MAX44009_ADDR_G ((uint8_t)0x4A) // A0 pin tied to GND //0100 1010
#define MAX44009_ADDR_V ((uint8_t)0x4B) // A0 pin tied to VCC //0100 1011
// Current I2C device address (depends on the pin A0 connection)
#define MAX44009_ADDR (MAX44009_ADDR_G << 1)
//#define MAX44009_ADDR (0x94)
// MAX44009 register definitions
#define MAX44009_REG_INTS ((uint8_t)0x00) // Interrupt status
#define MAX44009_REG_INTE ((uint8_t)0x01) // Interrupt enable
#define MAX44009_REG_CFG ((uint8_t)0x02) // Configuration
#define MAX44009_REG_LUXH ((uint8_t)0x03) // Lux reading high byte
#define MAX44009_REG_LUXL ((uint8_t)0x04) // Lux reading low byte
#define MAX44009_REG_THU ((uint8_t)0x05) // Upper threshold high byte
#define MAX44009_REG_THL ((uint8_t)0x06) // Lower threshold high byte
#define MAX44009_REG_THT ((uint8_t)0x07) // Threshold timer
// MAX44009 interrupt status end configuration definitions
#define MAX44009_INT_SET ((uint8_t)0x01) // Interrupt is asserted
#define MAX44009_INT_RESET ((uint8_t)0x00) // Interrupt is deasserted
#define MAX44009_INT_ENABLE ((uint8_t)0x01) // Interrupt enabled
#define MAX44009_INT_DISABLE ((uint8_t)0x00) // Interrupt disabled
// MAX44009 CFG register bits
#define MAX44009_CFG_CONT ((uint8_t)0x80) // Continuous mode
#define MAX44009_CFG_MANUAL ((uint8_t)0x40) // Manual configuration
// Current division ratio (CDR)
#define MAX44009_CDR_NORM ((uint8_t)0x00) // All photodiode current goes to the ADC
#define MAX44009_CDR_DIV8 ((uint8_t)0x08) // Only 1/8 of the photodiode current goes to the ADC
// Integration time
#define MAX44009_IT_800ms ((uint8_t)0x00) // 800ms
#define MAX44009_IT_400ms ((uint8_t)0x01) // 400ms
#define MAX44009_IT_200ms ((uint8_t)0x02) // 200ms
#define MAX44009_IT_100ms ((uint8_t)0x03) // 100ms
#define MAX44009_IT_50ms ((uint8_t)0x04) // 50ms
#define MAX44009_IT_25ms ((uint8_t)0x05) // 25ms
#define MAX44009_IT_12d5ms ((uint8_t)0x06) // 12.5ms
#define MAX44009_IT_6d25ms ((uint8_t)0x07) // 6.25ms
// Bit mask for MODE and RANGE bits
#define MAX44009_MASK_CFG_MODE ((uint8_t)0xC0)
// Bit mask for CDR bit and integration time
#define MAX44009_MASK_CDR_IT ((uint8_t)0x0F)
// Value indicating overrange condition (used in return of GetLux() function)
#define MAX44009_OVERRANGE ((uint32_t)1000000000U)
void MAX44009_Init(void);
u8 MAX44009_ReadOneByte(u8 ReadAddr);
void MAX44009_WriteOneByte(u8 WriteAddr,u8 DataToWrite);
void MAX44009_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len);
u32 MAX44009_ReadLenByte(u16 ReadAddr,u8 Len);
void MAX44009_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead);
void MAX44009_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite);
float MAX44009_ReadLux(void);
#endif
本网站文章版权均为本人所有,未经同意不得私自搬运复制,欢迎注明引用出处的合理转载,图片转载请留言。文章内容仅用于技术研究和探索,不得用于违法目的。