Saturday, September 29, 2012

Button (PIC16F690)

Action After event

Today we are going create a simple program that will enable a led after an event (button). So, we are going to need:
1. A Micro controller (i usually use PIC16F690)
2. Resistor, led, push button
3. Power 5 Vdc

For this program I created the flowchart below in order to help you understand the “flow” of the program. (it is recommended for every program a flow chart to be created)



Flow chart
So the first subroutine is the initialization. At this point we are going to configure the I/Os of our microncontroller. (more on this article: "Hello World" with micro controllers ). Then the main routine is just waiting the user to press the button. When this happen we wait a few microseconds (debounce time) and check if the button is still pressed, if its still pressed then someone (human) press it, so we have to respond by enabling the led light.
First lets take a look on the schematic:


Circuit of today's project

This is a very simple schematic, at the PORTC<0> we connected the led with a small resistor, and at the PORTA<0> we connected the push button with a pull up resistor. So when the user pushes the button the Vdd wil be applied to the microncontreller PORTA<0>, as a logic "1".

Below you can see the code i created based on the flow chart above.



  1. ........
  2. ;***** VARIABLE DEFINITIONS (examples)
  3. #define COUNTER_us 0x20
  4. #define COUNTER_ms 0x21
  5. #define COUNTER_100ms 0x22
  6. .........
  7. .........
  8. start

  9. ; remaining code goes here
  10. call Initialization
  11. main_routine
  12. btfss PORTA, 0 ;if PORTA<0> == 0 ask again
  13. goto main_routine
  14. call Delay100ms ;if PORTA<0> == 1 execute this
  15. btfss PORTA, 0 ;if PORTA<0> == 0 ask again
  16. goto main_routine
  17. enable_led
  18. bsf PORTC, 0 ;if PORTA<0> == 1 execute this
  19. goto enable_led

  20. Initialization
  21. bsf STATUS, RP0
  22. bcf STATUS, RP1 ;bank 1

  23. movlw 0xff
  24. movwf TRISA ;PORTA is input
  25. clrf TRISB ;PORTB is output
  26. clrf TRISC ;PORTC is output
  27. bcf STATUS, RP0
  28. bsf STATUS, RP1 ;bank 2
  29. clrf ANSEL
  30. clrf ANSELH ;set all I/Os as digitals

  31. bcf STATUS, RP1 ;bank0

  32. clrf PORTC
  33. clrf PORTB
  34. return
  35. goto start ; loop forever

  36. ; initialize eeprom locations
  37. ;subroutines
  38. Delay100us
  39. movlw d'31'
  40. movwf COUNTER_us
  41. delay100us
  42. decfsz COUNTER_us, 1
  43. goto delay100us
  44. nop
  45. nop
  46. return
  47. Delay1ms
  48. movlw d'9'
  49. movwf COUNTER_ms
  50. delay1ms
  51. nop
  52. nop
  53. nop
  54. nop
  55. nop
  56. nop
  57. nop
  58. call Delay100us
  59. decfsz COUNTER_ms, 1
  60. goto delay1ms
  61. nop
  62. nop
  63. nop
  64. nop
  65. nop
  66. return
  67. Delay1.5ms
  68. call Delay1ms
  69. call Delay100us
  70. call Delay100us
  71. call Delay100us
  72. call Delay100us
  73. movlw d'31'
  74. movwf COUNTER_us
  75. decfsz COUNTER_us, 1
  76. goto $-1
  77. nop
  78. nop
  79. return
  80. Delay100ms
  81. movlw d'98'
  82. movwf COUNTER_100ms
  83. delay100ms
  84. call Delay1ms
  85. decfsz COUNTER_100ms, 1
  86. goto delay100ms
  87. ;+ 700us
  88. movlw d'16'
  89. movwf COUNTER_100ms
  90. call Delay100us
  91. nop
  92. nop
  93. nop
  94. decfsz COUNTER_100ms, 1
  95. goto $-5
  96. nop
  97. nop
  98. nop
  99. nop
  100. return
  101. .......

Let me explain the code. The first part you can see (lines 3-5) is a definition of three variables which are going to be used by the delay subroutines (more on the delays you can find here http://electronsvsholes.blogspot.com/2012/09/delay.html ).
Then, as we used to, we have to use the initialization subroutine in order to set digital/analog and inputs/outputs.
The main program starts at line 12, where a loop will run forever until the input becomes high, when this happen we move to line 15 where we call a debounce delay (you could use less than 100ms) and after that small delay we check if the input is still high, if not that means that was a "false alarm" so we go back to line 12 and wait again, but if the input is still high we move to line 18-19 where the command bsf makes the output high and ...voilĂ !!! The led is ON!!!

Enabling led

You can find all the files of that article here.






No comments:

Post a Comment