miércoles, 24 de enero de 2018



PA5 as a switch 

TIVA C AMR CORTEX M4 TM4C123GH6PMI 


This program Configures terminal 5 of port A (PA5) to be used as a switch.
Then each time this switch is pressed the green LED connected to port F will light up.
Any question let me know.
If you want help with a project, a course or consultancy please contact me.
--------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------


#include "TM4C123.h"                    // Device header

// begins the definition of the registers of Port A to be used as a GPIO

#define SYSCTL_RCGC2_R     (*((volatile unsigned long *)0x400FE108))
#define GPIO_PORTA_PCTL_R  (*((volatile unsigned long *)0x4000452C))
#define GPIO_PORTA_AMSEL_R (*((volatile unsigned long *)0x40004528))
#define GPIO_PORTA_DIR_R   (*((volatile unsigned long *)0x40004400))
#define GPIO_PORTA_AFSEL_R (*((volatile unsigned long *)0x40004420))
#define GPIO_PORTA_DEN_R   (*((volatile unsigned long *)0x4000451C))
#define GPIO_PORTA_DATA_R  (*((volatile unsigned long *)0x400043FC))
// ends the definition of the registers of Port A 



// begins the definition of the registers of Port F to be used as a GPIO
#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))
// ends the definition of the registers of Port A 

unsigned long PA5; // We name the address of the port A


void Switch_PA5(void); //Function prototype that handles the PA as switch

void PortF_Init(void); //Prototype of the port F initialization function

int main()

{
Switch_PA5();
PortF_Init();

while(1)
{
    PA5 =GPIO_PORTA_DATA_R & 0x20; // This is an ADN of PORTA with 0010 0000
    if(PA5 == 0x20) // iF PA5 is 1
{
GPIO_PORTF_DATA_R |= 0x08; // Green LED of PORTF is on
}
else
{
GPIO_PORTF_DATA_R &= ~0x08; // Green LED of PORTF is off 
}
}
}

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 |= 0x08;          // 5) PF3 output
  GPIO_PORTF_AFSEL_R &= 0x00;        // 6) no alternate function
  GPIO_PORTF_DEN_R |= 0x1F;          // 7) enable digital pins PF4-PF0     
}


void Switch_PA5(void){

volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000001; //1) activate clock for Port A
delay = SYSCTL_RCGC2_R; //allow time for clock to start
GPIO_PORTA_AMSEL_R &= ~0x20; // 3) disable analog on PA5
GPIO_PORTA_PCTL_R &= ~0x00F00000; // 4) PCTL GPIO on PA5
GPIO_PORTA_DIR_R &= ~0x20; // 5) direction PA5 input
GPIO_PORTA_AFSEL_R &= ~0x20; // 6) PA5 regular port function
GPIO_PORTA_DEN_R |= 0x20; // 7) enable PA5 digital port
}

No hay comentarios.:

Publicar un comentario

BTFSS INSTRUCTION  IN ASSEMBLER FOR PIC18F In this sample, we show that how to use a switch input to make an action. Remember that I...