C Program #include <msp430.h> /* * Swtich S1 - P1.1 * LED1 - P1.0 */ void delay(
ID: 3883438 • Letter: C
Question
C Program
#include <msp430.h>
/*
* Swtich S1 - P1.1
* LED1 - P1.0
*/
void delay(void) {
volatile unsigned loops = 50000; // Start the delay counter at 50,000
while (--loops > 0); // Count down until the delay counter reaches 0
}
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1REN |= BIT1; // Connect resistor on P1.1 to P1OUT
P1OUT |= BIT1; // Set output register for P1.1 to '1'
P1DIR |= BIT0; // Make P1.0 an output
PM5CTL0 &= ~LOCKLPM5; // Unlock ports from power manager
for (;;) {
delay(); // Run the delay sub-routine
if (!(P1IN & BIT1)) // Read the input from P1.1 and check its state
P1OUT |= BIT0; // If the button was pressed, turn on the LED
else
P1OUT &= ~BIT0; // If the button wasn't pressed, turn off the LED.
}
}
Assembly Program
;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
;
;-------------------------------------------------------------------------------
.cdecls C,LIST,"msp430.h" ; Include device header file
;-------------------------------------------------------------------------------
.def RESET ; Export program entry-point to
; make it known to linker.
;-------------------------------------------------------------------------------
.text ; Assemble into program memory.
.retain ; Override ELF conditional linking
; and retain current section.
.retainrefs ; And retain any sections that have
; references to current section.
;-------------------------------------------------------------------------------
RESET mov.w #__STACK_END,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
;-------------------------------------------------------------------------------
; Main loop here
; NOTES: The input button is on P1.1 and the output LED is on P1.0
;-------------------------------------------------------------------------------
setup: bis.b #BIT1, &P1REN ; Connect resistor on P1.1 to P1OUT
bis.b #BIT1, &P1OUT ; Set output register for P1.1 to '1'
bis.b #BIT0, &P1DIR ; Make P1.0 an output
bic.w #LOCKLPM5, &PM5CTL0 ; Unlock ports from power manager
mainloop: ; The main polling loop
mov.w #50000, R4 ; Start the delay counter at 50,000
delay: dec.w R4 ; Decrement the delay counter for each loop
jnz delay ; Keep looping until the counter becomes 0
readinput: bit.b #BIT1, &P1IN ; Read the input from P1.1 and check its state
jnz notpressed
pressed: bis.b #BIT0, &P1OUT ; If the button was pressed, turn on the LED
jmp mainloop
notpressed: bic.b #BIT0, &P1OUT ; If the button wasn't pressed, turn off the LED
jmp mainloop
;-------------------------------------------------------------------------------
; Stack Pointer definition
;-------------------------------------------------------------------------------
.global __STACK_END
.sect .stack
;-------------------------------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------------------------------
.sect ".reset" ; MSP430 RESET Vector
.short RESET
From the MSP 430 C and ASM Program above:
1. Why does a delay loop of 50,000 results in a 0.5 second delay? Show the formula
2 .Why is there a difference in delay between the asm and c code?
3. Include a high-level software flow diagram and function descriptions. Provide additional explanations as required, in particular when interrupts and/or polling methods have been used.
Explanation / Answer
MSP430 Assembler Code Template for use with TI Code Composer Studio
;
;
;-------------------------------------------------------------------------------
.cdecls C,LIST,"msp430.h" ; Include device header file
;-------------------------------------------------------------------------------
.def RESET ; Export program entry-point to
; make it known to linker.
;-------------------------------------------------------------------------------
.text ; Assemble into program memory.
.retain ; Override ELF conditional linking
; and retain current section.
.retainrefs ; And retain any sections that have
; references to current section.
;-------------------------------------------------------------------------------
RESET mov.w #__STACK_END,SP StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
setup: bis.b #BIT1, &P1REN ; Connect resistor on P1.1 to P1OUT
bis.b #BIT1, &P1OUT ; Set output register for P1.1 to '1'
bis.b #BIT0, &P1DIR ; Make P1.0 an output
bic.w #LOCKLPM5, &PM5CTL0 ; Unlock ports from power manager
mainloop: ; The main polling loop
mov.w #50000, R4 ; Start the delay counter at 50,000
delay: dec.w R4
jnz delay
readinput: bit.b #BIT1, &P1IN
jnz notpressed
pressed: bis.b #BIT0, &P1OUT
jmp mainloop
notpressed: bic.b #BIT0, &P1OUT
jmp mainloop
.sect ".reset" ; MSP430 RESET Vector
.short RESET
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.