+ All Categories
Home > Education > Chapter5 dek3133

Chapter5 dek3133

Date post: 22-Nov-2014
Category:
Upload: hattori-sidek
View: 869 times
Download: 0 times
Share this document with a friend
Description:
 
34
Chapter 5 PIC Interfacing
Transcript
Page 1: Chapter5 dek3133

Chapter 5

PIC Interfacing

Page 2: Chapter5 dek3133

As the peripheral signals usually are substantially different from the ones that microcontroller can understand (zero and one), they have to be converted into a pattern which can be comprehended by a microcontroller.

This task is performed by a block for analog to digital conversion or by an ADC.

This block is responsible for converting an information about some analog value to a binary number and for follow it through to a CPU block so that CPU block can further process it.

Analog to digital conversion

Page 3: Chapter5 dek3133

Analog to digital conversion (cont..)

Up to now, we have considered inputs as being digital in operation, i.e. the input is either a 0 or 1. But suppose we wish to make temperature measurement, but not hot or cold (1 or 0). We may, for example, require to:

Sound a buzzer if the temperature drops below freezing. Turn a heater on if the temperature is below 18C. Turn on a fan if the temperature goes above 25C. Turn on an alarm if the temperature goes above 30C

Page 4: Chapter5 dek3133

Analog to digital conversion (cont..)

Example circuit using Thermistor to measure the temperature as analogue data input.

C

X

Thermistor

22k

5 V

0 V

Page 5: Chapter5 dek3133

Analog to digital conversion (cont..)

Making an A/D reading 0C gave a voltage 18C gave a reading of 1.4V 25C gave a reading of 2.4V 30C gave a reading of 3.6V

The microcontroller would read these voltages and convert them to an 8-bit number where 0V is 0 and 5V is 255. i.e. a reading of 51(0x33H) per volt (1 volt) or a resolution or 1/5V, i.e. 1 bit is (5V/255) = 19.6mV

Page 6: Chapter5 dek3133

Analog to digital conversion (cont..)

Analog to digital conversion was introduced to the PIC microcontrollers with the family 16C7X devices: 16C71, 16C73 and 16C74. Table below shows some of the specifications of these devices.

Page 7: Chapter5 dek3133

Analog to digital conversion (cont..)

16C7X device specifications

Page 8: Chapter5 dek3133

Analog to digital conversion (cont..)

In this lecture, we will consider the 16C711. The 16C711 has four A/D inputs (channels) and can

make four simultaneous analog measurement. The 16C711 device need extra registers, which the

16F84 does not have, to handle the A/D processing.

Page 9: Chapter5 dek3133

Analog to digital conversion (cont..)Memory map of PIC 16C711

BANK0 BANK1

Page 10: Chapter5 dek3133

A/D Conversion can be a fairly length process (when compared to the instruction speed).

So when, the PIC begin an A/D conversion it may be a while before a result is obtained and stored in the file register ADRES.

The time an A/D conversion takes can be changed by you; the longer you let it take, the more accurate the result.

This setting up should be placed in SFR ADCON0 at bit 7:6.

Analog to digital conversion (cont..)

Page 11: Chapter5 dek3133

Analog to digital conversion (cont..)

The 16C711 has four analog inputs AN0, AN1, AN2 and AN3. But these pins are also used as A0, A1, A2 and A3/Vref (digital pins and voltage reference).

Configuring the A/D device The header sets up PORTA as an input port with pins 17 and 18,

AN0 and AN1 are set as analog inputs. Pin 1 and 2, A2 and A3 have been set up as digital inputs and

the voltage reference have been set at 5v (Vdd). PORTB has been set as an output port. To configure the 16C711 for A/D measurements three registers

need to be set up: ADCON0, ADCON1 and ADRES.

Page 12: Chapter5 dek3133

Analog to digital conversion (cont..)

ADCON0 Switch the A/D converter on with ADON, bit 0. This bit turns the

A/D on when set and off when clear. Instruct the PIC to executes a conversion by setting the

GO/DONE bit, bit 2. When the GO/DONE bit is set the PIC does an A/D conversion. When the conversion is complete the hardware clears the GO/DONE bit.

Set the particular input to make the measurement from. This is done with two Channel Select bits, CHS0 and CHS1, bit 3 and 4.

Page 13: Chapter5 dek3133

Analog to digital conversion (cont..)

- CHS1 CHS0 GO/DONE - ADON

Bit 7 Bit 0

1 = A/D on2 = A/D off

1 = A/D in progress0 = A/D finished

Analog channel select00 = channel 0, AN001 = channel 1, AN110 = channel 2, AN211 = channel 3, AN3

ADCON0 register

ADCS1

ADCS0

Page 14: Chapter5 dek3133

Analog to digital conversion (cont..)

ADCON1 In ADCON1, A to D Conversion register 1, only bits 0 and 1 are

used. They are the Port Configuration bits, PCFG0 and PCFG1, that

determine which of the pins on PORTA will be analog inputs and which will be digital.

To provide a mixture of analog and digital inputs, we have to configure the 16C711 header with ADCON1 = 10, i.e. B’00000010’, to give two analog and two digital inputs

Page 15: Chapter5 dek3133

Analog to digital conversion (cont..)

- - - - - - PCFG1 PCFG0

Bit 7 Bit 0

A/D Port Configurationbits

ADCON1 Register

PCFG A0 A1 A2 A3 Ref

00 A A A A Vdd

01 A A A Vref A3

10 A A D D Vdd

11 D D D D Vdd

ADCON1 Port configuration

Page 16: Chapter5 dek3133

Example polling program to wait the A/D conversion.

Analog to digital conversion (cont..)bsf ADCON0, ADON ;A/D converter is on

bsf ADCON0, GO ;Start A/D conversion

ADLoop btfsc ADCON0, GO ;has conversion finished?

goto ADLoop ;no, so keep looping

etc…. ;yes, so exits loop

Page 17: Chapter5 dek3133

Analog to digital conversion (cont..) Flow chart of A/D

conversion using Interrupt.

Start of program : setup

Start A/D conversion

ISR...

Reset A/D interrupt Flag

Is water too cold?

Is water too hot?

Turn ON OK LED Turn ON hot LED Turn ON cold LED

Return from ISR

YES

NO

YES

NO

Page 18: Chapter5 dek3133

Serial Communication

SCI is an abbreviation for Serial Communication Interface and, as a special subsystem, it exists on most microcontrollers. When it is not available, as is the case with PIC16F84, it can be created in software.

Page 19: Chapter5 dek3133

Serial Communication (cont..)

As with hardware communication, we use standard NRZ (Non Return to Zero) format also known as 8 (9)-N-1, or 8 or 9 data bits, without parity bit and with one stop bit.

Free line is defined as the status of logic one. Start of transmission - Start Bit, has the status of logic zero.

The data bits follow the start bit (the first bit is the low significant bit), and after the bits we place the Stop Bit of logic one.

The duration of the stop bit 'T' depends on the transmission rate and is adjusted according to the needs of the transmission. For the transmission speed of 9600 baud, T is 104 s.

Page 20: Chapter5 dek3133

Serial Communication (cont..)

1. CD (Carrier Detect)

2. RXD (Receive Data)

3. TXD (Transmit Data)

4. DTR (Data terminal Ready)

5. GND (Ground)

6. DSR (Data Set Ready)

7. RTS (Request To Send)

8. CTS (Clear To Send)

9. RI (Ring Indicator)

Pin designations on RS232 connector

Page 21: Chapter5 dek3133

Serial Communication (cont..)

In order to connect a microcontroller to a serial port on a PC computer, we need to adjust the level of the signals so communicating can take place.

The signal level on a PC is -10V for logic zero, and +10V for logic one. Since the signal level on the microcontroller is +5V for logic one, and 0V for logic zero, we need an intermediary stage that will convert the levels.

One chip specially designed for this task is MAX232. This chip receives signals from -10 to +10V and converts them into 0 and 5V.

Page 22: Chapter5 dek3133

Serial Communication (cont..)

Connecting a microcontroller to a PC via a MAX232 line interface chip

Program Example

Page 23: Chapter5 dek3133

Serial Communication (cont..) Using the macro for serial communication:

rs232init Macro for initializing the pin for transmitting data (TX-pin).  Example: RS232init

Sendw Sending ASCII value of data found in W register. Example: movlw 't'

call Sendw

rs232text Sending ASCII value of a specified text Example: rs232 “HELLO WORLD"

rs232byte Sending ASCII value of decimal digits of 8-bit variable Example: movlw .123

movwf TXdatars232byte TXdata   ;Send '1', '2', '3'

Page 24: Chapter5 dek3133

Serial Communication (cont..)

When rs232.inc file is used, it is necessary to declare variables Rstemp and TXD at the beginning of the main program.

Example: As a demonstration of a serial communication, we have an

example which sends text and the content of variable cnt. Text is sent via macro rs232text, while variable cnt is sent via macro rs232byte. This operation repeats itself after 500ms, with incrementing cnt in the process. This example can be easily modified to have button(s) for sending specified data.

Page 25: Chapter5 dek3133

Pulse Width Modulation (PWM)

A PWM signal is simply a pulse of varying length, in effect a rectangular wave. This is illustrated in Figure 1, which also shows how a servo might react to different PWM inputs.

For example circuit, the maximum voltage outputted will be +5 VDC, and the minimum will be 0 VDC. The length of the pulse generated is some times characterized by a duty cycle.

The duty cycle is the percentage of the signal that the output remains high. For instance, a constant +5V would be equivalent to a 100% duty cycle. A typical square wave output from a function generator has a 50% duty cycle. 0V would correspond to a 0% duty cycle.

Page 26: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..)

Figure 1

Page 27: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..)

list p=16f84;************************************************************* PORTB EQU 0x06 ; port b equate duty EQU 0x0c ; length of duty cycle temp EQU 0x0d ; length of duty cycle

;*************************************************************

c EQU 0 ; status bit to check after subtraction

;*************************************************************

ORG 0x000

The only equate of signifficance here is PWM. This register will be used to store the length of the PWM signal to be generated.

Page 28: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..)The next portion of code contains the actual instructions that tell the PIC what to do.

start MOVLW 0x00 ;load W with 0x00 make port B output TRIS PORTB ;copy W tristate to port B outputs MOVLW 0x00 ;fill w with zeroes MOVWF PORTB ;set port b outputs to low These lines

;set up port B as outputs. ;All outputs are then set to low.

rstrt MOVLW d'0' MOVWF PORTB

MOVLW d'157' ;Duty cycle length movwf duty

After setting up the ports, the main loop is begun. At the beginning of the main loop, all port b pins are set to low just incase they are high when they shouldn't be. The duty cycle is then set to 157 (a 50% duty cycle. 255 corresponds to 100% and 0 corresponds to 0%).

Page 29: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..)

b0loop MOVF duty,w MOVWF temp BSF PORTB,0

pwma NOP NOP NOP NOP NOP NOP NOP NOP NOP NOPNOP NOP DECFSZ temp GOTO pwma

Page 30: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..)

The next bit of code is the loop for the PWM signal generated at pin B0. The pwm1a loop generates the high portion of the PWM signal. The duty cycle is stored in temp and then the pin is set high. after a pause, temp is decremented and so long as it doesnt reach zero the pause is repeated and temp is decremented again. After temp reaches zero, the code continues.

Page 31: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..)

MOVLW d'255' MOVWF temp MOVF duty,w SUBWF temp,f BCF PORTB,0

pwmb NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP

Page 32: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..)

DECFSZ temp GOTO pwmb GOTO rstrtEND

The next portion of code generates the low part of the PWM signal. The value 255 is stored in temp, and the duty cycle is subtracted from this. This gives the remaining length of signal to be generated. Temp is then decremented in the same manner as above, this time with B0 set to low. Once the entire PWM signal has been generated, the code repeats. This code causes a PWM signal to be generated with a duty cycle proportional to the value set. The frequency of the signal can also be adjusted by varying the delay (the number of nop's used).

Page 33: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..) - Application One common application of pwm signals is motor control. By varying the duty cycle of a pwm signal sent to a motor, you can

vary the effective power of the signal and thereby slow the motor down or speed the motor up depending on how long of a pulse you send to the motor.

The signal generated by the PIC can not be directly connected to the motor, however, because the PIC is unable to handle the power required by the motor.

It is therefore necessary to use a transistor to regulate the flow of current to the motor. A transistor is like an electric switch. When you send a logic high (+5V) to the transistor, it allows current to flow.

When a logic low (0V) is sent, it restricts the flow of current. For digital signals, this means that the signal can be reproduced exactly, except the new signal is scaled up to a much larger current. Figure 2 shows a schematic for controlling a motor using a TIP31 NPN transistor.

Page 34: Chapter5 dek3133

Pulse Width Modulation (PWM) – (Cont..) - Application

As the schematic shows, the output from the pick is wired to the base. The negative terminal of the motor is then connected to the base and the collector is connected to ground. When the PWM otuput from the PIC is sent to the transistor, it will flip the transistor on and off and subsequently generate the same PWM signal to the motor, allowing you to control the motor with a PWM signal.


Recommended