Activity 2. Create a moving dot

The purpose of this activity is to interrupt the main program. In the main program 8 LEDs flash. When an interrupt occurs from INT2, the LEDs create a moving dot.

(35 minutes)


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 C 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

#byte PORTD =0xF83 // We attribute to the memory position 0xF83

                                 // the name PORTD

                                 // This means that we define a 8 bit

                                 // variable whose value will be stored

                                 // to the memory position F83h

                                 // The memory position F83h is the PORTD

                                 // data register

 

void init(void);

void ext_int2(void);

 

void main(){

   init();        //initialization routine

   while(TRUE){   //flash 8 LEDs

      PORTD=0b11111111;   

      delay_ms(100);                                                    

      PORTD=0b00000000;

      delay_ms(100);

   }                                                                 

}                                                    

 

//initialization routine

void init(){     

   set_tris_d(0x00);            //PORTD is defined as output

   PORTD = 0b00000000;          //The PORTD data register is given the value 0

         

   ext_int_edge(2, H_TO_L);     //Activation of the interrupt from RB2

                                //during the transition from 1 to 0 (falling edge)

 

   enable_interrupts(GLOBAL);    //Enable global interrupts

   enable_interrupts(INT_EXT2);  //Enable external interrupt by RB2

}                                                

 

//external interrupt by RB2

#INT_EXT2                                                                      

void ext_int2() {   //moving dot   

   PORTD=0b00000000;    delay_ms(200);

   PORTD=0b10000000;    delay_ms(200);

   PORTD=0b01000000;    delay_ms(200);

   PORTD=0b00100000;    delay_ms(200); 

   PORTD=0b00100000;    delay_ms(200);

   PORTD=0b00010000;    delay_ms(200);

   PORTD=0b00001000;    delay_ms(200);   

   PORTD=0b00000100;    delay_ms(200); 

   PORTD=0b00000010;    delay_ms(200);          

   PORTD=0b00000001;    delay_ms(200);

   PORTD=0b00000010;    delay_ms(200); 

   PORTD=0b00000100;    delay_ms(200);

   PORTD=0b00001000;    delay_ms(200);

   PORTD=0b00010000;    delay_ms(200);

   PORTD=0b00100000;    delay_ms(200);

   PORTD=0b01000000;    delay_ms(200);

   PORTD=0b10000000;    delay_ms(200);

   PORTD=0b00000000;    delay_ms(200);

}



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.