ENG2008 lab 3 answer sheet
Complete the missing code in grey boxes.
Task 3.1 (lab3a.c)
// traffic light
#include
const uint32_t led_mask[] = {1UL << 18, 1UL << 19, 1UL << 1};
// LED #0, #1 are port B, LED #2 is port D
void LED_Config(void);
void LED_Set(void);
void LED_Clear(void);
__INLINE static void LED_On (uint32_t led);
__INLINE static void LED_Off (uint32_t led);
void Delay(uint32_t nCount);
int main(void)
{
Click here to enter text.
// VARIABLE DECLARATION IF REQUIRED
SystemCoreClockUpdate(); // Optional- Setup SystemCoreClock variable
// Configure LED outputs
LED_Config();
Click here to enter text.
while(1){ // YOUR CODE HERE (4 marks)
};
}
void Delay(uint32_t nCount) // delay subroutine
{
while(nCount--)
{
}
}
/*----------------------------------------------------------------------------
LED pin config
*----------------------------------------------------------------------------*/
void LED_Config(void)
{ // (3 marks)
/* Enable Clock to Port B & D */
Click here to enter text.
/* Pin PTB18 is GPIO */
Click here to enter text.
/* Pin PTB19 is GPIO */
Click here to enter text.
/* Pin PTD1 is GPIO */
Click here to enter text.
/* switch Red/Green LED off */
Click here to enter text.
/* enable PTB18/19 as Output */
Click here to enter text.
/* switch Blue LED off */
Click here to enter text.
/* enable PTD1 as Output */
Click here to enter text.
return;
}
/*---------------------------------------------------------------------------
Switch on LEDs
*---------------------------------------------------------------------------*/
void LED_Set(void)
{
LED_On(0); // RED
LED_On(1); // GREEN
LED_On(2); // BLUE
return;
}
/*---------------------------------------------------------------------------
Switch off LEDs
*---------------------------------------------------------------------------*/
void LED_Clear(void)
{
LED_Off(0); // RED
LED_Off(1); // GREEN
LED_Off(2); // BLUE
return;
}
/*---------------------------------------------------------------------------
Switch on LED (just one)
*---------------------------------------------------------------------------*/
__INLINE static void LED_On (uint32_t led) {
if (led == 2) FPTD->PCOR = led_mask[led];
else FPTB->PCOR = led_mask[led];
}
/*—————————————————————————
Switch off LED (just one)
*—————————————————————————*/
__INLINE static void LED_Off (uint32_t led) {
if (led == 2) FPTD->PSOR = led_mask[led];
else FPTB->PSOR = led_mask[led];
}
Task 3.2 (lab3b.c)
// INPUT CONTROLLED LEDs
#include
const uint32_t led_mask[] = {1UL << 18, 1UL << 19, 1UL << 1};
// LED #0, #1 are port B, LED #2 is port D
void IO_Config(void);
void LED_Set(void);
void LED_Clear(void);
__INLINE static void LED_On (uint32_t led);
__INLINE static void LED_Off (uint32_t led);
#define MASK(x) (1UL << (x))
int main(void)
{
// VARIABLE DECLARATION IF REQUIRED
Click here to enter text.
SystemCoreClockUpdate(); // Optional- Setup SystemCoreClock variable
// Configure LED outputs
IO_Config();
Click here to enter text.
while(1){ // YOUR CODE HERE (4 marks)
}
}
/*----------------------------------------------------------------------------
I/O pin config
*----------------------------------------------------------------------------*/
void IO_Config(void)
{ // (3 marks)
/* Enable Clock to Port B & D */
Click here to enter text.
/* Pin PTB18 is GPIO */
Click here to enter text.
/* Pin PTB19 is GPIO */
Click here to enter text.
/* Pin PTD1 is GPIO */
Click here to enter text.
/* Pin PTD0 is GPIO, Enable pull up resistor*/
Click here to enter text.
/* Pin PTD2 is GPIO, Enable pull up resistor */
Click here to enter text.
/* Pin PTD5 is GPIO, Enable pull up resistor */
Click here to enter text.
/* switch Red/Green LED off */
Click here to enter text.
/* enable PTB18/19 as Output */
Click here to enter text.
/* switch Blue LED off */
Click here to enter text.
/* enable PTD1 as Output */
Click here to enter text.
/* enable PTD0,2,5 as Input */
Click here to enter text.
return;
}
/*---------------------------------------------------------------------------
Switch on LEDs
*---------------------------------------------------------------------------*/
void LED_Set(void)
{
LED_On(0); // RED
LED_On(1); // GREEN
LED_On(2); // BLUE
return;
}
/*---------------------------------------------------------------------------
Switch off LEDs
*---------------------------------------------------------------------------*/
void LED_Clear(void)
{
LED_Off(0); // RED
LED_Off(1); // GREEN
LED_Off(2); // BLUE
return;
}
/*---------------------------------------------------------------------------
Switch on LED (just one)
*---------------------------------------------------------------------------*/
__INLINE static void LED_On (uint32_t led) {
if (led == 2) FPTD->PCOR = led_mask[led];
else FPTB->PCOR = led_mask[led];
}
/*—————————————————————————
Switch off LED (just one)
*—————————————————————————*/
__INLINE static void LED_Off (uint32_t led) {
if (led == 2) FPTD->PSOR = led_mask[led];
else FPTB->PSOR = led_mask[led];
}
Task 3.3 (lab3c.c)
// 7-segment, display only 1 digit
#include
// PTC pins connected to A-G and DP of 7–segment display
const uint32_t led_mask[] = {1UL << 0, 1UL << 6, 1UL << 12, 1UL << 16, 1UL << 17, 1UL << 3, 1UL << 11, 1UL << 13};
// PTC pins connected to DIGIT 1-4 of 7–segment display
const uint32_t led_ctrl_mask[] = {1UL << 7, 1UL << 4, 1UL << 5, 1UL << 10};
void IO_Config(void);
void LED_Display(int8_t x, int8_t y);
void LED_Clear(void);
void Delay(uint32_t nCount);
int main(void)
{
// VARIABLE DECLARATION IF REQUIRED
Click here to enter text.
SystemCoreClockUpdate(); // Optional- Setup SystemCoreClock variable
// Configure LED outputs
IO_Config();
while(1){ // YOUR CODE HERE (4 marks)
Click here to enter text.
};
}
/*----------------------------------------------------------------------------
I/O pin config
*----------------------------------------------------------------------------*/
void IO_Config(void)
{ // (3 marks)
/* Enable Clock to Port C */
Click here to enter text.
/* All PTC pins as output */
Click here to enter text.
/* Pin PTC17 is GPIO */
Click here to enter text.
/* Pin PTC16 is GPIO */
Click here to enter text.
/* Pin PTC13 is GPIO */
Click here to enter text.
/* Pin PTC12 is GPIO */
Click here to enter text.
/* Pin PTC11 is GPIO */
Click here to enter text.
/* Pin PTC10 is GPIO */
Click here to enter text.
/* Pin PTC7 is GPIO */
Click here to enter text.
/* Pin PTC6 is GPIO */
Click here to enter text.
/* Pin PTC5 is GPIO */
Click here to enter text.
/* Pin PTC4 is GPIO */
Click here to enter text.
/* Pin PTC3 is GPIO */
Click here to enter text.
/* Pin PTC0 is GPIO */
Click here to enter text.
return;
}
/*---------------------------------------------------------------------------------
display number, x (0-9), in digit y (1-4) of the 7-segment display
*---------------------------------------------------------------------------------*/
void LED_Display(int8_t x, int8_t y)
{ // (3 marks)
FPTC->PCOR = led_ctrl_mask[y-1]; // select digit
switch(x){
/* Display 0 */
case 0:
Click here to enter text.
break;
/* Display 1 */
case 1:
Click here to enter text.
break;
/* Display 2 */
case 2:
Click here to enter text.
break;
/* Display 3 */
case 3:
Click here to enter text.
break;
/* Display 4 */
case 4:
Click here to enter text.
break;
/* Display 5 */
case 5:
Click here to enter text.
break;
/* Display 6 */
case 6:
Click here to enter text.
break;
/* Display 7 */
case 7:
Click here to enter text.
break;
/* Display 8 */
case 8:
Click here to enter text.
break;
/* Display 9 */
case 9:
Click here to enter text.
break;
}
return;
}
/*—————————————————————————
Clear all digits on the 7-segment display
*—————————————————————————*/
void LED_Clear(void)
{ // (2 marks)
/* clear all display on the 7-segment display */
Click here to enter text.
/* disable all digits of the 7-segment display*/
Click here to enter text.
return;
}
void Delay(uint32_t nCount) // delay subroutine
{
while(nCount–)
{
}
}