Working version
This commit is contained in:
parent
7181dc2308
commit
77152f9c8c
12
ledfade.c
12
ledfade.c
@ -67,7 +67,7 @@ static uint8_t m_ledfade_timer_running;
|
||||
|
||||
/* *********************************************************************** */
|
||||
|
||||
static void ledfade_update(uint8_t channel)
|
||||
static void ledfade_update(uint8_t channel, uint8_t force_event)
|
||||
{
|
||||
uint8_t update_pwm = 0;
|
||||
|
||||
@ -91,7 +91,7 @@ static void ledfade_update(uint8_t channel)
|
||||
update_pwm = 1;
|
||||
}
|
||||
|
||||
if (update_pwm)
|
||||
if (update_pwm || force_event)
|
||||
{
|
||||
event_queue(EVENT_TYPE_PWM_VALUE,
|
||||
channel,
|
||||
@ -170,7 +170,7 @@ void ledfade_event_handler(event_entry_t * p_event)
|
||||
break;
|
||||
}
|
||||
|
||||
ledfade_update(channel);
|
||||
ledfade_update(channel, 1);
|
||||
}
|
||||
else if (p_event->type == EVENT_TYPE_LEDFADE_VALUE)
|
||||
{
|
||||
@ -185,7 +185,7 @@ void ledfade_event_handler(event_entry_t * p_event)
|
||||
m_ledfade_setpoint[channel] = (LEDFADE_TABLE_SIZE -1);
|
||||
}
|
||||
|
||||
ledfade_update(channel);
|
||||
ledfade_update(channel, 1);
|
||||
}
|
||||
else if ((p_event->type == EVENT_TYPE_TIMER_ELAPSED) &&
|
||||
(p_event->num == EVENT_NUM_TIMER_LEDFADE)
|
||||
@ -193,7 +193,7 @@ void ledfade_event_handler(event_entry_t * p_event)
|
||||
{
|
||||
m_ledfade_timer_running = 0;
|
||||
|
||||
ledfade_update(0);
|
||||
ledfade_update(1);
|
||||
ledfade_update(0, 0);
|
||||
ledfade_update(1, 0);
|
||||
}
|
||||
} /* ledfade_event_handler */
|
||||
|
15
main.c
15
main.c
@ -46,9 +46,6 @@ void disable_wdt_timer(void)
|
||||
* ************************************************************************* */
|
||||
int main(void)
|
||||
{
|
||||
DDRD = (1<<PORTD5);
|
||||
PORTD = (1<<PORTD5);
|
||||
|
||||
/* Disable clock division */
|
||||
clock_prescale_set(clock_div_1);
|
||||
|
||||
@ -60,8 +57,8 @@ int main(void)
|
||||
|
||||
// fade LEDs to max, set fan PWM to 50%
|
||||
event_queue(EVENT_TYPE_LEDFADE_COMMAND, EVENT_NUM_LED_CH0, EVENT_VALUE_LEDFADE_MAX);
|
||||
event_queue(EVENT_TYPE_LEDFADE_COMMAND, EVENT_NUM_LED_CH1, EVENT_VALUE_LEDFADE_MAX);
|
||||
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0x8000);
|
||||
event_queue(EVENT_TYPE_LEDFADE_COMMAND, EVENT_NUM_LED_CH1, EVENT_VALUE_LEDFADE_MIN);
|
||||
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0x58);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
@ -90,18 +87,10 @@ int main(void)
|
||||
case EVENT_TYPE_LEDFADE_COMMAND:
|
||||
case EVENT_TYPE_LEDFADE_VALUE:
|
||||
ledfade_event_handler(p_event);
|
||||
if (p_event->num == EVENT_NUM_LED_CH0)
|
||||
{
|
||||
PORTD &= ~(1<<PORTD5);
|
||||
}
|
||||
break;
|
||||
|
||||
case EVENT_TYPE_LEDFADE_STATUS:
|
||||
usbcdc_event_handler(p_event);
|
||||
if (p_event->num == EVENT_NUM_LED_CH0)
|
||||
{
|
||||
PORTD |= (1<<PORTD5);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
186
pwm.c
186
pwm.c
@ -16,16 +16,11 @@
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
#include "pwm.h"
|
||||
|
||||
/* *********************************************************************** */
|
||||
|
||||
#define PWM_MIN 0x0000
|
||||
#define PWM_MAX 0xFFFF
|
||||
|
||||
#define PWM_TIM_INIT() { \
|
||||
/* enable output for OC1A, OC1B, OC3A */ \
|
||||
DDRB |= (1<<PORTB5) | (1<<PORTB6); \
|
||||
@ -36,129 +31,120 @@
|
||||
ICR1 = 0xFFFF; \
|
||||
TCCR3A = (1<<WGM31); \
|
||||
TCCR3B = (1<<WGM33) | (1<<WGM32); \
|
||||
ICR3 = 0xFFFF; \
|
||||
ICR3 = 0xFF; \
|
||||
}
|
||||
|
||||
#define PWM_TIM1_ENABLE() { TCCR1B |= (1<<CS10); }
|
||||
#define PWM_TIM1_DISABLE() { TCCR1B &= ~(1<<CS10); }
|
||||
#define PWM_TIM1_RUNNING() (TCCR1B & ((1<<CS10) | (1<<CS11) | (1<<CS12)))
|
||||
#define PWM_TIM1_CHECK() (TCCR1A & ((1<<COM1A1) | (1<<COM1B1)))
|
||||
|
||||
#define PWM_TIM3_ENABLE() { TCCR3B |= (1<<CS30); }
|
||||
#define PWM_TIM3_DISABLE() { TCCR3B &= ~(1<<CS30); }
|
||||
#define PWM_TIM3_RUNNING() (TCCR3B & ((1<<CS30) | (1<<CS31) | (1<<CS32)))
|
||||
#define PWM_TIM3_CHECK() (TCCR3A & ((1<<COM3A1) | (1<<COM3B1)))
|
||||
|
||||
#define PWM_CH0_MIN 0
|
||||
#define PWM_CH0_MAX ICR1
|
||||
#define PWM_CH0_ENABLE() { TCCR1B |= (1<<CS10); }
|
||||
#define PWM_CH0_DISABLE() { TCCR1B &= ~(1<<CS10); }
|
||||
#define PWM_CH0_RUNNING() (TCCR1B & ((1<<CS10) | (1<<CS11) | (1<<CS12)))
|
||||
#define PWM_CH0_CHECK() (TCCR1A & ((1<<COM1A1) | (1<<COM1B1)))
|
||||
#define PWM_CH0_PORT PORTB
|
||||
#define PWM_CH0_NUM 5
|
||||
#define PWM_CH0_OFF() { PWM_CH0_PORT &= ~(1<<PWM_CH0_NUM); TCCR1A &= ~(1<<COM1A1); }
|
||||
#define PWM_CH0_ON() { PWM_CH0_PORT |= (1<<PWM_CH0_NUM); TCCR1A &= ~(1<<COM1A1); }
|
||||
#define PWM_CH0_PWM(x) { PWM_CH0_PORT &= ~(1<<PWM_CH0_NUM); TCCR1A |= (1<<COM1A1); OCR1A = x; }
|
||||
|
||||
#define PWM_CH1_PORT PORTC
|
||||
#define PWM_CH1_MIN 0
|
||||
#define PWM_CH1_MAX ICR1
|
||||
#define PWM_CH1_ENABLE() PWM_CH0_ENABLE()
|
||||
#define PWM_CH1_DISABLE() PWM_CH0_DISABLE()
|
||||
#define PWM_CH1_RUNNING() PWM_CH0_RUNNING()
|
||||
#define PWM_CH1_CHECK() PWM_CH0_CHECK()
|
||||
#define PWM_CH1_PORT PORTB
|
||||
#define PWM_CH1_NUM 6
|
||||
#define PWM_CH1_OFF() { PWM_CH1_PORT &= ~(1<<PWM_CH1_NUM); TCCR3A &= ~(1<<COM3A1); }
|
||||
#define PWM_CH1_ON() { PWM_CH1_PORT |= (1<<PWM_CH1_NUM); TCCR3A &= ~(1<<COM3A1); }
|
||||
#define PWM_CH1_PWM(x) { PWM_CH1_PORT &= ~(1<<PWM_CH1_NUM); TCCR3A |= (1<<COM3A1); OCR3A = x; }
|
||||
#define PWM_CH1_OFF() { PWM_CH1_PORT &= ~(1<<PWM_CH1_NUM); TCCR1A &= ~(1<<COM1B1); }
|
||||
#define PWM_CH1_ON() { PWM_CH1_PORT |= (1<<PWM_CH1_NUM); TCCR1A &= ~(1<<COM1B1); }
|
||||
#define PWM_CH1_PWM(x) { PWM_CH1_PORT &= ~(1<<PWM_CH1_NUM); TCCR1A |= (1<<COM1B1); OCR1B = x; }
|
||||
|
||||
#define PWM_CH2_PORT PORTB
|
||||
#define PWM_CH2_MIN 0
|
||||
#define PWM_CH2_MAX ICR3
|
||||
#define PWM_CH2_ENABLE() { TCCR3B |= (1<<CS30); }
|
||||
#define PWM_CH2_DISABLE() { TCCR3B &= ~(1<<CS30); }
|
||||
#define PWM_CH2_RUNNING() (TCCR3B & ((1<<CS30) | (1<<CS31) | (1<<CS32)))
|
||||
#define PWM_CH2_CHECK() (TCCR3A & ((1<<COM3A1) | (1<<COM3B1)))
|
||||
#define PWM_CH2_PORT PORTC
|
||||
#define PWM_CH2_NUM 6
|
||||
#define PWM_CH2_OFF() { PWM_CH2_PORT &= ~(1<<PWM_CH2_NUM); TCCR1A &= ~(1<<COM1B1); }
|
||||
#define PWM_CH2_ON() { PWM_CH2_PORT |= (1<<PWM_CH2_NUM); TCCR1A &= ~(1<<COM1B1); }
|
||||
#define PWM_CH2_PWM(x) { PWM_CH2_PORT &= ~(1<<PWM_CH2_NUM); TCCR1A |= (1<<COM1B1); OCR1B = x; }
|
||||
#define PWM_CH2_OFF() { PWM_CH2_PORT &= ~(1<<PWM_CH2_NUM); TCCR3A &= ~(1<<COM3A1); }
|
||||
#define PWM_CH2_ON() { PWM_CH2_PORT |= (1<<PWM_CH2_NUM); TCCR3A &= ~(1<<COM3A1); }
|
||||
#define PWM_CH2_PWM(x) { PWM_CH2_PORT &= ~(1<<PWM_CH2_NUM); TCCR3A |= (1<<COM3A1); OCR3A = x; }
|
||||
|
||||
/* *********************************************************************** */
|
||||
|
||||
static void pwm_update(uint8_t channel, uint16_t value)
|
||||
{
|
||||
/* if PWM is zero, disable output */
|
||||
if (value == PWM_MIN)
|
||||
{
|
||||
switch (channel)
|
||||
{
|
||||
case 0:
|
||||
PWM_CH0_OFF();
|
||||
break;
|
||||
|
||||
case 1:
|
||||
PWM_CH1_OFF();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
PWM_CH2_OFF();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* if PWM is max, enable output */
|
||||
else if (value == PWM_MAX)
|
||||
{
|
||||
switch (channel)
|
||||
{
|
||||
case 0:
|
||||
PWM_CH0_ON();
|
||||
break;
|
||||
|
||||
case 1:
|
||||
PWM_CH1_ON();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
PWM_CH2_ON();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* else load new PWM into timer */
|
||||
else
|
||||
{
|
||||
switch (channel)
|
||||
{
|
||||
case 0:
|
||||
PWM_CH0_PWM(value);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
PWM_CH1_PWM(value);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
PWM_CH2_PWM(value);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (channel)
|
||||
{
|
||||
case 0:
|
||||
case 2:
|
||||
/* disable timer if all channels are ON or OFF */
|
||||
if (PWM_TIM1_CHECK())
|
||||
if (value <= PWM_CH0_MIN)
|
||||
{
|
||||
PWM_TIM1_ENABLE();
|
||||
PWM_CH0_OFF();
|
||||
}
|
||||
else if (value >= PWM_CH0_MAX)
|
||||
{
|
||||
PWM_CH0_ON();
|
||||
}
|
||||
else
|
||||
{
|
||||
PWM_TIM1_DISABLE();
|
||||
PWM_CH0_PWM(value);
|
||||
}
|
||||
|
||||
if (PWM_CH0_CHECK())
|
||||
{
|
||||
PWM_CH0_ENABLE();
|
||||
}
|
||||
else
|
||||
{
|
||||
PWM_CH0_DISABLE();
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
/* disable timer if all channels are ON or OFF */
|
||||
if (PWM_TIM3_CHECK())
|
||||
if (value <= PWM_CH1_MIN)
|
||||
{
|
||||
PWM_TIM3_ENABLE();
|
||||
PWM_CH1_OFF();
|
||||
}
|
||||
else if (value >= PWM_CH1_MAX)
|
||||
{
|
||||
PWM_CH1_ON();
|
||||
}
|
||||
else
|
||||
{
|
||||
PWM_TIM3_DISABLE();
|
||||
PWM_CH1_PWM(value);
|
||||
}
|
||||
|
||||
if (PWM_CH1_CHECK())
|
||||
{
|
||||
PWM_CH1_ENABLE();
|
||||
}
|
||||
else
|
||||
{
|
||||
PWM_CH1_DISABLE();
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if (value <= PWM_CH2_MIN)
|
||||
{
|
||||
PWM_CH2_OFF();
|
||||
}
|
||||
else if (value >= PWM_CH2_MAX)
|
||||
{
|
||||
PWM_CH2_ON();
|
||||
}
|
||||
else
|
||||
{
|
||||
PWM_CH2_PWM(value);
|
||||
}
|
||||
|
||||
if (PWM_CH2_CHECK())
|
||||
{
|
||||
PWM_CH2_ENABLE();
|
||||
}
|
||||
else
|
||||
{
|
||||
PWM_CH2_DISABLE();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -174,10 +160,10 @@ void pwm_event_handler(event_entry_t * p_event)
|
||||
} /* pwm_event_handler */
|
||||
|
||||
|
||||
uint8_t pwm_get_sleep_mode(void)
|
||||
uint8_t pwm_need_hw_clock(void)
|
||||
{
|
||||
return !!(PWM_TIM1_RUNNING() || PWM_TIM3_RUNNING());
|
||||
} /* pwm_get_sleep_mode */
|
||||
return !!(PWM_CH0_RUNNING() || PWM_CH1_RUNNING() || PWM_CH2_RUNNING());
|
||||
} /* pwm_need_hw_clock */
|
||||
|
||||
|
||||
void pwm_init(void)
|
||||
|
2
timer.c
2
timer.c
@ -114,7 +114,7 @@ void timer_event_handler(event_entry_t * p_event)
|
||||
uint8_t timer_need_hw_clock(void)
|
||||
{
|
||||
return !!TIMER_TIM_RUNNING();
|
||||
} /* timer_get_sleep_mode */
|
||||
} /* timer_need_hw_clock */
|
||||
|
||||
|
||||
void timer_init(void)
|
||||
|
47
usbcdc.c
47
usbcdc.c
@ -293,15 +293,48 @@ void usbcdc_task(void)
|
||||
if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
|
||||
{
|
||||
uint8_t cmd = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
|
||||
uint16_t value = 0;
|
||||
if ((cmd >= '1') && (cmd <= '8'))
|
||||
switch (cmd)
|
||||
{
|
||||
value = ((cmd - '0') * 0x2000) -1;
|
||||
}
|
||||
/* turn off fan */
|
||||
case '0':
|
||||
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0x00);
|
||||
break;
|
||||
|
||||
event_queue(EVENT_TYPE_PWM_VALUE,
|
||||
EVENT_NUM_PWM_CH2,
|
||||
value);
|
||||
/* turn on fan, ~50% (12V) */
|
||||
case '1':
|
||||
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0x58);
|
||||
break;
|
||||
|
||||
/* turn on fan, ~75% (18V) */
|
||||
case '2':
|
||||
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0xC0);
|
||||
break;
|
||||
|
||||
/* turn on fan, 100% (24V) */
|
||||
case '3':
|
||||
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0xFF);
|
||||
break;
|
||||
|
||||
/* turn off LEDs */
|
||||
case 'a':
|
||||
event_queue(EVENT_TYPE_LEDFADE_VALUE, EVENT_NUM_LED_CH0, 0);
|
||||
break;
|
||||
|
||||
/* turn on LEDs */
|
||||
case 'b':
|
||||
event_queue(EVENT_TYPE_LEDFADE_VALUE, EVENT_NUM_LED_CH0, 0x50);
|
||||
break;
|
||||
|
||||
/* turn on LEDs */
|
||||
case 'c':
|
||||
event_queue(EVENT_TYPE_LEDFADE_VALUE, EVENT_NUM_LED_CH0, 0x70);
|
||||
break;
|
||||
|
||||
/* turn on LEDs, 100% */
|
||||
case 'd':
|
||||
event_queue(EVENT_TYPE_LEDFADE_VALUE, EVENT_NUM_LED_CH0, 0x80);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user