Activity 2. Analog to Digital Converter

The purpose of this activity is for the microcontroller to use the built-in Analog to Digital Converter.

(50 minutes)


Activity 2a

The PIC18F4550

    • read the analog voltage of a potentiometer and convert it to a value (0~1023)
    • display the value on a LCD

 

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.

 

 

//ADC = 10 Bit => values: 0~1023

unsigned int16 ADC_value;

 

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

void main() {

   set_tris_b(0x00);       //PORTB is defined as output

   lcd_init();          //initialization routine for the LCD 16x2               

   setup_adc(ADC_CLOCK_DIV_8);      // Set ADC conversion time to 8Tosc

   setup_adc_ports(AN0);            // Set RA0 as analog pin

   set_adc_channel(0);              // Select channel 0 (analog input 0)

   while(TRUE){

      delay_ms(1000);            //wait for 1 sec

      lcd_putc("\f");            //clear the screen

      lcd_putc(" ADC value:");   //send a message to the LCD

      lcd_gotoxy(1,2);           //first position of second line 

      ADC_value=read_adc();      //read value from ADC    

      printf(lcd_putc,"%Lu",ADC_value); //send adc value to the LCD  

   }         

}



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.