Untitled
unknown
plain_text
a year ago
3.3 kB
7
Indexable
#include "msp430.h"
#include "scheduler.h"
// Define a struct for PWM pin configuration
typedef struct {
volatile unsigned char *dir; // Direction register
volatile unsigned char *sel; // Function select register
volatile unsigned int *ccr; // Timer CCR register (volatile for hardware registers)
unsigned char pin; // Pin bit
} PinConfig;
// Define the PWM pin configurations (e.g., P2.0, P2.4, P2.5)
PinConfig pwmPins[3] = {
{.dir = &P2DIR, .sel = &P2SEL, .ccr = &TA1CCR1, .pin = BIT0}, // P2.0 -> TA1.1
{.dir = &P2DIR, .sel = &P2SEL, .ccr = &TA2CCR1, .pin = BIT4}, // P2.4 -> TA2.1
{.dir = &P2DIR, .sel = &P2SEL, .ccr = &TA2CCR2, .pin = BIT5} // P2.5 -> TA2.2
};
// Function prototypes for the PWM sweep tasks
void pwmSweepTask1(void);
void pwmSweepTask2(void);
void pwmSweepTask3(void);
// Define the task list
tCB taskList[TASK_COUNT];
// Variables for each PWM duty cycle
int duty1 = 0, duty2 = 0, duty3 = 0; // Duty cycle trackers
int direction1 = 1, direction2 = 1, direction3 = 1; // Direction trackers
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int i; // Declare the loop variable outside the loop
// Configure PWM pins
for (i = 0; i < 3; i++) { // Use the loop variable declared above
*pwmPins[i].dir |= pwmPins[i].pin; // Set PWM pin as output
*pwmPins[i].sel |= pwmPins[i].pin; // Select Timer_A function for the PWM pin
}
// Configure Timer_A1 for P2.0
TA1CCR0 = 2000 - 1; // Set PWM period (1 kHz)
TA1CCTL1 = OUTMOD_7; // Set output mode to reset/set for TA1.1
TA1CTL = TASSEL_2 | MC_1; // SMCLK, up mode
// Configure Timer_A2 for P2.4 and P2.5
TA2CCR0 = 2000 - 1; // Set PWM period (1 kHz)
TA2CCTL1 = OUTMOD_7; // Set output mode to reset/set for TA2.1
TA2CCTL2 = OUTMOD_7; // Set output mode to reset/set for TA2.2
TA2CTL = TASSEL_2 | MC_1; // SMCLK, up mode
// Initialize the task list
taskList[0] = initTask(pwmSweepTask1, 1, 0, true); // PWM sweep task 1 (P2.0)
taskList[1] = initTask(pwmSweepTask2, 1, 0, true); // PWM sweep task 2 (P2.4)
taskList[2] = initTask(pwmSweepTask3, 1, 0, true); // PWM sweep task 3 (P2.5)
// Main loop
while (1) {
scheduler(); // Run the scheduler
__delay_cycles(1000); // Adjust for the system's Minor Cycle duration
}
}
// PWM Sweep Task for P2.0
void pwmSweepTask1(void) {
*pwmPins[0].ccr = duty1; // Update duty cycle for TA1.1
// Sweep logic
duty1 += direction1;
if (duty1 >= TA1CCR0 || duty1 <= 0) {
direction1 = -direction1; // Reverse direction at limits
}
}
// PWM Sweep Task for P2.4
void pwmSweepTask2(void) {
*pwmPins[1].ccr = duty2; // Update duty cycle for TA2.1
// Sweep logic
duty2 += direction2;
if (duty2 >= TA2CCR0 || duty2 <= 0) {
direction2 = -direction2; // Reverse direction at limits
}
}
// PWM Sweep Task for P2.5
void pwmSweepTask3(void) {
*pwmPins[2].ccr = duty3; // Update duty cycle for TA2.2
// Sweep logic
duty3 += direction3;
if (duty3 >= TA2CCR0 || duty3 <= 0) {
direction3 = -direction3; // Reverse direction at limits
}
}
Editor is loading...
Leave a Comment