3. Given the following C program written for the MSP430 LaunchPad: include void
ID: 2292790 • Letter: 3
Question
3. Given the following C program written for the MSP430 LaunchPad: include void main(void) WDTCTL-WDTPW+ WDTHOLD, PIDIR BIT6; PI REN-BIT3; PIOUT-BIT3 PHE- BIT3; enable interruptO: while() ) #pragma vector-PORT 1 VECTOR - interrupt void Port_I(void) _delay_cycles(500000); PIOUT BIT6; PIIFG BIT3; Comment each line of code (10 pts) Run the code in CCS (Code Composer Studio) and tell what happens to the LED connected to P1.6.(15 pts) (Observe PI Registers) (Hint: Try pushing the button before and after the -enable-interrupt° function is executed! !!) (10 pts) Provide a demo video as proof of your findings from part c. (10 pts) a) b) c)Explanation / Answer
this is a very easy way to do this problem follow the programme instructions.
The constant declaration forces the compiler to create a variable in RAM at runtime
#define PIN3 3 //define PIN3 = 3
int pin3 = 3; // create a 16 bit variable in RAM and store 3 in it.
Variables change during runtime so are required to be stored in RAM.
Typical variable types:
unsigned char myvariable; // 8 bit unsigned integer (0,... 255)
char myvariable; // 8 bit unsigned integer (0,... 255)
signed char myvariable; // 8 bit signed integer (-128,... 127)
unsigned int myvariable; // 16 bit signed integer (0,... 65535)
signed int myvariable; // 16 bit signed integer (-32768,... 32767)
int // 16 bit signed integer (-32768,... 32767)
Static variables can be global: defined outside of a function and accessible everywhere, or local: defined within a function and only accessible from within that function
static int state = 0;
char debounce(void)
{
static char count=0;
if (PINB & (1< 10) return 1;
else return 0;
}
volatile int state = 0;
static volatile int state = 0;
Arrays
int myArray[5]; // define an array of 5 integers
chars myArray2[5]; // define an array of 5 bytes
int myArray3[3] = { 1, 2, 3}; // define an array of 3 integers and store (1, 2, 3)
int x = myArray3[2]; // copy the 3 third value from the myArray3 into x
#define ARRAYSIZE 100 // define array size constant
int myArray4[ARRAYSIZE]; // initialise array
int i; // initialise indexing variable
for (i=0; i<ARRAYSIZE; i++){ // loop through all array indices
myArray4[i] = i; // store the current index value in the array
}
Conditional statements: If, Switch
if (seconds == 59) {
minutes++;
seconds=0
} else seconds++;
switch(state) {
case 1:
{expression;}
break;
case 2:
{expression;}
break;
default:
{expression;}
break;
}
Loops: while, for
while ( condition ){
expression;
}
// Example of a while loop the iterates 1000 times
int x = 0;
int n = 1000;
while ( n-- ){ // Post decrement n
x = x+n;
}
int i;
?for (i=0,i<10,i++) {
expression;
}
Functions
// Function protoypes
void myfunction(int number, char code);
int myotherfunction(int number, char code);
// A function with no inputs and no output.
void myfunction(void)
{
Expressions;
}
// A function with two inputs and no output.
void myfunction(int number, char code)
{
Expressions;
}
// A function with two inputs and one output.
int myotherfunction(int number, char code)
{
int result;
result=number*code;
return result;
}
Pointers and Arrays
int myVariable = 1000; // define variable myVariable assign it a value of 1000
int *myPointer; // define a pointer variable
myPointer = &myVariable; //Create a pointer that contains the address of myVariable
int x; // Define new variable x
x = *myPointer; //Copy the value stored in the location that the pointer points to into x
Here the ‘*’ indicates that we want the contents stored at the memory location (in this case 1000) not the value of the pointer itself which simply contains the memory address.
These examples seem a little pointless as here we are only dealing with variables that refer to a single memory location. In fact the variable myVariable is really already a pointer to the memory location where the value is stored. However pointers become really useful when you have arrays. Consider an array (myArray) that is able to hold 5 integers.
int MyArray[5] = { 2, 5 , 3, 14, 300 }; // Define new array MyArray
int *myPointer; // Define pointer to integer type
myPointer = &MyArray[0]; // Create a pointer variable that points to the first value in the array.
Now whilst the code above is correct in practice you will never see this because for arrays you can drop the ‘&’ that is required to indicate you want the address of the Variable not its value. Note for variables that are not arrays i.e. only single values you must use the ‘&’ notation. For arrays the simplified code as shown below
int MyArray[5] = { 2, 5 , 3, 14, 300 }; // Define new array MyArray
int *myPointer; // Define pointer to integer type
myPointer = MyArray; // Create a pointer variable that points to the first value in the array.
You can now use the pointer to index the array consider the example given in the array section above
#define ARRAYSIZE 100 // define array size constant
int myArray[ARRAYSIZE]; // initialise array
int i; // initialise indexing variable
for (i=0; i<ARRAYSIZE; i++){ // loop through all array indices
myArray[i] = i; // store the current index value in the array
}
Using pointers
#define ARRAYSIZE 100 // define array size constant
int myArray[ARRAYSIZE]; // initialise array
int *myPointer = myArray; // initialise array pointer to first element of myArray
for (i=0; i<ARRAYSIZE; i++){ // loop through all array indices
*myPointer = i; // store the current index value in the location pointed to by myPointer
myPointer++; // increment pointer
}
This can be further reduced to
#define ARRAYSIZE 100 // define array size constant
int myArray[ARRAYSIZE]; // initialise array
int *myPointer = myArray; // initialise array pointer to first element of myArray
for (i=0; i<ARRAYSIZE; i++){ // loop through all array indices
*myPointer++ = i; // store the current index value in the location pointed to by myPointer
}
volatile char A;
volatile char B;
void SwapChars(char *a, char *b); // Function prototype
void main(void){
x1 = 'A';
x2 = 'B';
SwapChars(&x1, &x2); // Pass the pointers to variables x1 and x2 to the function
}
void SwapChars(char *a, char *b){
char c = 0; // initialise temporary variable c
P1OUT |= BIT6; // Set P1.0
c = *a; // copy the value in memory location a in variable c
*a = *b; // copy the value stored in memory location b into memory location a
*b = c; // copy the value temporarily stored in c into memory location b
}
char myString[5] = "Hello";
int data[2]={1023, 235};
void main(void){
UARTSendArray(myString, 5); // Send character string in myString
UARTSendArray("Bye", 3); // Send character string "Bye"
UARTSendArray(data, 4); // Send integer in array data as bytes
}
void UARTSendArray(char *TxArray, int ArrayLength){
while(ArrayLength--){ // Loop until StringLength == 0 and post decrement
while(!(IFG2 & UCA0TXIFG)); // Wait for TX buffer to be ready for new data
UCA0TXBUF = *TxArray++; //Write the character at the location specified by the pointer and post increment
}
}
this is the plot which is given for main programme
the final is given by
/* Example code demonstrating the use of pointers.
* It uses the hardware UART on the MSP430G2553 to receive
* and transmit data back to a host computer over the USB connection on the MSP430
* launchpad.
* Note: After programming using CCS it is necessary to stop debugging and reset the uC before
* connecting the terminal program to transmit and receive characters.
* This demo will transmit the characters AB in response to a S been sent each time S is sent the
* order of the characters is swaped. If agen any other character is sent and unknown command response is sent.
*/
#include "msp430g2553.h"
void UARTSendArray(char *TxArray, int ArrayLength);
void UARTSendChar(char *TxChar);
void SwapChars(char *a, char *b);
volatile char data;
volatile char x1;
volatile char x2;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= BIT0 + BIT6; // Set the LEDs on P1.0, P1.6 as outputs
P1OUT = BIT0; // Set P1.0
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ; // Set DCO to 1MHz
/* Configure hardware UART */
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // Use SMCLK
UCA0BR0 = 104; // Set baud rate to 9600 with 1MHz clock (Data Sheet 15.3.13)
UCA0BR1 = 0; // Set baud rate to 9600 with 1MHz clock
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
x1 = 'A';
x2 = 'B';
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
data = UCA0RXBUF;
switch(data){
case 'S':
{
SwapChars(&x1, &x2); // Pass the pointers to variables x1 and x2 to the function
UARTSendChar(x1);
UARTSendChar(x2);
UARTSendArray(" ", 2);
P1OUT &= ~BIT6; // Set P1.0
}
break;
default:
{
UARTSendArray("Unknown Command: ", 17);
UARTSendChar(data);
UARTSendArray(" ", 2);
}
break;
}
}
void UARTSendArray(char *TxArray, int ArrayLength){
// Send number of bytes Specified in ArrayLength in the array at using the hardware UART 0
// Example usage: UARTSendArray("Hello", 5);
// int data[2]={1023, 235};
// UARTSendArray(data, 4); // Note because the UART transmits bytes it is necessary to send two bytes for each integer hence the data length is twice the array length
while(ArrayLength--){ // Loop until StringLength == 0 and post decrement
while(!(IFG2 & UCA0TXIFG)); // Wait for TX buffer to be ready for new data
UCA0TXBUF = *TxArray++; //Write the character at the location specified by the pointer and post increment
}
}
void UARTSendChar(char *TxChar){
// Send number of bytes Specified in ArrayLength in the array at using the hardware UART 0
// Example usage: UARTSendArray('A');
while(!(IFG2 & UCA0TXIFG)); // Wait for TX buffer to be ready for new data
UCA0TXBUF = TxChar; //Write the character at the location specified py the pointer
}
void SwapChars(char *a, char *b){
char c = 0; // initialise temporary variable c
P1OUT |= BIT6; // Set P1.0
c = *a; // copy the value in memory location a in variable c
*a = *b; // copy the value stored in memory location b into memory location a
*b = c; // copy the value temporarily stored in c into memory location b
}
#define PIN3 3 //define PIN3 = 3
int pin3 = 3; // create a 16 bit variable in RAM and store 3 in it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.