能读出数据,但是数据会大范围跳变,在给的历程上做了改变,下面贴出代码,请教各位大神
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "MLX90614.h"
#include "key.h"
int main(void)
{
u16 OV=0;
double Temperature=0;
u8 str[20];
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LED_Init(); //初始化LED
LCD_Init(); //LCD初始化
MLX90614_Init(); //初始化I2C
MLX90614_PwmToSMBus(); //MLX90614切换到I2C模式
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"IIC TEST");
while(1)
{
OV=MLX90614_ReadOneByte(0x00);
Temperature=OV*0.02-273.15;
sprintf(str,"%6.2f",Temperature);
LCD_ShowString(100,110,200,12,24,str);
delay_ms(500);
}
}
#include "myiic.h"
#include "delay.h"
//初始化IIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB时钟
//GPIOB8,B9初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
IIC_SCL=1;
IIC_SDA=1;
}
//产生IIC起始信号
void IIC_Start(void)
{
SDA_OUT(); //sda线输出
IIC_SDA=1;
delay_us(4);
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总线,准备发送或接收数据
delay_us(4);
}
//产生IIC停止信号
void IIC_Stop(void)
{
SDA_OUT();//sda线输出
IIC_SCL=0;
delay_us(4);
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(4);
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(5);
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(1);
}
if (!ack)
IIC_NAck();//发送nACK
else
IIC_Ack(); //发送ACK
return receive;
}
#include "MLX90614.h"
#include "delay.h"
//初始化IIC接口
void MLX90614_Init(void)
{
IIC_Init();//IIC初始化
}
//在MLX90614指定地址读出一个数据
//ReadAddr:开始读数的地址
//返回值 :读到的数据
u16 MLX90614_ReadOneByte(u8 ReadAddr)
{
u16 data; // Data storage (DataH
ataL)
u8 Pec; // PEC byte storage
u16 DataL=0; // Low data byte storage
u16 DataH=0; // High data byte storage
// u8 arr[6]; // Buffer for the sent bytes
// u8 PecReg; // Calculated PEC byte storage
IIC_Start();
IIC_Send_Byte(ReadAddr); //发送器件地址0XB4,写数据
if(!IIC_Wait_Ack())
IIC_Send_Byte(0x07); //发送Command 0X07
else
return 90;
if(!IIC_Wait_Ack())
IIC_Start(); //重开始
else
return 91;
IIC_Send_Byte(ReadAddr+1); //写器件地址(读)
if(!IIC_Wait_Ack())
DataL=IIC_Read_Byte(1); //读低8位
else
return 92;
DataH=IIC_Read_Byte(1); //读高8位
Pec=IIC_Read_Byte(0); //读PEC
IIC_Stop();//产生一个停止条件
// arr[5] = ReadAddr; //
// arr[4] = 0x07; //
// arr[3] = ReadAddr+1; //Load array arr
// arr[2] = DataL; //
// arr[1] = DataH; //
// arr[0] = 0; //
// PecReg=PEC_Calculation(arr);//Calculate CRC
//
// while(PecReg != Pec); //If received and calculated CRC are equal go out from do-while{}
data = (DataH<<8) | DataL; //data=DataH
ataL
return data;
}
void MLX90614_PwmToSMBus(void)
{
IIC_SCL=0;
delay_ms(5); //大于1.44ms
IIC_SCL=1;
}
//u8 PEC_Calculation(u8 pec[])
//{
// u8 crc[6];
// u8 BitPosition=47;
// u8 shift;
// u8 i;
// u8 j;
// u8 temp;
// do
// {
// /*Load pattern value 0x000000000107*/
// crc[5]=0;
// crc[4]=0;
// crc[3]=0;
// crc[2]=0;
// crc[1]=0x01;
// crc[0]=0x07;
// /*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
// BitPosition=47;
// /*Set shift position at 0*/
// shift=0;
// /*Find first "1" in the transmited message beginning from the MSByte byte5*/
// i=5;
// j=0;
// while((pec[i]&(0x80>>j))==0 && i>0)
// {
// BitPosition--;
// if(j<7)
// {
// j++;
// }
// else
// {
// j=0x00;
// i--;
// }
// }/*End of while */
// /*Get shift value for pattern value*/
// shift=BitPosition-8;
// /*Shift pattern value */
// while(shift)
// {
// for(i=5; i<0xFF; i--)
// {
// if((crc[i-1]&0x80) && (i>0))
// {
// temp=1;
// }
// else
// {
// temp=0;
// }
// crc[i]<<=1;
// crc[i]+=temp;
// }/*End of for*/
// shift--;
// }/*End of while*/
// /*Exclusive OR between pec and crc*/
// for(i=0; i<=5; i++)
// {
// pec[i] ^=crc[i];
// }/*End of for*/
// }
// while(BitPosition>8); /*End of do-while*/
// return pec[0];
//}
一周热门 更多>