Fade a LED connected to a counter compare output (method 2).
#include BOARD_H
#define PWM (counter0,compare0)
#define CLKDIV 64
#define COUNTMODE up_loop
#define TOP 0xFF
#define STRCMP(s1,s2) __builtin_strcmp(s1,s2)
HW_INLINE
void setup_hwa_context ( hwa_t *
hwa )
{
sleep, enabled,
sleep_mode, idle );
hwa( configure, (PWM,counter),
clock, ioclk / CLKDIV,
direction, COUNTMODE,
bottom, 0,
top, TOP );
if ( !STRCMP(
HW_Q(COUNTMODE),
"updown_loop") )
hwa( configure, PWM, output, clear_after_match_up_set_after_match_down );
else
hwa( configure, PWM, output, set_at_bottom_clear_after_match );
hwa( enable, (PWM,counter,irq,overflow) );
}
HW_ISR( (PWM,counter,irq,overflow) )
{
static uint8_t duty ;
static uint8_t phase ;
if ( phase == 0 )
else if ( phase == 1 )
duty++ ;
if ( duty==0 ) {
phase = (phase + 1) & 3 ;
if ( !STRCMP(
HW_Q(COUNTMODE),
"up_loop") ) {
setup_hwa_context(
hwa );
if ( phase == 2 ) {
hwa( configure, PWM, output, set_at_bottom_clear_after_match );
hwa( configure, PWM, output, disconnected );
}
else if ( phase == 0 ) {
hwa( configure, PWM, output, disconnected );
hwa( configure, PWM, output, set_at_bottom_clear_after_match );
}
}
}
}
int main ( )
{
setup_hwa_context(
hwa );
hw( enable, interrupts );
for(;;)
}