14
Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5 Make an LED toggle state when button is pressed Need to read RA3, drive RA4 and RA5 Button needs to debounced or behavior will be wr

Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5 Make an LED toggle state when button is pressed Need to read RA3,

Embed Size (px)

Citation preview

Page 1: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Inputs and Outputs

PIC

Vcc

RA3

RA4

RA5

Make an LED toggle state when button is pressedNeed to read RA3, drive RA4 and RA5Button needs to debounced or behavior will be wrong

Page 2: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Debouncing Buttons

PIC

Vcc

RA3

RA3

10ms

Mechanical bounce in switch causes signal to bounce

Noticable at MHz clock rates

Need to wait until signal settles before sampling it

Page 3: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Wait to Settle

settletime is the time a button signal must stay constant to be sure that it is settledAfter a signal change, wait settletime clksDebounce rising edge, reset counter every signal change to 0

i = 0;while (i < settletime) {

if (RA3 == 0) i = 0; else i = i + 1;

}

Reset counterAdvance counter

Need to debounce falling edge as well as rising edge

Page 4: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Debouncing Toggle Code

TRISA = 0b001111;PORTA = 0b111111;while (1 == 1) { i = 0; while (i < settletime) {

if (RA3 == 0) i = 0; else i = i + 1;

} i = 0; while (i < settletime) {

if (RA3 == 1) i = 0; else i = i + 1;

} RA5 = RA5 ^ 1;}

Wait for rising edge to settle

Wait for falling edge to settle

Toggle LED

Make RA3 input, RA4, RA5 outputs

Page 5: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Analog to Digital Conversion

Built-in 10-bit analog-to-digital converterCan read analog data from several different inputsControlled via 5 special registers

ADRESH and ADRESL

•Contain results of conversion•10-bit result stored in 2 8-bit registers

ADRESH ADRESHADRESL ADRESL

Left Justified Format Right Justified Format

Page 6: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

ADCON0 Register

7 Output Format 0 = left justified, 1 = right justified

6 Voltage Reference 0 = Vref pin, 1 = Vdd

5

4:2 Analog Channel Select 000:011 = RA0:RA3100:111 = RC0:RC3

1 Start/End Indicator Set to start conversionADC sets to indicate completion

0 ADC Power 0 = off, 1 = power on

Internal or external voltage reference can be usedADC must be powered on before conversion

Page 7: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

ADCON1 Register

Conversion requires 11 conversion clocksADCON1<6:4> determine the frequency of the conversion clockMinimum conversion time is 1.6 us

000 Fosc/2

100 Fosc/4

001 Fosc/8

101 Fosc/16

010 Fosc/32

110 Fosc/64

x11 internal

ADCON1<6:4> Conv. Clk.

Page 8: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

ANSEL Register

Some I/O pins can be configured as either digital or analog inputs

• RA0:RA3, RC0:RC3

ANSEL register bits correspond to these I/O

ANSEL ith bit = 1 means that input i is an analog input

• TRIS bit for i must also be set to 1

RC3 RC2 RC1 RC0 RA3 RA2 RA1 RA0

ANSEL

Page 9: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Initializing ADC Conversion

Select analog inputsANSEL = 0b00000001; // RA0 is an analog input

Set ADC parameters and power onADCON0 = 0b 0 0 0 000 0 1;

Left justified

Vdd is reference

Convert channel 0 (RA0)

Do not start

Power on ADC

Set conversion frequencyADCON1 = 0b0 011 0000;

Internal clock, TAD = 4us

Page 10: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Performing ADC Conversion

Start conversionADCON0 = ADCON0 | 0b00000010;GODONE = 1;

Wait for conversion to finish- ADC will clear GODONE bit when finishedwhile (GODONE);

Get the result valueresult = ADRESH;

Page 11: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Comparators

Comparator – compares two analog voltages

Outputs 1 if Vin+ > Vin-

The PIC contains two comparators

Very simple ADC

Image taken from PIC16F684 Datasheet, Microchip Technology Inc.

Page 12: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Comparator Inputs/Outputs

Image taken from PIC16F684 Datasheet, Microchip Technology Inc.

Comparators receive inputs from RA0, RA1 (C1) and RC0, RC1 (C2)Comparator outputs appear in CMCON0 register, bits 6 and 7

•C1IN- is RA1

•C1IN+ is RA0

•C2IN- is RC1

•C2IN+ is RC0

C1OUT is (CMCON0 & 0b01000000) >> 6C2OUT is (CMCON0 & 0b10000000) >> 7

Page 13: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Comparator Control

CMCON0 register contains results and controls “mode”Comparator mode determines comparator inputs and on/offCIS selects comparator inputs in specific modes

Bit

Name

Function

7 C2OUT Output of C2

6 C1OUT Output of C1

5 C2INV C2INV = 1 inverts C2OUT

Invert output bits

4 C1INV C1INV = 1 inverts C1OUT

3 CIS Comparator input switch

2:0 CM2:CM0 Comparator Mode bits

Page 14: Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,

Slides created by: Professor Ian G. Harris

Comparator Modes

Only mode 111 allows RA0, RA1, RC0, RC1 to be digital I/OCMCON0 = 0b00000111;

Images taken from PIC16F684 Datasheet, Microchip Technology Inc.