Activity 2. Reading a push-button toggle

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.

(45 minutes)


Activity 2


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 main()

{  

   set_tris_d(0x00);//PortD becomes output

   set_tris_b(0xFF);//PortB becomes input

   PORTD=0x0F;

   //PortD is given initial value 00001111

   while(TRUE) {  

            

   // Wait until the button is pressed

   // 50ms delay to avoid bounce effect

   

 

   // Wait until the button is released

   // 50ms delay to avoid bounce effect

 

 

      PORTD=PORTD^0b11111111;  

   //Invert PORTD bits via XOR logic gate

      }                

}        


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 5.  Run the simulation and check the correct operation of the circuit.