Activity 3. Reading a Push Button with function
In this activity we want every time a push-button is pressed and released the state of each pin of PORTD changes, i.e. 1's become 0's and 0's become 1's. PORTD will be given the initial value 00001111. A function will be used to read the push button.
(75 minutes)
Activity 3
Step 1. The circuit is drawn in the Proteus Design Suite.

Step 2. Complete the program in C language is written.
#include <main.h> // the file main.h with the
// initial settings is included.
// This file must be placed in the same
// folder with the project.
// Also the 18F4550.h file must exist
// in the same folder with the project
#byte PORTB =0xF81
/* We attribute to the memory position 0xF81 the name PORTB. This means that we define an 8-bit variable whose value will be stored to the memory position F81h.*/
#byte PORTD =0xF83
// The position F83h is the PORTD data register.
void push_button(void);
//Declaration of push-button function
void main()
{
//PortD becomes output
//PortB becomes input
PORTD=0x0F;
//PortD is given initial value 00001111
while(TRUE) {
//The push-button function is called
//switch states in PORTD bits
}
}
//Define push_button function
//The function definition is written after the main program
//With this function the microcontroller waits
//a full button press (press+release)
//and then go to the next command.
void push_button(void) {
// Wait until the button is pressed
// In the wait state, no command is executed
// When the button is pressed the loop is exited
// and the next command is executed
// 50ms delay to avoid bounce effect
//Wait until the button is released
// In the wait state, no command is executed
// When the button is released the loop is exited
// and the next command is executed
// 50ms delay to avoid bounce effect
}Step 3. Compile the program in C in order to create the program in the microcontroller machine code (hex file).
Step 4. Load to the microcontroller the hex file (program in machine code) that was created from the CCS C Compiler.
Step 6. Suggested modifications and discussion
Modify the hardware and the code so that the push-button been connected in RD0