Upcoming SlideShare
×

# Chapter5 dek3133

876 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
876
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
34
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Chapter5 dek3133

1. 1. Chapter 5 PIC Interfacing
2. 2. <ul><li>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. </li></ul><ul><li>This task is performed by a block for analog to digital conversion or by an ADC. </li></ul><ul><li>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. </li></ul>Analog to digital conversion
3. 3. Analog to digital conversion (cont..) <ul><li>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: </li></ul><ul><ul><li>Sound a buzzer if the temperature drops below freezing. </li></ul></ul><ul><ul><li>Turn a heater on if the temperature is below 18  C. </li></ul></ul><ul><ul><li>Turn on a fan if the temperature goes above 25  C. </li></ul></ul><ul><ul><li>Turn on an alarm if the temperature goes above 30  C </li></ul></ul>
4. 4. Analog to digital conversion (cont..) <ul><li>Example circuit using Thermistor to measure the temperature as analogue data input. </li></ul> C X Thermistor 22k 5 V 0 V
5. 5. Analog to digital conversion (cont..) <ul><li>Making an A/D reading </li></ul><ul><ul><li>0  C gave a voltage </li></ul></ul><ul><ul><li>18  C gave a reading of 1.4V </li></ul></ul><ul><ul><li>25  C gave a reading of 2.4V </li></ul></ul><ul><ul><li>30  C gave a reading of 3.6V </li></ul></ul><ul><li>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 </li></ul>
6. 6. Analog to digital conversion (cont..) <ul><li>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. </li></ul>
7. 7. Analog to digital conversion (cont..) 16C7X device specifications
8. 8. Analog to digital conversion (cont..) <ul><li>In this lecture, we will consider the 16C711. </li></ul><ul><li>The 16C711 has four A/D inputs (channels) and can make four simultaneous analog measurement. </li></ul><ul><li>The 16C711 device need extra registers, which the 16F84 does not have, to handle the A/D processing. </li></ul>
9. 9. Analog to digital conversion (cont..) Memory map of PIC 16C711 BANK0 BANK1
10. 10. <ul><li>A/D Conversion can be a fairly length process (when compared to the instruction speed). </li></ul><ul><li>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. </li></ul><ul><li>The time an A/D conversion takes can be changed by you; the longer you let it take, the more accurate the result. </li></ul><ul><li>This setting up should be placed in SFR ADCON0 at bit 7:6. </li></ul>Analog to digital conversion (cont..)
11. 11. Analog to digital conversion (cont..) <ul><li>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). </li></ul><ul><li>Configuring the A/D device </li></ul><ul><ul><li>The header sets up PORTA as an input port with pins 17 and 18, AN0 and AN1 are set as analog inputs. </li></ul></ul><ul><ul><li>Pin 1 and 2, A2 and A3 have been set up as digital inputs and the voltage reference have been set at 5v (Vdd). </li></ul></ul><ul><ul><li>PORTB has been set as an output port. </li></ul></ul><ul><ul><li>To configure the 16C711 for A/D measurements three registers need to be set up: ADCON0, ADCON1 and ADRES. </li></ul></ul>
12. 12. Analog to digital conversion (cont..) <ul><li>ADCON0 </li></ul><ul><ul><li>Switch the A/D converter on with ADON, bit 0. This bit turns the A/D on when set and off when clear. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>Set the particular input to make the measurement from. This is done with two Channel Select bits, CHS0 and CHS1, bit 3 and 4. </li></ul></ul>
13. 13. Analog to digital conversion (cont..) ADCON0 register ADCS1 ADCS0 - CHS1 CHS0 GO/DONE - ADON Bit 7 Bit 0 1 = A/D on 2 = A/D off 1 = A/D in progress 0 = A/D finished Analog channel select 00 = channel 0, AN0 01 = channel 1, AN1 10 = channel 2, AN2 11 = channel 3, AN3
14. 14. Analog to digital conversion (cont..) <ul><li>ADCON1 </li></ul><ul><ul><li>In ADCON1, A to D Conversion register 1, only bits 0 and 1 are used. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>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 </li></ul></ul>
15. 15. Analog to digital conversion (cont..) Bit 7 Bit 0 A/D Port Configuration bits ADCON1 Register ADCON1 Port configuration - - - - - - PCFG1 PCFG0 Vdd D D D D 11 Vdd D D A A 10 A3 Vref A A A 01 Vdd A A A A 00 Ref A3 A2 A1 A0 PCFG
16. 16. <ul><li>Example polling program to wait the A/D conversion. </li></ul>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
17. 17. Analog to digital conversion (cont..) <ul><li>Flow chart of A/D conversion using Interrupt. </li></ul>
18. 18. Serial Communication <ul><li>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. </li></ul>
19. 19. Serial Communication (cont..) <ul><li>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. </li></ul><ul><li>Free line is defined as the status of logic one . Start of transmission - Start Bit, has the status of logic zero . </li></ul><ul><li>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 . </li></ul><ul><li>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. </li></ul>
20. 20. Serial Communication (cont..) Pin designations on RS232 connector (Ring Indicator) 9. RI (Clear To Send) 8. CTS (Request To Send) 7. RTS (Data Set Ready) 6. DSR (Ground) 5. GND (Data terminal Ready) 4. DTR (Transmit Data) 3. TXD (Receive Data) 2. RXD (Carrier Detect) 1. CD
21. 21. Serial Communication (cont..) <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>One chip specially designed for this task is MAX232. This chip receives signals from -10 to +10V and converts them into 0 and 5V. </li></ul>
22. 22. Serial Communication (cont..) Connecting a microcontroller to a PC via a MAX232 line interface chip Program Example
23. 23. Serial Communication (cont..) <ul><li>Using the macro for serial communication: </li></ul>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&quot; rs232byte Sending ASCII value of decimal digits of 8-bit variable Example: movlw .123 movwf TXdata rs232byte TXdata   ;Send '1', '2', '3'
24. 24. Serial Communication (cont..) <ul><li>When rs232.inc file is used, it is necessary to declare variables Rstemp and TXD at the beginning of the main program. </li></ul><ul><li>Example: </li></ul><ul><ul><li>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. </li></ul></ul>
25. 25. Pulse Width Modulation (PWM) <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
26. 26. Pulse Width Modulation (PWM) – (Cont..) Figure 1
27. 27. 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.
28. 28. 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%).
29. 29. Pulse Width Modulation (PWM) – (Cont..) b0loop MOVF duty,w MOVWF temp BSF PORTB ,0 pwma NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP DECFSZ temp GOTO pwma
30. 30. 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.
31. 31. 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
32. 32. Pulse Width Modulation (PWM) – (Cont..) DECFSZ temp GOTO pwmb GOTO rstrt END 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).
33. 33. Pulse Width Modulation (PWM) – (Cont..) - Application <ul><li>One common application of pwm signals is motor control. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
34. 34. Pulse Width Modulation (PWM) – (Cont..) - Application <ul><li>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 </li></ul><ul><li>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. </li></ul>