Capacitance meter using pic16f88 help please

Discussion in 'Programmer's Corner' started by Supernova, Nov 19, 2012.

  1. Supernova

    Thread Starter New Member

    Nov 19, 2012
    2
    0
    I really need help on an collage assignment that I have to do, but there are a few things that are not working right, but I do not know what is wrong with it. Can somebody just look over my code and give any advice. Thanx in advance

    sorry that there is no comments

    Code ( (Unknown Language)):
    1.  
    2.     List p=PIC16F88
    3.     #INCLUDE <P16F88.INC>
    4.  
    5.     __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
    6.        __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF
    7. ;------------------------------------------------------------------------------------------------------------------------------------------------------------  
    8. current_state        equ 0x20  
    9. cap_current_state    equ 0x25
    10. charge_time      equ 0x26
    11. WORK_REG_PORTA       equ 0x22
    12. WORK_REG_PORTB   equ 0x23
    13. ;------------------------------------------------------------------------------------------------------------------------------------------------------------
    14. digit1_state                 equ    0
    15. digit2_state                 equ    1
    16. digit3_state                 equ    2
    17. digit4_state                 equ    3
    18. cap_wait_state                      equ     0
    19. cap_reset_state              equ    1
    20. cap_test_state                      equ     2
    21. cap_tested_state             equ    3
    22.  
    23. ;------------------------------------------------------------------------------------------------------------------------------------------------------------
    24. W_TEMP                   equ  0x7D  ; w register for context saving (ACCESS)
    25. STATUS_TEMP          equ  0x7E  ; status used for context saving (ACCESS)
    26. PCLATH_TEMP          equ  0x7F  ; variable used for context saving
    27. ;------------------------------------------------------------------------------------------------------------------------------------------------------------
    28. number_thousands        equ 0x27
    29. number_hundreds     equ 0x28
    30. number_tens             equ 0x29
    31. number_units            equ 0x30
    32.    
    33. ;------------------------------------------------------------------------------------------------------------------------------------------------------------
    34. START      
    35.     ORG 0x0000
    36.        goto initialize
    37. ;------------------------------------------------------------------------------------------------------------------------------------------------------------    
    38.        ORG 0x0004
    39. ISR:
    40. ;******************************
    41.     movwf   W_TEMP            ; save off current W register contents
    42.        movf    STATUS,W          ; move status register into W register
    43.        movwf   STATUS_TEMP       ; save off contents of STATUS register
    44.        movf    PCLATH,W          ; move pclath register into W register
    45.        movwf   PCLATH_TEMP       ; save off contents of PCLATH register
    46. ;******************************
    47.  
    48.     bcf INTCON, TMR0IF
    49.        
    50.     call STATE_CAP_TEST
    51.     call NUM2DIGIT
    52.     call STATE_DIGITS
    53.  
    54.  
    55.  
    56. ;******************************
    57.     movf    PCLATH_TEMP,W     ; retrieve copy of PCLATH register
    58.        movwf   PCLATH            ; restore pre-isr PCLATH register contents
    59.        movf    STATUS_TEMP,W     ; retrieve copy of STATUS register
    60.        movwf   STATUS            ; restore pre-isr STATUS register contents
    61.        swapf   W_TEMP,F  ; restore pre-isr W register contents
    62.        swapf   W_TEMP,W  
    63. ;******************************
    64.     retfie    
    65. ;------------------------------------------------------------------------------------------------------------------------------------------------------------    
    66. initialize:
    67.    
    68.      banksel TRISB
    69.         movlw b'00000000'
    70.         movwf TRISB
    71.      movlw b'01000000'
    72.      movwf TRISA
    73.      
    74.      movlw b'00000000'
    75.      movwf OSCCON
    76.      movlw b'00000111'
    77.      movwf OPTION_REG
    78.      banksel PORTA
    79.      clrf PORTA
    80.      clrf PORTB
    81.      clrf current_state
    82.      clrf cap_current_state
    83.      banksel INTCON  
    84.      bcf INTCON,TMR0IF
    85.      bsf INTCON,TMR0IE
    86.      bsf INTCON,GIE
    87.      
    88.     init_loop:
    89.     goto init_loop
    90.          
    91. ;***********************************************************************************************************************     
    92. STATE_DIGITS:
    93.  
    94.     clrz
    95.  
    96.     movf    digit1_state,w
    97.     xorwf   current_state, w
    98.     btfsc   STATUS, Z
    99.     goto    digit1
    100.    
    101.     clrz
    102.     movf    digit2_state,w
    103.     xorwf   current_state, w
    104.     btfsc   STATUS, Z
    105.     goto    digit2
    106.    
    107.     clrz
    108.     movf    digit3_state,w
    109.     xorwf   current_state, w
    110.     btfsc   STATUS, Z
    111.     goto    digit3
    112.    
    113.     goto    digit4
    114.    
    115. ;=========================================
    116.  
    117. digit1:
    118.    
    119.     banksel PORTB
    120.     bcf PORTB, RB5
    121.     movlw   high    LED_DISP_LUT       
    122.     movwf   PCLATH
    123.     call NUM2DIGIT
    124.     movf number_thousands,w
    125.     call     LED_DISP_LUT
    126.     call  LED_OUTPUT
    127.     movf    digit2_state,w
    128.     movwf  current_state  
    129.     bsf     PORTB, RB2    
    130.     goto  state_end
    131.    
    132. digit2:
    133.     banksel PORTB
    134.     bcf PORTB, RB2
    135.     movlw   high    LED_DISP_LUT       
    136.     movwf   PCLATH
    137.     call NUM2DIGIT
    138.     movf number_hundreds,w
    139.     call LED_DISP_LUT
    140.     call LED_OUTPUT
    141.     movf      digit3_state,w
    142.     movwf      current_state  
    143.     bsf PORTB, RB3
    144.     goto state_end
    145.    
    146. digit3:
    147.     banksel PORTB
    148.     bcf PORTB, RB3
    149.     movlw   high    LED_DISP_LUT       
    150.     movwf   PCLATH
    151.     call NUM2DIGIT
    152.     movf number_tens,w
    153.     call LED_DISP_LUT
    154.     call LED_OUTPUT
    155.     movf      digit4_state,w
    156.     movwf      current_state  
    157.     bsf PORTB, RB4
    158.     goto state_end
    159.    
    160. digit4
    161.     banksel PORTB
    162.     bcf PORTB, RB4
    163.     movlw   high    LED_DISP_LUT       
    164.     movwf   PCLATH
    165.     call NUM2DIGIT
    166.     movf number_units,w
    167.     call LED_DISP_LUT
    168.     call LED_OUTPUT
    169.     movf     digit1_state,w
    170.     movwf      current_state  
    171.     bsf PORTB, RB5
    172.     goto state_end
    173.  
    174. state_end:
    175.     return
    176.      
    177. ;***********************************************************************************************************************     
    178. STATE_CAP_TEST:
    179.      
    180.       clrz
    181.         movf    cap_wait_state,w
    182.     xorwf   cap_current_state, w
    183.     btfsc   STATUS, Z
    184.     goto    wait_cap_read
    185.    
    186.     clrz
    187.     movf    cap_reset_state,w
    188.     xorwf   cap_current_state, w
    189.     btfsc   STATUS, Z
    190.     goto    cap_reset
    191.    
    192.     clrz
    193.     movf    cap_test_state,w
    194.     xorwf   cap_current_state, w
    195.     btfsc   STATUS, Z
    196.     goto    cap_test
    197.    
    198.     goto    cap_tested
    199.    
    200. ;=========================================
    201.    
    202.    
    203.    
    204.    
    205.    
    206. wait_cap_read:
    207.     banksel PORTA
    208.     bsf PORTA,RA7
    209.     nop
    210.     bcf PORTA,RA7
    211.     btfss PORTA, RA6
    212.     goto capstate_end
    213.     movf   cap_reset_state,w
    214.     movwf   cap_current_state
    215.     goto capstate_end
    216.    
    217. cap_reset:
    218.     banksel PORTA
    219.        bsf PORTA,RA7
    220.        movf    cap_test_state,w
    221.     movwf      cap_current_state
    222.     goto capstate_end
    223.    
    224. cap_test:
    225.     banksel T1CON
    226.     btfsc PORTA, RA6
    227.        goto capstate_end
    228.        bcf PORTA,RA7
    229.        bsf T1CON, TMR1ON
    230.        movf cap_tested_state,w
    231.        movwf cap_current_state
    232.        goto capstate_end
    233.    
    234. cap_tested:
    235.     banksel T1CON
    236.     btfss PORTA, RA6
    237.     goto capstate_end
    238.     bcf T1CON, TMR1ON
    239.     movf cap_wait_state,w
    240.     movwf      cap_current_state
    241.     ;clrf TMR1L
    242.     ;clrf TMR1H
    243.     goto capstate_end
    244.    
    245. capstate_end
    246.         return
    247. ;***********************************************************************************************************************    
    248.     ; Bit Number      6 10 43210
    249.     ;   PORTB,6 =>  0 00 00000 <=PORTA
    250.     ;                  ^^^ < = PORTB        
    251.    
    252. LED_OUTPUT
    253.     movwf WORK_REG_PORTA
    254.     movwf WORK_REG_PORTB
    255.     banksel PORTA
    256. ;============================= 
    257.     ; Bit Number      6 10 43210
    258.     ;  PORTB,6 =>  0 00 00000 <=PORTA
    259.     ;             ^ < = PORTB      
    260.    
    261.     btfsc WORK_REG_PORTB, 7
    262.     bsf PORTB, RB6         
    263.     movlw b'01100000'
    264.     andwf WORK_REG_PORTB,f
    265.    
    266.     rrf WORK_REG_PORTB,f
    267.     rrf WORK_REG_PORTB,f
    268.     rrf WORK_REG_PORTB,f
    269.     rrf WORK_REG_PORTB,f
    270.     rrf WORK_REG_PORTB,f
    271.    
    272.     movf WORK_REG_PORTB,w
    273.     movlw PORTB
    274. ;============================= 
    275.     ; Bit Number      6 10 43210
    276.     ;  PORTB,6 =>  0 00 00000 <=PORTA
    277.     ;                     ^ < = PORTB      
    278.    
    279.     movlw b'00011111'
    280.     andwf WORK_REG_PORTA,f
    281.     movf WORK_REG_PORTA,w
    282.     movlw PORTA
    283.  
    284.     return
    285.    
    286. ;***********************************************************************************************************************  
    287.    
    288. NUM2DIGIT:
    289.  
    290. thousands_loop
    291.     clrc
    292.     banksel TMR1L
    293.     movlw 1000
    294.     subwf TMR1L,f
    295.     btfss STATUS,C
    296.     goto thousands_loop
    297.     addwf TMR1L
    298.     movlw .1
    299.     addwf number_thousands
    300.        
    301. hundreds_loop:
    302.     clrc
    303.     movlw .100
    304.     subwf TMR1L,f
    305.     btfss STATUS,C
    306.     goto hundreds_loop
    307.     addwf TMR1L
    308.     movlw .1
    309.     addwf number_hundreds
    310.    
    311. tens_loop:
    312.     clrc
    313.     movlw .10
    314.     subwf TMR1L,f
    315.     btfss STATUS,C
    316.     goto tens_loop
    317.     addwf TMR1L
    318.     movlw .1
    319.     addwf number_hundreds
    320.  
    321. units_loop:
    322.     clrc
    323.     movf TMR1L,w
    324.     movlw number_units
    325.  
    326.     return
    327. ;***********************************************************************************************************************   
    328. LED_DISP_LUT:
    329.     ; Bit Number      6 10 43210
    330.     ;  PORTB,6 =>  0 00 00000 <=PORTA
    331.     ;                     ^ < = PORTB          
    332.     addwf   PCL, f
    333.     retlw   b'00111111' ;0        
    334.     retlw   b'00000110' ;1
    335.     retlw   b'01011011' ;2
    336.     retlw   b'01001111' ;3
    337.     retlw   b'01100110' ;4
    338.     retlw   b'01101101' ;5
    339.     retlw   b'01111101' ;6
    340.     retlw   b'00000111' ;7
    341.     retlw   b'01111111' ;8
    342.     retlw   b'01101111' ;9
    343.     retlw     b'10000000' ; .
    344.    
    345. end_loop:
    346.     goto end_loop
    347.    
    348.     end
    349.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    No comments, also no description of the issue. And no schematic either.

    <accessing mind reading and other divination techniques>

    Nope, got nothing for you.

    Break your project down to smaller pieces. You make a reading, convert that to decimal, then output the results. Try each piece in turn by forcing known values. I'd work this backwards, get the routine that outputs the results to work from a known number you hard code in. Once that works, give the reading conversion routine a whack. Finally check the measurement portion .

    If you have a PICkit you may want to try some in circuit debugging so you can at least see what number is coming back from your reading. You can also use it to force register values without rebuilding the program over and over, though that is what I frequently do when debugging & testing code.
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I agree with ErnieM, you can't just throw code out there without any sort of reference, comments, schematic, justification for doing something, etc. and expect help. We need help to be able to help you.

    How are you trying to measure capacitance? Why do you think this will work? What effort have you put it, since, as far as we know, you just copied this code from a classmate...

    You can step through your program and see everything that is happening with a pickit(with the right devices...), it is a good investment...
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,367
    Lesson #1 - Writing code is the last thing you do.

    Step #1 - Start off by defining the problem - i.e. your goals
    Step #2 - Draw a block diagram
    Step #3 - Draw a flow chart

    Do these steps before writing code and then you will have a chance that someone will help you.
     
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    A Schematic that shows which pins are connected to the ports, as well as commented code would help.

    A bigger help would be going into detail about "some problems with it", as stated many times, the more information you provide, the more assistance you'll receive.

    Combine this info with the rest of the replies above.
     
  6. Supernova

    Thread Starter New Member

    Nov 19, 2012
    2
    0
    Hello there thanx for all the helpful advice, It is my first time doing this so i didnt know where to start. And NO tshuck i did not copy it from a classmate that was my attempt at writing a code. I will try and solve my problems using all the advice I received. If I have a problem ill ask it properly. Thanx again
     
  7. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    You need to discharge the capacitor to start with.
    After it is discharged start charging it up and measure how much time it takes to reach a threshold. Have a timeout on this in case the cap doesn't charge up.
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I did not accuse you of copying it. All I said was that we don't know one way or the other, so why would we help when you could have put no effort in already?
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    I feel sorry for SuperNova's suot mechanic.

    "Something is wrong with my automobile, please fix it. " :rolleyes:
     
  10. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
Loading...