Hi
I am working on small lift in my code i used delay cycles but i want to use timerA.And tried something but it didn't work.
#include "msp430g2553.h"
#define SW1 BIT3
#define SW2 BIT4
#define SW3 BIT5
#define SEG_A BIT0
#define SEG_B BIT1
#define SEG_C BIT2
#define SEG_D BIT3
const unsigned int digits[5] = {SEG_A, SEG_B ,SEG_C, SEG_D};
#define DMASK ~(SEG_A + SEG_B + SEG_C + SEG_D)
volatile unsigned int x;
int currentposition;
int a;
int b;
int c;
int t;
int language;
int nextposition;
void go_up(void);
void go_down(void);
void main(void)
{
currentposition = 2;
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
P2DIR = (SEG_A + SEG_B + SEG_C + SEG_D );
P1DIR = ~SW1 + ~SW2 + ~SW3; // Set SW pin -> Input
P1REN = SW1 + SW2 + SW3; // Enable Resistor for SW pin
P1OUT = SW1 + SW2 + SW3;
P1IE |= BIT3 + BIT4 + BIT5;
P1IES |= BIT3 + BIT4 + BIT5;
P1REN |= BIT3 + BIT4 + BIT5;
P1OUT |= BIT3;
P1IFG &= ~BIT3 + ~BIT4 + ~BIT5;
while(1) //Loop forever, we work with interrupts!
{_BIS_SR(CPUOFF + GIE); }
// Enter LPM0 w/ interrupt
}
// The address of Port 1 interrupt service routine is PORT1_VECTOR
#pragma vector=PORT1_VECTOR
__interrupt void button (void)
{
if(!(P1IN & SW1)) // Check if SW is pressed
{
__delay_cycles(10000);
a = 1;
nextposition = currentposition - a;
if(nextposition == 1)
{
language = 1;
go_dow();
}
if(nextposition == 2)
{
language = 4;
go_dow();
}
currentposition = 1;
}
if(!(P1IN & SW2)) // Check if SW is pressed
{
__delay_cycles(10000);
b = 2;
nextposition = currentposition - b;
if(nextposition == -1)
{
language = 2;
go_dow();
}
if(nextposition == 1)
{
language = 1;
go_dow();
}
currentposition = 2;
}
if(!(P1IN & SW3)) // Check if SW is pressed
{
c = 3;
nextposition = currentposition - c ;
if(nextposition == -2)
{
language = 3;
go_dow();
}
if(nextposition == -1)
{
language = 2;
go_dow();
}
currentposition = 3;
}
P1IFG &= ~BIT3;
__delay_cycles(1000);
}
void go_up(void) {
volatile unsigned int i;
for(x = 0; x < 100; x++)
{
for(i = 0; i < 4; i++)
{
P2OUT = (P2OUT & DMASK) + digits[i];
__delay_cycles(10000);
}
__delay_cycles(1000);
}
P2OUT = 0;
}
void go_down(void) {
volatile unsigned int i;
for(t = 100; t > 0; t--)
{
for(i = 4; i > 0; i--)
{
P2OUT = (P2OUT & DMASK) + digits[i];
__delay_cycles(10000);
}
__delay_cycles(1000);
}
P2OUT = 0;
}
void go_dow(void) {
switch (language) {
case 1:
{
go_down();
language = 0;
break;
}
case 2:
{
go_up();
language = 0;
break;
}
case 3:
{
go_up();
go_up();
language = 0;
break;
}
case 4:
{
go_down();
go_down();
language = 0;
break;
}
}
}
#include "msp430g2553.h"
#define SW1 BIT3
#define SW2 BIT4
#define SW3 BIT5
#define SEG_A BIT0
#define SEG_B BIT1
#define SEG_C BIT2
#define SEG_D BIT3
#define DMASK ~(SEG_A + SEG_B + SEG_C + SEG_D)
const unsigned int digits[5] = { SEG_A, SEG_B, SEG_C, SEG_D };
volatile unsigned int x;
int currentposition;
int a;
int b;
int c;
int t;
int language;
int nextposition;
void go_up(void);
void go_down(void);
volatile unsigned char delay;
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void)
{
delay = 0;
}
void timer_delay(unsigned int msec)
{
delay = 1;
if(msec > 174)
msec = 174;
TACCR0 = msec*375 + TA0R;
TACCTL0 = CCIE;
while(delay>0);
TACCTL0 &= ~CCIE;
}
void timer_init(void)
{
BCSCTL2 |= DIVS_3;
TACTL |= TASSEL_2 | ID_2;
TACTL |= TACLR | MC_2;
}
void main(void)
{
currentposition = 2;
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
P2DIR |= (SEG_A + SEG_B + SEG_C + SEG_D);
P1DIR &= ~(SW1 + SW2 + SW3); // Set SW pin -> Input
P1REN |= (SW1 + SW2 + SW3); // Enable Resistor for SW pin
P1OUT |= (SW1 + SW2 + SW3);
P1IE |= BIT3 + BIT4 + BIT5;
P1IES |= BIT3 + BIT4 + BIT5;
P1REN |= BIT3 + BIT4 + BIT5;
P1OUT |= BIT3;
P1IFG &= ~(BIT3 + BIT4 + BIT5);
timer_init();
while (1) //Loop forever, we work with interrupts!
{
_BIS_SR(CPUOFF + GIE);
}
// Enter LPM0 w/ interrupt
}
void go_dow(void);
// The address of Port 1 interrupt service routine is PORT1_VECTOR
#pragma vector=PORT1_VECTOR
__interrupt void button(void)
{
if (!(P1IN & SW1)) // Check if SW is pressed
{
timer_delay(8);
//
a = 1;
nextposition = currentposition - a;
if (nextposition == 1)
{
language = 1;
go_dow();
}
if (nextposition == 2)
{
language = 4;
go_dow();
}
currentposition = 1;
}
if (!(P1IN & SW2)) // Check if SW is pressed
{
timer_delay(8);
//
b = 2;
nextposition = currentposition - b;
if (nextposition == -1)
{
language = 2;
go_dow();
}
if (nextposition == 1)
{
language = 1;
go_dow();
}
currentposition = 2;
}
if (!(P1IN & SW3)) // Check if SW is pressed
{
c = 3;
nextposition = currentposition - c;
if (nextposition == -2)
{
language = 3;
go_dow();
}
if (nextposition == -1)
{
language = 2;
go_dow();
}
currentposition = 3;
}
P1IFG &= ~BIT3;
timer_delay(1);
}
void go_up(void)
{
volatile unsigned int i;
for (x = 0; x < 100; x++)
{
for (i = 0; i < 4; i++)
{
P2OUT = (P2OUT & DMASK) + digits[i];
timer_delay(8);
//
}
timer_delay(1);
//
}
P2OUT = 0;
}
void go_down(void)
{
volatile unsigned int i;
for (t = 100; t > 0; t--)
{
for (i = 4; i > 0; i--)
{
P2OUT = (P2OUT & DMASK) + digits[i];
timer_delay(8);
//
}
timer_delay(1);
//
}
P2OUT = 0;
}
void go_dow(void)
{
switch (language)
{
case 1:
{
go_down();
language = 0;
break;
}
case 2:
{
go_up();
language = 0;
break;
}
case 3:
{
go_up();
go_up();
language = 0;
break;
}
case 4:
{
go_down();
go_down();
language = 0;
break;
}
}
}