9:36 AM ooooo T-Mobile LTE 100% this is the code to toggle the LED at P1.0 pin c
ID: 3785102 • Letter: 9
Question
9:36 AM ooooo T-Mobile LTE 100% this is the code to toggle the LED at P1.0 pin connect Led anode pin to P1.0 and cathode to Ground #include "msp430.h". /header file for msp430 int main0 //code execution start from here WDTCTL WDTPW. WDTHOLD://watchdog timer disable for infinite running the code PIDIR F0x07//setting P1.0 as OUTPUT while(1)//forever loop P1OUT 1//writing HIGH on Pl.0 for(int i 0; ik 5000; i++);//delay for 5 sec P1OUT 0;//writing Low on P1.0 for int i 0; i a 5000 i +0;//delay while main for programing easy with msp430 use energia ide 09493148685 for supportExplanation / Answer
Find below the proper coding along with the explanation.
Before moving to coding like to explain regarding the MSP430 LED’s & Switches
We have the board and software ready lets blink a few led’s and switches present on the Launch Pad. The Launch pad comes with 2 LED’s,
green connected to the pin P1.0
red connected to the pin P1.6.
There are 2 switches,
S1 connected to reset
S2 connected to pin P1.3.
The IO’s are memory mapped in msp430 and hence no separate instructions are required to control them. The Ports are usually related to the following registers:.
>> PxDIR >> PxIE
>> PxOUT >> PxIFG
>> PxIN >> PxIES
>> PxSEL >> PxREN
We need to understand the first three for controlling the IO’s.
The PxDIR register is used to configure a particular port pin as input or output.Each bit corresponds to a pin on the port.Setting a particular bit makes that particular pin output and resetting it makes it input i.e,
1 = output
0 = input.
So to make the first pin P1.0 & pin P1.6 as an output we set the bits and to make the 3rd pin P1.3 input we reset the bit. So we write 0x41 to the register P1DIR i.e, binary 0100 0001.
To write the values to the pins we set a bit in the PxOUT register to make the pin high and reset the bit to make it
To make the leds’s light up, which are active high we set the first and the sixth bit.
The following code is used
P1OUT = 0x41; or
P1OUT = BIT0 + BIT6; // this format makes use of the macros defined in the library files of the particular device
It is equal to 0x01 and BIT6 is equal to 0x40.These are easy to use and to understand and are defined from BIT0 to BIT15 with values 0x00 – 0xff.
There are other macros defined for other registers as well which we will be seeing some other time.
To read a particular value we need to mask the PxIN register to check the here we need to check the fourth pin or pin P1.3.
This is done as follows.
(P1IN & BIT3) //expression equates to a 8 bit value which will be 0 if the 4th pin is reset and equal to 0x08 if the pin is set.
#include "io430.h"
int main( void )
{
int i; // Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1DIR=BIT0+BIT6; // make pin 0 and 6 output
P1OUT=BIT0+BIT6; // make pin 0 and 6 high
while(1)
{
P1OUT^=(BIT0+BIT6); // toggle the led's
for(i=0;i<9999;i++); // delay between the toggle.
}
}
/* In the above code we XOR port 1 with BIT0 and BIT6 to toggle those particular bits.The loop is to give a delay so that toggling is seen.
Compile the might give a warning as statement unreachable which is because of the return statement.
Deleting it will give zero set the target options and hit download and
Debug window will F5 to run the program or F11 to start Single step
The above code as WDCTL=WDTPW+WDTHOLD; //This is stop the watchdog timer which will be discussed later. This line needs to be included else the cpu will reset every 32ms.
A program to toggle LED when a switch is pressed. The delay should be properly selected to compensate for the switch denouncing effect.
#include "io430.h"
int main( void ) {
int i; // Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1DIR=BIT0+BIT6; // make pin 0 and 6 output
P1OUT=BIT0+BIT6; // make pin 0 and 6 high
while(1) { if((P1IN & BIT3) == 0)
{
P1OUT^=(BIT0+BIT6); // toggle the led's
}
for(i=0;i<27000;i++); // delay between the toggle.
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.