PIC assembler Analog

Thread Starter

marktyers

Joined Dec 29, 2010
7
Hi
After my previous post on the forum I have made good progress but am very confused with setting up the ADC on a PIC12F675. I have found lots of example code for different processors but can't get any of it working. Does anyone have a really simple example that does not use interupts. e.g. an analog voltage on PIN5 (AN2) is measured and switches on an LED on either PIN2 (GP5) or PIN3 (GP4) depending on whether the converted value is >512 or <512.

Another problem. I am using a PICkit2 connected to pins 4,1,8,7,6. Obviously pins 1 and 8 are for power and ground but which of the other pins can I use (4,7, 6) and for what without frying my chip when I try to program it in-circuit?

All help is greatly appreciated!

Mark.
 

Markd77

Joined Sep 7, 2009
2,806
This is bad in many ways, but it reads the 8 highest bits of an analog value and outputs a (very fast) variable duty cycle on another pin. If you hook an LED + resistor you should see it dimming if you change the voltage at the analog input.

I tend to just program small PICs in a ZIF socket and then move them to the breadboard. Putting the PIC in a turned pin IC socket makes this easy. Cheap IC sockets with the flat pins bend too easily. I've had some success with LEDs and resistors on the programming pins in circuit but sometimes the programmer won't recognise the PIC.

Rich (BB code):
    list    p=12F675 
    radix    hex 
    title "Timelapse" 
        #include <p12f675.inc> 
    __config _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT 
 
 
    cblock 0x20 
    count 
    count2 
 
    ADRESHcopy 
    W_TEMP 
    STATUS_TEMP 
    eetemp 
    GPIOtemp 
  
    endc 
;     
;---------------------------------------------------------------------- 
 
    org    0x000 
    goto init 
    org 0x004    ;interrupt goes here 
 
    retfie 
     
init    ;initialise stuff here 
    movlw B'00000111' 
    movwf CMCON                ;comparitors off     
    BSF STATUS, RP0 ; Bank 1 
    movlw B'00010001'         
    movwf ANSEL                ;AN0 analog and Fosc/8 for 4MHz 
;    BSF STATUS, RP0 ; Bank 1     
    clrf   OPTION_REG      ;  
    bcf     STATUS, RP0    ; bank 0     
    clrf   INTCON          ;   
    CLRF GPIO                 ; Initialize GPIO by 
                            ; clearing output 
                            ; data latches 
    BSF STATUS, RP0         ; Select Bank 1 
    MOVLW B'00100001'         ; Value used to 
                            ; initialize data 
                            ; direction 
    MOVWF TRISIO             ; Set GP<4-1> as output, GP0 input 
    BCF STATUS, RP0         ;bank 0 
    movlw B'00000001' 
    movwf ADCON0            ;left justified, A/D on, AN0, ref VDD 
 
 
clrf count2 
main 
    bsf ADCON0, 1            ;start conversion 
 
    movf ADRESH, W            ;most recent value not there yet 
    movwf ADRESHcopy 
    movf ADRESHcopy            ;test if ADRESH is zero
bsf GPIO, 1                         ;assume it isn't
btfss STATUS, Z 
 
    bcf GPIO, 1 
m2 
    incfsz count2, F 
    goto m3 
    goto main 
m3 
    movf ADRESHcopy, W 
    subwf count2, W 
    btfsc STATUS,Z 
    bsf GPIO, 1 
    nop 
    nop 
    nop 
    goto m2 
     
  
 
    goto main 
 
 
    end
 

Thread Starter

marktyers

Joined Dec 29, 2010
7
thanks for the fast response. I have copied your code into a fresh asm file and am poring over it to ensure I understand. I have already identified a block of code I don't understand!

Can you explain what a cblock is (see the following code)?
Rich (BB code):
cblock 0x20 
        count 
        count2 
 
        ADRESHcopy 
        W_TEMP 
        STATUS_TEMP 
        eetemp 
        GPIOtemp 
  
endc
Many thanks
Mark.
 

Markd77

Joined Sep 7, 2009
2,806
It's just a way of declaring variables. It's the equivalent of
Count equ 20
Count2 equ 21
ADRESHcopy equ 22
Etc
 

Tahmid

Joined Jul 2, 2008
343
Hi,
cblock is a way of declaring variables. You know that the GPRs in 12F675 are from 0x20. By stating
Rich (BB code):
cblock 0x20
you are showing that all the variables that follow until [endc] are variables that have addresses starting from 0x20 and continuing by minute.

Count equ 20
Count2 equ 21
ADRESHcopy equ 22
W_TEMP equ 23
STATUS_TEMP equ 24
eetemp equ 25
GPIOtemp equ 26

Hope this helps.
Tahmid.
 
Top