才发现attiny45居然自带温度检测功能,放一个测试小程序
程序功能为:三色灯随温度变化,做个小摆件还是不错滴。
//ICC-AVR application builder : 2012/11/15 13:43:00
// Target : T45
// Crystal: 8.0000Mhz
#include <iot45v.h>
#include <macros.h>
int value; //温度
unsigned int adc_c;
#define red_on (DDRB |=BIT(3));
#define blue_on (DDRB |=BIT(1));
#define green_on (DDRB |=BIT(4));
#define red_off (DDRB &=~ BIT(3));
#define blue_off (DDRB &=~ BIT(1));
#define green_off (DDRB &=~ BIT(4));
#define adc_on (ADCSRA |=BIT(ADSC));
void port_init(void)
{
PORTB = 0x00;
DDRB= 0x00;
}
void adc_init(void)
{
ADMUX=0x8f; //参考电压必须选择1.1v,采集端口必须ADC4
ADCSRA=0x8D;
ADCH=0x00;
ADCL=0x00;
ADCSRB=0x00;
DIDR0=0x00;
}
#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr(void)
{
value=ADCL;
value|=(int)ADCH << 8;
pwm_led();
adc_c=0;
}
void init_devices(void)
{
CLI();
port_init();
adc_init();
GIMSK= 0x00 ;
MCUCR= 0x00 ;
PLLCSR = 0x00;
SEI();
}
void pwm_led() //颜色控制
{
if(value==0)
{
green_on;
blue_off;
red_off;
}
if(value<295 & value>0) //value可以根据实际情况进行设置
{
blue_on;
red_off;
green_off;
}
if(value>295)
{
red_on;
blue_off;
green_off;
}
}
void main()
{
adc_c=0;
value=0;
init_devices();
while(1)
{
if(adc_c==0)
{
adc_on;
adc_c=1;
}
}
} 嗯,谢谢楼主分享
AVR好多芯片内部带有温度传感器,通过AD转换读取, ATtiny24内部也有温度传感器 一直都没注意到,感谢楼主提醒啊 手头一堆T44,回头研究下Datasheet瞅瞅。 路过.........
页:
[1]