This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430F2131T

Other Parts Discussed in Thread: TPS62260, MSP430F2131, MAX3221

Hi,

I'm trying to read some data with the UART through the PIN 2.0 but I read every-time 255.

You can see my code below:

/******************************************************************************/
/*  Project: Bubble Tube Demonstrator.                                        */
/*           RGB-LED colour cycle using TI MSP430F2131 and TI TPS62260.       */
/*                                                                            */
/*  (c) Pulsonic Technologies Ltd., 2012. All Rights Reserved.                */
/*                                                                            */
/*  v1.00   26/03/2012  DPP Initial Release.                                  */
/*                                                                            */
/*  Description:                                                              */
/*  Timer_A3 is used to generate 3 PWM signals. Timer overflow generates an   */
/*  interrupt and in its interrupt service routine the PWM outputs are        */
/*  reset. Output Units will set the PWM outputs.                             */
/*  Software starts in a demo mode that automatically changes the PWM         */
/*  settings. As soon as the incremental encoder is operated the automatic    */
/*  mode stops and the adjustment of the colour can be done manually.         */
/*                                                                            */
/*  Hardware platform: MSP430F21x1                                            */
/*                                                                            */
/*  Added MSP430x21x1 - Timer_A UART 9600, 1MHz DCO SMCLK                     */
/*                                                                            */
/*  A full-duplex 9600-baud UART using Timer_A3 and the DCO. A character is   */
/*  echoed on the Hyperterminal of a PC. The DCO frequency settings are stored*/
/*  in INFOA flash segment.                                                   */
/*  ACLK = n/a, MCLK = SMCLK = saved DCO 1Mhz                                 */
/*  External watch crystal installed on XIN XOUT is required for ACLK         */
/*                                                                            */
/*                MSP430F21x1                                                 */
/*            -----------------                                               */
/*        /|\|              XIN|-                                             */
/*         | |                 |                                              */
/*         --|RST          XOUT|-                                             */
/*           |                 |                                              */
/*           |             P1.3|--------> Power for MAX3221                   */
/*           |   CCI0A/TXD/P1.1|-------->                                     */
/*           |                 | 9600 8N1                                     */
/*           |   CCI0B/RXD/P2.2|<--------                                     */
/*              
//
//        ???????   25/05/12
//                  MSP430F21x1
//            -----------------
//        /|\|              XIN|-
//         | |                 | 32.768kHz
//         --|RST          XOUT|-
//           |                 |
//           |             P2.1|----------->
//           |                 | 9600 - 8N1
//           |             P2.0|<-----------                                   

*/
/*----------------------------------------------------------------------------*/
/******************************************************************************/
#include "msp430x21x1.h"

unsigned int i=0,j=0;
unsigned char freerun = 0;

#define  LED_TabLength    252*4

#define REDBTN     0x90
#define GREENBTN   0x70
#define BLUEBTN    0x30
#define CYANBTN    0x80
#define MAGENTABTN 0x60
#define YELLOWBTN  0x20
#define STARTBTN   0x50
#define STOPBTN    0x40

// Blue LED colour table.
const unsigned int BlueLEDs[]=
{
  65385,65385,65385,65385,65385,65385,65385,65385,
  65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
  65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
  65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,57866,52635,47404,
  42173,36943,34327,31712,29096,26481,23866,21250,19942,18635,17327,16019,14712,
  13404,12096,10789, 9481, 8173, 7519, 6865, 6212, 5558, 4904, 4577, 4250, 3923,
   3596, 3269, 2942, 2615, 2288, 1962, 1635, 1308,  981,  654,  327,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  327,  654,  981, 1308, 1635, 1962, 2288, 2615,
   2942, 3269, 3596, 3923, 4250, 4577, 4904, 5558, 6212, 6865, 7519, 8173, 9481,
  10789,12096,13404,14712,16019,17327,18635,19942,21250,23866,26481,29096,31712,
  34327,36943,39558,42173,44789,47404,52635,57866,65385,65385,65385,65385,65385,
  65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
  65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
  65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385
}; //blue LED

// Green LED colour table.
const unsigned int GreenLEDs[]=
{  
    150,  295,  622,  949, 1276, 1603, 1930, 2256,
   2583, 2910, 3237, 3564, 3891, 4218, 4545, 4872, 5526, 6180, 6833, 7487, 8141,
   9449,10757,12064,13372,14680,15987,17295,18603,19910,21218,23834,26449,29064,
  31680,34295,36911,39526,42141,44757,47372,52603,57834,65353,65353,65353,65353,
  65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
  65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
  65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
  65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
  65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
  65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
  65353,65353,65353,57834,52603,47372,42141,36911,34295,31680,29064,26449,23834,
  21218,19910,18603,17295,15987,14680,13372,12064,10757, 9449, 8141, 7487, 6833,
   6180, 5526, 4872, 4545, 4218, 3891, 3564, 3237, 2910, 2583, 2256, 1930, 1603,
   1276,  949,  622,  295,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150
};  //green LED

// Red LED colour table.
const unsigned int RedLEDs[]=
{
    150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
    150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  311,
    638,  965, 1292, 1619, 1946, 2272, 2599, 2926, 3253, 3580, 3907, 4234, 4561,
   4888, 5542, 6196, 6849, 7503, 8157, 9465,10773,12080,13388,14696,16003,17311,
  18619,19926,21234,23850,26465,29080,31696,34311,36927,39542,42157,44773,47388,
  52619,57850,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
  65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
  65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
  65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
  65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
  65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
  65369,65369,65369,65369,65369,65369,65369,65369,65369,57850,52619,47388,42157,
  36927,34311,31696,29080,26465,23850,21234,19926,18619,17311,16003,14696,13388,
  12080,10773, 9465, 8157, 7503, 6849, 6196, 5542, 4888, 4561, 4234, 3907, 3580,
   3253, 2926, 2599, 2272, 1946, 1619, 1292,  965,  638,  311,  150
};  //red LED


unsigned int LEDptr=0;               
unsigned char BAold=0;

#define RAISE 0x01
#define CLEAR 0x00

#define RXD   0x01                          // RXD on P2.0
#define TXD   0x02                          // TXD on P2.1

//   Conditions for 9600 Baud SW UART, DCO = 1MHz
#define Bitime_5  52                        // ~ 0.5 bit length
#define Bitime    104                       // ~ 9615 baud

unsigned int RXTXData=0;
unsigned char BitCnt=0;

void Inc_Decoder(unsigned char BAnew);

void Delay(void);
void Delay2(void);
void Delay3(void);

void TX_Byte(void);
void RX_Ready(void);

//------------------------------------------------------------------------------
// Main Program
void main(void)
{
  unsigned int ix=0, tmp=0, resync=0x01;
  unsigned char i=0;
 
  WDTCTL = WDTPW + WDTHOLD;       // disable Watchdog

//  BCSCTL1 = CALBC1_1MHZ;          // Set DCO for UART
//  DCOCTL = CALDCO_1MHZ;
  BCSCTL1= CALBC1_8MHZ;       //--- System Clock Settings ----------------------
  DCOCTL = CALDCO_8MHZ;       // use calibrated 8MHz settings
 
  //TACTL = TASSEL_1 + MC_2 + TACLR;      // SMCLK, cont-mode, clear for UART
 
  //---- PWM Timer Initialization ------------------
  TACTL = TASSEL_2+ID_0+MC_0+TACLR+TAIE;  // Timer clock = SMCLK = 8MHz
 
  TACCTL0 = CM_0+CCIS_2+OUTMOD_1; // All Output Units will set PWM outputs if   
  TACCTL1 = CM_0+CCIS_2+OUTMOD_1; // TACCRx=TAR. Resetting PWM outputs is done
  TACCTL2 = CM_0+CCIS_2+OUTMOD_1; // by software.
 
  LEDptr=0;
  TACCR0=BlueLEDs[LEDptr>>2]; // LEDptr is shifted right twice,
  TACCR1=GreenLEDs[LEDptr>>2]; //                  this means divided by 4
  TACCR2=RedLEDs[LEDptr>>2];
 
  // UART Port Initialisation  // right port number  are P2.0 and P2.1

  CCTL1 = OUT;                              // TXD Idle as Mark
  P2SEL = TXD;                              // P2.1/TA0 for TXD function
  P2DIR = TXD;                              // TXD output on P2
  P2SEL = RXD;                              // P2.0/TA0 as RXD input

  P1DIR |= 0x08;                            // Power MAX3221
  P1OUT |= 0x08;                            //
     
  //--- Port Initialization ----------------------------------
  P1SEL = 0x0E;     // P1.1, P1.2, P1.3 are used as PWM Timer Outputs
  P1OUT = 0x00;     // P1.0 is output (Enable for TPS62260)
  P1DIR = 0xFF;     // P1.4, P1.5, P1.6, P1.7 are not used => digital outputs
  //P2OUT = 0x04;     // P2.0 and P2.1 are not used => digital outputs =========>  NOW USED FOR UART
  P2DIR|= 0xE4;     // P2.3, P2.4 are digital inputs => incremental encoder
                    // P2.5, P2.6, P2.7 are not used => digital inputs

  BAold=0x01;       //--- initialize decoder for incremental encoder -----------

  Delay();          // Delay loop
  TACTL |= MC_2;    // start Timer_A (continuous mode)

  __enable_interrupt();   // enables maskable interrupts

  tmp = P2IN & 0x18;

//--- Main Loop ----------------------------------------------------------------
  for (;;)
  {
      RXTXData = 0;
      RX_Ready();                               // UART ready to RX one Byte
     
     /* P1DIR |=0x00;
       while(1)
      {  
        if ((0x01 & P2IN))         P1OUT |=0x01;
        else P1OUT &= ~0x01;
      }*/
      
      if ((0x01 & P2IN))
      {
       for (i = 0 ; i<BitCnt ; i++)
       {
          RXTXData = RXTXData >> 1;
          if (CCTL1 & SCCI)           // Get bit waiting in receive latch
             RXTXData |= 0x80;
       }
      }

      if( RXTXData)
      {
        switch (RXTXData)
        {
          
        // red  
          case REDBTN:
            LEDptr = 138;
            break;
              
          // green
          case GREENBTN:
            LEDptr = 47;
            break;
            
          //blue
          case BLUEBTN:
            LEDptr = 242;
            break;
            
          //cyan
          case CYANBTN:
            LEDptr = 47;
            break;
            
          //magenta
          case MAGENTABTN:
            LEDptr = 138;
            break;
          
          // yellow
          case YELLOWBTN:
              LEDptr = 0;
                break;
                
          // start
          case STARTBTN:
            LEDptr = 0;
            freerun = 1;
            break;
            
          // stop
          case STOPBTN:
            freerun = 0;
            break;
            
          // default
          default:
            break;
        }
      }

      
    if (freerun)
    {
       if (LEDptr == 0)                 // Raise signal to other unit to
      {                                // resynch.
        resync = RAISE;
        P1OUT = RAISE;
        //for(ix=0; ix<=1000; ix++) {;}    // delay loop (used for debouncing)
        for(ix=0; ix<=500; ix++) {;}    // delay loop (used for debouncing)
      }
      if ((P1IN & RAISE) == resync)
      {
        LEDptr = 0;        
        resync = CLEAR;
        P1OUT = CLEAR;
        //for(ix=0; ix<=1000; ix++) {;}    // delay loop (used for debouncing)
        for(ix=0; ix<=500; ix++) {;}    // delay loop (used for debouncing)
      }
      LEDptr++;
      if (LEDptr >= LED_TabLength)    // if at end of colour arrays tables...
        LEDptr = 0;                   // ..wraparound colour arrays index
      if ((P2IN & 0x18) != tmp)       // reset LEDptr if encoder operated.
      {
        LEDptr = 0;
        tmp = (P2IN & 0x18);
      }
    }
    else
    {
        TX_Byte();
     }
    
    //Delay();
    Delay3();
      
    
  }

 
#if 0  
//--- Main Loops ---------------------------------------------------------------
  // Automatic settings loop (until encoder input detected).
/*  while ((P2IN & 0x18) == tmp) //--- change settings automatically until the
  {                             //             incremental encoder is operated
    Delay();
    LEDptr++;
    if (LEDptr >= LED_TabLength)
      LEDptr = 0;
  }

  // Manual settings loop (forever).
  for(;;)                  //--- change settings manually (incremental encoder)
  {
    Inc_Decoder(0x03 & (P2IN>>3));   // check incremental decoder
    for(ix=0; ix<=1000; ix++) {;}    // delay loop (used for debouncing)
    RX_Ready();                      // UART ready to RX one Byte
    _BIS_SR(LPM4_bits + GIE);        // Enter LPM4 w/ interr until char RXed
    Delay();
    TX_Byte();                       // TX Back RXed Byte Received
  }*/
#endif //0  
}

// Function Transmits Character from RXTXData Buffer
void TX_Byte(void)
{
  ////// added dpp 27-03-12
  if (RXTXData > 0)
    LEDptr += (RXTXData>>2);
  if (LEDptr >= LED_TabLength)
    LEDptr = 0;   
  ////// added dpp 27-03-12
 
  BitCnt = 0xA;                      // Load Bit counter, 8data + ST/SP
  CCR0 = TAR;                        // Current state of TA counter
  CCR0 += Bitime;                    // Some time till first bit
  RXTXData |= 0x100;                 // Add mark stop bit to RXTXData
  RXTXData = RXTXData << 1;          // Add space start bit
  CCTL0 = OUTMOD0 + CCIE;            // TXD = mark = idle
  while ( CCTL0 & CCIE );            // Wait for TX completion
}

// Function Readies UART to Receive Character into RXTXData Buffer
// Sync capture not possible as DCO=TACLK=SMCLK can be off !!
void RX_Ready (void)
{
  BitCnt = 0x8;                             // Load Bit counter
  //CCTL1 = CM1 + CCIS1 + OUTMOD1 + CAP + CCIE; // Neg Edge, Cap
  CCTL1 = SCS + OUTMOD0 + CM1 + CAP + CCIE + CCIS_1;   // Sync, Neg Edge, Cap
}

//------------------------------------------------------------------------------
// Delay Loop
void Delay(void)
{
  unsigned int i=0, j=0;
 
  for(i=0;i<=10000;i++)   // delay loop
    for(j=0;j<=3;j++);

}

// Delay Loop
void Delay2(void)
{
  unsigned int i=0;
 
  for(i=0;i<=10000;i++) {;}   // delay loop

}

void Delay3(void)
{
  unsigned int i=0, j=0;
 
  for(i=0;i<=10000;i++)   // delay loop
    for(j=0;j<=5;j++);

}

//------------------------------------------------------------------------------
// Incremental Encoder Subroutine:
void Inc_Decoder(unsigned char BAnew)
{
  if (BAnew == 0x02)
  {
    if (BAold == 0x00)
    {
      LEDptr--;                  // decrement pointer if new state is 'b' and
      if (LEDptr>=LED_TabLength) //    old state was 'a'
        LEDptr=LED_TabLength;
    }
  }
 
  if (BAnew == 0x00)
  {
    if (BAold == 0x02)
    {
      LEDptr++;                  // increment pointer if new state is 'a' and
      if (LEDptr>=LED_TabLength) //    old state was 'b'
        LEDptr = 0;
    }       
  }
 
  BAold = BAnew;                   // store new state
}

//------------------------------------------------------------------------------
// Timer_A1 Interrupt Service Routine:
//  (a) update PWM duty cycle settings using colour table.
//  (b) generate PWM signal.
//------------------------------------------------------------------------------
#pragma vector=TIMERA1_VECTOR
__interrupt void ISR_TimerA1(void)
{
  P1OUT |= 0x01;          // activate LEDs
 
  //--- update PWM duty cycle settings using colour table
  TACCR0 = BlueLEDs[LEDptr>>2]; // LEDptr is shifted right twice,
  TACCR1 = GreenLEDs[LEDptr>>2]; //                this means divided by 4
  TACCR2 = RedLEDs[LEDptr>>2];

  //--- PWM signal generation
  TACTL   &= ~TAIFG;
  TACCTL0 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
  TACCTL0 |= OUTMOD_1;  // OUTMOD_1 => set PWM output TA0 as soon as TACCR0=TAR
  TACCTL1 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
  TACCTL1 |= OUTMOD_1;  // OUTMOD_1 => set PWM output TA1 as soon as TACCR1=TAR
  TACCTL2 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
  TACCTL2 |= OUTMOD_1;  // OUTMOD_1 => set PWM otuput TA2 as soon as TACCR2=TAR
                        //     TAR = Timer_A counter
}

//------------------------------------------------------------------------------
// Timer A0 interrupt service routine:
//  (a) transmit or receive data via UART.
//------------------------------------------------------------------------------
/*#pragma vector=TIMERA0_VECTOR
__interrupt void ISR_TimerA0(void)
{
  CCR0 += Bitime;                 // Add Offset to CCR0

  if (CCTL0 & CCIS0)              // RX on CCI0B?
  { // RX
    if( CCTL0 & CAP )             // Capture mode = start bit edge
    {
      CCTL0 &= ~ CAP;             // Switch from capture to compare mode
      CCR0 += Bitime_5;
      _BIC_SR_IRQ(SCG1 + SCG0);   // DCO reamins on after reti
    }
    else
    {
      RXTXData = RXTXData >> 1;
      if (CCTL0 & SCCI)           // Get bit waiting in receive latch
      RXTXData |= 0x80;
      BitCnt --;                  // All bits RXed?
      if ( BitCnt == 0)
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      {
      CCTL0 &= ~ CCIE;            // All bits RXed, disable interrupt
      _BIC_SR_IRQ(LPM4_bits);     // Clear LPM4 bits from 0(SR)
      }
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      LEDptr=0;                   // reset arrays ptr on RX.
    }
  }
  else
  { // TX
    if ( BitCnt == 0)
    CCTL0 &= ~ CCIE;              // All bits TXed, disable interrupt
    else
    {
      CCTL0 |=  OUTMOD2;          // TX Space
      if (RXTXData & 0x01)
      CCTL0 &= ~ OUTMOD2;         // TX Mark
      RXTXData = RXTXData >> 1;
      BitCnt --;
    }
  }
}
*/
/*** End of file: HPA338RevA.c ***/

  • Dimitri Marquois said:
      P2SEL = TXD;                              // P2.1/TA0 for TXD function
      P2DIR = TXD;                              // TXD output on P2
      P2SEL = RXD;                              // P2.0/TA0 as RXD input

    The second assignment to P2SEL undoes the first one. You probably meant "P2SEL|=TXD;"
    There are several similar cases throughout your code. So your port configurations are messed-up.
    If you fixed this and still have problems, I'll look deeper into the code.

  • My UART works, and I can control LEDs. Now I have to work for add the pwm. Can I use a interrupt for PWM an UART ?

    /******************************************************************************/
    /*  Project: Bubble Tube Demonstrator.                                        */
    /*           RGB-LED colour cycle using TI MSP430F2131 and TI TPS62260.       */
    /*                                                                            */
    /*  (c) Pulsonic Technologies Ltd., 2012. All Rights Reserved.                */
    /*                                                                            */
    /*  v1.00   26/03/2012  DPP Initial Release.                                  */
    /*                                                                            */
    /*  Description:                                                              */
    /*  Timer_A3 is used to generate 3 PWM signals. Timer overflow generates an   */
    /*  interrupt and in its interrupt service routine the PWM outputs are        */
    /*  reset. Output Units will set the PWM outputs.                             */
    /*  Software starts in a demo mode that automatically changes the PWM         */
    /*  settings. As soon as the incremental encoder is operated the automatic    */
    /*  mode stops and the adjustment of the colour can be done manually.         */
    /*                                                                            */
    /*  Hardware platform: MSP430F21x1                                            */
    /*                                                                            */
    /*  Added MSP430x21x1 - Timer_A UART 9600, 1MHz DCO SMCLK                     */
    /*                                                                            */
    /*  A full-duplex 9600-baud UART using Timer_A3 and the DCO. A character is   */
    /*  echoed on the Hyperterminal of a PC. The DCO frequency settings are stored*/
    /*  in INFOA flash segment.                                                   */
    /*  ACLK = n/a, MCLK = SMCLK = saved DCO 1Mhz                                 */
    /*  External watch crystal installed on XIN XOUT is required for ACLK         */
    /*                                                                            */
    /*                MSP430F21x1                                                 */
    /*            -----------------                                               */
    /*        /|\|              XIN|-                                             */
    /*         | |                 |                                              */
    /*         --|RST          XOUT|-                                             */
    /*           |                 |                                              */
    /*           |             P1.3|--------> Power for MAX3221                   */
    /*           |   CCI0A/TXD/P1.1|-------->                                     */
    /*           |                 | 9600 8N1                                     */
    /*           |   CCI0B/RXD/P2.2|<--------                                     */
    /*              
    //
    //        ???????   25/05/12
    //                  MSP430F21x1
    //            -----------------
    //        /|\|              XIN|-
    //         | |                 | 32.768kHz
    //         --|RST          XOUT|-
    //           |                 |
    //           |             P2.1|----------->
    //           |                 | 9600 - 8N1
    //           |             P2.0|<-----------                                   

    */
    /*----------------------------------------------------------------------------*/
    /******************************************************************************/
    #include "msp430x21x1.h"

    unsigned int i=0,j=0;
    unsigned char freerun = 0;

    #define  LED_TabLength    252*4

    #define REDBTN     0x5A     // 90
    #define GREENBTN   0x46     // 70
    #define BLUEBTN    0x1E     // 30
    #define CYANBTN    0x50     // 80
    #define MAGENTABTN 0x3C     // 60
    #define YELLOWBTN  0x14     // 20
    #define STARTBTN   0x32     // 50
    #define STOPBTN    0x28     // 40

    // Blue LED colour table.
    const unsigned int BlueLEDs[]=
    {
      65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,57866,52635,47404,
      42173,36943,34327,31712,29096,26481,23866,21250,19942,18635,17327,16019,14712,
      13404,12096,10789, 9481, 8173, 7519, 6865, 6212, 5558, 4904, 4577, 4250, 3923,
       3596, 3269, 2942, 2615, 2288, 1962, 1635, 1308,  981,  654,  327,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  327,  654,  981, 1308, 1635, 1962, 2288, 2615,
       2942, 3269, 3596, 3923, 4250, 4577, 4904, 5558, 6212, 6865, 7519, 8173, 9481,
      10789,12096,13404,14712,16019,17327,18635,19942,21250,23866,26481,29096,31712,
      34327,36943,39558,42173,44789,47404,52635,57866,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385
    }; //blue LED

    // Green LED colour table.
    const unsigned int GreenLEDs[]=
    {  
        150,  295,  622,  949, 1276, 1603, 1930, 2256,
       2583, 2910, 3237, 3564, 3891, 4218, 4545, 4872, 5526, 6180, 6833, 7487, 8141,
       9449,10757,12064,13372,14680,15987,17295,18603,19910,21218,23834,26449,29064,
      31680,34295,36911,39526,42141,44757,47372,52603,57834,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,57834,52603,47372,42141,36911,34295,31680,29064,26449,23834,
      21218,19910,18603,17295,15987,14680,13372,12064,10757, 9449, 8141, 7487, 6833,
       6180, 5526, 4872, 4545, 4218, 3891, 3564, 3237, 2910, 2583, 2256, 1930, 1603,
       1276,  949,  622,  295,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150
    };  //green LED

    // Red LED colour table.
    const unsigned int RedLEDs[]=
    {
        150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,
        150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  150,  311,
        638,  965, 1292, 1619, 1946, 2272, 2599, 2926, 3253, 3580, 3907, 4234, 4561,
       4888, 5542, 6196, 6849, 7503, 8157, 9465,10773,12080,13388,14696,16003,17311,
      18619,19926,21234,23850,26465,29080,31696,34311,36927,39542,42157,44773,47388,
      52619,57850,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,57850,52619,47388,42157,
      36927,34311,31696,29080,26465,23850,21234,19926,18619,17311,16003,14696,13388,
      12080,10773, 9465, 8157, 7503, 6849, 6196, 5542, 4888, 4561, 4234, 3907, 3580,
       3253, 2926, 2599, 2272, 1946, 1619, 1292,  965,  638,  311,  150
    };  //red LED


    unsigned int LEDptr=0;               

    #define RAISE 0x01
    #define CLEAR 0x00

    #define RXD   0x01                          // RXD on P2.0
    #define TXD   0x02                          // TXD on P2.1

    //   Conditions for 9600 Baud SW UART, DCO = 1MHz
    #define Bitime_5  52                        // ~ 0.5 bit length
    #define Bitime    104                       // ~ 9615 baud

    unsigned int RXTXData=0;
    unsigned char BitCnt=8;

    void Delay(unsigned int d);
    void TX_Byte(void);
    void RX_Ready(void);
    //------------------------------------------------------------------------------
    // Main Program
    void main(void)
    {
     
    //  unsigned int resync=0x01;
    //  unsigned char i=0;
     
      WDTCTL = WDTPW + WDTHOLD;       // disable Watchdog

    //  BCSCTL1 = CALBC1_1MHZ;          // Set DCO for UART
    //  DCOCTL = CALDCO_1MHZ;
      BCSCTL1= CALBC1_8MHZ;       //--- System Clock Settings ----------------------
      DCOCTL = CALDCO_8MHZ;       // use calibrated 8MHz settings
     
      //TACTL = TASSEL_1 + MC_2 + TACLR;      // SMCLK, cont-mode, clear for UART
     
      //---- PWM Timer Initialization ------------------
      TACTL = TASSEL_2+ID_0+MC_2+TACLR+TAIE;  // Timer clock = SMCLK = 8MHz
        P1DIR = 0x0F;
        P1OUT = 0x0F;

      //LEDptr=0;
      //P1OUT |= 0x03;          // activate LEDs


      // UART Port Initialisation  // right port number  are P2.0 and P2.1
      //CCTL0 = OUT;                              // TXD Idle as Mark
      P2SEL = TXD + RXD;                              // P2.1/TA0 for TXD function
      P2DIR = TXD;                              // TXD output on P2
     
      //P1DIR |= 0x08;                            // Power MAX3221
      //P1OUT |= 0x08;                            //
         
      //--- Port Initialization ----------------------------------
     // P1SEL = 0x0E;     // P1.1, P1.2, P1.3 are used as PWM Timer Outputs
      //P1OUT = 0x00;     // P1.0 is output (Enable for TPS62260)
     // P1DIR = 0xFF;     // P1.4, P1.5, P1.6, P1.7 are not used => digital outputs
      //P2OUT = 0x04;     // P2.0 and P2.1 are not used => digital outputs =========>  NOW USED FOR UART
      //P2DIR|= 0xE4;     // P2.3, P2.4 are digital inputs => incremental encoder
                        // P2.5, P2.6, P2.7 are not used => digital inputs

      //============================================================================
      //Delay(30000);          // Delay loop
      TACTL |= MC_2;    // start Timer_A (continuous mode)
     
      __enable_interrupt();   // enables maskable interrupts

    //--- Main Loop ----------------------------------------------------------------
      for (;;)
      {
      //============================================================================
          //CCTL0 &= ~CCIE;
          while (0x01 & P2IN);    // wait for serial start
          CCTL0 &= CCIE;   
          RX_Ready();                               // UART ready to RX one Byte
          CCR0=TAR;
          CCR0 += 1250;
          CCTL0 = 0x0010;   // enable interrupt in compare mode.
         
          // Clear input bit register -- which one is this??      
      //============================================================================
          //RXTXData = 0;
          //RX_Ready();                               // UART ready to RX one Byte
          //while (0x01 & P2IN);    // wait for serial start
          //for (i = 0 ; i<8 ; i++)
          //{
          //    RXTXData = RXTXData >> 1;
          //    while (!(CCTL1 & SCCI)) ;          // Get bit waiting in receive latch
          //    if (0x01 & P2IN) RXTXData |= 0x80; // Set MSB.
         //}
      //============================================================================
          //Delay(2000);
          while(BitCnt != 0);
          
          if(RXTXData)
          {
            switch (RXTXData)
            {
              case REDBTN:      // red  
                P1OUT = 0x07;
                break;
                  
              case GREENBTN:    // green
                P1OUT = 0x0B;
                break;
                
              case BLUEBTN:     // blue
                P1OUT = 0x0D;
                break;
                
              case CYANBTN:     // cyan
                P1OUT = 0x09;
                break;
                
              case MAGENTABTN:  // magenta
                P1OUT = 0x05;
                break;
              
              case YELLOWBTN:   // yellow
                P1OUT = 0x03;
                    break;
                    
              case STARTBTN:    // start
              case STOPBTN:     // stop
              default:          // catch-all
                break;
            }
          }
          
      RXTXData = 0;
        
      }
    }

    //------------------------------------------------------------------------------
    // Function Transmits Character from RXTXData Buffer
    //------------------------------------------------------------------------------
    void TX_Byte(void)
    {
      if (RXTXData > 0)
        LEDptr += (RXTXData>>2);
      if (LEDptr >= LED_TabLength)
        LEDptr = 0;   
     
      BitCnt = 0xA;                      // Load Bit counter, 8data + ST/SP
      CCR0 = TAR;                        // Current state of TA counter
      CCR0 = TAR;                        // Current state of TA counter
      CCR0 += Bitime;                    // Some time till first bit
      RXTXData |= 0x100;                 // Add mark stop bit to RXTXData
      RXTXData = RXTXData << 1;          // Add space start bit
      CCTL0 = OUTMOD0 + CCIE;            // TXD = mark = idle
      while ( CCTL0 & CCIE );            // Wait for TX completion
    }

    //------------------------------------------------------------------------------
    // Function Readies UART to Receive Character into RXTXData Buffer
    // Sync capture not possible as DCO=TACLK=SMCLK can be off !!
    //------------------------------------------------------------------------------
    void RX_Ready (void)
    {
      BitCnt = 0x8;                             // Load Bit counter
      //CCTL1 = CM1 + CCIS1 + OUTMOD1 + CAP + CCIE; // Neg Edge, Cap
      //CCTL1 = SCS + OUTMOD0 + CM1 + CAP + CCIE + CCIS_1;   // Sync, Neg Edge, Cap
    }

    //------------------------------------------------------------------------------
    // Delay Loop
    //------------------------------------------------------------------------------
    void Delay(unsigned int d)
    {
      unsigned int i=0;
     
      for(i=0;i<d;i++) {;}   // delay loop
    }

    //------------------------------------------------------------------------------
    // Timer_A1 Interrupt Service Routine:
    //  (a) update PWM duty cycle settings using colour table.
    //  (b) generate PWM signal.
    //------------------------------------------------------------------------------

    #pragma vector=TIMERA1_VECTOR
    __interrupt void ISR_TimerA1(void)
    {
      P1OUT |= 0x01;          // activate LEDs
     
      //--- update PWM duty cycle settings using colour table
      TACCR0 = BlueLEDs[LEDptr>>2]; // LEDptr is shifted right twice,
      TACCR1 = GreenLEDs[LEDptr>>2]; //                this means divided by 4
      TACCR2 = RedLEDs[LEDptr>>2];

      //--- PWM signal generation
      TACTL   &= ~TAIFG;
      TACCTL0 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
      TACCTL0 |= OUTMOD_1;  // OUTMOD_1 => set PWM output TA0 as soon as TACCR0=TAR
      TACCTL1 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
      TACCTL1 |= OUTMOD_1;  // OUTMOD_1 => set PWM output TA1 as soon as TACCR1=TAR
      TACCTL2 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
      TACCTL2 |= OUTMOD_1;  // OUTMOD_1 => set PWM otuput TA2 as soon as TACCR2=TAR
                            //     TAR = Timer_A counter
    }


    //------------------------------------------------------------------------------
    // Timer A0 interrupt service routine:
    //  (a) transmit or receive data via UART.
    //------------------------------------------------------------------------------

    #pragma vector=TIMERA0_VECTOR
    __interrupt void TimerA0(void)
    {
      CCR0 += 833;    // set interrupt for next bit.
      RXTXData = RXTXData >> 1;
      if (0x01 & P2IN)  RXTXData |= 0x80;
      BitCnt--;                  // All bits RXed?
      if (BitCnt == 0)
      {
          CCTL0 &= ~ CCIE;            // All bits RXed, disable interrupt
          //BitCnt = 8;                 // Reset for next time.
      }
      /*
      CCR0 += Bitime;                 // Add Offset to CCR0
      if (CCTL0 & CCIS0)              // RX on CCI0B?
      { // RX
        if( CCTL0 & CAP )             // Capture mode = start bit edge
        {
          CCTL0 &= ~ CAP;             // Switch from capture to compare mode
          CCR0 += Bitime_5;
          _BIC_SR_IRQ(SCG1 + SCG0);   // DCO reamins on after reti
        }
        else
        {
          RXTXData = RXTXData >> 1;
          if (CCTL0 & SCCI)           // Get bit waiting in receive latch
            RXTXData |= 0x80;
          BitCnt --;                  // All bits RXed?
          if ( BitCnt == 0)
    //>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
          {
          CCTL0 &= ~ CCIE;            // All bits RXed, disable interrupt
          _BIC_SR_IRQ(LPM4_bits);     // Clear LPM4 bits from 0(SR)
          }
    //>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        }
      }
      else
      { // TX
        if ( BitCnt == 0)
          CCTL0 &= ~ CCIE;              // All bits TXed, disable interrupt
        else
        {
          CCTL0 |=  OUTMOD2;          // TX Space
          if (RXTXData & 0x01)
            CCTL0 &= ~ OUTMOD2;         // TX Mark
          RXTXData = RXTXData >> 1;
          BitCnt --;
        }
      }
      */
    }

    /*** End of file: HPA338RevA.c ***/

  • okay, let me make some wild guesses:

    Your tables contain brightness values for red, green and blue LED for different colors. These colors aren't RGB values but rather from an indexed color table.

    The values inside the tables are timer-count values for a count of 0..65535.

    On each timer overflow, you write a new value to the CCR units so the LEDs are on or off for the specified amount of time for the next cycle.

    I think you made it too complex. What about this approach:

    The color value is not an index, but an RGB value. A long int. Like 0xff0078 for R=255, G=0 and B= 127. This is a common notation.

    Inside the ISR, you only do:

    CCTL2 = (color >>8) |0xff: // R
    CCTL1 = (color|0xff); // G
    CCTL0 = (color<<8)|0xff; // B

    The CCR units are always configured for RESET/SET mode (OUTMOD_7). And the timer continuously runs with at least 1MHz clock (so the LEDs flicker with >15Hz)

    The LEDs will be on for at least 256 timer ticks (0.4% DC), which is enough for the ISR to do its job without risk to set a CCR value after the timer has already passed the target value (turning the LED on for a DC > 100% for this cycle). The |0xff will do so.

    Well, if you need to calibrate the brightness of the individual LEDs, you might need a table, or alter your RGB color values, which in most cases will be the better solution (No space wasted for tables or indexing or whatever).

    Keep in mind that TACCR0 == CCR0, so the PWM interferes with the software UART. Both use the TimerA CCR units.

  • Ok thank your for the advices. I try to implement a code, with an interruption on the Pin 2.0 which is the one that received the UART signal. I will let you know how it does tomorrow if I have time.

  • Hi,

    This is my final code. It s run with the eZ430-RF2500 devices

    /******************************************************************************/
    /*  Project: Bubble Tube Demonstrator.                                        */
    /*           RGB-LED colour cycle using TI MSP430F2131 and TI TPS62260.       */
    /*                                                                            */
    /*  (c) Pulsonic Technologies Ltd., 2012. All Rights Reserved.                */
    /*                                                                            */
    /*  v1.00   06/06/2012  DPP Initial Release.                                  */
    /*                                                                            */
    /*  Description:                                                              */
    /*  Timer_A3 is used to generate 3 PWM signals. Timer overflow generates an   */
    /*  interrupt and in its interrupt service routine the PWM outputs are        */
    /*  reset. Output Units will set the PWM outputs.                             */
    /*  Software starts in a demo mode that automatically changes the PWM         */
    /*  settings. As soon as the incremental encoder is operated the automatic    */
    /*  mode stops and the adjustment of the colour can be done manually.         */
    /*                                                                            */
    /*  Hardware platform: MSP430F21x1                                            */
    /*                                                                            */
    /*  Added MSP430x21x1 - Timer_A UART 9600, 1MHz DCO SMCLK                     */
    /*                                                                            */
    /*  A full-duplex 9600-baud UART using PIN interupt. A character is           */
    /*                                                                            */
    /*                                                                            */
    /*                                                                            */
    /*                                                                            */
    /*                                                                            */
    /*                MSP430F21x1                                                 */
    /*            -----------------                                               */
    /*        /|\|              XIN|-                                             */
    /*         | |                 |                                              */
    /*         --|RST          XOUT|-                                             */
    /*           |                 |                                              */
    /*           |             P1.3|--------> Power for MAX3221                   */
    /*           |   CCI0A/TXD/P2.1|-------->                                     */
    /*           |                 | 9600 8N1                                     */
    /*           |   CCI0B/RXD/P2.0|<--------                                     */
    /*                                                                            */
    /*----------------------------------------------------------------------------*/
    /******************************************************************************/

    #include "msp430x21x1.h"
     unsigned int LED_TabLength = 252*4;
      const unsigned int LED1[]={65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,57866,52635,47404,
      42173,36943,34327,31712,29096,26481,23866,21250,19942,18635,17327,16019,14712,
      13404,12096,10789, 9481, 8173, 7519, 6865, 6212, 5558, 4904, 4577, 4250, 3923,
      3596, 3269, 2942, 2615, 2288, 1962, 1635, 1308, 981, 654, 327, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 327, 654, 981, 1308, 1635, 1962, 2288, 2615,
      2942, 3269, 3596, 3923, 4250, 4577, 4904, 5558, 6212, 6865, 7519, 8173, 9481,
      10789,12096,13404,14712,16019,17327,18635,19942,21250,23866,26481,29096,31712,
      34327,36943,39558,42173,44789,47404,52635,57866,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,
      65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,65385,150};
    //blue LED
      const unsigned int LED2[]={ 150, 295, 622, 949, 1276, 1603, 1930, 2256,
      2583, 2910, 3237, 3564, 3891, 4218, 4545, 4872, 5526, 6180, 6833, 7487, 8141,
      9449,10757,12064,13372,14680,15987,17295,18603,19910,21218,23834,26449,29064,
      31680,34295,36911,39526,42141,44757,47372,52603,57834,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,65353,
      65353,65353,65353,57834,52603,47372,42141,36911,34295,31680,29064,26449,23834,
      21218,19910,18603,17295,15987,14680,13372,12064,10757, 9449, 8141, 7487, 6833,
      6180, 5526, 4872, 4545, 4218, 3891, 3564, 3237, 2910, 2583, 2256, 1930, 1603,
      1276, 949, 622, 295, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,65385,150}; //green LED
      const unsigned int LED3[]={ 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
      150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 311,
      638, 965, 1292, 1619, 1946, 2272, 2599, 2926, 3253, 3580, 3907, 4234, 4561,
      4888, 5542, 6196, 6849, 7503, 8157, 9465,10773,12080,13388,14696,16003,17311,
      18619,19926,21234,23850,26465,29080,31696,34311,36927,39542,42157,44773,47388,
      52619,57850,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,65369,
      65369,65369,65369,65369,65369,65369,65369,65369,65369,57850,52619,47388,42157,
      36927,34311,31696,29080,26465,23850,21234,19926,18619,17311,16003,14696,13388,
      12080,10773, 9465, 8157, 7503, 6849, 6196, 5542, 4888, 4561, 4234, 3907, 3580,
      3253, 2926, 2599, 2272, 1946, 1619, 1292, 965, 638, 311, 150,65385 ,150};
      //red LED
     
     
    #define REDBTN          0x5A     // 90
    #define CYANBTN         0x5B     // 91
     
    #define GREENBTN        0x46     // 70
    #define MAGENTABTN      0x47     // 71
     
    #define BLUEBTN         0x1E     // 30
    #define YELLOWBTN       0x1F     // 31to be define
     
    #define DOWNDELAYBTN    0x50     // 80
    #define ALLONBTN        0x51     // 81
     
    #define UPDELAYBTN      0x3C     // 60
    #define ALLOFFBTN       0x3D     // 61
     
    #define DEFAULTDELAYBTN       0x14     // 20
      //#define TESTBTN         0x15     // 21 to be define

    #define TOGGLEBTN       0x28     // 40
      //#define TOGGLEBTN       0x29     // 41to be define
    #define UNREACHABLE     1500   
     
      unsigned int LEDptr, LEDptr_TARG = UNREACHABLE , LEDptr_DELAY = 25000;
      unsigned char BAold;
      void Inc_Decoder(unsigned char BAnew);
      void Delay();
      unsigned int RXTXData=0;
      unsigned char BitCnt=8;
      unsigned char loop=1;
      unsigned int X=0;

      //------------------------------------------------------------------------------
      // Main Program
      void main(void)
      {

      WDTCTL=WDTPW+WDTHOLD; // disable Watchdog
      BCSCTL1= CALBC1_8MHZ; //--- System Clock Settings ----------------------
      DCOCTL = CALDCO_8MHZ; // use calibrated 8MHz settings
     
      //---- PWM Timer Initialization ------------------
      TACTL = TASSEL_2+ID_0+MC_0+TACLR+TAIE; // Timer clock = SMCLK = 8MHz
      TACCTL0 = CM_0+CCIS_2+OUTMOD_1; // All Output Units will set PWM outputs if
      TACCTL1 = CM_0+CCIS_2+OUTMOD_1; // TACCRx=TAR. Resetting PWM outputs is done
      TACCTL2 = CM_0+CCIS_2+OUTMOD_1; // by software.
      LEDptr=0;
      TACCR0=LED1[LEDptr>>2]; // LEDptr is shifted right twice,
      TACCR1=LED2[LEDptr>>2]; // this means divided by 4
      TACCR2=LED3[LEDptr>>2];
     
      //--- Port Initialization ----------------------------------
     
      P2DIR |= 0xfe;                            // Set P1.0 to output direction
      P2IE |= 0x01;                             // P1.4 interrupt enabled
      P2IES |= 0x01;                            // P1.4 Hi/lo edge
      P2IFG &= ~0x01;                           // P1.4 IFG cleared
       
      P1SEL = 0x0E; // P1.1, P1.2, P1.3 are used as PWM Timer Outputs
      P1OUT = 0x00; // P1.0 is output (Enable for TPS62260)
      P1DIR = 0xFF; // P1.4, P1.5, P1.6, P1.7 are not used => digital outputs
     // P2OUT = 0x04; // P2.0 and P2.1 are not used => digital outputs
      P2DIR|= 0xE4; // P2.3, P2.4 are digital inputs => incremental encoder
      // P2.5, P2.6, P2.7 are not used => digital outputs
     
      BAold=0x01; //--- initialize decoder for incremental encoder -----------
     
      Delay(); // Delay loop
     
      TACTL |= MC_2; // start Timer_A (continuous mode)
     
      __enable_interrupt(); // enables maskable interrupts
     
      //--- Main Loops ---------------------------------------------------------------
        for (;;) //--- change settings manually (incremental encoder)
        {
       
          RXTXData = 0;
          
          if (loop)
          {
            Delay(LEDptr_DELAY);
            if(LEDptr == LEDptr_TARG)
            {
              loop = 0;
              LED_TabLength = 252*4;
             }       
             else
              {
                LEDptr=LEDptr+1;
                if (LEDptr>=LED_TabLength)
                LEDptr=0;
              }
            }
           }
    }
     
      //------------------------------------------------------------------------------
      // Delay Loop

     
      void Delay(unsigned int d)
    {

      unsigned int i=0,j;
     
      for(i=0;i<d;i++) {;}   // delay loop
      for(j=0;j<=3;j++);
    }
     
      //------------------------------------------------------------------------------
      // Timer_A Interrupt Service Routine:
      #pragma vector=TIMERA1_VECTOR
      __interrupt void ISR_TimerA(void)
      {
          P1OUT |= 0x01; // activate LEDs
          //--- update PWM duty cycle settings using colour table
          TACCR0=LED1[LEDptr>>2]; // LEDptr is shifted right twice,
          TACCR1=LED2[LEDptr>>2]; // this means divided by 4
          TACCR2=LED3[LEDptr>>2];
          //--- PWM signal generation
          TACTL &= ~TAIFG;
          TACCTL0 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
          TACCTL0 |= OUTMOD_1; // OUTMOD_1 => set PWM output TA0 as soon as TACCR0=TAR
          TACCTL1 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
          TACCTL1 |= OUTMOD_1; // OUTMOD_1 => set PWM output TA1 as soon as TACCR1=TAR
          TACCTL2 &= ~OUTMOD_7; // OUTMOD_0 => PWM output=L
          TACCTL2 |= OUTMOD_1; // OUTMOD_1 => set PWM otuput TA2 as soon as TACCR2=TAR
      }

    // Port 2 interrupt service routine
    #pragma vector=PORT2_VECTOR
    __interrupt void Port_2(void)
    {
          RXTXData = 0;
          P2IFG = 0x00;                           // P2.0 IFG cleared
          X = TAR;
          X += 1250;
          
          while(X > TAR); // wait center of next value
          
          for(BitCnt=8; BitCnt>0; BitCnt--)
          {  
            RXTXData = RXTXData >> 1;
            if (0x01 & P2IN)  RXTXData |= 0x80;
            X += 833;
            while(X > TAR);  // wait next value
          }     
         
          switch (RXTXData)
            {
              case REDBTN:      // red  
                LEDptr_TARG = 676;
                LEDptr_DELAY = 3000;
                loop = 1;        
              break;
                  
              case GREENBTN:    // green
                 LEDptr_TARG = 336;
                 loop = 1;
                 LEDptr_DELAY = 3000;
              break;
                
              case BLUEBTN:     // blue
                 LEDptr_TARG = 5;
                 LEDptr_DELAY = 3000;
                 loop = 1;
              break;
                
              case CYANBTN:     // cyan
                 LEDptr_TARG = 170;
                 LEDptr_DELAY = 3000;
                 loop = 1;
               break;
                
              case MAGENTABTN:  // magenta
                LEDptr_TARG = 847;
                LEDptr_DELAY = 3000;
                loop = 1;
              break;
              
              case YELLOWBTN:   // yellow
                LEDptr_TARG = 512;
                LEDptr_DELAY = 3000;
                loop = 1;
              break;
                    
              case ALLOFFBTN:   // all off
                 LED_TabLength += 2; // catch the value for turn off all led
                 LEDptr_TARG = 1016;
                 LEDptr = 1016;
                 LEDptr_DELAY = 3000;
                 loop = 1;
              break;  
         
              case ALLONBTN:    // all on
                  LED_TabLength += 2;  // catch the value for turn on all led
                  LEDptr_TARG = 1014;
                  LEDptr = 1014;
                  LEDptr_DELAY = 3000;
                  loop = 1;
              break;
     
             case UPDELAYBTN:   // decrease speed
                LEDptr_DELAY += 2000;
                if( LEDptr_DELAY > 62000) LEDptr_DELAY = 62000;    // limit
                loop = 1;
             break;  
         
             case DOWNDELAYBTN:    //  increase speed
                LEDptr_DELAY -= 2000;
                if( LEDptr_DELAY < 2500) LEDptr_DELAY = 2500;     // limit
                loop = 1;
            break;
                      
             case DEFAULTDELAYBTN:    // default delay
               LEDptr_TARG = UNREACHABLE;
               LEDptr_DELAY = 25000;
               loop = 1;
             break;     
                 
                          
             case TOGGLEBTN:     // Toggle in a previous delay or freez
                if(loop == 0)
                {
                   LEDptr_TARG = UNREACHABLE;
                   loop = 1;
                }
                else
                {
                 loop = 0;   
                }

              break;
              default:          // catch-all
              break;
            }
     
    }

    /*** End of file: HPA338RevA.c ***/

  • Dear Dimitri Marquois

    Can you tell me how did you program ez430-rf2500? 

**Attention** This is a public forum