Generating a square wave with a PIC12F

In summary, the programmer is trying to toggle an input pin every time the timer overflows, but is not getting a square wave. They have followed the instructions correctly, but may not be using the best method.
  • #1
Marmoteer
8
0
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:
        PROCESSOR 12F683
        __CONFIG _INTOSCIO & _WDT_OFF & _PWRTE_ON & _BOREN_OFF
        include "P12F683.inc"
       
bank0   MACRO
        bcf STATUS, RP0         ;Clear STATUS[RP0] to switch to bank 0
        endm
bank1   MACRO
        bsf STATUS, RP0         ;Set STATUS[RP0] to switch to bank 1
        endm
         
#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
 
        code
main:
        bank1
        movlw OUTPINS
        movwf TRISIO            ; Set I/O on pins
       
        bsf PIE1, TMR1IE        ; Enable the Timer1 overflow interrupt
       
        bank0
        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
       
idle:
        goto idle
       
int_vec:
        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
retf_vec:
        retfie                  ; return from interrupt
        end

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.
 
Engineering news on Phys.org
  • #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...
 
  • #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:
bank0
movlw b'00000111' ; Disables comparator functions
movwf CMCON0 

bank1
clrf ANSEL ; Disables analog stuff

This can be found in the GPIO seciton.
 

1. How does a PIC12F generate a square wave?

The PIC12F microcontroller has a built-in feature called Pulse Width Modulation (PWM) which allows it to generate a square wave. This is achieved by varying the duty cycle, or the amount of time the signal is high compared to the total period of the wave.

2. What are the benefits of using a square wave generated by a PIC12F?

Square waves have a variety of applications in electronics, such as in digital communication, motor control, and power supply regulation. The square wave generated by a PIC12F is precise and can be easily controlled through software, making it a versatile and efficient option for many projects.

3. Can the frequency of the square wave be adjusted on a PIC12F?

Yes, the frequency of the square wave can be adjusted by changing the value of the timer and the duty cycle. The timer determines the period of the wave, while the duty cycle determines the length of time the signal is high within that period. By adjusting these values, the frequency of the square wave can be modified.

4. How accurate is the square wave generated by a PIC12F?

The accuracy of the square wave depends on the internal clock of the PIC12F. The internal oscillator can have a tolerance of up to 2%, which means the frequency of the square wave can vary by this amount. However, external crystals can be used to improve the accuracy of the internal clock.

5. Can a PIC12F generate multiple square waves simultaneously?

Yes, a PIC12F can generate multiple square waves simultaneously by using different timer and duty cycle values for each wave. This allows for more complex and precise control of multiple electronic components in a circuit.

Similar threads

  • Electrical Engineering
Replies
2
Views
2K
  • Electrical Engineering
Replies
10
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • General Engineering
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
Back
Top