need help getting started with a pic16f84a

Discussion in 'Embedded Systems and Microcontrollers' started by optionfa, Nov 10, 2009.

  1. optionfa

    Thread Starter New Member

    Nov 2, 2009
    15
    0
    Hello all,

    I am currently working on a term project, it is a single to three phase converter, I am focusing on the control stage of the project and lets just say I am slightly overwhelmed.

    What I need to achieve in this stage is a three phase sine PWM switching signal, I have found an open source code on the internet that uses the pic16f84a MCU to generate this switching signal. I have very limited experience with MCU programming, I took one introductory class so I know some basics.

    If I purchase the following pic16f84a: http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=PIC16F84A-20/P-ND I assume I will need a development board or some way of interfacing to my compiler. If someone would be so kind and recommend me a good development board or what not for this particular application I would be grateful.

    Unfortunately this whole process needs to be very turn-key as my expertise lies more in power engineering, embedded systems is beyond me.

    Here is the code I hope to use with this MCU..
    Code ( (Unknown Language)):
    1.  
    2.  
    3. ;==============================================================================
    4. ; P r o c e s s o r
    5. ;==============================================================================
    6.     list p=16f84a
    7.     include p16f84a.inc
    8.  
    9. ;==============================================================================
    10. ; C o n s t a n t s
    11. ;==============================================================================
    12.     phaseA equ 2        ;RB2
    13.     phaseB equ 1        ;RB1
    14.     phaseC equ 0        ;RB0
    15.     outPort equ PORTB
    16.  
    17. ;==============================================================================
    18. ; D a t a   M e m o r y
    19. ;==============================================================================
    20.     cblock 0x0C
    21.         outPortBuffer
    22.         valueA
    23.         valueB
    24.         valueC
    25.         timeA
    26.         timeB
    27.         timeC
    28.         stateVar
    29.     endc
    30.  
    31. ;==============================================================================
    32. ; M A C R O S
    33. ;==============================================================================
    34.  
    35. BANK0 macro
    36.     bcf STATUS, RP0
    37.     endm
    38.  
    39. BANK1 macro
    40.     bsf STATUS, RP0
    41.     endm
    42.  
    43. ;==============================================================================
    44. ; R e s e t   V e c t o r
    45. ;==============================================================================
    46.     org 0x0000
    47.     call Initialize
    48.     goto Main
    49.  
    50. ;==============================================================================
    51. ; I n t e r r u p t   V e c t o r
    52. ;==============================================================================
    53.     org 0x0004          ;Do not have to save context because of implementation
    54.  
    55.     ;Interrupt code
    56.  
    57.     clrf outPortBuffer
    58.  
    59.     incf timeA, f       ;Advance time
    60.     movf timeA ,w
    61.     addlw D'21'
    62.     movwf timeB
    63.     addlw D'22'
    64.     movwf timeC
    65.  
    66.     call PWM            ;Update
    67.  
    68.     btfsc stateVar, 0   ;Phase rotation is rounded depending upon state
    69.         goto StateB
    70.     btfsc stateVar, 1
    71.         goto StateC
    72. StateA
    73.     bsf stateVar, 0
    74.     goto EndState
    75. StateB
    76.     incf timeB, f
    77.     bsf stateVar, 1
    78.     goto EndState
    79. StateC
    80.     incf timeB, f
    81.     incf timeC, f
    82.     clrf stateVar
    83. EndState
    84.  
    85.     call PWM            ;Update
    86.  
    87.     movlw D'65'         ;Ensure between 0 and 64
    88.     subwf timeA, f
    89.     btfss STATUS, C
    90.         addwf timeA, f
    91.     subwf timeB, f
    92.     btfss STATUS, C
    93.         addwf timeB, f
    94.     subwf timeC, f
    95.     btfss STATUS, C
    96.         addwf timeC, f
    97.  
    98.     call PWM            ;Update
    99.  
    100.     movf timeA, w       ;Get sinewave value, phase A
    101.     call SineWave
    102.     movwf valueA
    103.     movf timeB, w       ;Get sinewave value, phase B
    104.     call SineWave
    105.     movwf valueB
    106.     movf timeC, w       ;Get sinewave value, phase C
    107.     call SineWave
    108.     movwf valueC
    109.     bcf INTCON, T0IF    ;Reset interrupt flag
    110.  
    111.     retfie
    112.  
    113. ;==============================================================================
    114. ; M A I N
    115. ;==============================================================================
    116. Main
    117.     bcf INTCON, GIE                 ;Disable interrupts
    118.     call PWM
    119.     bsf INTCON, GIE                 ;Enable interrupts
    120.     goto Main
    121.  
    122. ;------------------------------------------------------------------------------
    123. Initialize
    124.     ;Set up output port
    125.     BANK1
    126.     movlw ~(1 << phaseA | 1 << phaseB | 1 << phaseC)
    127.     movwf outPort
    128.  
    129.     ;Set up TIMER0
    130.     movlw B'10001000'
    131.     movwf OPTION_REG
    132.     BANK0
    133.  
    134.     ;Set up for interrupt routine
    135.     clrf stateVar
    136.     clrf timeA
    137.  
    138.     ;Set up values
    139.     movlw D'127'
    140.     movwf valueA
    141.     movlw D'241'
    142.     movwf valueB
    143.     movlw D'19'
    144.     movwf valueC
    145.  
    146.     ;Set up timer interrupt
    147.     movlw B'10100000'
    148.     movwf INTCON
    149.  
    150.     return
    151.  
    152. ;------------------------------------------------------------------------------
    153. PWM
    154.     movf TMR0, w                    ;Get time
    155.     subwf valueA, f                 ;Calculate for Phase A
    156.     btfss STATUS, C
    157.         bsf outPortBuffer, phaseA
    158.     addwf valueA, f                 ;Restore ValueA
    159.  
    160.     subwf valueB, f                 ;Calculate for Phase B
    161.     btfss STATUS, C
    162.         bsf outPortBuffer, phaseB
    163.     addwf valueB, f                 ;Restore ValueB
    164.  
    165.     subwf valueC, w                 ;Calculate for Phase C
    166.     btfss STATUS, C
    167.         bsf outPortBuffer, phaseC
    168.     movf outPortBuffer, w
    169.     movwf outPort
    170.     return
    171.  
    172. ;------------------------------------------------------------------------------
    173. SineWave
    174.     if (high TableStart)
    175.         movlw high TableStart
    176.         movwf PCLATH
    177.     endif
    178.     if (high TableStart) == (high TableEnd)
    179.         addwf PCL, f
    180.     else
    181.         messg "Table crosses page boundary"
    182.         addwf PCL, w
    183.         btfss STATUS, C
    184.             incf PCLATH, f
    185.         movwf PCL
    186.     endif
    187. TableStart
    188.     retlw D'127'
    189.     retlw D'139'
    190.     retlw D'151'
    191.     retlw D'163'
    192.     retlw D'175'
    193.     retlw D'186'
    194.     retlw D'197'
    195.     retlw D'207'
    196.     retlw D'216'
    197.     retlw D'224'
    198.     retlw D'232'
    199.     retlw D'238'
    200.     retlw D'243'
    201.     retlw D'248'
    202.     retlw D'251'
    203.     retlw D'253'
    204.     retlw D'254'
    205.     retlw D'254'
    206.     retlw D'252'
    207.     retlw D'250'
    208.     retlw D'246'
    209.     retlw D'241'
    210.     retlw D'235'
    211.     retlw D'228'
    212.     retlw D'220'
    213.     retlw D'211'
    214.     retlw D'202'
    215.     retlw D'191'
    216.     retlw D'181'
    217.     retlw D'169'
    218.     retlw D'157'
    219.     retlw D'145'
    220.     retlw D'133'
    221.     retlw D'121'
    222.     retlw D'109'
    223.     retlw D'97'
    224.     retlw D'85'
    225.     retlw D'73'
    226.     retlw D'63'
    227.     retlw D'52'
    228.     retlw D'43'
    229.     retlw D'34'
    230.     retlw D'26'
    231.     retlw D'19'
    232.     retlw D'13'
    233.     retlw D'8'
    234.     retlw D'4'
    235.     retlw D'2'
    236.     retlw D'0'
    237.     retlw D'0'
    238.     retlw D'1'
    239.     retlw D'3'
    240.     retlw D'6'
    241.     retlw D'11'
    242.     retlw D'16'
    243.     retlw D'22'
    244.     retlw D'30'
    245.     retlw D'38'
    246.     retlw D'47'
    247.     retlw D'57'
    248.     retlw D'68'
    249.     retlw D'79'
    250.     retlw D'91'
    251.     retlw D'103'
    252. TableEnd
    253.     retlw D'115'
    254.  
    255. ;==============================================================================
    256. ; I N C L U D E S
    257. ;==============================================================================
    258.  
    259.     end
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
  3. optionfa

    Thread Starter New Member

    Nov 2, 2009
    15
    0
    Thanks for the fast reply BMorse, what is the advantage of using the F628 over the F84? The reason I chose the F84 is the code I have is written specifically for that MCU, how much would the code have to be altered to be compatible?

    Also, that Pickit 2 looks great, very reasonable price too. Does it come with the demo board, or will I have to wire the chip to a port on my breadboard?
     
  4. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    F84's are obsolete, and the F628's are the pin to pin replacements of the F84 and also the F628's have more memory and PWM output ports.....

    for F84s and F628's I would just wire them to a breadboard, only 5 wires needed, MCLR,RB6,RB7,VSS(-),and VCC(+).......

    The Pickit 2 kit does not come with any dev boards, the Pickit 2 Starter Kit does >> http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=DV164120-ND << and so does the Pickit 2 Debug Express >> http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=DV164121-ND <<

    My .02
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  6. optionfa

    Thread Starter New Member

    Nov 2, 2009
    15
    0
    What is the difference between the debugger express and the starter kit?
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Both kit use the pickit 2 programmer. The starter kit comes with a 16F690. The debug express kit use a 16F917 chip. The two kit have the some cost. The 16F917 chip will give you some simple incircuit debugging via pickit 2. The 16F690 will not without extra HW. I would have gone for debug express.
     
  8. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Although obsolete, as you already have the code and if you don't want to do any changes, I'd just use the 16F84A as you can be sure it will work. If you want to experiment with PICs, then get a more modern one too. The 84A is a little more expensive because they don't make that many any more.
    I noticed that the code above is missing a line similar to:
    __config _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC
    so you would have to put one into the file above or manually input it into the configure menu. Hopefully the required setting was with the code you posted.
    You also need to make sure you get the right frequency crystal.
     
  9. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    Pic16F84A and Pic16F628A are pin to pin compatible, only have to change inc files to reflect the new mcu....

    It was just a suggestion to the OP......

    but yes, the op can stay with F84A until he determines the circuit and code works, and once that is done and he wants to update his circuit, then he can go with the newer replacement uc to make his circuit more "up to date".
    My .02
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    If you want to stay with the 16F628 or 16F84 you can use the pickit 2 starterkit. The pic in the strater kit 2 is placed in socket. Just take it out and replace with 16F628. Perhaps the 16F629 and 16F628 is compatible also. But I have not checked
     
  11. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    If you go with the 16F628 you would also have to change the line:
    cblock 0x0C
    to
    cblock 0x20
    because the general purpose registers start at a different point.
    After changing that and the list and include lines, it compiles without warnings so would probably work OK.
     
  12. optionfa

    Thread Starter New Member

    Nov 2, 2009
    15
    0
    Thanks a lot for the very helpful input everyone, I think I will stay with the f84 for now I can always upgrade the MCU later on.

    The mentioned code revisions are noted.. I will tackle that once I can run it through the compiler, yall can probably expect a similar post with all the errors I get haha.

    cheers.
     
  13. optionfa

    Thread Starter New Member

    Nov 2, 2009
    15
    0
    Hey all,

    Got my PICKIT2 and f84a but I didn't get too far before I encountered my first problem.

    MPLAB IDE claims that the f84a processor is not compatible with pickit2, only pickit3 and several others.

    I went for the starter kit which includes the f690 which of course are compatible but the code I have is written for a f84a..

    Now I ask, how much trouble will it be to edit this code for the f690?
     
  14. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    It is MUCH easier to change it to suit the 16F628A that people have already suggested. The 16F628A is the direct replacement for the 16F84.
     
  15. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  17. optionfa

    Thread Starter New Member

    Nov 2, 2009
    15
    0
    PICKit 2.61 recognizes f84a, thanks mark. Sadly when I try and write the device I get the "no device detected" yellow message.

    At first glance the socket is a 20pin (f84a = 18 pin) and the pin outs are different (Vdd is pin 1 on the board, pin 15/16 on the f84a).

    Looks like I have to play around with it and try to get the right wiring down on my breadboard. Lets hope I didn't damage it by powering up the incorrect pins :(
     
  18. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I got this from:
    Best to check for accuracy but it looks about right. This is how you would program any part which isn't 8/14/20 pin with the starter kit and is called in circuit programming (ICP).

    http://forum.allaboutcircuits.com/showthread.php?t=30020&page=2

    in case anyone else has trouble with the pic16f84a and pickit 2 with low pin count demo board, here is how the pins match up.
    all you need are those five from pickit 2 to the chip and you're ready to go

    PICKIT 2 PINS PIC16F84A PINS
    1 (13+V) 4 (mclr)
    2 (5+v) 14 (Vdd)
    3 (GROUND) 5 (Vss)
    4 (PGD) 13 (RB7/PGD)
    5 (PGC) 12 (RB6/PGC)
     
  19. optionfa

    Thread Starter New Member

    Nov 2, 2009
    15
    0
    Thanks again Mark, rewired it on my breadboard and it seems to work well now, I can write and read to the MCU!

    Unfortunately I think I made an error by buying the 20MHz clock version of the 16f84a (got the -20 instead of the -04).. The code "assumes a pic16f84a running at 4MHz.

    How much work would it be to edit this code for 20MHz? I didn't seem to get any errors when I built the .asm in MPLAB does any of the Pickit2 software provide some sort of interface that would allow me to see the running program? Right now I am simply writing the the MCU and checking the output pins with my scope..
     
  20. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The 20MHz is just a maximum. Presumably the development board has a 20MHz crystal on it. You just need to swap that for a 4MHz crystal in your final circuit.
     
Loading...