HWA
Bare metal programming with style
Class _cta: 8-bit counter with two compare units

This class is used by:

Relatives

Interrupts

Actions



hwa( configure, ... ): configure the counting unit

hwa( configure, counter0,
// How the counter is clocked
//
clock, none // No clock, the counter is stopped
| ioclk [/ 8|64|256|1024] // IOCLK divided by 1,8,64,256,1024
| external_rising // External input, rising edge
| external_falling, // External input, falling edge
// How does this counter count
//
direction, up_loop // Count from bottom to top and loop
| updown_loop, // Count up and down alternately
[ bottom, 0, ] // Default, no other choice
// The maximum value the counter reaches
//
[ top, 0xFF | 0x00FF | 255 | max // Hardware fixed value 0xFF (default)
| compare0,] // Value stored in compare0
// When the overflow flag is set
//
[ overflow, after_bottom // When the counter counts from 0 to 1
| after_top // When the counter counts from top to 0
| after_max ] // When the counter counts from max to 0
);
Note
If a compare unit is used, its configuration must be done inside the same context as the counter so that HWA has all the necessary informations to choose the correct register settings.
If the optionnal argument overflow is not stated, an acceptable value will be selected according to the configuration of the compare units found in the HWA context. If overflow is stated, the validity of its value will be verified.

Atmel modes:

Mode direction top overflow Overflow flag Compare update
0 Normal up_loop max after_max max -> 0 Immediate
1 PWM, Phase correct updown_loop max after_bottom 0 -> 1 At top
2 CTC up_loop compare0 after_max max -> 0 Immediate
3 Fast PWM up_loop max after_max max -> 0 At bottom
5 PWM, Phase correct updown_loop compare0 after_bottom 0 -> 1 At top
7 Fast PWM up_loop max after_top top -> 0 At bottom


Example 1 - An overflow IRQ is triggered when the counter counts from 255 to 0, the frequency of the IRQs is HW_SYSHZ/8/256:

hw( configure, counter0,
clock, ioclk/8,
direction, up_loop,
top, max,
overflow, after_max );


Example 2 - Counting: 0-1-2-3-2-1-0-1..., an overflow IRQ is triggered when the counter counts from 0 to 1, the frequency of the IRQs is HW_SYSHZ/64/6.

hwa( configure, counter0,
clock, ioclk/64,
direction, updown_loop,
top, compare0,
overflow, after_bottom );
hwa( write, (counter0,compare0), 3 );



hw( read, ... ): get the count

uint8_t n = hw( read, counter0 );



hw( write, ... ), hwa( write, ... ): set the count

hw( write, counter0, value );
hwa( write, counter0, value );




hw( clear, ... ): set the count to 0

hw( clear, counter0 );

Registers

Hardware registers:

Logical registers:

hwa
#define hwa(...)
hwa( action, object [,...] ) stores an action for an object into a HWA context.
Definition: hwa_macros.h:552
hw
#define hw(...)
hw( action, object [,...] ) executes an action immediately on an object.
Definition: hwa_macros.h:523