#define F_CPU 1000000UL // 1 MHz
#include <u
til/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define Cali_temp 25
#define Cali_value 0x12E
#define Melting_point 273
#define LED_port PORTB
#define LED_ddr DDRB
#define LED_pin 0
#define Ring_scale 5
/*
#define Hysteresis_T_NtoH 40
#define Hysteresis_T_HtoN 35
*/
#define Hysteresis_T_NtoH 38
#define Hysteresis_T_HtoN 33
#define Hysteresis_T_NtoC 5
#define Hysteresis_T_CtoN 10
ISR(ADC_vect){
}
void send_led(unsigned char r,unsigned char g,unsigned char b){
for (unsigned char i=0;i<6;i++){
if (b&1){
LED_port|=(1<<LED_pin);
}else{
LED_port&=~(1<<LED_pin);
}
LED_ddr|=(1<<LED_pin);
LED_ddr&=~(1<<LED_pin);
b=b/2;
}
for (unsigned char i=0;i<6;i++){
if (g&1){
LED_port|=(1<<LED_pin);
}else{
LED_port&=~(1<<LED_pin);
}
LED_ddr|=(1<<LED_pin);
LED_ddr&=~(1<<LED_pin);
g>>=1;
}
for (unsigned char i=0;i<6;i++){
if (r&1){
LED_port|=(1<<LED_pin);
}else{
LED_port&=~(1<<LED_pin);
}
LED_ddr|=(1<<LED_pin);
LED_ddr&=~(1<<LED_pin);
r>>=1;
}
}
void conv_phase_to_rgb(unsigned char* ptr, unsigned char phase){
unsigned char r_t=0,g_t=0,b_t=0;
//0~41 R->G->B->R
//42~49 BLACK->R
//50~57 BLACK->G
//58~65 BLACK->B
if (phase<42){ //normal phase
if (phase<=7){
r_t=7;g_t=phase;
}else if(phase<=14){
r_t=14-phase;g_t=7;
}else if(phase<=21){
g_t=7;b_t=phase-14;
}else if(phase<=28){
g_t=28-phase;b_t=7;
}else if(phase<=35){
b_t=7;r_t=phase-28;
}else if(phase<42){
b_t=42-phase;r_t=7;
}
}else if(phase<66){
if (phase<50){
r_t=phase-42;
}else if (phase<58){
g_t=phase-50;
}else{
b_t=phase-58;
}
}
ptr[0]=r_t;
ptr[1]=g_t;
ptr[2]=b_t;
}
void delay_display(unsigned char* ptr){
_delay_ms(100);
for(unsigned char i=0;i<6;i++){
send_led(ptr[3*i],ptr[3*i+1],ptr[3*i+2]);
}
}
unsigned char RGB_data;
volatile unsigned int temp_in_Kelvin;
int main(void){
unsigned char i,j,swap;
unsigned char phase[6]={0,7,14,21,28,35};
unsigned char led_color[6*3]={0};
unsigned char status=0,last_status=0;
unsigned char ring_position=3;
MCUCR|=(1<<PUD);//Disable pull up
MCUCR|=(0b01<<SM0);//ADC Nosie Reduction
ADMUX=(0b10<<REFS0)|(0b100010<<MUX0);//temperature
ADCSRA=(1<<ADEN)|(1<<ADIE)|(0b100<ADPS0);
MCUCR|=(1<<SE);
sei();
asm("sleep");
cli();
MCUCR&=~(1<<SE);
for (j=0;j<8;j++){ //Initialize fade in
for(i=0;i<6;i++){
phase
++;
if (phase==42) phase=0;
conv_phase_to_rgb(led_color+3*i, phase);
}
for(i=0;i<6*3;i++){
if (led_color>7-j){
led_color-=7-j;
}else{
led_color=0;
};
}
delay_display(led_color);
}
while(1){
MCUCR|=(1<<SE);
sei();
asm("sleep");
cli();
MCUCR&=~(1<<SE);
temp_in_Kelvin=ADC+273+Cali_temp-Cali_value;
asm("nop");
if (last_status==0){//0 Normal; 1 Cold; 2 Hot
if (temp_in_Kelvin>Hysteresis_T_NtoH + Melting_point){
status=2;
}else if (temp_in_Kelvin<Hysteresis_T_NtoC + Melting_point){
status=1;
}else{
status=0;
}
}else if (last_status==2){
if (temp_in_Kelvin<Hysteresis_T_HtoN + Melting_point){
status=0;
}else{
status=2;
}
}else if (last_status==1){
if (temp_in_Kelvin>Hysteresis_T_CtoN + Melting_point){
status=0;
}else{
status=1;
}
}
if (status==0){ //NOW NORMAL
if (last_status==0){
for(i=0;i<6;i++){
phase++;
if (phase==42) phase=0;
conv_phase_to_rgb(led_color+3*i, phase);
}
delay_display(led_color);
}else{
for (j=0;j<10;j++){//all fade out
for (i=0;i<6*3;i++){
if (led_color>0) led_color--;
if (led_color>0) led_color--;
if (led_color>0) led_color--;
}
delay_display(led_color);
}
for (i=0;i<6*3;i++){
led_color=0;
}
for (j=0;j<8;j++){ //fade in
for(i=0;i<6;i++){
phase++;
if (phase==42) phase=0;
conv_phase_to_rgb(led_color+3*i, phase);
}
for(i=0;i<6*3;i++){
if (led_color>7-j){
led_color-=7-j;
}else{
led_color=0;
};
}
delay_display(led_color);
}
}
}else if (status==1) { //NOW COLD
if (last_status==1){
for(i=0;i<6;i++){
led_color[3*i+2]++;
if (led_color[3*i+2]>5*Ring_scale+Ring_scale) led_color[3*i+2]=1;
}
delay_display(led_color);
}else if (last_status==0){
for (j=0;j<7;j++){//all fade out
for (i=0;i<6*3;i++){
if (led_color>0) led_color--;
}
delay_display(led_color);
}
for (j=0;j<5*Ring_scale+1;j++){//blue ring fade in
for (i=0;i<6;i++){
if (i<ring_position){
swap=i+6-ring_position;
}else{
swap=i-ring_position;
}
if (led_color[i*3+2]<swap*Ring_scale+1) led_color[3*i+2]++;
}
delay_display(led_color);
}
}
}else if (status==2) { //NOW HOT
if (last_status==2){
for(i=0;i<6;i++){
led_color[3*i]++;
if (led_color[3*i]>5*Ring_scale+Ring_scale) led_color[3*i]=1;
}
delay_display(led_color);
}else if (last_status==0){
for (j=0;j<7;j++){//all fade out
for (i=0;i<6*3;i++){
if (led_color>0) led_color--;
}
delay_display(led_color);
}
for (j=0;j<5*Ring_scale+1;j++){//red ring fade in
for (i=0;i<6;i++){
if (i<ring_position){
swap=i+6-ring_position;
}else{
swap=i-ring_position;
}
if (led_color[i*3]<swap*Ring_scale+1) led_color[3*i]++;
}
delay_display(led_color);
}
}
}
last_status=status;
}
}
code.zip
(1.68 KB, 下载次数: 4)
一周热门 更多>