Help to program PIC16F88

Discussion in 'Programmer's Corner' started by Kai-Itza, Aug 3, 2010.

  1. Kai-Itza

    Thread Starter New Member

    Aug 3, 2010
    1
    0
    Hey guys :)


    I'm in a bit of a muddle with a little home project of mine and am wondering if any of you want to help.
    I'm building a very simple light sensing circuit using a 1M ohm LDR (with the 1M value being the max value when in the dark), as it's getting dark the program (written in Assembler) will monitor and test the incoming ADC values until it reaches ~60% of the LDR's resistance value through the voltage that it carries, once it reaches this threshold the program would then activate a simple LED to symbolise this. Easy right? Yeah, that's what I thought until I found out that I can't get it to work :(.
    This is but the first stage of a much larger project, of which I want to build up in 'building blocks', this project would eventually grow to that which looks after plantlife within my conservatory so I don't have to.
    I'm relatively new to programming in general and I thought that Assembler was a good place to start, I know all the basics as I've been learning this through the Version 3 PICmicro® microcontroller development board from Matrix Multimedia, it's a little old I know being as it is a evaluation board that was released way back in 2003 and it still has usage in it yet. So I'll be developing this project off of this for the time being.

    I'm using the PIC:16F88 for this project.

    And the following is the Program that I've been working on for the last 2 weeks:

    Code ( (Unknown Language)):
    1. ;PROGRAMMING OPTIONS************************************************
    2. ;
    3. ; When programming the PIC 16f88 using the Assembler (ASM:IDE), the following options are selected:
    4. ;
    5. ; Oscillator selection; EXTRC as clock-out
    6. ;Watchdog timer - disabled
    7. ; Power up timer - disabled
    8. ; Pin function select; MCLR
    9. ; Brown-out protection - enabled
    10. ; Code Proection - diabled
    11. ;Low Voltage program - enabled
    12. ; data EE read protect - disabled
    13. ;Flash program write enable: Write protect - disabled
    14. ;Background debug - disabled
    15. ;CCP1 Mux pin; RB0
    16. ;code proect - disabled
    17. ;fail-safe clock monitor - enabled
    18. ;internal/eternal switch over mode - enabled
    19. ;
    20. ;I/O ALLOCATION*****************************************************
    21. ;
    22. ;INPUTS...........................................................
    23. ;
    24. ;Analogue sensors (input) AN4-AN6(VDD = 5.0V) ; There is only going to be one temperature sensor to be used through one of these pins, the reason why three have been selected is for other sensors to be added after the temperature sensor has been tried and tested successfully
    25. ;Digital I/O   RA0-RA3  
    26. ;
    27. ;OUPUTS...........................................................
    28. ;
    29. ; LED
    30. ;
    31. ;PORT DATA DIRECTION CODES REQUIRED...............................;
    32. ;
    33. ;
    34. ;ADC SETUP********************************************************
    35. ;
    36. ;ANSEL bit 7 0= unimplimated
    37. ; bits 6543 1= analog input
    38. ; bits  210 0= digital I/O
    39. ;
    40. ;ANSEL = 01111000
    41. ;
    42. ;ADCON1 bit 7 1= right justify result in ADESH/ADRESL
    43. ; bit 6 0= disabled (not needed)
    44. ; bits 54 10== VREF-VSS
    45. ; bits 3210 0= unimplemented
    46. ;
    47. ;ADCON1 = 1010000
    48. ;
    49. ;ADCON0  bits 76 0= A/D clock = f/8
    50. ; bits  543 channel select (AN0-AN7)
    51. ; bit 2 GO=1/DONE=0
    52. ; bit 0 A/D Module enable = 1
    53. ;
    54. ;ADCON0 = 01111001
    55. ;
    56. ; Create  list file and select processor :
    57.  list p = 16f88
    58. ;
    59. #DEFINE PAGE0 BCF STATUS,5
    60. #DEFINE PAGE1 BSF STATUS,5
    61. ;
    62. ; Include file containing register labels:
    63.  Include "p16f88.inc"
    64. ;
    65. STATUS EQU H'03' ; STATUS register
    66. count  EQU 020  ; assign General Purpose Register, Bank 0 for counter
    67. z EQU 2  ;Zero flag
    68. ;
    69. ;set origin at address 000
    70. org 0x000
    71.  
    72.  ORG 0  ; Reset vector
    73.  GOTO 5  ; Goto start of program
    74.  ORG 4  ; Interrupt vector
    75.  GOTO 5  ; Goto start of program
    76.  ORG 5  ; Start of program memory
    77.  ; set up code for 16F88 ports
    78.  BSF 3,5 ;switch to page 1
    79.  CLRF H'9b' ;clear ansel file - set ports to digital io
    80.  BCF  3,5 ;switch to page 0
    81. ;
    82. ;SETUP STATUS REGISTER............................................
    83. ;
    84. ;
    85. ;SETUP ADC........................................................
    86. ;
    87.  banksel ANSEL  ;select register bank
    88.  movlw b'01111000'
    89.  movwf ANSEL  ;write A/D control word
    90. ;
    91.  banksel ADCON1  ;select register bank
    92.  movlw b'10100000'
    93.  movwf ADCON1  ;write A/D control word
    94. ;
    95.  banksel ADCON0  ;select register bank
    96.  movlw b'01111001'
    97.  movwf ADCON0  ;write A/D control word
    98. ;
    99. ;SUBROUTINE TO WAIT ABOUT 0.8MS..................................
    100. ;
    101. delay movlw 256  ;load time delay of 256x3=768
    102.  movwf count  ;load counter down decfsz count  ;decrement and test counter
    103.  goto  count
    104. ;
    105. ;SUBROUTINE TO GET ANALOGU INPUT - 20US SETTLING TIME.............
    106. ;
    107. A2D movlw 007  ;time delay of 7x3=21us
    108.  movwf count  ;load counter
    109. again decfsz count  ;decrement until zero
    110.  goto  again  ;decrement until zero
    111. ;
    112. ;GET ANALOGUE INPUT..............................................
    113. ;
    114.  movlw b'01000001'
    115.  movwf ADCON0
    116. adc bsf ADCON0, GO ;start a2d conversion
    117. wait btfsc ADCON0, GO ;wait for conversion to complete
    118.  goto  wait
    119.  movf ADRESL, h'08' ;place in register location for testing
    120.  movf ADRESH, h'09' ;place in register loaction for testing
    121. ;
    122.  movlw H'66'  ;place the total of 614-bits
    123.  movwf h'35'  ;in two empty locations of memory bank 0
    124.  movlw H'02'  ;these are to test the thermistor component
    125.  movwf H'36'  ;to see if it is >60% of it's original value
    126. ;
    127.  movf H'35', w  
    128.  subwf H'08', H'08' ;subtract loaded ADC value from 102-bytes and place into memory
    129.  movf H'36', w
    130.  subwf H'09', H'09' ;subtract loaded ADC value from 2-bytes and palce into memory
    131. ;
    132.  btfsc h'08', z ;and test ADRESL...
    133.  goto  part2
    134.  movlw H'0F'  ;...if the thermistor is >60% of it's original value, place the value 0F...
    135.  movwf H'4B'  ;...into an empty register
    136. ;
    137. part2 btfsc H'09', z ;and then test ADRESH...
    138.  goto adc
    139.  movlw H'0F'  ;...if the thermistor is >60% of it's original value, place the value 0F...
    140.  movwf H'4A'  ;...into an empty register
    141. ;
    142. ;TEST TO SEE IF BOTH ADRESL AND ADRESH ARE BOTH POSITIVE.................
    143. ;
    144.  movf H'4A', w ;load the value of ADRESH into the working register
    145.  ADDWF H'4B', H'4A' ;add both ADRESH's and ADRESL's positive 'flags' (the values of 0F for both results), which would make the value of H'1E'
    146.  btfss H'4A', H'1E' ;...and test to see if this result is positive
    147.  goto adc
    148.  goto ledop  ;if it is; turn on LED RB0
    149. ;
    150. ;
    151. ;
    152. ;LED OUTPUT IF THERMISTOR READS 61% OF ITS MAXIMUM VALUE..........
    153. ;
    154. ledop  CLRF 6  ; set all Port B pins to logic 0
    155.   BSF 3,5  ; instruct program that a Page 1  
    156.       ; command comes next
    157.   CLRF 6  ; set all Port B pins as outputs
    158.   BCF 3,5  ; instruct program that a Page 0  
    159.       ; command comes next
    160.   BSF 6,0  ; set Port B pin 0 to logic
    161.  goto adc  ;keep testing and provide outputs as per programme parameters
    162.  end
    I've connected this up like this, the board itself is connected to the mains supply using an adapter set to 13.5v. The ADC inputs uses a +Vref of 2.55v connected into pin 2(Vref) and from that, this is then connected to the LDR which is connected to the ADC input pin 17 (AN0). The LED is already part of the Eval. Board itself.
    References to the relevant datasheets can be found in the following:

    PIC:16F88
    Version 3 PICmicro® microcontroller development board

    And, I'm using the ASM:IDE v1 debugger for assembling/debugging/building it onto the PIC:16F88
    If there's anything that I missed let me know and I reply as best as I'll can, as I don't really know what's wrong with it for as far as I know, the programming and connections are correct. Can someone help me on this as I'm getting a bit confused over this :(.
    Thanks for your time.



    -Kai-Itza-
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Please declare variables like this:
    Code ( (Unknown Language)):
    1.     cblock 0x20
    2.     count
    3.     ledon1
    4.     ledon2
    5.     ledon3
    6.     ledon4
    7.     ledon5
    8.     ledon6
    9.     direction
    10.     position
    11.     endc
    It's so much easier to read code like:
    movf count, W
    than
    movf h'20', 0

    I think at some point you are using register 8 and 9 which don't exist. That wouldn't happen if you defined the variables.
     
Loading...