Taltech_embedded/common/pwm.c

51 lines
1.5 KiB
C

#include "pwm.h"
#include "inc/msp432p401r.h"
// PWM on P2.4/.5 using TimerA0 TA0.CCR1/2
// period is 1.333us (1.333us = 2 * 666.7ns because of up/down)
// SMCLK = 48MHz/4 = 12 MHz, 83.33ns
// Let Timerclock period T = 8/12MHz = 666.7ns
// P2.4/.5 = 1 when timer equals TA0CCR1/2 on way down,
// P2.4/.5 = 0 when timer equals TA0CCR1/2 on way up
void PWMInitP2_4_P2_5(uint16_t period) {
P2->DIR |= 0x30;
P2->SEL0 |= 0x30; // Timer0A functions
P2->SEL1 &= ~0x30;
TIMER_A0->CCR[0] = period;
TIMER_A0->EX0 = 0; // Divide by 1
TIMER_A0->CCTL[1] = 0x40; // Toggle/reset
TIMER_A0->CCTL[2] = 0x40;
TIMER_A0->CCR[1] = 0; // Initial duty cycle to 0
TIMER_A0->CCR[2] = 0;
TIMER_A0->CTL = 0x2F0; // SMCLK = 12MHz, divide by 8, up-down mode
}
void SetPWMDutyP2_4(uint16_t duty) {
if (duty >= TIMER_A0->CCR[0]) return;
TIMER_A0->CCR[1] = duty;
}
void SetPWMDutyP2_5(uint16_t duty) {
if (duty >= TIMER_A0->CCR[0]) return;
TIMER_A0->CCR[2] = duty;
}
// PWM on P2.6/.7 using TimerA0 TA0.CCR3/4
// SMCLK = 48MHz/4 = 12 MHz, 83.33ns
// period is 1.333us
// Let Timerclock period T = 8/12MHz = 666.7ns
// P2.6/.7 = 1 when timer equals TA0CCR3/4 on way down,
// P2.6/.7 = 0 when timer equals TA0CCR3/4 on way up
void PWMInitMotor(uint16_t period) {
// write this code
}
void SetPWMDutyRightMotor(uint16_t duty) {
// write this code
}
void SetPWMDutyLeftMotor(uint16_t duty) {
// write this code
}