- Get link
- X
- Other Apps
Pulse Width Modulation
Program
C Program Code for PWM Generation (Step wise)
#include <msp430.h>
int a[5] = {0,32,64,128,255};
int i = 0;
void main(void){
WDTCTL = WDTPW|WDTHOLD; // Stop WDT
//IE1 |= WDTIE; // enable Watchdog timer interrupts
P1DIR |= BIT6; // Green LED for output
P1SEL |= BIT6; // Green LED Pulse width modulation
TA0CCR0 = 512; // PWM period
TA0CCR1 = a[0]; // PWM duty cycle
TA0CCTL1 = OUTMOD_7; // TA0CCR1 reset/set-high voltage
// below count, low voltage when past
TA0CTL = TASSEL_2 + MC_1 + TAIE +ID_3;
// Timer A control set to SMCLK, 1MHz and count up mode MC_1
_bis_SR_register(LPM0_bits + GIE); // Enter Low power mode 0
while (1);
}
#pragma vector= TIMER0_A1_VECTOR // Watchdog Timer ISR
__interrupt void timer(void) {
TA0CTL &= ~TAIFG;
TA0CCR1 = a[++i];
_delay_cycles(500000);
if (i>4)
{
i=0;
}
}
C Program Code for PWM Generation (0 to maximum continuous brightness)
#include <msp430.h>
int i = 0;
void main(void){
WDTCTL = WDTPW|WDTHOLD; // Stop WDT
//IE1 |= WDTIE; // enable Watchdog timer interrupts
P1DIR |= BIT6; // Green LED for output
P1SEL |= BIT6; // Green LED Pulse width modulation
TA0CCR0 = 512; // PWM period
TA0CCR1 = 1; // PWM duty cycle
TA0CCTL1 = OUTMOD_7; // TA0CCR1 reset/set-high voltage
// below count, low voltage when past
TA0CTL = TASSEL_2 + MC_1 + TAIE +ID_3;
// Timer A control set to SMCLK, 1MHz and count up mode MC_1
_bis_SR_register(LPM0_bits + GIE); // Enter Low power mode 0
while (1);
}
#pragma vector= TIMER0_A1_VECTOR // Watchdog Timer ISR
__interrupt void timer(void) {
TA0CTL &= ~TAIFG;
TA0CCR1 = TA0CCR1+1;
_delay_cycles(10000);
if (TA0CCR1>512)
{
TA0CCR1=1;
}
}
C Program Code for PWM Generation (maximum to 0 continuous brightness)
#include <msp430.h>
int i = 0;
void main(void){
WDTCTL = WDTPW|WDTHOLD; // Stop WDT
//IE1 |= WDTIE; // enable Watchdog timer interrupts
P1DIR |= BIT6; // Green LED for output
P1SEL |= BIT6; // Green LED Pulse width modulation
TA0CCR0 = 512; // PWM period
TA0CCR1 = 1; // PWM duty cycle
TA0CCTL1 = OUTMOD_7; // TA0CCR1 reset/set-high voltage
// below count, low voltage when past
TA0CTL = TASSEL_2 + MC_3 + TAIE +ID_3;
// Timer A control set to SMCLK, 1MHz and count down mode MC_3
_bis_SR_register(LPM0_bits + GIE); // Enter Low power mode 0
while (1);
}
#pragma vector= TIMER0_A1_VECTOR // Watchdog Timer ISR
__interrupt void timer(void) {
TA0CTL &= ~TAIFG;
TA0CCR1 = TA0CCR1+1;
_delay_cycles(25000);
if (TA0CCR1>512)
{
TA0CCR1=1;
}
}
Comments
Post a Comment