Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

[SOLVED] MSP430 first code. Setup for the MCU

Status
Not open for further replies.

marco.fruhauf

Newbie level 2
Joined
Dec 6, 2012
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,300
Hi guys;

Below I copy my first code for MPS430F5438A.
It doesn't debbug. In all instructions in the function "while" the IAR saw an error with tihs description "espected an expression".
1What this it mean?
2these instructions are really bad?
3and finally, what more configuration the MCU need to run?
I ask tihs becuse I try to find this information in manuals but I haven't. I'm talking about setup of MCU clock, for example.

Sorry for my english, I'm learning. I'm learning a lot. :)



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "io430x54x.h"
#include <stdio.h>
 
#define LED1_on   P10OUT_6 & = ~0x0040,
#define LED1_off  P10OUT_6 | = 0x0040,
#define LED2_on   P10OUT_7 & = ~0x0080,
#define LED2_off  P10OUT_7 | = 0x0080,
#define switch_1   P4IN_0  | = 0x0001,
 
  int main( void )
{
   // Stop watchdog timer to prevent time out reset
   WDTCTL = WDTPW + WDTHOLD;
   
  P4DIR = 0xff;
  P10OUT = 0xff;
  
    while (1)
        {             
          if(switch_1 == 1)
          LED1_off;
          else
          LED1_on;
          LED2_on;
          }
}

 
Last edited by a moderator:

sky_123

Advanced Member level 4
Joined
Feb 26, 2011
Messages
1,170
Helped
279
Reputation
566
Reaction score
273
Trophy points
1,363
Activity points
9,599
It's complaining because your #define for switch_1 is not an expression that makes sense in an if statement.
Here is a neater example for a different microcontroller but the principle is the same (Everyone has their own methods..).
Notice what the SWITCH_ON and SWITCH_OFF definitions look like.
Now this is for a different microcontroller (I don't have the MPS430 compiler to compare with), but if you inspect the
header files in an editor, you should be able to figure out the correct code.

Code:
#include <iom32u2.h>
#include <intrinsics.h>
#include <avr_macros.h>
#include <stdio.h>
// bits
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80
// port D stuff
#define LED BIT6
#define BUTTON BIT7
// inputs
#define SWITCH_ON (PIND & BUTTON) == 0
#define SWITCH_OFF (PIND & BUTTON) != 0
// outputs
#define LED_ON PORTD |= LED
#define LED_OFF PORTD &= ~LED
int main( void )
{
// Set only pin6 (LED) as an output pin
DDRD=0x40;
while(1)
{
if (SWITCH_ON)
LED_OFF;
else
LED_ON;
}
return 0;
}
 

marco.fruhauf

Newbie level 2
Joined
Dec 6, 2012
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,300
Sky;

Thanks for your help!
I found some definitions for this MCU on the MSP library (io430x54x.h) wich worked with all the statements I'm using in this code. But it isn't works.
Because the problem were with the board, is a prototype used for reserach, so it have a lot of wires/jumpers and CI's damage. Actually this is another problem and just me can fix it.
Now I have a work to do.
I copy my code just to show a way to do a first step with this MCU.

Regards. Thanks a lot!



Code:
#include "io430x54x.h"
#include <stdio.h>

#define BIT0 (0x0001)
#define BIT6 (0x0040)
#define BIT7 (0x0080)

int main( void )
{
  // Stop watchdog timer to prevent time out reset

	WDTCTL = WDTPW + WDTHOLD;

	P10DIR |= (BIT6); //Put P10.6 to Direct Register
	P10DIR |= (BIT7); //Put P10.7 to Direct Register
	P4DIR &= ~(BIT0); //Put P4.0 to Direct Register

	while (1)
	{
		P10OUT |= (BIT6) ; // P10.6 as an output, High level
		if((P4IN & BIT0) == 0x01){ //P4.0 as an input
		P10OUT|= (BIT7) ; // P10.7  as an output, High level
	}
	else
	{
		P10OUT&=~(BIT7); // P10.7 as an output, Low level
	}
}
}
 

doraemon

Super Moderator
Staff member
Joined
Jun 21, 2009
Messages
1,068
Helped
273
Reputation
554
Reaction score
248
Trophy points
1,343
Location
Japan
Activity points
10,597
Hello!

Some comments:

1. Don't overuse parentheses.

Example: #define BIT0 (0x0001).
You need parentheses when you have more than one term.
Example: #define SWITCH_ON (P1IN & 0x01)
But 0x0001 is an atomic expression, so whatever happens, it will be seen as 0x0001.
I know that TI defines BIT0 as (0x0001u).
When you use (BIT6) in your code, it is translated by the preprocessor to ((0x0001)),
which looks like suspenders + belt for somebody without trousers.
Note that it works, but it overloads your code unnecessarily.

2. By looking at your comments, there might be a misunderstanding of what it does.

P10DIR |= BIT6; //Put P10.6 to Direct Register

I don't know what you mean by "direct register", but what this instruction does is simply
configuring bit 6 of port10 to output, so "// Set Port 10 bit 6 to output" would be more
understandable for most programmers.

Then, P10OUT |= (BIT6) ; // P10.6 as an output, High level

P10 bit 6 is already an output, so your comment does not really correspond to what it
does. It should br "// P10.6 high level", "// Set P10.6 high" or something like that.

3. I think BIT0 ~ BIT7 are already defined. Probably it depends on the CPU header file,
but in my present case (MSP430F5510), it is defined.

4. I'm not sure stdio.h makes sense for a microcontroller without terminal output,
file system, etc... It doesn't make sense in this program anyway.

5. Beside this, about the functionality of your code:
When you configure your port as output, you should also set the initial conditions
because when entering the loop, the ports' state might not be defined. Note that I'm
not sure of this, you can check in the 5438 manual what the default status after a
reset can be. But just in case, set the PxOUT status BEFORE setting the direction
to output, otherwise, you may have an unwanted status after you set to output
and before you set the state you want.
Next, and this will be the last for the time being: P10.6 is set high in the loop, but
is never set low in the whole program, so there is no need to repeatedly set it high
in the loop and the initial conditions are enough.

6. This time, it's the last one: be careful with the indentation: your while is aligned
with an else. The else should be aligned with an if.

Suggestion:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "io430x54x.h"
 
int main( void ) {
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    // Set port 10 initial conditions
    P10OUT |= BIT6;             // Set P10.6 initially high
    P10OUT &= ~BIT7;            // Set P10.7 initially low
    // Configure ports' direction
    P10DIR |= (BIT6 + BIT7);        // Set port 10, bits 6 & 7 to output
    P4DIR &= ~BIT0;             // Set port 4, bit 0 to input
    // Loop
    while (1) {
        if((P4IN & BIT0) == 0x01) { // P4.0 as an input
            P10OUT|= BIT7;      // Set P10. high
        }
        else {
            P10OUT &= ~BIT7 ;   // Set P10.7 low
        }
    }
}



Dora.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top