Shop OBEX P1 Docs P2 Docs Learn Events
Help programming a Pic 12f675 — Parallax Forums

Help programming a Pic 12f675

W9GFOW9GFO Posts: 4,010
edited 2009-11-22 23:44 in General Discussion
I'm working on a project where I need the Pic to read a servo pulse and then output a high or low depending on what that pulse value is. I would think that it would be a very simple program.

Last night I dug out my PicKit 1 (never used it) and downloaded the MPlab IDE. I expected that with a few years worth of experience using Basic Stamps and the Propeller chip that I would be able to muddle my way through this Pic thing. Not so.

Then I spend a couple hours searching the interwebs for a good tutorial. I find lots of info but it none of it is what I am looking for. I just need a sample program (in Basic or C) so that I can understand how to put together my own code.

A while back someone offered to help with Pic programming but I can't remember who that was. I really don't want to have to join another forum for something so simple when I know that there are many here that can help. That would be like telling someone to join a semiconductor forum when they have a question about a diode.

I really wish Parallax made a single cog, 8 pin, < 1$ Propeller chip. Then I wouldn't have to turn to MicroChip.

Rich H

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.

Comments

  • Mike2545Mike2545 Posts: 433
    edited 2009-11-22 20:49
    Getting Mplab/pickit software set up is the hardest part for programming a PIC, then you have to configure the pic with some code, either C or Basic, try the MIcrochip forum for specific help on this...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike2545

    This message sent to you on 100% recycled electrons.
  • LeonLeon Posts: 7,620
    edited 2009-11-22 20:50
    I use PICs a lot (including the 12F675), and can probably help. I've always found MPLAB very easy to use. It's best to program the 12F675 in assembler, here's a little test program of mine:

    ;flasher.asm
    ;simple program for PIC12F675 to flash LED on pin 5
    ;uses Timer0 for delay
    
        list      p=12f675    ;list directive to define processor
        #include "p12f675.inc"    ;processor specific variable definitions
    
    ;-----------------------------------------------------------------
    ;defines
    ;-----------------------------------------------------------------
    
        #define LED 2    ;GP2 (pin 5)
        #define INIT_COUNT 10    ;
    
    
    ;-----------------------------------------------------------------
    ;variables
    ;-----------------------------------------------------------------
        cblock    0x20
        Shadow
        tick_counter
        endc
    
    ;------------------------------------------------------------------
    ;initialisation
    ;------------------------------------------------------------------
    
        ;reset vector
        org    0
        goto    Main
    
        ;interrupt vector
        org    4
        banksel    INTCON
        bcf        INTCON,T0IF        ;clear Timer0 interrupt flag
        movlw    INIT_COUNT        ;re-initialise count
        movwf    TMR0
        decf    tick_counter,f
        retfie
    
    Main:
        banksel ANSEL
        movlw     11h           ;AN0 as analog input,conversion clock Fosc/8
        movwf     ANSEL
        bankseL    CMCON
        movlw     07h            ;comparators off
        movwf     CMCON 
        banksel    TRISIO            
        bcf        TRISIO,LED    ;LED output GPIO5 (pin 2)
        banksel    OPTION_REG
        movlw    b'00000011'        ;prescaler 1/128
        movwf    OPTION_REG        ;
        banksel    TMR0
        movlw    INIT_COUNT        ;initialise timer count value
        clrf    tick_counter
        movwf    TMR0
        bsf        INTCON,GIE        ;enable global interrupt
        bsf        INTCON,T0IE        ;enable Timer0 interrupt
        banksel    GPIO
    
    ;-----------------------------------------------------------------------
    ;main program loop
    ;-----------------------------------------------------------------------
    
    ;loop:
    ;    bsf        Shadow,LED    ;LED off
    ;    movfw    Shadow
    ;    movwf    GPIO
    ;    call    dly
    ;    bcf        Shadow,LED    ;LED off
    ;    movfw    Shadow
    ;    movwf    GPIO
    ;    call    dly
    ;    goto    loop
    
    
    loop:
        bsf        GPIO,LED
        call    dly
        bcf        GPIO,LED
        call    dly
        goto    loop
    
    
    dly:
        movlw    0x10
        movwf    tick_counter
    dly1:
        movf    tick_counter,f
        skpz
        goto    dly1
        return
    
        end
    
    



    I can let you have the MPLAB project directory, if that helps.

    Read-Modify-Write isn't a problem with this program because of the delays, but I've included some commented-out code that uses a shadow register to avoid R-M-W effects.

    73, Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM

    Post Edited (Leon) : 11/22/2009 9:00:37 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-22 20:55
    I've used microEngineering Labs' PICBasic for this sort of thing. It's not free ($99), but it handles sort of a BS1 PBasic on steroids (melabs.com/products/pbc.htm).
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-11-22 21:47
    When I designed the MoBoStamp-pe, I needed an 8-pin micro to use as a coprocessor. I selected the Atmel Tiny13 over the PIC offerings, because they're so easy to program. (On the MoBo, it's done in-circuit with the BASIC Stamp.) Plus, without an external clock, they run at nearly 9.6 MIPS. The development tools are available for free from Atmel. There's a link to them on the MoBo product page. If you choose to use this over the PIC, I can dig out my programming software, which will allow you to program the part on a BOE.

    -Phil
  • W9GFOW9GFO Posts: 4,010
    edited 2009-11-22 21:50
    Thanks Leon, but I'm afraid I need a dumbed down version. That assembly stuff is way over my head. I was just thinking last night that with the Prop II on the horizon that I have even less reason to learn assembly.

    PICBasic looks to be a good fit for me - wish they had a trial version. Would I need to purchase a programmer as well or is there a way I can use MPLab to to get the compiled code into the chip?

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-22 22:20
    It's been a while since I used it, but I used the PICkit1 and PICkit2 to download the compiled code. I think the whole thing integrates into MPLab.
  • LeonLeon Posts: 7,620
    edited 2009-11-22 22:33
    Get a PICkit 2, it only costs $35. You need an adapter if you want to debug your code as the 12F675 doesn't have on-chip debug hardware. They are integrated into MPLAB, or you can use the PICkit 2 standalone software.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
  • Mike2545Mike2545 Posts: 433
    edited 2009-11-22 23:29
    Pic basic pro has a demo version www.melabs.com/products/pbp.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike2545

    This message sent to you on 100% recycled electrons.
  • W9GFOW9GFO Posts: 4,010
    edited 2009-11-22 23:44
    Mike2545 said...
    Pic basic pro has a demo version www.melabs.com/products/pbp.htm

    I have found Proton PicBasic Lite which seems very similar to the MeLabs PicBasic and it does support the 12f675 - the PicBasic Pro demo does not.

    I'm geting closer, Proton's PicBasis is supposed to be accessible from within MPlab, I'll see if I can get it to work soon.

    I requested some AtTiny 13a samples to see how I get along with those but for now I want to see if I can use these 12f675s.

    Here is what I have so far, it compiles fine but I have yet to put it on a chip. I would be surprised if it worked - seems like there is configurations missing, ie., the XTal freq.

    Device = 12F675
    
    Dim Pulse As Byte
    
    Symbol P0 = PORTB.0                         ' Define pulse input pin
    Symbol P1 = PORTB.1
    
    ALL_DIGITAL = True                            ' Set ports to digital mode
        
           
    Main:
    
    
        GoSub ReadPulse
        If Pulse <  150 Then 
        High P1
        Else Low P1
        EndIf
        GoTo Main      
           
    
            
    ReadPulse:  PulsIn P0, High, Pulse                                ' Read pulse width
                DelayMS 20                                   
                Return
    



    I'll replace the "GoTo" when I figure out the equivalent of a DO - LOOP.

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
Sign In or Register to comment.