You are on page 1of 9

ADC AND UART

I/ Introduction

- In this assignment students will learn how to build a primitive analog communication
system A/D resources embedded within the 16F887.

- Analog-to-digital converters are among the most widely used devices for data acquisition

II/ Experimental objectives:

On completion of this exercise, students should be able to:

- Become familiar with the ADC module in PIC16F887

- Recognize that the accuracy of ADC dependency resolution

- Become familiar with the Analog sensors

- Become familiar with transmitting data via Comport using UART module

III/ Equipments And Facilities

- Computer

- MPlab software using version 8.63, Protues software V7.8 or higher , Virtual Serial
Port

- PICKit2, USB B Type cable. DC Power Supply

- Workbench

- Projector

IV/ Apparatus And Material Learnning

- Lab manual: Eastern International University

- Electronic tools, electronic devices.

V/ Preparation

1. Backround

- Micro processor and digital circuit

Page 19
2. Preparation

- The software must be installed.

- Required read lab manual at home.

VI/ Content

1. ADC (Analog-to-Digital Converter)

- PIC16F887 has 14 ADC channels with 10bit resolution

Figure 3.1 Block diagram of the ADC module.

- The source of the conversion clock is software selectable via the ADCS bits of the
ADCON0 register. There are four possible clock options:

Page 20
- The 10-bit A/D conversion result can be supplied in two formats, left justified or right
justified. The ADFM bit of the ADCON0 register controls the output format.

Figure 3.2 Conversion ADC result format.

- Starting A Conversion

To enable the ADC module, the ADON bit of the ADCON0 register must be set to a
‘1’. Setting the GO/DONE bit of the ADCON0 register to a ‘1’ will start the Analog-to-
Digital conversion.

- Completion Of A Conversion

When the conversion is complete, the ADC module will:


• Clear the GO/DONE bit
• Set the ADIF flag bit
• Update the ADRESH:ADRESL registers with new conversion result

Note: After initializing the A/D at the RECEIVING 16F887 it makes sense to wait a while
before activiting the A/D. If you immediately activate the A/D the received voltages will likely
vary wildly and many messages will be printed to the terminal. I suggest putting in a loop in
your code and waiting a few milliseconds before setting the A/D into action

Page 21
- Flowchart:

Start
Start

Set bit GO/DONE


Configure Analog to start conversion
Input
No

Voltage
Reference
definition
ADC done?

Result formatting
Yes

Choosing ADC
Read ADC Value
clock source

Enable ADC
End
conversion
ADC configuration Read ADC results

Figure 3.3 ADC flowchart for programming.

2. LDR (Light-Dependent Resistor)

- The resistance of a photoresistor decreases with increasing incident light intensity, in other
words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive
detector circuits, and light- and dark-activated switching circuits.

Figure 3.4 LDR 5-10mm

- In this section, we use this sensor and ADC module to build the aplications. We can

Page 22
design the simple circuit in Fig 3.8.

3. Enhanced Universal Synchronous Asynchronous Receiver Transmitter (Eusart)

Sometimes called Serial Communications Interface (SCI)

3.1 Transmit

Figure 3.5 Transmit block diagram

3.2 Receive

Figure 3.6 Receive block diagram

The operation of the EUSART module is controlled through three registers:


• Transmit Status and Control (TXSTA)
• Receive Status and Control (RCSTA)
• Baud Rate Control (BAUDCTL). Reference at page 165 of datasheet

Page 23
3.3 Flowchart

Start Data need to


transmit TXREG

YES
Interrupt happen
Configure USART
RCIF=1

While (TXIF==0)?
Configure Baud Save data to
rate register
NO

Enable interrupt End End

UART configuration Data transmitting process Data received interrupt process

Figure 3.7 UART flowchart for programming.

4. Lab Examples Code:

Lab Circuit: The schematic below is used for Lab-3 programming

Page 24
VCC 1k
MCLR 1 U1 40 PGD RB7
RA0 2 MCLR/VPP/RE3 RB7/ICSPDAT 39 PGC RB6
RA1 3 RA0/AN0/ULPWU/C12IN0- RB6/ICSPCLK 38 RB5
RA2 4 RA1/IN1/C12IN1- RB5/AN13/T1G 37 RB4 LCD CHARACTERS 16 * 2 LINES
RA3 5 RA2/AN2/VREF-/CVREF/C2IN+ RB4/AN11 36 RB3
RA4 6 RA3/AN3/VREF+/C1IN+ RB3/AN9/PGM/C12IN2- 35 RB2
RA4/T0CKI/C1OUT RB2/AN8

10
11
12
13
14
15

16
RA5 7 34 RB1

1
2
3

4
RW 5
RS 6

7
8
9
RE0 8 RA5/AN4/SS/C2OUT RB1/AN10/C12IN3- 33 RB0 VCC
RE0/AN5 RB0/AN12/INT

D4
D5
D6
D7
RE1 9 32

E
VCC RE2 10 RE1/AN6 VDD1 31
RE2/AN7 VSS1 D7

R3 100
11 30
12 VDD RD7/P1D 29 D6
RA7
RA6
RC0
13
14
15
VSS
RA7/OSC1/CLKIN
RA6/OSC2/CLKOUT
RD6/P1C
RD5/P1B
RD4
28
27
26
D5
D4
RC7 RX
R4

20k
LCD VCC
RC1 16 RC0/T1OSO/T1CK1 RC7/RX/DT 25 RC6 TX
RC2 17 RC1/T1OSI/CCP2 RC6/TX/CK 24 RC5
RC3 SCL
18 RC2/P1A/CCP1 RC5/SDO 23 RC4 SDA
RD0 RC3/SCK/SCL RC4/SDI/SDA VCC
19 22 RS
E 20 RD0 RD3 21 RW
RD1 pic16f 887 RD2 VCC

VCC
CONTROLLER LDR1
R
VCC
J1 U2 R1 10k R12
C3 1 MCLR VCC RE1 2 1 RE2 RE0

GND
RA6 2 VOUT VS+ 10k
3 R8
22P Y1 4 PGD LM35/TO C6 R
5 PGC 4.7uF

3
C2 RA7 20MHz 6

22P CON6

PROGRAMMER SENSOR POTENTIOMETER

P1
1
6
2 J4
7
3 U3 RX 1
8 13 12 RX TX 2
4 14 R1IN R1OUT 11 TX 3
9 T1OUT T1IN
5 8 9
7 R2IN R2OUT 10 VCC TTL
C4 T2OUT T2IN
10uF
C5
+

CONNECTOR DB9 1 10uF


C1+ 2
3 V+
C1- +
C8
4 10uF
C9 C2+ 6
5 V-
+

C2-
+

MAX232
10uF

RS232
Figure 3.8: Schematic of ADC and UART

Example 1:

Write an 16F887 C program to read analog values from a variable resistor and display
on Text LCD.

#include<htc.h>
#include "lcd.h"
#include <stdio.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS &
IESODIS & FCMDIS & LVPDIS);
void main()
{
int kq;
ANSEL = ANSELH = 0;
lcd_init();
//configure ADC Module
ANS5 = 1; //Analog
VCFG0 = VCFG1 = 0; //Vref VCC - GND
ADCS0 = ADCS1 = 1; //Internal Osc using Frc
ADFM = 1; //Right Justify
CHS3 = CHS1 = 0;
CHS2 = CHS0 = 1; //Chanel 5
ADON = 1; //Enable ADC

Page 25
while(1){
GODONE = 1;
while(GODONE);

kq = ADRESH;
kq <<= 8;
kq = kq + ADRESL;
lcd_putc('\f');
lcd_gotoxy(4, 0);
printf("ADC:%d",kq);
_delay(200000);

}
}
void putch(char c)
{
lcd_putc(c);
}

Example 2:

Write an 16F887 C program to read analog values from a variable resistor and send
analog value via UART, using Terminal software to observe.

#include<htc.h>
#include "lcd.h"
#include <stdio.h>
__CONFIG(INTIO & WDTDIS & PWRTEN &
MCLREN & UNPROTECT & DUNPROTECT & BORDIS &
IESODIS & FCMDIS & LVPDIS);
void uart_tx(char data) ;
void delay_ms(int m);
void main()
{
int kq;
char c;
ANSEL = ANSELH = 0;
TXSTA = 0x20;
RCSTA = 0x90;
BRG16 = 1;
SPBRGH = 0;
SPBRG = 25; //9600 baud
c = RCREG;
RCIE = 1;
PEIE =1;
GIE = 1;

lcd_init();
//configure ADC Module
ANS5 = 1; //Analog
VCFG0 = VCFG1 = 0; //Vref VCC - GND
ADCS0 = ADCS1 = 1; //Internal Osc using Frc
ADFM = 1; //Right Justify
CHS3 = CHS1 = 0;
CHS2 = CHS0 = 1; //Chanel 5
ADON = 1; //Enable ADC

while(1){
GODONE = 1;
while(GODONE);
kq = ADRESH;

Page 26
kq <<= 8;
kq = kq + ADRESL;

uart_tx(ADRESH); // send data via uart


uart_tx(ADRESL);

lcd_putc('\f');
lcd_gotoxy(4, 0);
printf("ADC:%d",kq);
delay_ms(1000);
}
}
void putch(char c)
{
lcd_putc(c);
}
void uart_tx(char data)
{
TXREG = data;
while(TXIF==0);
}

void delay_ms(int m){


int i=0;
for(;i<m;i++){
_delay(1000);
}
}

5. Practices:

Exercise 1: Write an 16F887 C program to read analog values from a photosensor and
display on LCD

Exercise 2: Write an 16F887 C program to read the temperature values from LM35 and
display on LCD

Exercise 3: Write an 16F887 C program to read analog values from a 2 variable resistor and
send analog value via UART, using Terminal software to observe.

Page 27

You might also like