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.

Reading Input pin and blinking LED

Other Parts Discussed in Thread: MSP430F5528

HI,

     i what to read the voltage on the port 1 pins and blink the LED if = F3 or else LED=high. Below is the code for the same but there is no output for it (LED is not glowing or remaining ON) in any situation 

is the code written properly? 

#include <msp430.h>
#include <msp430f5528.h>
#include <math.h>
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P4DIR |= BIT7;
if (P1IN == 0xF3)
{
P4OUT|= BIT7 ; // P4.7 High
__delay_cycles(10000);
P4OUT&=~BIT7; // P4.7 Low
__delay_cycles(10000);
}
else
{
P4OUT|= BIT7 ; // P4.7 High
__delay_cycles(10000);
}

}

Please help me with it 

Thank you,

  • Hi Pratiksha!

    Next time when posting code please use the Syntaxhighlighter that is available in rich formatting mode after pressing on reply. When doing so, your code looks like this and is easier to read:

    #include <msp430.h>
    #include <msp430f5528.h>
    #include <math.h>
    
    int main( void )
    {
      WDTCTL = (WDTPW | WDTHOLD); // Stop watchdog timer
    
      P4DIR |= BIT7;
    
      if( P1IN == 0xF3 )
      {
        P4OUT |= BIT7;            // P4.7 High
        __delay_cycles( 10000 );
        P4OUT &= ~BIT7;           // P4.7 Low
        __delay_cycles( 10000 );
      }
      else
      {
        P4OUT |= BIT7;            // P4.7 High
        __delay_cycles( 10000 );
      }
    }

    Everything that shall be repeated has to be placed in an endless loop:

    void main( void )
    {
      // Configuration
    
      while( 1 )
      {
        // Your program
      }
    }

    Your program ends after one cycle.

    Dennis

  • HI,

    Now it is working all fine, thank you for the help. What if i want to transmit a value instead of glowing the LED and display it on hyperterminal then what should be done ?? How to configure the UART module or should i use SPI for the same


    Thank you.
    Regards
    Pratiksha Bhuta
  • There are a few code examples available for the UART:

    • MSP430F55xx_uscia0_uart_01.c      USCI_A0, 115200 UART Echo ISR, DCO SMCLK
    • MSP430F55xx_uscia0_uart_02.c      USCI_A0, Ultra-Low Pwr UART 2400 Echo ISR, 32kHz ACLK
    • MSP430F55xx_uscia0_uart_03.c      USCI_A0, Ultra-Low Pwr UART 9600 Echo ISR, 32kHz ACLK
    • MSP430F55xx_uscia0_uart_04.c      USCI_A0, 9600 UART, SMCLK, LPM0, Echo with over-sampling

    Dennis

  • i'll go through it thanks for the guidance
  • Sorry, I missed this...

    Pratiksha Bhuta said:
    or should i use SPI for the same

    No, SPI is not for communication between MSP and the serial port of the PC.

    Dennis

  • hi,

         Below is the code i am using to transmit data using UART but there is no output on terminal and i am not able to find out the error for this, please help me out with it. Do i need UART source files for this to work ? ans if yes how can i get it ?

     

    #include <msp430.h>

    #include <msp430f5528.h>

    unsigned char data[2]= {0x41,0x41};

     

    void main(void)

    {

           WDTCTL = WDTPW + WDTHOLD; // Stop WDT

           UCA1CTL1 |= UCSWRST; // **Put state machine in reset**

           UCA1CTL1 |= UCSSEL_2; // SMCLK

           UCA1BR0 = 6; // 1MHz 9600 (see User's Guide)

           UCA1BR1 = 0; // 1MHz 9600

           UCA1MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,

     

     

           UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**

           UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt

     

           __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled

           //__no_operation(); // For debugger

     

           }

    unsigned char uartSend(unsigned char *pucData, unsigned char ucLength)

    {

    while(ucLength)

    {

    // Wait for TX buffer to be ready for new data

    while(!(UCA1IFG & UCTXIFG));

     

    // Push data to TX buffer

    UCA1TXBUF = *pucData;

     

    // Update variables

    ucLength--;

    pucData++;

    }

     

    // Wait until the last byte is completely sent

    while(UCA1STAT & UCBUSY);

    }

     

    // Echo back RXed character, confirm TX buffer is ready first

    #pragma vector=USCI_A1_VECTOR

    __interrupt void USCI_A1_ISR(void)

    {

     

     

       uartSend(data ,2);

       P4DIR  |= BIT7;

       P4OUT = BIT7;

    }

     

    Thank You,

    Pratiksha Bhuta

  • Hi Pratiksha!

    As mentioned in my first answer, please edit your post and use the Syntaxhighlighter for posting code.

    Dennis
  • HI,

          I am Sorry for the inconvenience i was trying to edit it as you suggested but it did not happen. I am unable to find an option like syntax highlighter in rich formatting

  • This is a trial (testing as per your instructions) Sorry for inconvenience 

    #include <msp430.h> #include <msp430f5528.h> volatile unsigned int tx_flag; //Mailbox Flag for the tx_char. volatile unsigned char tx_char; //This char is the most current char to go into the UART volatile unsigned int rx_flag; //Mailbox Flag for the rx_char. volatile unsigned char rx_char; //This char is the most current char to come out of the UART void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT UCA1CTL1 |= UCSWRST; // **Put state machine in reset** UCA1CTL1 |= UCSSEL_2; // SMCLK UCA1BR0 = 6; // 1MHz 9600 (see User's Guide) UCA1BR1 = 0; // 1MHz 9600 UCA1MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0, UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine** UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt rx_flag = 0; //Set rx_flag to 0 tx_flag = 0; //Set tx_flag to 0 __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled //__no_operation(); // For debugger } /*uart_putc * Sends a char to the UART. Will wait if the UART is busy * INPUT: Char to send * RETURN: None */ void uart_putc(unsigned char c) { tx_char = c; //Put the char into the tx_char UCA1IE |= UCTXIE; //Enable USCI_A0 TX interrupt while(tx_flag == 1); //Have to wait for the TX buffer tx_flag = 1; //Reset the tx_flag return; }

  • Doesn't that look better?

    But I think not everything from before is in here now or it is different.

  • 
    

    Hi Dennis Eichmann,

                                               First of all thank you for the help so far with the code and editing as well.  I want to transmit HELLO to the hyperterminal using UART and below is the code for the same but the beow code does no display anything on the Hyperterminal. I am using MSP430F5528 for then same and the RX TX pins are connected at UCA1. What can be the reason for the same.

    #include "msp430.h"
    #include "msp430f5528.h"
    void UARTSendArray(char *TxArray, int ArrayLength);
    
    
    void main(void)
    
    {
      WDTCTL = WDTPW + WDTHOLD; // Stop WDT
    
      P4DIR |= BIT7;    // Set the LEDs on P4.7 as outputs
      P4OUT = BIT7;            // Set P4.7
    
      /* Configure hardware UART */
      P4SEL = BIT4 + BIT5 ;    // P1.1 = RXD, P1.2=TXD
      UCA1CTL1 |= UCSWRST;
      UCA1CTL1 |= 0X80;    // Use SMCLK
      UCA1CTLW0 = 0X0080;
      UCA1BR0 = 0X6D;           // Set baud rate to 9600 with 1MHz clock (Data Sheet 15.3.13)
      UCA1BR1 = 0;             // Set baud rate to 9600 with 1MHz clock
      UCA1MCTL = 0X04;       // Modulation UCBRSx = 1
      UCA1CTL1 &= ~UCSWRST;    // Initialize USCI state machine
      //IE2 |= UCA0RXIE;         // Enable USCI_A0 RX interrupt
    
    
    
      __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
    }
    
    
    void UARTSendArray(char *TxArray, int ArrayLength){
    
      UARTSendArray("Hello", 5);
    
        while(ArrayLength--){             // Loop until StringLength == 0 and post decrement
        while(!(UCA1IFG & UCTXIFG));      // Wait for TX buffer to be ready for new data
        UCA1TXBUF = *TxArray++;           //Write the character at the location specified by the pointer and post increment
      }
    }
    
    
    
    
    

    Thank you,

    Pratiksha Bhuta

  • There is no function call for UARTSendArray in your code except inside the function itself. How shall that work? And what is your SMCLK? Do you know it's exact value? UART needs precise clocks, you are working with the default clock after startup. I don't know the value, but from your BR0 value of 109 it must be something like 1.047MHz. Did you check this?
  • Yes i checked the clock it is 1.047 MHZ. What about the no call function for UARTSendArray??? how do i call the function ??????

  • You have to call it from your main(). Your program now does some initialization and then goes to sleep waiting for an interrupt, but there will never be one.
  • i just called the function by mentioning it in the main() but still no output

    #include "msp430.h"
    #include "msp430f5528.h"
    void UARTSendArray(char *TxArray, int ArrayLength);
    
    
    void main(void)
    
    {
      WDTCTL = WDTPW + WDTHOLD; // Stop WDT
    
      P4DIR |= BIT7;    // Set the LEDs on P4.7 as outputs
      P4OUT = BIT7;            // Set P4.7
    
      /* Configure hardware UART */
      P4SEL = BIT4 + BIT5 ;    // P1.1 = RXD, P1.2=TXD
      UCA1CTL1 |= UCSWRST;
      UCA1CTL1 |= 0X80;    // Use SMCLK
      UCA1CTLW0 = 0X0080;
      UCA1BR0 = 0X6D;           // Set baud rate to 9600 with 1MHz clock (Data Sheet 15.3.13)
      UCA1BR1 = 0;             // Set baud rate to 9600 with 1MHz clock
      UCA1MCTL = 0X04;       // Modulation UCBRSx = 1
      UCA1CTL1 &= ~UCSWRST;    // Initialize USCI state machine
      void UARTSendArray(char *TxArray, int ArrayLength);
    
    
      __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
    }
    
    
    void UARTSendArray(char *TxArray, int ArrayLength){
    
      UARTSendArray("Hello", 5);
    
        while(ArrayLength--){             // Loop until StringLength == 0 and post decrement
        while(!(UCA1IFG & UCTXIFG));      // Wait for TX buffer to be ready for new data
        UCA1TXBUF = *TxArray++;           //Write the character at the location specified by the pointer and post increment
      }
    }
    
    
    
    
    

  • 1) Why does the function call itself?
    2) You do not call the function in the main(), you simply copied the function prototype inside the main(). Doesn't the compiler complain?

  • No it did not,can you give me an example for the same as i am very new to coding and MSP
  • This line

    UARTSendArray( "Hello", 5 );

    has to be placed in the main() and removed from inside the function itself.

  • Am i suppose to change anything apart from this ??? Because even after trying this there is no display on terminal.

**Attention** This is a public forum