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 ***/