diff options
Diffstat (limited to 'picl-firmware/fan_control.c')
| -rw-r--r-- | picl-firmware/fan_control.c | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/picl-firmware/fan_control.c b/picl-firmware/fan_control.c index 9e87963..6b0bd99 100644 --- a/picl-firmware/fan_control.c +++ b/picl-firmware/fan_control.c @@ -2,6 +2,11 @@ #include <avr/interrupt.h> #include <avr/sleep.h> +void setup_clock() { + CLKPR |= (1UL << CLKPCE); //enable setting of prescaler. + CLKPR &= ~(15 << CLKPS0); //set prescaler to 1, system clock 8MHz. +} + void setup_adc() { //enable interrupts sei(); @@ -15,15 +20,28 @@ void setup_adc() { } void setup_timers() { - //use system clock for timers, no prescaler. - TCCR0B &= ~(7UL << CS00); - TCCR0B |= (1UL << CS00); - - //set mode 5 (PWM, phase correct, top defined by OCRA) - TCCR0A &= ~(3UL << WGM00); - TCCR0B &- ~(1UL << WGM02); - TCCR0A |= (1UL << WGM00); - TCCR0B |= (1UL << WGM02); + //Set channel A to inverted mode + TCCR0A &= ~(3UL << COM0A0); + TCCR0A |= (2UL << COM0A0); + + //Set channel B to inverted mode + TCCR0A &= ~(3UL << COM0B0); + TCCR0A |= (2UL << COM0B0); + + //Set Fast PWM mode + //TCCR0A &= ~(3UL << WGM00); + TCCR0A |= (3UL << WGM00); + + TCCR0B &= ~(1UL << WGM02); + //TCCR0B |= (1UL << WGM02); + + //TCCR0B &= ~(3UL << FOC0B); + + TCCR0B &= ~(7UL << CS00); + TCCR0B |= (1UL << CS00); + + DDRA |= (1UL << PA7); + DDRB |= (1UL << PB2); } void enable_ADC() { @@ -44,7 +62,8 @@ void select_ADC_channel(int channel) { uint8_t get_ADC_result() { uint8_t result = ADCL; //conversion would be blocked if we don't also read ADCH although we won't use the upper 2 bits. - uint8_t unblock = ADCH; + uint8_t unused = ADCH; + (void)unused; return result; } @@ -63,24 +82,32 @@ void start_polling_fans() { } ISR(ADC_vect) { - if(ADMUX & (7UL << MUX0) == ADC1D) { + if(((ADMUX & (7UL << MUX0) >> MUX0)) == ADC1D) { fan1 = get_ADC_result(); select_ADC_channel(ADC2D); start_ADC(); return; } - if(ADMUX & (7UL << MUX0) == ADC2D) { + if(((ADMUX & (7UL << MUX0) >> MUX0)) == ADC2D) { fan2 = get_ADC_result(); disable_ADC(); return; } } +volatile int b = 0; + int main() { - set_sleep_mode(0); + setup_clock(); setup_adc(); setup_timers(); + + OCR0A = 0x80; + OCR0B = 0xB0; + + while(1) { - while(1); + b+=1; + } } |
