Question: I need to write a code in Code Composer Studio for an MSP432 that changes the brightness of an LED using PWM from systick as

I need to write a code in Code Composer Studio for an MSP432 that changes the brightness of an LED using PWM from systick as a pseudo-analog signal. The user will input a number from 0-100 over a membrane keypad, and the LED will light to the corresponding percentage. Here is the code I have written so far, unfortunately, it always returns zeros from the Read_Keypad function.

#include "msp.h" #include #include #include

//Function names are initialized, and an array is created

uint8_t num, dutyCycle; void keypad_init(void); uint8_t Read_Keypad(void); uint8_t Delay_Cyl_Input(); uint8_t PWM_LED(uint8_t dutyCycle); uint8_t keyEntry[3];

//The main function is called

void main(void) { WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer

keypad_init();

printf("Please enter a 3 digit delay cycle length (0-100) followed by #. "); printf("The asterisk key is disabled, and has no function. ");

Delay_Cyl_Input(); PWM_LED(dutyCycle);

}

//The keypad is initialized as an input

void keypad_init(void) { P4SEL0 &= ~0xFF; P4SEL1 &= ~0xFF; P4->DIR &= ~(BIT0| BIT1 | BIT2| BIT3); P4REN |= (BIT0 | BIT1 | BIT2| BIT3); P4OUT |= (BIT0 | BIT1 | BIT2| BIT3); P4DIR &= ~(BIT4 | BIT5| BIT6); }

//This function reads the value of a single key press

uint8_t Read_Keypad (void) { uint8_t col, row; for(col=0; col<3; col++) { P4DIR =0x00; P4DIR |= BIT(4 + col); P4OUT &=~ BIT(4 + col); _delay_cycles(10); row = P4IN & 0x0F;

while(!(P4IN & BIT0) || !(P4IN & BIT1)|| !(P4IN & BIT2) || !(P4IN & BIT3)); { if(row != 0x0F) break; } }

P4DIR = 0x00; if (col ==3) return 0; if (row== 0x0E) num = col+1; if (row == 0x0D) num = 3 + col +1; if (row == 0x0B) num = 6 + col +1; if (row== 0x07) num = 9 + col+ 1; return 1; }

void SysTick_Init(void) { SysTick -> CTRL = 0; SysTick -> LOAD = 0x00FFFFFF; SysTick -> VAL = 0; SysTick -> CTRL = 0x00000005; }

void SysTick_delay(uint16_t delay) { SysTick -> LOAD = ((delay*3000) - 1); SysTick -> VAL = 0;

while((SysTick -> CTRL & 0x00010000) == 0); }

uint8_t PWM_LED(uint8_t dutyCycle) { P3SEL0 &= ~BIT0; P3SEL1 &= ~BIT0; P3DIR |= BIT0;

P3OUT &= ~BIT0;

if(dutyCycle == 0) { P3OUT &= ~BIT0; } else if(dutyCycle == 100) { P3OUT |= BIT0; } else { P3OUT &= ~BIT0; SysTick_delay(100 - dutyCycle); P3OUT |= BIT0; SysTick_delay(dutyCycle); } }

uint8_t Delay_Cyl_Input() { while(1) { int i;

for(i=0;i<2;i++) { if(Read_Keypad()) { if(num < 12) { keyEntry[0] = keyEntry[1]; keyEntry[1] = keyEntry[2]; keyEntry[2] = num; } } }

printf("%d%d%d ", keyEntry[0], keyEntry[1], keyEntry[2] );

if(num == 12) { dutyCycle = strtol(keyEntry, NULL, 10);

if(dutyCycle >= 100) { printf("This delay cycle is not 0-100. "); return 0; } else { return dutyCycle; } } break; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!