You are on page 1of 21

PIC16 C Analog Input

Microcontrollers

1
PIC16 MCU Configuration
• Clock oscillator types
• Watchdog, power-up, brown-out timers
• Low-voltage programming
• Code protection
Microcontrollers

• In-circuit debug mode

2
Clock oscillator types
• The PIC16F87XA can be operated in four different
oscillator modes.
– LP Low-Power Crystal
– XT Crystal/Resonator
– HS High-Speed Crystal/Resonator

Microcontrollers

RC Resistor/Capacitor

(Page 145 [4]) 3


Configuration Options
• Watchdog Timer: When enabled, the watchdog timer (WDT)
automatically resets the processor after a given period (default 18
ms).
• Power-up Timer: The power-up timer (PuT) provides a nominal 72
ms delay between the power supply voltage reaching the operating
value and the start of program execution.
Microcontrollers

• Oscillator Start-up Timer: After the power-up timer has expired, a


further delay allows the clock to stabilize before program execution
begins.
• Brown-out Reset (BoR): When enabled, the brown-out detection
circuit holds the MCU in reset while the supply voltage is below a
given threshold and releases it when the supply has recovered.

4
Configuration Options
• Code Protection (CP): The chip can be configured during
programming to prevent the machine code being read back from the
chip to protect commercially valuable or secure code.
• In-Circuit Programming and Debugging: Most PIC chips now
support in-circuit programming and debugging (ICPD), which allows
the program code to be downloaded and tested in the target
Microcontrollers

hardware, under the control of the host system.


• Low-Voltage Programming Mode: The low-voltage
programming mode can be selected during programming so that the
customary high (12V) programming voltage is not needed.
• Electrically Erasable Programmable Read Only Memory: Many
PIC MCUs have a block of nonvolatile user memory where data can
be stored during power-down. These data could, for example, be the
secure code for an electronic lock or smart card reader.
5
Configuration in C
• The preprocessor directive #fuses is used to set the
configuration fuses in C programs for PICs.
#fuses XT,PUT,NOWDT,NOPROTECT,NOBROWNOUT

• The default condition for the fuses if no such directive is


Microcontrollers

included is equivalent to
#fuses RC,WDT,NOPUT,BROWNOUT,LVP,NOCPD,NOWRT
This corresponds to all the bits of configuration register
being default high.

6
Configuration in C
The options defined in the standard CCS C 16F877 header file are
• Clock Type Select LP, XT, HS, RC
• Watchdog Timer Enable WDT, NOWDT
• Power Up Timer Enable PUT, NOPUT
• Program Code Protect PROTECT, NOPROTECT
• In Circuit Debugging Enable DEBUG, NODEBUG
Microcontrollers

• Brownout Reset Enable BROWNOUT, NOBROWNOUT


• EEPROM Write Protect CPD, NOCPD
• Program Memory Write Protect WRT_50%, WRT_25%,
• (with percentage protected) WRT_5%, NOWRT

7
PIC16 MCU Peripherals
• Digital I/O
• Timers
• A/D converter
• Comparator
Microcontrollers

• Parallel slave port


• Interrupts

8
Digital I/O
The 16F877 has the following digital I/O ports available:

• Port A RA0–RA5 6 bits


• Port B RB0–RB7 8 bits
• Port C RC0–RC7 8 bits
Microcontrollers

• Port D RD0–RD7 8 bits


• Port E RE0–RE2 3 bits
Total digital I/O available 33 pins.

9
Microcontrollers

Digital I/O

10
PIC16 C Analog Input
(Page 127 [4])

• The PIC16F877 has eight analog inputs, which are


accessed via RA0, RA1, RA2, RA3, RA5, RE0, RE1,
and RE2, being renamed AN0 to AN7 in this mode.
• All these pins default to analog operation.
• But a combination of analog and digital inputs can be
Microcontrollers

selected using the system function set_up_adc_ports()


• These inputs are multiplexed into a single converter, so
they can be read only one at a time.
• The function set_ADC_channel(n) selects the input
channel. The analog-to-digital converter module has a
resolution of 10 bits.
• A 16-bit integer or floating point variable is needed to 11
receive this result.
ADC TRANSFER FUNCTION
Microcontrollers
A/D BLOCK DIAGRAM
Microcontrollers

13
CCS C Analogue Input Functions

ANALOGUE INPUTS

ADC SETUP Initialise ADC setup_adc(ADC_CLOCK_INTERNAL);

ADC PINS SETUP Initialise ADC pins setup_adc_ports(RA0_ANALOG);


Microcontrollers

ADC CHANNEL SELECT Select ADC input set_adc_channel(0);

ADC READ Read analogue input inval = read_adc();

setup_adc_ports( )
Syntax: setup_adc_ports (value)
setup_adc_ports (ports, [reference])

Parameters: value - a constant defined in the devices .h file

(Page 40 [5]) ports - is a constant specifying the ADC pins to use


Header file: 16F877A.h reference - is an optional constant specifying the ADC reference
to use
By default, the reference voltage are Vss and Vdd
Simple analogue input test

#include "16F877A.h"
#device ADC=8 //8-bit conversion
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_D0, rcv=PIN_D1) //LCD output

void main()
{
unsigned int16 vin0; // Input variable
setup_adc(ADC_CLOCK_INTERNAL); // ADC clock
Microcontrollers

setup_adc_ports(ALL_ANALOG); // Input combination


set_adc_channel(0); // Select RA0

for(;;)
{ delay_ms(500);
vin0 = read_adc(); //Get input byte
vin0 = (vin0*5/255) + 0x30; //Convert to ASCII
putc(254); putc(1); delay_ms(10); // Clear screen
printf("Input = "); putc(vin0); // Display input
}
}
Microcontrollers

16
Voltage Measurement
Microcontrollers

17
Input Voltage Measurement and Display
Microcontrollers Test Program for Voltage Measurement

18
Microcontrollers

19
Temperature controller
#include "16F877A.h"
#device ADC=8
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_D0, rcv=PIN_D1)
void main(){
float refin, numin, temp; int1 flag;
setup_adc(ADC_CLOCK_INTERNAL); setup_adc_ports(ALL_ANALOG);
for(;;) {
delay_ms(500); set_adc_channel(0); refin = read_adc();
set_adc_channel(1);numin = read_adc();
Microcontrollers

temp = (numin*50)/256;putc(254); putc(1); delay_ms(10);printf(" Temp = %3.0g ",temp);


putc(254); putc(192); delay_ms(10);
if (numin<(refin-10)) {
output_high(PIN_B1); output_low(PIN_B2);flag = 1;
}
if (flag==1) printf(" Heater ON ");
if (numin>(refin+10)) {
output_low(PIN_B1); output_high(PIN_B2); flag = 0;
}
if (flag==0) printf(" Fan ON ");
}
} 20
Temperature controller
Microcontrollers

21

You might also like