PIC12F629 Comparator

Thread Starter

popcalent

Joined Mar 17, 2018
128
Hi, all.

I was going through some of the programs I wrote for a course when I was in college 20 years ago, and I got curious about something. I made an analog comparator with the PIC12F629, and I want to change some of the code and the circuit. Before someone says the PIC12F629 is obsolete, again, this is something I did 20 years ago and I just want to tweak it a little bit for fun. I have some PIC12F629 microcontrollers from back then, so I'm not going to buy a modern microcontroller for this.

This is the circuit I made. Turning the potentiometer on GP0 turns the LED on GP5 on and off depending on the reference voltage on GP1 .

circuit.png




This is the code I wrote, which works fine:
Code:
;**********************************************************************
; VARIABLES
;**********************************************************************
        CBLOCK 0x20     ;20h is the first file of the General Purpose Registers
                   VGPIO    ;Virtual GPIO
                ENDC

;**********************************************************************
; CODE STARTS
;**********************************************************************
        ORG     0       ;reset vector
        GOTO    BEGIN
;**********************************************************************
; PROGRAM BEGIN
;**********************************************************************
BEGIN
    MOVLW    ~(1<<5)        ;GP5 IS OUTPUT
    BANKSEL    TRISIO
    MOVWF    TRISIO

    ;CONFIGURE COMPARATOR
    MOVLW    0<<CINV|b'010'
                ;SELECT MODE 2 (CM=010)
                ;    +REF IS CIN+, -REF IS CIN-,
                ;    COMPARATOR ON, NO EXTERNAL OUTPUT
                ;OUTPUT NOT INVERTED (CINV=0)
                ; -> COUT=1 IF CIN+ > CIN-
    BANKSEL    CMCON
    MOVWF    CMCON

MAINLOOP
    ;DISPLAY COMPARATOR OUTPUT
    CLRF    VGPIO        ;ASSUME COUT=0 (LED OFF)
    BANKSEL    CMCON
    BTFSC    CMCON, COUT    ;IF COMPARATOR OUTPUT HIGH
    BSF    VGPIO, 5    ;TURN ON LED
    MOVF    VGPIO, W    ;COPY VIRTUAL GPIO TO GPIO
    BANKSEL    GPIO
    MOVWF    GPIO

    GOTO    MAINLOOP
    END
I'm setting CM2-0 to 010, which is the setup where the comparator's output is not connected to anything. That's why in the main loop, I read the value of COUT, I put in VGPIO, which is a memory address (0x20), and then put that in the GPIO. This works fine, but I was looking at the PIC12F629 datasheet to understand the code, and I realized that it would had been easier to just set CM2-0 to 001, which connects the output of the comparator to GP2, and then put the LED on GP2. So I wrote this new code (I highlight the lines that are different after the code):
Code:
;**********************************************************************
; VARIABLES
;**********************************************************************
        CBLOCK 0x20     ;20h is the first file of the General Purpose Registers
                VGPIO   ;Virtual GPIO
                ENDC

;**********************************************************************
; CODE STARTS
;**********************************************************************
        ORG     0       ;reset vector
        GOTO    BEGIN
;**********************************************************************
; PROGRAM BEGIN
;**********************************************************************
BEGIN
       ;INIT GPIO
        MOVLW   ~(1<<2)         ;GP2 IS OUTPUT
        BANKSEL TRISIO
        MOVWF   TRISIO

        ;CONFIGURE COMPARATOR
        MOVLW   0<<CINV|b'001'
                                ;SELECT MODE 1 (CM=001)
                                ;       +REF IS CIN+, -REF IS CIN-,
                                ;       COMPARATOR ON, EXTERNAL OUTPUT IS GP2
                                ;OUTPUT NOT INVERTED (CINV=0)
                                ; -> COUT=1 IF CIN+ > CIN-
        BANKSEL CMCON
        MOVWF   CMCON

MAINLOOP
        GOTO    MAINLOOP
        END
Differences:
1) MOVLW ~(1<<2) ;GP2 IS OUTPUT (instead of MOVLW ~(1<<5))
2) MOVLW 0<<CINV|b'001' (instead of MOVLW 0<<CINV|b'010')
3) The MAINLOOP is empty.

In this code, the main loop is empty because supposedly, the output comparator is directly connected to GP2, which is where I put the LED now. However, no matter how much I turn the potentiometer, the LED is always on. What am I doing wrong?
 
Top