This program fades LED1 (porta,2) using one channel output of a counter.
The counting direction of the counter (up/down) is output on LED2. It can synchronize an oscilloscope to view the PWM signal.
You can direct the output signal to (portb,10) instead of LED2 (porta,3) after having powered its port and the afio. This requires a remapping of the counter that is handled by HWA.
- main.c
#include BOARD_H
#define AHBHZ HW_DEVICE_HSIHZ // AHB frequency
#define COUNTER counter2
#define CHANNEL channel3
#define DUTYMAX 100
#if HW_ADDRESS(LED2) == -1
# define LED2 fake
#endif
{
static uint16_t duty ;
static uint8_t phase ;
hw( clear, (COUNTER,irq) );
hw( write,
LED2,
hw(read,(COUNTER,dir))==0 );
if ( phase == 0 )
hw( write, (COUNTER,CHANNEL), duty );
else if ( phase == 1 )
hw( write, (COUNTER,CHANNEL), DUTYMAX-duty );
duty++ ;
if ( duty > DUTYMAX ) {
duty = 0 ;
phase = (phase+1) & 3 ;
}
}
int main ( )
{
hwa( power, COUNTER, on );
hwa( configure,
LED1,
function, (COUNTER,CHANNEL), mode, digital_output );
hwa( configure,
LED2,
function, gpio, mode, digital_output );
clock, from_apb1_psc,
direction, updown_loop,
prescaler, AHBHZ/DUTYMAX/100 - 1,
reload, DUTYMAX,
run, yes );
hwa( configure, (COUNTER,CHANNEL),
function, compare,
level, clear_on_match_up_set_on_match_down,
output, connected );
hw( enable, (nvic,COUNTER) );
hw( enable, (COUNTER,irq) );
}