Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Generating a square wave with a PIC12F

  1. Jan 6, 2013 #1
    Hi, using a PIC12F683 I'm attempting to toggle an IO pin every time the internal timer overflows (via the timer interrupt) but I can't seem to get it working. I'm viewing the voltage of the pin on a scope, expecting a square wave and getting a constant 5 volts. Also I've read the official documentation multiple times and believe my code is correct (I could be way off). I'm also aware that this may not be the best way to generate a square wave. Regardless I'd at least like to realize what I'm doing wrong before I take a different approach.
    Anyhow, here's the source, it was assembled with GPASM.

    Code (Text):

            PROCESSOR 12F683
            include "P12F683.inc"
    bank0   MACRO
            bcf STATUS, RP0         ;Clear STATUS[RP0] to switch to bank 0
    bank1   MACRO
            bsf STATUS, RP0         ;Set STATUS[RP0] to switch to bank 1
    #define T1CONF  b'00000101'    
    #define OUTPINS b'00001000'     ; MCLR pin set to input and rest set to output
    #define GP2TOG  b'00000100'     ; XOR mask for toggling GP2 pin
    #define DELAY   h'FF'
    reset_vec code 0x00
            goto main
    int_svc code 0x04
            goto int_vec
            movlw OUTPINS
            movwf TRISIO            ; Set I/O on pins
            bsf PIE1, TMR1IE        ; Enable the Timer1 overflow interrupt
            bsf INTCON, PEIE        ; Enable all unmasked peripheral interrupts
            bsf INTCON, GIE         ; Enable all unmasked interrupts
            movlw T1CONF            ; Move timer configuration into W reg
            movwf T1CON             ; Configure Timer 1
            bcf PIR1, TMR1IF        ; Clear Timer interrupt flag just in case.
            clrf GPIO
            goto idle
            btfss PIR1, TMR1IF      ; Is this interrupt from the Timer?
            goto retf_vec           ; If not timer int, return
            movlw GP2TOG            ; Load GP2 xor mask
            xorwf GPIO, 1           ; Toggle GP2 bit and store result in GPIO
            bcf PIR1, TMR1IF        ; Clear interrupt
            retfie                  ; return from interrupt
    GPASM - http://ww1.microchip.com/downloads/en/devicedoc/41211d_.pdf

    Instruction set reference - http://www.dcc.unicamp.br/~celio/mc404/pic/pic_instructionset_summary.html

    Official Datasheet - http://ww1.microchip.com/downloads/en/devicedoc/41211d_.pdf

    Important pages:
    Pin Diagram - 2
    Pin IO - 31
    Timer Interrupt - 46
    Interrupt Registers - 13, 14, 15

    Thanks in advance.
  2. jcsd
  3. Jan 6, 2013 #2
    Your code looks reasonable, although that is no guarantee of correctness since I only do a little PICASM here and there. Just in case, I would move the INT enables to _after_ the timer setup -- just before your infinite loop.

    You can check your startup and logic by putting the XOR in the idle loop to see if you actually get anything on the output pin. Do you have RESET pulled up? Maybe it's never getting started...
  4. Jan 6, 2013 #3
    Man I'm dumb. The problem was just that the pins weren't set for regular IO. Should have read more closely, to fix it this is all that has to be added
    Code (Text):

    movlw b'00000111' ; Disables comparator functions
    movwf CMCON0

    clrf ANSEL ; Disables analog stuff
    This can be found in the GPIO seciton.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Generating square wave Date
How does does a DC generator work? Yesterday at 10:05 AM
Square wave generator what? Aug 19, 2011
Generating Clean 50% Duty Cycle Square Waves Aug 12, 2010
Square Wave Generator May 23, 2008
Square Wave Generator Nov 16, 2007