No luck trying to program PIC10F204

Discussion in 'Embedded Systems and Microcontrollers' started by praondevou, May 20, 2016.

  1. praondevou

    Thread Starter AAC Fanatic!

    Jul 9, 2011
    2,936
    488
    Hi,

    I was trying for hours now to transfer a very simple program from a PIC10F202 (SOT23) towards a PIC10F204 (DFN). Electrical connections are ok, I double and triple checked.

    I'm using MPLAB8.92 and a PICkit2 compatible programmer and I was able to program the PIC10F202 and it works. I assume on the PIC10F204 the comparator needs to be disabled for the GP1 to be used as an output so I added that line.

    When programming the PIC it tells me everything is fine but then nothing happens on the GP1 (it's 0).

    What am I missing? (BTW, I even tried to just turn the output GP1 ON with a bsf instruction, it doesn't work, it's as if the uC is not running)

    Here is the code which I took from the awesome baseline tutorial from Gooligum, I hope it's ok to post it here, it was provided for free:

    Thanks!
    Code (Microchip Assembler):
    1.  
    2. ;************************************************************************
    3. ;                                                                    
    4. ;   Filename:      BA_L2-Flash_LED-10F200.asm                        
    5. ;   Date:          19/9/12                                            
    6. ;   File Version:  1.1                                                
    7. ;                                                                    
    8. ;   Author:        David Meiklejohn                                  
    9. ;   Company:       Gooligum Electronics                              
    10. ;                                                                    
    11. ;************************************************************************
    12. ;                                                                    
    13. ;   Architecture:  Baseline PIC                                      
    14. ;   Processor:     10F200                                            
    15. ;                                                                    
    16. ;************************************************************************
    17. ;                                                                    
    18. ;   Files required: none                                              
    19. ;                                                                    
    20. ;************************************************************************
    21. ;                                                                    
    22. ;   Description:    Lesson 2, example 1                              
    23. ;                                                                    
    24. ;   Flashes a LED at approx 1 Hz.                                    
    25. ;   LED continues to flash until power is removed.                    
    26. ;                                                                    
    27. ;************************************************************************
    28. ;                                                                    
    29. ;   Pin assignments:                                                  
    30. ;       GP1 = flashing LED                                            
    31. ;                                                                    
    32. ;************************************************************************
    33.  
    34.     list        p=10F204           [B];[/B]had these changed to 10F202 for the other PIC and it worked
    35.  
    36.     #include    <p10F204.inc>
    37.  
    38. ;***** CONFIGURATION
    39.                 ; ext reset, no code protect, no watchdog
    40.     __CONFIG    _MCLRE_ON & _CP_OFF & _WDT_OFF
    41.  
    42.  
    43. ;***** VARIABLE DEFINITIONS
    44.         UDATA
    45. sGPIO   res 1               ; shadow copy of GPIO
    46. dc1     res 1                  ; delay loop counters
    47. dc2     res 1
    48.  
    49.  
    50. ;***** RC CALIBRATION
    51. RCCAL   CODE    0x0FF       ; processor reset vector
    52.         res 1                              ; holds internal RC cal value, as a movlw k
    53.  
    54.  
    55. ;***** RESET VECTOR *****************************************************
    56. RESET   CODE    0x000       ; effective reset vector
    57.         movwf   OSCCAL          ; apply internal RC factory calibration
    58.  
    59.  
    60. ;***** MAIN PROGRAM *****************************************************
    61.  
    62. ;***** Initialisation
    63. start  
    64.        movlw   b'1101'         ; configure GP1 (only) as an output
    65.        tris    GPIO
    66.  
    67.        clrf    sGPIO              ; start with shadow GPIO zeroed
    68.  
    69. ;****************** I ADDED HERE COMPARATOR DISABLE *********************
    70.     movlw    b'11110111'        ;disable comparator
    71.     movfw    CMCON0
    72. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    73.  
    74. ;***** Main loop
    75. main_loop
    76.                                           ; toggle LED on GP1
    77.        movf    sGPIO,w         ; get shadow copy of GPIO
    78.         xorlw   b'0010'            ; toggle bit corresponding to GP1 (bit 1)
    79.        movwf   sGPIO           ;   in shadow register
    80.        movwf   GPIO             ; and write to GPIO
    81.  
    82. ; delay 500ms
    83.         movlw   .244               ; outer loop: 244 x (1023 + 1023 + 3) + 2
    84.        movwf   dc2                ;   = 499,958 cycles
    85.       clrf    dc1                     ; inner loop: 256 x 4 - 1
    86. dly1    nop                           ; inner loop 1 = 1023 cycles
    87.       decfsz  dc1,f
    88.       goto    dly1
    89. dly2    nop                          ; inner loop 2 = 1023 cycles
    90.        decfsz  dc1,f
    91.        goto    dly2
    92.       decfsz  dc2,f
    93.       goto    dly1
    94.  
    95.        goto    main_loop       ; repeat forever
    96.  
    97.      END
    Mod edit: cleaned up source and added code tags.
     
    Last edited by a moderator: May 20, 2016
  2. praondevou

    Thread Starter AAC Fanatic!

    Jul 9, 2011
    2,936
    488
    Well I should probably remove the thread but it seems I can't. I falsely used movfw CMCON0 when I should have used movwf CMCON0 !!!!
    Man, it took me hours to spot that error, can't believe it.
    Now that part is working ok.
     
  3. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    With software it takes 1% of the time to write correct software and 99% of the time to find the bugs.
     
    praondevou likes this.
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    Glad you found it.
    FWIW, movfw is not part of the PIC instruction set. It is implemented as a built-in macro and, IMHO, can lead to errors - as you found out. A better construct is the traditional
    movf reg,W ; get to W from reg
    movwf reg ; save W in reg
    Have fun.
     
    atferrari and praondevou like this.
  5. NorthGuy

    Active Member

    Jun 28, 2014
    605
    121
    You won't believe how many times this happened to me. I often miss the keys on keyboard, so such typos are not rare, and they're very difficult to spot. The compiler could've spotted this for me, but "nice" people at Microchip decided to interpret this non-existent command as movf :mad:
     
    atferrari and praondevou like this.
  6. Brevor

    Active Member

    Apr 9, 2011
    279
    54
    It's not non-existent, its a recognized "pseudo instruction" that decodes to movf,0.
    See here:
    http://picprojects.org.uk/projects/pseudoins.htm
     
    praondevou likes this.
  7. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    It shows that assembly programming is expensive: something as trivial as that can be much better and far more efficiently handled by a compiler. A human could have been freed to concentrate on things a computer / compiler cannot do, or cannot do as efficiently as a human can, like design the flow of controls, etc.
     
  8. NorthGuy

    Active Member

    Jun 28, 2014
    605
    121
    Any language will find a way to get you.
     
Loading...