Reputation: 1638
// PWM frequency:
// 0 - 48 kHz
// 1 - 12 kHz
// 2 - 3 kHz
enum { MOTOR_FREQUENCY = 1 };
// Configure Timer 2 w. 250x period.
T2CON = 1 << 2 | MOTOR_FREQUENCY /* << 0 */;
Have i understood this right?
11111111 Arithmetic left-shift-by-two of 0 or 1 or 2
Means:
T2CON = 1 << 2 | 0 = 1111 1100
T2CON = 1 << 2 | 1 = 1111 1000
T2CON = 1 << 2 | 2 = 1111 0000
Kind Regards, Sonite
Upvotes: 0
Views: 750
Reputation: 26727
Context:
TCON2
is a timer register on PIC MCUs, where the last two bits configure the prescaler.
T2CKPS[1:0]
= 0b00
= 0 => /1 prescalerT2CKPS[1:0]
= 0b01
= 1 => /4 prescalerT2CKPS[1:0]
= 0b1x
= 2 or 3 => /16 prescalerBit 2 actually switches the timer on, so it always needs to be set to do anything, hence the 1 << 2
(which really should be written as 1 << T2CON_TMR2ON_bit
with T2CON_TMR2ON_bit
being defined in some CPU-configuration header)
All said and done, the three settings are 0b100
, 0b101
, and 0b110
, which turn on the timer, and tweak the prescaler to get those frequencies mentioned in the comments.
Also, using an enum
with one element is just about pointless; use #define
.
Upvotes: 2
Reputation: 3458
Assuming that you are playing with a microcontroller with 8-bit registers .
0000 0001 << 2 = 0000 0100
then
0000 0100 OR 0000 0000 = 0000 0100
-----
0000 0001 << 2 = 0000 0100
then
0000 0100 OR 0000 0001 = 0000 0101
-----
0000 0001 << 2 = 0000 0100
then
0000 0100 OR 0000 0010 = 0000 0110
Upvotes: 3