tmp275 地址是0x98,
#include "iic.h"
void IIC_Delay(void)
{
delay_us(2);
}
void IIC_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE );
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_SetBits(GPIOA,GPIO_Pin_6|GPIO_Pin_7);
}
void IIC_Start()
{
SDA_OUT();
IIC_SCL = 1;
IIC_SDA = 1;
IIC_Delay();
IIC_SDA = 0;
IIC_Delay();
IIC_SCL = 0;
}
void IIC_Stop()
{
SDA_OUT();
IIC_SCL = 0;
IIC_SDA = 0;
IIC_Delay();
IIC_SCL = 1;
IIC_SDA = 1;
IIC_Delay();
}
u8 IIC_Wait_Ack()
{
uint8_t Errtime;
SDA_IN();
IIC_SDA = 1;IIC_Delay(); //êí·Åêy¾Y×üÏß
IIC_SCL = 1;IIC_Delay(); //êí·ÅμØÖ·×üÏß
while(IIC_SDA)
{
Errtime++;
if(Errtime>200)
{
IIC_Stop();
return 1;
}
}
IIC_SCL = 0;
return 0;
}
void IIC_Send_Ack()
{
IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 0;
IIC_Delay();
IIC_SCL = 1;
IIC_Delay();
IIC_SCL = 0;
IIC_Delay();
}
void IIC_Send_Nack()
{
IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 1;
IIC_Delay();
IIC_SCL = 1;
IIC_Delay();
IIC_SCL = 0;
IIC_Delay();
}
void IIC_Write_Byte(u8 data)
{
uint8_t i;
SDA_OUT();
IIC_SCL = 0;
for(i=0;i<8;i++)
{
IIC_SDA = (data&0x80)>>7;
data<<=1;
IIC_Delay();
IIC_SCL = 1;
IIC_Delay();
IIC_SCL = 0;
IIC_Delay();
}
}
u8 IIC_Read_Byte(u8 ack)
{
uint8_t i,receive;
SDA_IN();
IIC_Delay();
for(i=0;i<8;i++)
{
IIC_SCL = 0;
IIC_Delay();
IIC_SCL = 1;
receive<<=1;
if(IIC_SDA)
receive++;
IIC_Delay();
}
if(!ack)
IIC_Send_Nack();
else
IIC_Send_Ack();
return receive;
}
#include "tmp275.h"
#include "iic.h"
void TMP275_Init()
{
//IIC_Init();
IIC_Start();
IIC_Write_Byte(0x98);
IIC_Wait_Ack();
IIC_Write_Byte(0x01);
IIC_Wait_Ack();
IIC_Start();
IIC_Write_Byte(0xFE);
IIC_Wait_Ack();
IIC_Stop();
}
void TMP275_Write_Byte(u8 reg,u16 data)
{
u8 data_H,data_L;
data_H = (u8)data>>8;
data_L = (u8)data;
IIC_Start();
IIC_Write_Byte(0x98);
IIC_Wait_Ack();
IIC_Write_Byte(0x00);
IIC_Wait_Ack();
IIC_Write_Byte(data_H);
IIC_Wait_Ack();
IIC_Write_Byte(data_L);
IIC_Wait_Ack();
IIC_Stop();
}
u16 TMP275_Read_Byte()
{
u8 data_H,data_L;
u16 temp = 0;
IIC_Start();
IIC_Write_Byte(0x98);
IIC_Wait_Ack();
IIC_Write_Byte(0x00);
IIC_Wait_Ack();
IIC_Start();
IIC_Write_Byte(0x99);
IIC_Wait_Ack();
data_H = IIC_Read_Byte(1);
data_L = IIC_Read_Byte(0);
temp = (((u16)data_H)<<8|data_L)>>4;
IIC_Stop();
return temp;
}
#include "pwm.h"
#include "delay.h"
#include "usart1.h"
#include "stm32f10x.h"
#include "tmp275.h"
float data;
u16 temp;
int main()
{
//u16 led0pwmval=0;
//u8 dir=1;
//TIME3_PWM_Init(999,7199);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART1_Init(115200);
delay_init();
while(1)
{
u16 len,t;
//delay_ms(10);
//if(dir)led0pwmval++;
//else led0pwmval--;
//if(led0pwmval>300)dir=0;
//if(led0pwmval==0)dir=1;
//TIM_SetCompare3(TIM3,300);
//TIM_SetCompare4(TIM3,500);
delay_ms(1000);
temp = TMP275_Read_Byte();
printf("%d
",temp);
if((temp&0x80)!=0)
{
temp=~(temp-1);
data = 0.625*temp;
printf("-%.1f
",data);
}
else
{
data = 0.625*temp;
printf("%.1f
",data);
}
//data = 0.625*TMP275_Read_Byte(00);
//data=(data/16.0);
//printf("%f
",data);
if(USART1_RX_STA&0X8000)
{
len = USART1_RX_STA&0x3fff;
printf("Äã·¢ËíμÄÏûÏ¢Îa:
");
for(t=0;t<len;t++)
{
USART_SendData(USART1,USART1_RX_BUF[t]); //·¢Ëíêy¾Y
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET); //μè′y·¢Ëí½áêø
}
printf("
");
USART1_RX_STA =0;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>