Chapter5 dek3133

  • 486 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
486
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
19
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Chapter 5 PIC Interfacing
  • 2.
    • 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
  • 3. 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 18  C.
      • Turn on a fan if the temperature goes above 25  C.
      • Turn on an alarm if the temperature goes above 30  C
  • 4. 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
  • 5. Analog to digital conversion (cont..)
    • Making an A/D reading
      • 0  C gave a voltage
      • 18  C gave a reading of 1.4V
      • 25  C gave a reading of 2.4V
      • 30  C 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
  • 6. 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.
  • 7. Analog to digital conversion (cont..) 16C7X device specifications
  • 8. 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.
  • 9. Analog to digital conversion (cont..) Memory map of PIC 16C711 BANK0 BANK1
  • 10.
    • 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..)
  • 11. 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.
  • 12. 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.
  • 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. 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
  • 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.
    • 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
  • 17. Analog to digital conversion (cont..)
    • Flow chart of A/D conversion using Interrupt.
  • 18. 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.
  • 19. 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.
  • 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. 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.
  • 22. Serial Communication (cont..) Connecting a microcontroller to a PC via a MAX232 line interface chip Program Example
  • 23. 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 TXdata rs232byte TXdata   ;Send '1', '2', '3'
  • 24. 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.
  • 25. 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.
  • 26. Pulse Width Modulation (PWM) – (Cont..) Figure 1
  • 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. 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. 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. 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. 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. 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. 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.
  • 34. 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.