17
ELCT706 MicroLab Session #3 7-segment LEDs and Analog to Digital Conversion ELCT 708 Session #3 Dr. Mohamed Abdel Ghany Eng. Salma Hesham

ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ELCT706 MicroLabSession #3

7-segment LEDs and Analog to Digital Conversion

ELCT 708 Session #3 Dr. Mohamed Abdel GhanyEng. Salma Hesham

Page 2: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

7-Segment LED Display

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

g f com a b

e d com c P

Page 3: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

7-Segment LED Display

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

Common Cathode - Com Pin = Gnd- Active high inputs- Example inputs to write 3:

pins = a b c d e f g P3 = 1 1 1 1 0 0 1 0

Common Anode - Com Pin = VCC- Active low inputs- Example inputs to write 3:

pins = a b c d e f g P3 = 0 0 0 0 1 1 0 1

Page 4: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

Common Cathode 7-Segment input values

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

Page 5: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

7-Segment LED Display

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

Task 1

Write the MikroC code to control a one digit 7-segment display connected to PORTB of the PIC16F877A such that it counts from 0 to 9 with 1 second delay.

Page 6: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

Analog to Digital Converter

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

Analog voltage values binary numbers (digital values)- Larger number of bits= higher resolution/ more accuracy

Example: Input voltage range from 0 to 5VAvailable bits = 5 bits 23=8 different valuesResolution = full range/#values = 5/32 = 0.625V

Analog input voltage Digital values

0 – 0.625 V 000

0.625 – 1.25 V 001

1.25 – 1.875 V 010

1.875 – 2.5 V 011

2.5 – 3.125 V 100

3.125 – 3.75 V 101

3.75 – 4.375 V 110

4.375 – 5 V 111

Digital value = Analog value × #values/full range

Analog value = Digital value × full range/#values

Page 7: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

- PIC16F877A: 10-bit ADC 210 =1024: from 0 to 1023

- PIC16F877A has 4 Specific Function 8-bit Registers for ADC module: 1. ADRESH 2. ADRESL 3. ADCON04. ADCON1

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

ADRESH A2D Result Register - High Byte

ADRESL A2D Result Register - Low Byte

ADCON0 ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE - ADON

ADCON1 ADFM - - - PCFG3 PCFG2 PCFG1 PCFG0

Page 8: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

1. ADRESH & 2. ADRESL Registers- PIC: an 8-bit microcontroller- ADC result = 10 bits

ADC result needs 2 registers ADRESH & ADRESL.

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

ADRESH A2D Result Register - High Byte

ADRESL A2D Result Register - Low Byte

ADCON0 ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE - ADON

ADCON1 ADFM - - - PCFG3 PCFG2 PCFG1 PCFG0

Page 9: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

3. ADCON0 & 4. ADCON1 Registers:Used to configure the ADC module

ADCON1:

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

ADCON1 ADFM - - - PCFG3 PCFG2 PCFG1 PCFG0

Bit 7: ADFM: Analog to Digital Format of the 10-bit result in ADRESH and ADRESL

Bit 7=0 Left justified result Bit 7=1 Right justified result

ADRESH ADRESL

10-bit result

ADRESH ADRESL

10-bit result

Page 10: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

ADCON1:

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

ADCON1 ADFM - - - PCFG3 PCFG2 PCFG1 PCFG0

Bits 6,5,4: Not used so typically = “000”Bits 3,2,1,0: PCFG3-0:PORT Configuration of ADC module

Page 11: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

Analog to Digital Converter

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

Page 12: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

3. ADCON0:

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

ADCON0 ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE - ADON

Bit 7&6: ADCS1, ADCS0: clock frequency selects

ADCS1 ADCS0 Clock frequency Max freq

00 Fosc/2 1.25 MHz

01 Fosc/8 5 MHz

10 Fosc/32 20 MHz

11 FRC internal Typ. 4us

Page 13: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

3. ADCON0:

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

ADCON0 ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE - ADON

CHS2 CHS1 CHS0 Analog Input channel

000 Channel 0 AN0/RA0

001 Channel 1 AN1/RA1

010 Channel 2 AN2/RA2

011 Channel 3 AN3/RA3

100 Channel 4 AN4/RA5

101 Channel 5 AN5/RE0

110 Channel 6 AN6/RE1

111 Channel 7 AN7/RE2

Bit 5,4,3: CHS2, CHS1, CHS0: analog input channel selects

Page 14: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

3. ADCON0:

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

ADCON0 ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE - ADON

Bit 2: GO/DONE’: Conversion statusbit 2 = 1 Conversion in progressbit 2 = 0 Conversion done

Bit 1: not used typically ‘0’

Bit 0: ADON: Activate ADC modulebit 0 = 1 ADC activatedbit 0 = 0 ADC OFF

Page 15: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

Steps for A to D Conversion in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

1. Configure analog & digital pins and Vref (ADCON1)2. Select ADC clock frequency (ADCON0)3. Turn ON ADC module (ADCON0)4. Select ADC input channel (ADCON0)5. Wait for Acquisition time (≈ 20µs) 6. Start conversion (set Go/Done bit = 1 in ADCON0)7. Wait for conversion to complete(Check on Go/Done)8. Read ADC result from ADRESH and ADRESL

Next conversion go back to step 4.

Page 16: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

Show temperature on 7-segment display using LM35 and ADC in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

Task 2Write the MikroC code to display the room temperature on 2 digit 7- segment displays connected to PORTB and PORTD of the PIC16F877A.

- The temperature is read by the sensor LM35 which inputs the analog voltage Vin to the analog channel AN0/RA0

- The output of the sensor is linearly dependent on the temperature such that each 1 degree Celsius = 10mV.

Page 17: ELCT706 MicroLab Session #3 7-segment LEDs and Analog to ... · 000 Channel 0 AN0/RA0 001 Channel 1 AN1/RA1 010 Channel 2 AN2/RA2 011 Channel 3 AN3/RA3 100 Channel 4 AN4/RA5 101 Channel

Example of inputs and outputs to the circuit If Temperature sensed by LM35 = 25°C, it will output

25 × 10mV = 250mV to Pin AN0 of PIC16F877A ADC module converts the 250mV to digital

value = 250mV × (1024/5V)= 51 Read output from ADRESH and ADRESL = 51

Calculate the equivalent voltage from the DAC output : Veq = 51 × (5V/1024)= 249mV

calculate the temperature from the ADC output: 249mV/10mV=24.9Send each digit of temperature to one 7 segment display

Steps for A to D Conversion in PIC16F877A

ELCT 708 Session #3Dr. Mohamed Abdel GhanyEng. Salma Hesham

LM35 sensor