Skip to main content

Project 4 - LED Color change with buttons

With the concept of interfacing a push button form previous post, lets interface another push button which is connected to PF_0 as SW2.

But PF_0 has an alternative function which is MISO of spi-1. So because of this we need to follow the below mentioed procedure to read data from PF_0 pin.

Before we learn how to read data from PF_0, let look into GPIO_LOCK and GPIO_CR registers.

GPIO_LOCK:
This register controls the write access to GPIO_CR. Writing 0x4C4F434B to this register unlocks GPIO_CR, writing any other value enables the lock on GPIO_CR. After the GPIO_CR is updated or written with new values, it reapplies the lock. This process needs to be repeated every time GPIO_CR register needs to be written.

GPIO_CR:
This is a commit register. The value of this register determines which bits of GPIO_AFSEL, GPIO_PUR, GPIO_PDR, GPIO_DEN are committed when write to these registers is performed. If the bit in the GPIO_CR  is cleared, the corresponding bit in above mentioned registers retains the previous value.
Also as the above paragraph highlights that GPIO_CR controls the write ability to the above mentioned registers, first the GPIO_LOCK needs to be unlocked then followed by GPIO_CR, then rest of the registered can be configured.

Program: Click here to view the file

In the program you will come across the variable "flag", this is responsible to control the push button interface. If for the first time the button is pressed, the flag is set and this value retains till the button is released. Once the button is released the flag resets and waits for the next push signal.

This feature prevents the counter (variable "count") to increment indefinitely when the button is pressed at the system is clocked at 80 MHz.

Back to Project Home Page 

Comments