need help getting started with a pic16f84a

Thread Starter

optionfa

Joined Nov 2, 2009
15
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..
Rich (BB code):
;==============================================================================
; P r o c e s s o r
;==============================================================================
    list p=16f84a
    include p16f84a.inc

;==============================================================================
; C o n s t a n t s
;==============================================================================
    phaseA equ 2        ;RB2
    phaseB equ 1        ;RB1
    phaseC equ 0        ;RB0
    outPort equ PORTB

;==============================================================================
; D a t a   M e m o r y
;==============================================================================
    cblock 0x0C
        outPortBuffer
        valueA
        valueB
        valueC
        timeA
        timeB
        timeC
        stateVar
    endc

;==============================================================================
; M A C R O S
;==============================================================================

BANK0 macro
    bcf STATUS, RP0
    endm

BANK1 macro
    bsf STATUS, RP0
    endm

;==============================================================================
; R e s e t   V e c t o r
;==============================================================================
    org 0x0000
    call Initialize
    goto Main

;==============================================================================
; I n t e r r u p t   V e c t o r
;==============================================================================
    org 0x0004          ;Do not have to save context because of implementation

    ;Interrupt code

    clrf outPortBuffer

    incf timeA, f       ;Advance time
    movf timeA ,w
    addlw D'21'
    movwf timeB
    addlw D'22'
    movwf timeC

    call PWM            ;Update

    btfsc stateVar, 0   ;Phase rotation is rounded depending upon state
        goto StateB
    btfsc stateVar, 1
        goto StateC
StateA
    bsf stateVar, 0
    goto EndState
StateB
    incf timeB, f
    bsf stateVar, 1
    goto EndState
StateC
    incf timeB, f
    incf timeC, f
    clrf stateVar
EndState

    call PWM            ;Update

    movlw D'65'         ;Ensure between 0 and 64
    subwf timeA, f
    btfss STATUS, C
        addwf timeA, f
    subwf timeB, f
    btfss STATUS, C
        addwf timeB, f
    subwf timeC, f
    btfss STATUS, C
        addwf timeC, f

    call PWM            ;Update

    movf timeA, w       ;Get sinewave value, phase A
    call SineWave
    movwf valueA
    movf timeB, w       ;Get sinewave value, phase B
    call SineWave
    movwf valueB
    movf timeC, w       ;Get sinewave value, phase C
    call SineWave
    movwf valueC
    bcf INTCON, T0IF    ;Reset interrupt flag

    retfie

;==============================================================================
; M A I N
;==============================================================================
Main
    bcf INTCON, GIE                 ;Disable interrupts
    call PWM
    bsf INTCON, GIE                 ;Enable interrupts
    goto Main

;------------------------------------------------------------------------------
Initialize
    ;Set up output port
    BANK1
    movlw ~(1 << phaseA | 1 << phaseB | 1 << phaseC)
    movwf outPort

    ;Set up TIMER0
    movlw B'10001000'
    movwf OPTION_REG
    BANK0

    ;Set up for interrupt routine
    clrf stateVar
    clrf timeA

    ;Set up values
    movlw D'127'
    movwf valueA
    movlw D'241'
    movwf valueB
    movlw D'19'
    movwf valueC

    ;Set up timer interrupt
    movlw B'10100000'
    movwf INTCON

    return

;------------------------------------------------------------------------------
PWM
    movf TMR0, w                    ;Get time
    subwf valueA, f                 ;Calculate for Phase A
    btfss STATUS, C
        bsf outPortBuffer, phaseA
    addwf valueA, f                 ;Restore ValueA

    subwf valueB, f                 ;Calculate for Phase B
    btfss STATUS, C
        bsf outPortBuffer, phaseB
    addwf valueB, f                 ;Restore ValueB

    subwf valueC, w                 ;Calculate for Phase C
    btfss STATUS, C
        bsf outPortBuffer, phaseC
    movf outPortBuffer, w
    movwf outPort
    return

;------------------------------------------------------------------------------
SineWave
    if (high TableStart)
        movlw high TableStart
        movwf PCLATH
    endif
    if (high TableStart) == (high TableEnd)
        addwf PCL, f
    else
        messg "Table crosses page boundary"
        addwf PCL, w
        btfss STATUS, C
            incf PCLATH, f
        movwf PCL
    endif
TableStart
    retlw D'127'
    retlw D'139'
    retlw D'151'
    retlw D'163'
    retlw D'175'
    retlw D'186'
    retlw D'197'
    retlw D'207'
    retlw D'216'
    retlw D'224'
    retlw D'232'
    retlw D'238'
    retlw D'243'
    retlw D'248'
    retlw D'251'
    retlw D'253'
    retlw D'254'
    retlw D'254'
    retlw D'252'
    retlw D'250'
    retlw D'246'
    retlw D'241'
    retlw D'235'
    retlw D'228'
    retlw D'220'
    retlw D'211'
    retlw D'202'
    retlw D'191'
    retlw D'181'
    retlw D'169'
    retlw D'157'
    retlw D'145'
    retlw D'133'
    retlw D'121'
    retlw D'109'
    retlw D'97'
    retlw D'85'
    retlw D'73'
    retlw D'63'
    retlw D'52'
    retlw D'43'
    retlw D'34'
    retlw D'26'
    retlw D'19'
    retlw D'13'
    retlw D'8'
    retlw D'4'
    retlw D'2'
    retlw D'0'
    retlw D'0'
    retlw D'1'
    retlw D'3'
    retlw D'6'
    retlw D'11'
    retlw D'16'
    retlw D'22'
    retlw D'30'
    retlw D'38'
    retlw D'47'
    retlw D'57'
    retlw D'68'
    retlw D'79'
    retlw D'91'
    retlw D'103'
TableEnd
    retlw D'115'

;==============================================================================
; I N C L U D E S
;==============================================================================

    end
 

Thread Starter

optionfa

Joined Nov 2, 2009
15
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?
 

BMorse

Joined Sep 26, 2009
2,675
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?

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
 

t06afre

Joined May 11, 2009
5,934
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.
 

Markd77

Joined Sep 7, 2009
2,806
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.
 

BMorse

Joined Sep 26, 2009
2,675
I'd just use the 16F84A as you can be sure it will work.

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
 

t06afre

Joined May 11, 2009
5,934
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
 

Markd77

Joined Sep 7, 2009
2,806
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.
 

Thread Starter

optionfa

Joined Nov 2, 2009
15
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.
 

Thread Starter

optionfa

Joined Nov 2, 2009
15
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?
 

THE_RB

Joined Feb 11, 2008
5,438
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.
 

Thread Starter

optionfa

Joined Nov 2, 2009
15
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 :(
 

Markd77

Joined Sep 7, 2009
2,806
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)
 

Thread Starter

optionfa

Joined Nov 2, 2009
15
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..
 

Markd77

Joined Sep 7, 2009
2,806
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.
 
Top