Jump to content
43oh

Recommended Posts

Hi

am new to the msp430,and trying to receive a string and transmit the same,am not getting the output.can anyone tell me the what is the problem.This is my code

 
 
 unsigned char rcvdata,datareceived;
 unsigned char Rxbuff[5];
 unsigned int  Rxdatalen;
 unsigned int datalen;
 unsigned char databuf[5];
 unsigned char Rxtoutcnt;
 unsigned char compltflag=1;
 
 
void delay_ms(unsigned int cnt)
{
 volatile unsigned int i,j;
 
for(i=0;i<cnt;i++)
for(j=0;j<88;j++);
 
}
 
void init_serial(void)
{
 
   P1IN = 0X04;
   P1DIR = 0x08;
   P1OUT = 0x00;
  P1SEL = BIT1+BIT2;
P1SEL2= BIT1+BIT2;
UCA0CTL1 |= UCSSEL_2;
UCA0BR0 = 104;
UCA0BR1 = 0;
UCA0MCTL = UCBRS0;
UCA0CTL1 &=~ UCSWRST;
IE2 |= UCA0RXIE;
}
 
 
void init_timer (void)
{
   TACCTL0 = CCIE;
TACCR0 = 5225-1;
TACTL = TASSEL_2 + MC_1;
 
}
 
 
 
void main(void) 
{
    
WDTCTL = WDTPW + WDTHOLD;      // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
init_timer();
init_serial();
__bis_SR_register(GIE);
 
while(1)
{
if(datareceived == 1)
serialmsg();
}
}
 
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
 
   if (IFG2&UCA0RXIFG)
   {
rcvdata = UCA0RXBUF;
compltflag = 0;
Rxbuff[Rxdatalen]= rcvdata;
 
if(++Rxdatalen>=BUFSIZE)
Rxdatalen = 0;
 
datareceived = 0;
Rxtoutcnt = 0;
clearbit(P1OUT,3);
   }
}
 
unsigned char serialmsg(void)
{
setbit(P1OUT,3);
unsigned int j;
unsigned int cnt;
 
for(cnt=0;cnt<Rxdatalen;cnt++)
databuf[cnt]=Rxbuff[cnt];
 
datalen=Rxdatalen;
 
for(j=0;j<sizeof(databuf);j++)
{
UCA0TXBUF = databuf[j];
while(!(IFG2&UCA0TXIFG));
}
    delay_ms(10);
   
}
 
 
 
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
 
Rxtoutcnt = (Rxtoutcnt+1);
 
if(Rxtoutcnt==2)
{
if(compltflag == 0)
{
datareceived = 1;
Rxtoutcnt = 0;
compltflag = 1;
}
}
 
}
 

 

 

Thanks&Regards

 

Link to post
Share on other sites

Here are a few things I noticed:

 

P1IN is a read-only register that gives the current state of the port 1 pins. Writing to it has no effect. That said, assuming you want P1.2 to be treated as input all should be well - P1DIR bit 2 is set to zero by the next line.

 

The globals datareceived, Rxbuff, Rxdatalen, Rxtoutcnt should be volatile, since they're accessed in various ISRs as well as from the main loop. If this isn't done there's a risk that the compiler might treat them as having constant values (it doesn't account for interrupts changing the flow of execution).

 

The code looks susceptible to problems caused by interrupts firing during transmit. The data being processed by serialmsg could end up in an inconsistent state.

 

Finally, you said that you're not getting the output. Do you mean that it's not received by the other device (whatever that might be), or that the serialmsg function isn't even being called? You appear to be using P1.3 as a debug output pin, so have you tried setting that at different points in the code to see what's actually being executed?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...