4.1 Práctica 1. Uso los registros GPIODEN, GPIODIR, GPIODATA.
Tema adicional complementario 1. Inicialización del Puerto F usando Keil
Codigo para inizializar el puerto F:
#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))
void PortF_Init(void);
int main()
{
PortF_Init();
while(1)
{
GPIO_PORTF_DATA_R |= 0x08;
GPIO_PORTF_DATA_R &= ~0x08;
}
}
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 &= ~0x11; // 5.1) PF4,PF0 input,
GPIO_PORTF_DIR_R |= 0x08; // 5.2) PF3 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
}
#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))
void PortF_Init(void);
int main()
{
PortF_Init();
while(1)
{
GPIO_PORTF_DATA_R |= 0x08;
GPIO_PORTF_DATA_R &= ~0x08;
}
}
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 &= ~0x11; // 5.1) PF4,PF0 input,
GPIO_PORTF_DIR_R |= 0x08; // 5.2) PF3 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
}
4.2 Práctica 2. Uso del push button.
Codigo de la práctica 2.
#include "TM4C123.h" // Device header
unsigned long SW1;
int main()
{
SYSCTL->RCGCGPIO |= 0x01 << 5;
GPIOF->DEN |= 0x1F; //0001 1111
GPIOF->DIR |= 0x0E; //0000 1110
//PF4 = 0 -> input - SW1
//PF3 = 1 ->Output - GEEN LED
//PF2 = 1 ->Output - BLUE LED
//PF1 = 1 ->Output - RED LED
//PF0 = 0 -> input - SW2
GPIOF->PUR |= 0x10; //0001 0000 -> PF4 is an input
while(1)
{
GPIOF->DATA |= 0x04;
SW1 = (GPIOF->DATA) & 0x10;
if(SW1){
GPIOF->DATA &= 0x0B; //0000 1011 ->PF2 BLUE LED
}
else{
GPIOF->DATA &= 0x0B; //0000 1011 ->PF2 BLUE LED
GPIOF->DATA |= 0x08;
}
}//this is the end of while(1)
return 0;
}//this is the end of main
unsigned long SW1;
int main()
{
SYSCTL->RCGCGPIO |= 0x01 << 5;
GPIOF->DEN |= 0x1F; //0001 1111
GPIOF->DIR |= 0x0E; //0000 1110
//PF4 = 0 -> input - SW1
//PF3 = 1 ->Output - GEEN LED
//PF2 = 1 ->Output - BLUE LED
//PF1 = 1 ->Output - RED LED
//PF0 = 0 -> input - SW2
GPIOF->PUR |= 0x10; //0001 0000 -> PF4 is an input
while(1)
{
GPIOF->DATA |= 0x04;
SW1 = (GPIOF->DATA) & 0x10;
if(SW1){
GPIOF->DATA &= 0x0B; //0000 1011 ->PF2 BLUE LED
}
else{
GPIOF->DATA &= 0x0B; //0000 1011 ->PF2 BLUE LED
GPIOF->DATA |= 0x08;
}
}//this is the end of while(1)
return 0;
}//this is the end of main
Tema adicional complementario 2. Switch Usando el pueto A terminal 5 PA5.
En este tema adicional. Se busca que se refuerce la configuración de rgistos para la inizializacion de puertos como entras y salidas. En este ejemplo se usa una entrada en la terminal PA5 y un LED como terminal de salida PF3.
Cuando se presiona el boton en la terminal PA5 se enciende el LED verde.
Cuando se presiona el boton en la terminal PA5 se enciende el LED verde.
#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 AND 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
}
// 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 AND 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
}
Reto para el estudiante 1. Configurar el PB6 como como entrada de la misma manera que se configuró el PA5 en el tema adicional 2.
Despues de concluir con el ejercicio pueden enviar el condigo que funciene a : [email protected] con el titulo de: Reto1 ARM cortex M4 2019.
Sugerencia de solución:
Hacer los siguientes cambios.
1) Cambiar PA5 por PB6 en todos lados.
2) Cambiar la direccion 400040080 a 40005100, la cual corresponde a PB6
3) Cambiar 0x00000001 por 0x00000002 para activar el pueto Port B
4) Cambiar 0x00F00000 por 0x0F000000 para mover usar el bit 6 en lugar del 5
5) Cambiar 0x20 por 0x40 en todos los casos para usar el bit 6 en lugar del 5.
6) Cambiar PORTA por PORTB .
Sugerencia de solución:
Hacer los siguientes cambios.
1) Cambiar PA5 por PB6 en todos lados.
2) Cambiar la direccion 400040080 a 40005100, la cual corresponde a PB6
3) Cambiar 0x00000001 por 0x00000002 para activar el pueto Port B
4) Cambiar 0x00F00000 por 0x0F000000 para mover usar el bit 6 en lugar del 5
5) Cambiar 0x20 por 0x40 en todos los casos para usar el bit 6 en lugar del 5.
6) Cambiar PORTA por PORTB .
4.3 Práctica 3. Uso del display de 7 segmentos.
Codigo de la práctica 3.
#include "TM4C123.h" // Device header
// begins the definition of the registers of Port B to be used as a GPIO
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_DATA_R (*((volatile unsigned long *)0x400053FC))
// ends the definition of the registers of Port B
// 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 F
int i=0;
void PortB_Init(void);
void delay(unsigned long halfsecs);
int main()
{
char x [] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
PortB_Init();
while(1)
{
for(i = 0; i < 10; i++)
{
GPIO_PORTB_DATA_R |= x[i];
delay(5);
GPIO_PORTB_DATA_R &= 0x00;
}
}
}
void PortB_Init(void){
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000002; //1) activate clock for Port B
delay = SYSCTL_RCGC2_R; //allow time for clock to start
GPIO_PORTB_AMSEL_R &= ~0xFF; // 3) disable analog on PA5
GPIO_PORTB_PCTL_R &= ~0xFFFFFFFF; // 4) PCTL GPIO on PA5
GPIO_PORTB_DIR_R |= 0xFF; // 5) direction PortB output
GPIO_PORTB_AFSEL_R &= ~0xFF; // 6) PB regular port function
GPIO_PORTB_DEN_R |= 0xFF; // 7) enable the PB as a digital port
}
void delay(unsigned long halfsecs){
unsigned long count;
while(halfsecs > 0 ) { // repeat while there are still halfsecs to delay
count = 1538460; // 400000*0.5/0.13 that it takes 0.13 sec to count down to zero
while (count > 0) {
count--;
} // This while loop takes approximately 3 cycles
halfsecs--;
}
}
// begins the definition of the registers of Port B to be used as a GPIO
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_DATA_R (*((volatile unsigned long *)0x400053FC))
// ends the definition of the registers of Port B
// 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 F
int i=0;
void PortB_Init(void);
void delay(unsigned long halfsecs);
int main()
{
char x [] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
PortB_Init();
while(1)
{
for(i = 0; i < 10; i++)
{
GPIO_PORTB_DATA_R |= x[i];
delay(5);
GPIO_PORTB_DATA_R &= 0x00;
}
}
}
void PortB_Init(void){
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000002; //1) activate clock for Port B
delay = SYSCTL_RCGC2_R; //allow time for clock to start
GPIO_PORTB_AMSEL_R &= ~0xFF; // 3) disable analog on PA5
GPIO_PORTB_PCTL_R &= ~0xFFFFFFFF; // 4) PCTL GPIO on PA5
GPIO_PORTB_DIR_R |= 0xFF; // 5) direction PortB output
GPIO_PORTB_AFSEL_R &= ~0xFF; // 6) PB regular port function
GPIO_PORTB_DEN_R |= 0xFF; // 7) enable the PB as a digital port
}
void delay(unsigned long halfsecs){
unsigned long count;
while(halfsecs > 0 ) { // repeat while there are still halfsecs to delay
count = 1538460; // 400000*0.5/0.13 that it takes 0.13 sec to count down to zero
while (count > 0) {
count--;
} // This while loop takes approximately 3 cycles
halfsecs--;
}
}
Reto para el estudiante numero 2. Explique clara y brevemente cuado es importante configurar el registro PCTL o GPIOPCTL.
Nota: Envie su respuesta a [email protected]
Referencias.
[1] Jonathan Valvano, Embedded Systems: Real-Time Operating Systems for Arm Cortex M Microcontrollers 2nd ed.