corrected LED switching and add temperature reading

This commit is contained in:
King Kévin 2013-10-26 23:15:56 +02:00
parent e4ed9649b5
commit 87f25ce53a
2 changed files with 36 additions and 16 deletions

View File

@ -42,16 +42,17 @@ const char help_03[] PROGMEM = "\tpower show power state\n"
const char help_04[] PROGMEM = "\tpower on switch power on\n"; const char help_04[] PROGMEM = "\tpower on switch power on\n";
const char help_05[] PROGMEM = "\tpower off switch power off\n"; const char help_05[] PROGMEM = "\tpower off switch power off\n";
const char help_06[] PROGMEM = "\tfan show fan speed\n"; const char help_06[] PROGMEM = "\tfan show fan speed\n";
const char help_07[] PROGMEM = "\tmode show current mode\n"; const char help_07[] PROGMEM = "\ttemp show temperature\n";
const char help_08[] PROGMEM = "\tmode N select mode\n"; const char help_08[] PROGMEM = "\tmode show current mode\n";
const char help_09[] PROGMEM = "\tch X Y show channel [1-2].[1-5] brightness\n"; const char help_09[] PROGMEM = "\tmode N select mode\n";
const char help_10[] PROGMEM = "\tch X Y Z set channel [1-2].[1-5] brightness [0-255]\n"; const char help_10[] PROGMEM = "\tch X Y show channel [1-2].[1-5] brightness\n";
const char help_11[] PROGMEM = "\tir learn power learn the IR command to power on/off\n"; const char help_11[] PROGMEM = "\tch X Y Z set channel [1-2].[1-5] brightness [0-255]\n";
const char help_12[] PROGMEM = "\tir learn mode learn the IR command to change between modes\n"; const char help_12[] PROGMEM = "\tir learn power learn the IR command to power on/off\n";
const char help_13[] PROGMEM = "\tir learn brightness up learn the IR command to increase brightness\n"; const char help_13[] PROGMEM = "\tir learn mode learn the IR command to change between modes\n";
const char help_14[] PROGMEM = "\tir learn brightness down learn the IR command to decrease brightness\n"; const char help_14[] PROGMEM = "\tir learn brightness up learn the IR command to increase brightness\n";
const char help_15[] PROGMEM = "\tir learn channel next learn the IR command to select next channel\n"; const char help_15[] PROGMEM = "\tir learn brightness down learn the IR command to decrease brightness\n";
const char help_16[] PROGMEM = "\tir learn channel previous learn the IR command to select previous channel\n"; const char help_16[] PROGMEM = "\tir learn channel next learn the IR command to select next channel\n";
const char help_17[] PROGMEM = "\tir learn channel previous learn the IR command to select previous channel\n";
PGM_P const help_table[] PROGMEM = { PGM_P const help_table[] PROGMEM = {
help_00, help_00,
help_01, help_01,
@ -69,7 +70,8 @@ PGM_P const help_table[] PROGMEM = {
help_13, help_13,
help_14, help_14,
help_15, help_15,
help_16 help_16,
help_17
}; };
//volatile uint8_t* PORTS[CHANNELS_1+CHANNELS_2] = {&PORTC,&PORTC,&PORTC,&PORTC,&PORTC,&PORTD,&PORTD,&PORTD,&PORTD,&PORTD}; //volatile uint8_t* PORTS[CHANNELS_1+CHANNELS_2] = {&PORTC,&PORTC,&PORTC,&PORTC,&PORTC,&PORTD,&PORTD,&PORTD,&PORTD,&PORTD};
@ -276,6 +278,18 @@ void ioinit(void)
TIFR0 = (1<<OCF0A); /* clear timer 0 compare interrupt flag */ TIFR0 = (1<<OCF0A); /* clear timer 0 compare interrupt flag */
TIMSK0 |= (1<<OCIE0A); /* enable timer 0 compare interrupt */ TIMSK0 |= (1<<OCIE0A); /* enable timer 0 compare interrupt */
/* configure internal temperature sensor */
ADMUX |= (1<<REFS1)|(1<<REFS0); /* select internal 1.1V voltage reference */
/* select ADC8 internal temperature sensor */
ADMUX |= (1<<MUX3);
ADMUX &= ~((1<<MUX2)|(1<<MUX1)|(1<<MUX0));
ADCSRA |= (1<<ADEN); /* enable ADC */
ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); /* prescaler at 128 */
/* continuously do the conversion (then don't wait for ADCS to clear) */
//ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); /* free-running mode */
//ADCSRA |= (1<<ADATE); /* conversion can be triggered */
//ADCSRA |= (1<<ADSC); /* start analogue to digital conversion */
/* use UART as terminal */ /* use UART as terminal */
uart_init(); uart_init();
stdout = &uart_output; stdout = &uart_output;
@ -332,7 +346,7 @@ int main(void)
PORTB |= (1<<nPS_ON); PORTB |= (1<<nPS_ON);
} }
PIND |= ~(1<<LED); /* switch on LED */ PORTD &= ~(1<<LED); /* switch on LED */
while (true) { while (true) {
/* calculated PWM values (on/off times) */ /* calculated PWM values (on/off times) */
while (channel_flag) { while (channel_flag) {
@ -347,7 +361,7 @@ int main(void)
} }
/* handle UART input */ /* handle UART input */
while (uart_flag) { while (uart_flag) {
PIND &= (1<<LED); /* switch off LED */ PORTD |= (1<<LED); /* switch off LED */
/* echo back */ /* echo back */
char c = 0; char c = 0;
while (command_i<input_i) { while (command_i<input_i) {
@ -368,7 +382,7 @@ int main(void)
input_i = command_i = 0; /* reset input buffer */ input_i = command_i = 0; /* reset input buffer */
} }
uart_flag = false; uart_flag = false;
PIND |= ~(1<<LED); /* switch on LED */ PORTD &= ~(1<<LED); /* switch on LED */
} }
/* handle power state */ /* handle power state */
while (power_flag) { while (power_flag) {
@ -394,7 +408,7 @@ int main(void)
} }
/* handle IR input */ /* handle IR input */
while (ir_flag) { while (ir_flag) {
PIND &= (1<<LED); /* switch off LED */ PORTD |= (1<<LED); /* switch off LED */
time2nec(burst,pulse-1); /* convert raw time burst in NEC format */ time2nec(burst,pulse-1); /* convert raw time burst in NEC format */
struct nec ir_tmp = nec2data(burst,pulse-1); /* decode NEC burst */ struct nec ir_tmp = nec2data(burst,pulse-1); /* decode NEC burst */
if (ir_tmp.valid) { if (ir_tmp.valid) {
@ -423,7 +437,7 @@ int main(void)
} }
pulse = 0; /* reset burst */ pulse = 0; /* reset burst */
ir_flag = false; ir_flag = false;
PIND |= ~(1<<LED); /* switch on LED */ PORTD &= ~(1<<LED); /* switch on LED */
} }
} }
return 0; return 0;
@ -478,6 +492,10 @@ void uart_action(char* str)
} else { } else {
printf("fan is off (or not detected)\n"); printf("fan is off (or not detected)\n");
} }
} else if (0==strcmp(word,"temp")) {
ADCSRA |= (1<<ADSC); /* start analogue to digital conversion */
loop_until_bit_is_clear(ADCSRA,ADSC); /* wait until conversion is finished */
printf("temperature: %u°C\n",(uint16_t)((ADCW-T_OFFSET)*T_FACTOR));
} else if (0==strcmp(word,"mode")) { } else if (0==strcmp(word,"mode")) {
word = strtok(NULL,delimiter); word = strtok(NULL,delimiter);
if (!word) { if (!word) {

View File

@ -22,6 +22,8 @@
#define FAN PC5 /* FAN output */ #define FAN PC5 /* FAN output */
#define PWR_OK PB1 /* ATX power OK */ #define PWR_OK PB1 /* ATX power OK */
#define nPS_ON PB2 /* ATX power supply ON switch */ #define nPS_ON PB2 /* ATX power supply ON switch */
#define T_OFFSET 320 /* measured temperature offset (see AVR122) */
#define T_FACTOR 0.653 /* measured temperature factor (see AVR122) */
/* channel definitions */ /* channel definitions */
//#define CHANNELS_1 5 /* the number of outputs (zo use) for channel 1 (up to 5) */ //#define CHANNELS_1 5 /* the number of outputs (zo use) for channel 1 (up to 5) */