📄 lm75.c
字号:
//ICC-AVR application builder : 2007-6-4 20:56:48
// Target : M16
// Crystal: 3.6864Mhz
//功能 数字温度测量
//包含文件 LM75.h,I2C.H,LM75.H
#include <iom16v.h>
#include <macros.h>
#include <LM75.h>
void send_a_byte(unsigned char i);
unsigned char get_temp;
void port_init(void)
{
//PORTB = 0x00;
//DDRB = 0x00;
PORTC = 0x03; //SCL SDA上拉
DDRC = 0x00;
//PORTD = 0x00;
//DDRD = 0x00;
}
void c_tmp(unsigned int aa) //字符方式显示温度值
{//正温度显示
unsigned char cc;
if((aa&0x0400)==0)
{
send_a_byte('+');
}
else //负温度显示
{
aa=(0x07ff-aa)+1;
send_a_byte('-');
}
cc=(char)(aa>>3);
send_a_byte(cc/100+0x30);
send_a_byte(cc%100/10+0x30);
send_a_byte(cc%100%10+0x30);
send_a_byte('.');
send_a_byte('C');
send_a_byte(0x0a);
send_a_byte(0x0d);
}
void LM75_init(void)
{
LM75_Write(CONFIG,2,0,0); //POINT
// LM75_Write(TSET,0x7d,0,1);//125 c
// LM75_Write(THYST,0x64,0,1);//100 c
}
void send_a_byte(unsigned char i)
{
while ( !( UCSRA & (1<<UDRE)) );//发送数据
UDR=i;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x17; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}
//
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
unsigned char i;
i=UDR;
send_a_byte('T');
send_a_byte(0x0a);
send_a_byte(0x0d);
get_temp=1;
}
//TWI initialize
// bit rate:8
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x08; //set bit rate 100KHz
TWSR= 0x00; //set prescale
TWAR= 0x00; //set slave address
TWCR= 0x04; //enable twi
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
twi_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
//unsigned char i;
unsigned char *resever,re;
unsigned int temper;
init_devices();
//insert your functional code here...
while(1)
{
if(get_temp)
{
get_temp=0;
re=LM75_Read(TEMP,resever,2);//)==0);//返回0=错误
temper=*resever-1;
temper=((((temper<<8)+*resever)>>5)&0x07ff); //存放温度值
//re=(char)temper;//存放温度值
//send_a_byte(re);
c_tmp(temper);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -