2 blinky explained


################################################################
#   blinky.mpy    Flash the green led
################################################################

pindir(P1_6,OUT)   # Set pin P1_0 to be an output (this is connected to the Launchpad green led)
while 1:           # loop forever
   out(P1_6,1)     # turn the green led on
   wait(500)       # wait for 500ms  (half a second)
   out(P1_6,0)     # turn the green led off
   wait(500)

Comments

Lines that start with a # character are comments,  and everything after the # character on a line is a  comment too. Comments are ignored by the mpyEditor and have no effect on the program. Good programmers put comments into their code to remind them what the program does. Comments are good.  Get into the habit of writing comments in your programs.

Completely blank lines are ignored also.

IO pin direction

The first proper mpy command is the line


pindir(P1_6,OUT)

pindir is a mpy command, it is short for direction output. What it does is tell the microcontroller to make the pin P1_6 an output pin. P1_6 is the name of the pin on the microcontroller that is connected to the green LED at the bottom of the Launchpad board.

The MSP430 microcontroller chip has many pins which you can control with a mpy program. Each pin can be used as an input or an output. The dpindir command makes the pin an output, and pindir(pin,IN) would make it an input.  By making this pin an output we can use the out(pin,OUT) command to change the voltage on the pin, and this will turn the green LED on or off.

IO Pin names

You can access  any of the pins on the MSP430 chip using its pinname. There are two rows of pins one on each side of the Launchpad board and each pin is labelled with its pinname. The pinnames on the board  are labelled  P1.0 through to P2.7. However in the mpy program the name is of the form P1_0 through to P2_7. (You must use the  underscore ‘_’ character)

Some of the pinnames have alternative names to make them easier to remember. This is the case for the green LED, it is connected to the pin P1_6, but it also has the name GREENLED.   pindir(GREENLED, OUT) and pindir(P1_6, OUT) are the same. Also the name  REDLED is the same as pinname P1_0 and name SWITCH is the same as pinname P1_3.

while loop

The next line is


while 1:

This means loop through the lines that follow forever. All the commands that follow this will be run, and when they are done,  it will run through the loop again. It will continue forever until we switch off  the power to the microcontroller, or we program a new program into the microcontroller.

Flash the LED

The next line is


    out(P1_6,1)

The out() command is an output command that can write a 1 or 0 to the P1_6 pin, and this will turn the green LED on. A 1 means that a positive voltage will be put on the output pin. The actual voltage will be the same as the power supply voltage that is connected to the MSP430 chip. (This will normally be 3.3v on a Launchpad board) . A 0 will mean that 0v will be put on the output pin, and this would turn the LED off.

The next line is


   wait(500)

The wait command will wait 500 milliseconds before moving onto the next command. (A millisecond is 1/1000th of a second, so 500 miliseconds is the sama as a 1/2 of a second). This will mean that the green LED will be on for 1/2 a second

The next lines are


   out(P1_6,0)
   wait(500)

This is a repeat of the previous two lines but notice now that the output value for the out( ) command is a 0, which will put 0v on the P1_6 pin and the green LED will be turned off.  Then we have another  500 millisecond wait.

So the green LED will be turned on for 1/2 a second and then turned off for 1/2 a second, the green LED will flash once.

However because these lines are inside the while loop it will repeat the flash forever once every second.

If you followed this then you are well on your way to understanding mpy programming.