Chp7 pic 16 f84 interfacing - copy

1,216 views

Published on

Published in: Education, Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,216
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
110
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Chp7 pic 16 f84 interfacing - copy

  1. 1. PIC 16F84 Interfacing
  2. 2. I/O Ports <ul><li>To be used as input or output </li></ul><ul><li>Also being used for other functions such as timer, interrupt and serial communication </li></ul>
  3. 3. Port A & TRISA Registers <ul><li>BCF STATUS, RP0 </li></ul><ul><li>CLR PORTA ; Initialize PORTA by clearing </li></ul><ul><li>; output data latches </li></ul><ul><li>BSF STATUS, RP0 ; Select Bank 1 </li></ul><ul><li>MOVLW 0x0F ; Value used to initialize data </li></ul><ul><li>; direction </li></ul><ul><li>MOVWF TRISA ; Set RA<3:0> as inputs </li></ul><ul><li>; RA4 as output </li></ul><ul><li>; TRISA<7:5> are always </li></ul><ul><li>; read as ’0’. </li></ul>
  4. 4. Port A & TRISA Registers Block diagram of Pins RA3:RA0 Block diagram of Pin RA4
  5. 5. Port A & TRISA Registers Inputting (Reading) 1 from a Pin the PIC16 1 1 0 x 1 OFF 0 OFF 1 1 1 1 1 1 TRIS = 1
  6. 6. Port A & TRISA Registers Inputting (Reading) 0 from a Pin the PIC16 1 1 0 x 1 OFF 0 OFF 0 0 0 0 0 0 TRIS = 1
  7. 7. Port A & TRISA Registers Outputting (Writing) 0 to a Pin in the PIC16 0 0 1 1 1 OFF 1 ON zero 0 TRIS = 0
  8. 8. Port A & TRISA Registers Outputting (Writing) 1 to a Pin in the PIC16 0 0 1 0 0 ON 0 OFF one 1 TRIS = 0
  9. 9. Port B & TRISB Registers <ul><li>BCF STATUS, RP0 ; </li></ul><ul><li>CLRF PORTB ; Initialize PORTB by clearing </li></ul><ul><li>; output data latches </li></ul><ul><li>BSF STATUS, RP0 ; Select Bank 1 </li></ul><ul><li>MOVLW 0xCF ; Value used to initialize data </li></ul><ul><li>; direction </li></ul><ul><li>MOVWF TRISB ; Set RB<3:0> as inputs </li></ul><ul><li>; RB<5:4> as outputs </li></ul><ul><li>; RB<7:6> as inputs </li></ul>
  10. 10. Port B & TRISB Registers Block diagram of Pins RB7:RB4 Block diagram of Pin RB3:RB0
  11. 11. Interrupt <ul><li>A mechanism that allows  C responds to some events at the moment they occur, regardless of what  C is doing at that time </li></ul><ul><li>Interrupt changes the program flow and invokes interrupt subroutine (Interrupt Service Routine, ISR) </li></ul><ul><li>INTCON is control register of interrupt </li></ul>
  12. 12. Interrupt Cont…
  13. 13. Interrupt Cont… <ul><li>Interrupt sources: </li></ul><ul><ul><li>Termination of writing data to EEPROM </li></ul></ul><ul><ul><li>TMR0 interrupt caused by timer overflow </li></ul></ul><ul><ul><li>Interrupt during alteration on RB4, RB5, RB6 & RB7 of port B </li></ul></ul><ul><ul><li>External interrupt from RB0/INT pin </li></ul></ul>
  14. 14. INTCON Register INT External Interrupt Enable TMR0 Overflow Interrupt Enable EEPROM Write Complete Interrupt Enable Global Interrupt Enable RB Port Change Interrupt Enable TMR0 Overflow Interrupt Flag INT External Interrupt Flag RB Port Change Interrupt Flag
  15. 15. INTCON Register Cont… EECON1
  16. 16. Interrupt Execution
  17. 17. Example 7.1 The normal activity of program is to output binary count value on Port B. This process will be interrupted by RB0 that will be pulsed manually. The interrupt service routine causes all the outputs to be switched on, and waits for the button RA4 to be pressed. The routine then terminates by restoring the previous value in Port B data register and returns to the main program at the original point.
  18. 18. Example 7.1 cont… No Yes
  19. 19. Example 7.1 Cont… Yes No
  20. 20. Example 7.1 Cont…
  21. 21. Example 7.1 Cont…
  22. 22. Example 7.1 Cont…
  23. 23. Interrupt on Pins RB4-RB7
  24. 24. Interrupt on Pins RB4-RB7 cont…
  25. 25. Interrupt on Pins RB4-RB7 cont…
  26. 26. Interrupt on Pin RB0
  27. 27. Interrupt on Pin RB0 cont…
  28. 28. Interrupt on Pin RB0 cont…
  29. 29. Hardware Counter/Timer <ul><li>For accurate timing and counting </li></ul><ul><li>Sensor on motor shaft  pulse/resolution </li></ul><ul><li>Shaft speed  no of pulses / second </li></ul><ul><li>Timer measures interval between pulse  calculate speed </li></ul><ul><li>Process:- </li></ul><ul><li>1. Wait for pulse </li></ul><ul><li>2. Read and reset the timer </li></ul><ul><li>3. Restart the timer </li></ul><ul><li>4. Process previous timer reading </li></ul><ul><li>5. Go to 1 </li></ul>
  30. 30. Using TMR0
  31. 31. Counter Mode
  32. 32. Timer Mode <ul><li>Set OPTION register to select internal clock (bit 5 = 0) </li></ul><ul><li>Crystal oscillator, 4MHz/4  1  s  TMR0 </li></ul>
  33. 33. Example 7.2 <ul><li>Adjustable CR clock with freq. of 65536Hz </li></ul><ul><li>65536/(4x64=256)=256Hz </li></ul><ul><li>Timer counts from 0  256 & overflow every seconds </li></ul><ul><li>Need 256s to complete 8-bit binary o/p count </li></ul>Freq division prescalar
  34. 34. Example 7.3 <ul><li>Each example in the program takes four clock cycles to complete, with jumps taking eight cycles . If the program sequence is studied carefully, extra time is taken in completing the program loop before the timer is restarted . In this application, it will cause only a small error, but in other applications it may be significant. </li></ul><ul><li>Efficient solution: run timer while carrying on other process on the processor and allow time-out condition to interrupt the main program when it is finished. </li></ul>
  35. 35. Example 7.3 cont…
  36. 36. Example 7.3 cont…
  37. 37. Interrupt by Overflow on TMR0
  38. 38. Interrupt by Overflow on TMR0 cont…
  39. 39. Interrupt by Overflow on TMR0 cont…
  40. 40. Serial Communication
  41. 41. Serial Communication cont… <ul><li>Two methods of serial data communication: asynchronous & synchronous </li></ul><ul><li>Asynchronous serial communication: </li></ul>104us (9600 baud)
  42. 42. RS232 Standard <ul><li>Short distance: 15m </li></ul><ul><li>data rates: 50 to 76800 baud (bits/s) </li></ul>
  43. 43. Electrical Signal Characteristic <ul><li>Negative logic & bipolar power supplies </li></ul><ul><li>Letter ‘Q’  51h(ASCII) </li></ul>
  44. 44. Example 7.4 <ul><li>Given ‘J’ = 49h = 1001001 & ‘K’ = 4Ah = 1001010 </li></ul><ul><li>‘ J’ transmitted in 7-bit even parity : 10010011 </li></ul><ul><li>‘ J’ transmitted in 7-bit odd parity : 10010010 </li></ul><ul><li>‘ K’ transmitted in 7-bit even parity : 01010011 </li></ul><ul><li>‘ K’ transmitted in 7-bit odd parity : 01010010 </li></ul><ul><li>Baud rates: 75, 150, 300, 600, 1200, 2400, 4800 </li></ul><ul><li>Data rate: </li></ul><ul><li> baud rate/(start bit + bit of data + stop bit) </li></ul><ul><li>Example: </li></ul><ul><li>300/(1 + 8 + 1) 30 bytes per second </li></ul>
  45. 45. Voltage Level Conversion <ul><li>Two internal power supplies: </li></ul><ul><li>- V oltage doubler: raise the +5 V to +10 V </li></ul><ul><li>- Voltage inverter: convert +10 V to -10 V </li></ul>
  46. 46. Voltage Level Conversion cont… Macro for serial communication
  47. 47. Analogue to Digital Conversion <ul><li>Temperature measurement </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><ul><li>A/D reading </li></ul><ul><ul><li>0 ° C gives a reading of 0V </li></ul></ul><ul><ul><li>18 ° C gives a reading of 1.4V </li></ul></ul><ul><ul><li>25 ° C gives a reading of 2.4V </li></ul></ul><ul><ul><li>30° C gives a reading of 3.6V </li></ul></ul>
  48. 48. Analogue to Digital Conversion cont… <ul><li>0V  0 & 5V  255 </li></ul><ul><li>51 per volt @ resolution @ 1/51 V, i.e. 1 bit is 19.6 mV </li></ul><ul><li>Therefore </li></ul><ul><ul><li>0 ° C = 0.6V reading of 31 (0.6 x 51 = 30.6) </li></ul></ul><ul><ul><li>18C = 1.4v = 71 (1.4 x 51 = 71.4) </li></ul></ul><ul><ul><li>25°C = 2.4v = 122 (2.4 x 51 = 122.4) </li></ul></ul><ul><ul><li>30°C = 3.6' = 184 (3.6 x 51 = 1836) </li></ul></ul>
  49. 49. Analogue to Digital Conversion cont… 16C7X device specifications Device I/O A/D Channels Program Memory Data Memory Current Source/Sink 16C71 13 4 1k 36 25mA 16C73 22 5 4k 192 25mA 16C75 33 8 4k 192 25mA 16C710 13 4 512 36 25mA 16C711 13 4 1k 68 25mA 16C715 13 4 2k 128 25mA 16C72 22 5 2k 128 25mA 16C76 22 5 8k 368 25mA 16C77 33 8 8k 368 25mA
  50. 50. Analogue to Digital Conversion cont… Memory map of the 16C711
  51. 51. Analogue to Digital Conversion cont… ADCON0 file register
  52. 52. Analogue to Digital Conversion cont… INTCON register for 16C711
  53. 53. Analogue to Digital Conversion cont… <ul><li>BSF ADCON0,2 ;start A/D conversion </li></ul><ul><li>ADLOOP BTFSC ADCON0,2 ;has conversion finished? </li></ul><ul><li>GOTO ADLOOP ;no, so keep looking </li></ul><ul><li>.... ;yes, so exit loop </li></ul>A/D clock source Device frequency Operation ADC 1:0 20MHz 16MHz 4MHz 1MHz 333.3kHz 2Tosc 00 100ns 125ns 500ns 2.0µs 6µs 8Tosc 01 400ns 500ns 2.0µs 8.0µs 24µs 32Tosc 10 2.0µs 2.0µs 8.0µs 32µs 96µs RC 11 2-6µs 2-6µs 2-6µs 2-6µs
  54. 54. Example <ul><li>You have connected a 4 MHz oscillator to the PIC, and set the A/D clock cycle to Fosc/2. The frequency for the A/D clock is therefore 2 MHz (=4/2), and one cycle lasts 0.5 µs. </li></ul><ul><li>Note: 0.5 µs is too little time, something between </li></ul><ul><li> 2 µs and 8 µs is recommended. </li></ul>
  55. 55. Example cont… Possible combination of ADCON1 Bit 1 Bit 0 RA0 RA1 RA2 RA3 Ref 0 0 A A A A VDD 0 1 A A A Vref RA3 1 0 A A D D VDD 1 1 D D D D VDD
  56. 56. Example 7.5 <ul><li>The following figure shows the circuit for a water temperature alarm system. The operations of the system are as follow: </li></ul><ul><ul><li>If temperature is lower than 18  C, then the water is too cold, so turn on the L1 LED (others off). </li></ul></ul><ul><ul><li>If temperature is higher than 23  C, then the water is too hot, so turn on the L3 LED (others off). </li></ul></ul><ul><ul><li>If temperature is in between 18  C and 23  C, then the water temperature is OK, so turn on the L2 LED (others off). </li></ul></ul>
  57. 57. Example 7.5 cont…
  58. 58. Example 7.5 cont… Main loop Interrupt Service Routine
  59. 59. Example 7.5 cont…
  60. 60. Example 7.5 cont…
  61. 61. Example 7.5 cont…

×