Other Parts Discussed in Thread: MSP430F149
Hi.. folks...
I have a master device M and slave device S. M polls request from S. M would reply a handshake as soon as it gets a valid request from S. [Both reply and request is 8 bytes long.] M and S are connected by two wire, one for ground and the other for usart data trans/recv. And S uses MSP430F149.
To make single wire communication work. S sends data while its recv module is disabled and vice versa for recv procedure. To make my question clear, here's my code:
#include <msp430x14x.h>
unsigned char txbuf[8];
unsigned char rxbuf[8];
unsigned char rxIndex;
unsigned char txIndex;
void init_UART()
{
P3SEL |= 0x10; //P3.4,P3.5
P3SEL &= ~0x20; //P3.4 select peripheral function and P3.5 selected as GPIO.
ME1 |= UTXE0;
ME1 &= ~URXE0; //Slave always send data first, so enable TX module and disable RX module.
U0CTL = CHAR+SWRST;
U0BR0 = 0x03; //Baud Rate 9600
U0BR1 = 0x00;
U0MCTL = 0x4A; //Modulation
U0CTL &= ~SWRST; //reset
IE1 |= UTXIE0;
IE1 &= ~URXIE0;
U0TCTL = SSEL0; //32.768kHz
IFG1 &= ~UTXIFG0;
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
// BCSCTL1 = 0x86;
// BCSCTL2 = 0x00; // Sys Clock cofigured to ~800kHz DCO when BCSCTLx is not assigned any value?
rxIndex = 0;
txIndex = 0;
unsigned char i;
for(i = 0; i < 8; i++){
txbuf[i] = 0xb0+i;
}
for(i = 0; i < 8; i++){
rxbuf[i] = 0;
}
init_UART();
_EINT();
while(1)
{
//_NOP();
if(key_pressed())
{
IE1 |= UTXIE0;
IFG1 |= UTXIFG0; //?
_EINT();
}
delay();//Buy enough time for slave to enter 8 RX ISR to recieve master's handshake.
_NOP(); //Break point here to see if slave receive master's reply in rxbuf[8]
}
}
#pragma vector = UART0TX_VECTOR
__interrupt void UART0_TX()
{
if(txIndex < 8)
{
U0TXBUF = txbuf[txIndex];
txIndex ++;
}
else
{
txIndex = 0;
unsigned int i;
for(i = 0; i < 150; i ---);
//while((U0TCTL&TXEPT) == 0);
// As what I understand in USR Guide, TXEPT set indicates the 8th byte has been sent.
//And this should be the exactly moment to switch TXD(P3.4 for this MCU) to make P3.5(RXD port) ready to recv master's //reply.
// The problem is here, '' while((U0TCTL&TXEPT) == 0); " comment out as it does not work as I thought. When prog runs to breakpoint mentioned earlier, rxbuf gets some
// corruputed bytes. While " for(i = 0; i < 150; i ---); " works fine. I get exactly want I expected in rxbuf.
IE1 &= ~UTXIE0;
IFG1 &= ~UTXIFG0;
ME1 &= ~UTXE0;
P3SEL &= ~0x10;
P3DIR &= ~0x10;
P3SEL |= 0x20;
P3DIR &= ~0x20;
ME1 |= URXE0;
IE1 |= URXIE0;
_NOP();
}
}
#pragma vector = UART0RX_VECTOR
__interrupt void UART0_RX()
{
if(rxIndex < 8)
{
rxbuf[rxIndex] = U0RXBUF;
rxIndex++;
}
else
{
rxIndex = 0;
IE1 &= ~URXIE0;
IFG1 &= ~URXIFG0;
ME1 &= ~URXE0;
P3SEL &= ~0x20;
P3DIR &= ~0x20;
P3SEL |= 0x10;
P3DIR |= 0x10;
ME1 |= UTXE0;
IE1 |= UTXIE0;
//IFG1 |= UTXIFG0;
_NOP();
}
}
----------------------------------------------
// MSP430F149
// -----------------
// /|\| XIN|-
// | | | 32kHz
// -- |RST XOUT|-
// | | |
// | P3.4|----------->
// | | 9600 - 8N1
// | P3.5|<----------- // P3.4 and P3.5 is connected by a 1uF capacitor instead of shorted by a wire.
// data wire connect to P3.5