Turn on three LEDs with two switches

 This program should  run on the LaunchPad
 You will debug this program as your Lab 4

Below are the conditions that should be met:


  •  If both switches SW1 and SW2 are pressed, the LED should be blue
  •  If just SW1 switch is pressed, the LED should be red
  •  If just SW2 switch is pressed, the LED should be green
  •  If neither SW1 or SW2 is pressed, the LED should be off


 LaunchPad built-in hardware
 SW1 left switch is negative logic PF4 on the Launchpad
 SW2 right switch is negative logic PF0 on the Launchpad
 red LED connected to PF1 on the Launchpad
 blue LED connected to PF2 on the Launchpad
 green LED connected to PF3 on the Launchpad

 Solution:
Solved by Tolulope Ademilua:
Copyright 2016 by Jonathan W. Valvano, valvano@mail.utexas.edu
You may use, edit, run or distribute this file
 as long as the above copyright notice remains
 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,

 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
#include "TExaS.h"
#define GPIO_PORTF_DATA_R       (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R           (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R      (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R          (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R          (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R       (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R             (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R      (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R          (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R               (*((volatile unsigned long *)0x400FE108))
// READ ONLY THE SPECIFIC PORT NUMBER
#define  PF4_R                                     (*((volatile unsigned long *)0x40025040))
#define  PF0_R                                     (*((volatile unsigned long *)0x40025004))
#define  PF321_R                                 (*((volatile unsigned long *)0x40025038))

// 2. Declarations Section
//   Global Variables
   unsigned long SW1,SW2;  // input from PF4,PF0
//   Function Prototypes
void PortF_Init(void);
void Delay(void);
void EnableInterrupts(void);

void PortF_Init(void){ volatile unsigned long delay;
  SYSCTL_RCGC2_R |= 0x00000020;     // 1) F clock
  delay = SYSCTL_RCGC2_R;           // delay
  GPIO_PORTF_LOCK_R = 0x4C4F434B;   // 2) unlock PortF PF0
  GPIO_PORTF_CR_R = 0x1F;           // allow changes to PF4-0   
  GPIO_PORTF_AMSEL_R = 0x00;        // 3) disable analog function
  GPIO_PORTF_PCTL_R = 0x00000000;   // 4) GPIO clear bit PCTL
  GPIO_PORTF_DIR_R = 0x0E;          // 5) PF4,PF0 input, PF3,PF2,PF1 output
  GPIO_PORTF_AFSEL_R = 0x00;        // 6) no alternate function
  GPIO_PORTF_PUR_R = 0x11;          // enable pullup resistors on PF4,PF0   
  GPIO_PORTF_DEN_R = 0x1F;          // 7) enable digital pins PF4-PF0     
}

// 3. Subroutines Section
// MAIN: Mandatory for a C Program to be executable
int main(void){ 
  TExaS_Init(SW_PIN_PF40,LED_PIN_PF321);
  // TExaS_Init initializes the real board grader for lab 4
  PortF_Init();              // Call initialization of port PF4, PF3, PF2, PF1, PF0 
  EnableInterrupts();   // The grader uses interrupts
  while(1){
SW1 = PF4_R&0x10;     //read PF4 into SW1
  SW2 = PF0_R&0x01;     //read PF0 into SW2
  //If both switches SW1 and SW2 are pressed, the LED should be blue
if(SW1 && SW2){
PF321_R = 0x00;
}else{
//If just SW1 switch is pressed, the LED should be red
if(SW1 && (!SW2)){
PF321_R = 0x08;
}else{
 //If just SW2 switch is pressed,the LED should be green
  if(!SW1 && SW2){
    PF321_R = 0x02;
}else{
 //If neither SW1 or SW2 is pressed,the LED should be off
PF321_R = 0x04;
       }

    }

   }
  }
}
Color    LED(s) PortF
 dark     ---    0
 red      R--    0x02
 blue     --B    0x04
 green    -G-    0x08
 yellow   RG-    0x0A
 sky blue -GB    0x0C
 white    RGB    0x0E
 pink     R-B      0x06

Turning On and Off LEDs

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

New Post

New style string formatting in Python

In this section, you will learn the usage of the new style formatting. Learn more here . Python 3 introduced a new way to do string formatti...