Activity 2. Counting the 1s of PORTD

The purpose of this activity is to count the 1s of PORTD and show the result on the LCD 16x2.

(45minutes)


Activity 2

Step 1. The circuit is drawn in the Proteus Design Suite.



Step 2. The program in C language is written.

Write in CCS Compiler the program in C language

 #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

               

#include <flex_lcd.h> // The h file of the lcd driver

                      // should be in the same folder where we will save our program.

                      // The #define LCD_DB4 PIN_B4 etc statements in flex_lcd.c

                      // should be checked and possibly modified.

                      // These statements determine the pins of the microcontroller

                      // that are connected to LCD 16x2.

                           

#byte PORTB =0xF81  

                     // We attribute to the memory position 0xF81 the name PORTB.

                     // This means that we define a 8 bit variable whose value

                     // will be stored to the memory position F81h.

                     // The memory position F81h is the PORTD data register.

 

#byte PORTD=0xF83    // F83h is the position or PORTD data register

                     // at the data memory of the microcontroller

                     // SFR Special Function Register

 

 

// ********* main program ************************

 

void main(){                   // Opening bracket of main

   set_tris_b(0x00);   // PORTB is set as output port

                       // (PORTB Direction Register = 0000 0000)

 

   set_tris_d(0xff);  // PORTD is set as input port 

                      // (PORTD Direction Register = 1111 1111)

 

   lcd_init();              // Initialization routine

   PORTB=0b00000000;   // PORTB takes the initial value of 0000 0000

   int i=0;            // Integer varable used in the  for() { }

   int a;              // Integer varable a

   while(TRUE) {       // Endless loop(condition always true)

      delay_ms(500);   // Wait for 0.5 second

      a=0;

      for (i=0; i<=7; i++){

         a = a + bit_test(PORTD,i);

         // The function bit_test(PORTD,i) checks the bit i of PORTD data register

         // If the bit equals to 1 then the function returns the value 1.

         // If the bit is 0, then the function returns the value 0.

         // Variable a equals the multitude of 1s of  PORTD

      }  // Closing bracket of for() { }

     

      lcd_gotoxy(1,1);  // The cursor moves to the first position of the first line

      printf(lcd_putc,"%d",a); // The value of variable a is shown on the LCD

   }        // Closing bracket of while

}           // Closing bracket of main



Step 3. Use the CCS C Compiler to translate the programm from C language to the microcontroller machine code. Load to the microcontroller the hex file (machine code) that was created from the CCS Compiler.

.


Step 4. Run the simulation and check the correct operation of the circuit.



Step 5. Suggested modifications and discussion:

    • what do we need to change in the circuit and in the code so that the LCD 16x2 is connected to the PORTC?