Hello Dennis,
/*****************************
* This program generates a PWM output on P1.4 using push buttons P1.1 and P1.2.
* The defalut period and duty-cycle of the clock are set to 1000 and 100.
* The normal operating mode is LPM0 in which CPU is disabled and ACLK = 32KHz is active
* By pressing P1.1 and P1.2 the level of stimulation increases and decreases by 10% respectively.
* The timer period is ~(1000/32000)=31.2 ms.
*/
#include <msp430f5529.h>
#include <stdio.h>
#include <inttypes.h>
int DC = 0; //Duty cycle set to 100
int Period = 1000-1; //Period set to 1000
int main()
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= BIT4; //set P1.4 as output
P1SEL |= BIT4; //set P1.4 as digital IO
TA0CCR0 = Period; //Timer A0 introduced
P1REN |= BIT1; //P1.1 Resistor enabled
P1OUT |= BIT1; //P1.1 pull-up resistor
P2REN |= BIT1; //P2.1 Resistor enabled
P2OUT |= BIT1; //P2.1 pull-up resistor
P1IE |= 0x02; //enable P1.1 interrupt
P1IES |= 0x02; //select high to low edge
P1IFG &= ~0x02; //clear P1.1 flag
P2IE |= 0x02; //enable P2.1 interrupt
P2IES |= 0x02; //select high to low edge
P1IFG &= ~0x02; //clear P2.1 flag
__enable_interrupt();
__bis_SR_register(LPM0_bits); //set interrupts and go to LPM0
__no_operation();
while(1)
{}
}
//Port1 interrupt
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
if((P1IN & BIT1) != BIT1)
{
if(DC<999)
{
int i;
DC = DC + 100;
for(i=0; i<30000; i++);
printf("%d\n", DC);
__low_power_mode_3();
}
}
TA0CCTL3 = OUTMOD_7;
TA0CCR3 = DC;
TA0CTL = MC_1 + TASSEL_2 + TACLR;
P1IE |= BIT1;
P1IFG &= ~BIT1;
}
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{
if((P2IN & BIT1) != BIT1)
{
if(DC>1)
{
int j;
DC = DC - 100;
for(j=0; j<30000; j++);
printf("%d\n", DC);
__low_power_mode_3();
}
}
TA0CCTL3 = OUTMOD_7;
TA0CCR3 = DC;
TA0CTL = MC_1 + TASSEL_2 + TACLR;
P2IE |= BIT1;
P2IFG &= ~BIT1;
}
----------------------------------------------------------------------------------------
And here is the code using timer A1 to debounce the push buttons:
#include <msp430f5529.h>
#include <stdio.h>
#include <inttypes.h>
int DC = 0; //Duty cycle set to 0
int Period = 1000-1; //Period set to 1000
int main()
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= BIT4; //set P1.4 as output
P1SEL |= BIT4; //set P1.4 as digital IO
TA0CCR0 = Period; //Timer A0 introduced
P1REN |= BIT1; //P1.1 Resistor enabled
P1OUT |= BIT1; //P1.1 pull-up resistor
P2REN |= BIT1; //P2.1 Resistor enabled
P2OUT |= BIT1; //P2.1 pull-up resistor
P1IE |= 0x02; //enable P1.1 interrupt
P1IES |= 0x02; //select high to low edge
P1IFG &= ~0x02; //clear P1.1 flag
P2IE |= 0x02; //enable P2.1 interrupt
P2IES |= 0x02; //select high to low edge
P1IFG &= ~0x02; //clear P2.1 flag
TA1CCR0 = 160; //Timer A1 introduced
__enable_interrupt();
__bis_SR_register(LPM0_bits); //set interrupts and go to LPM0
__no_operation();
while(1)
{}
}
//Port1 interrupt
int PB = 0; //PB is a variable to control the debounce status of push button ( 0 meaning it is not debounced)
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
if((P1IN & BIT1) != BIT1) //if P1.1 is pressed
{
if(DC<999)
{
TA1CCTL0 = CCIE; //Timer-A1 enabled
TA1CTL = TASSEL_2 + MC_2 + TACLR; //timer-A1 countmode
if (PB == 1) // the push button is debounced
{
DC = DC + 100; //increment the duty cycle
__low_power_mode_3();
}
}
}
//pwm signal
TA0CCTL4 = OUTMOD_7;
TA0CCR4 = DC;
TA0CTL = MC_1 + TASSEL_2 + TACLR;
P1IE |= BIT1;
P1IFG &= ~BIT1;
}
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{
if((P2IN & BIT1) != BIT1) //if P2.1 is pressed
{
if(DC>1)
{
TA1CCTL0 = CCIE; //Timer A1 enabled
TA1CTL = TASSEL_2 + MC_2 + TACLR; //timer A1 countmode
if (PB == 1) //push button is debounced
{
DC = DC - 100; //decrement the duty cycle
__low_power_mode_3();
}
}
}
//pwm signal
TA0CCTL4 = OUTMOD_7;
TA0CCR4 = DC;
TA0CTL = MC_1 + TASSEL_2 + TACLR;
P2IE |= BIT1;
P2IFG &= ~BIT1;
}
//Timer A1 ISR
#pragma vector = TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void)
{
long x = 5000;
x += 160;
if (x>=5320) //2 successive period of 160 added
{
//button is pressed
x -= 160;
if(x <= 4360) //6 successive period of 160 subtracted
{
//button is released
PB = 1;
}
}
}