PIC assembler Analog

Discussion in 'Embedded Systems and Microcontrollers' started by marktyers, Jan 1, 2011.

  1. marktyers

    Thread Starter New Member

    Dec 29, 2010
    7
    0
    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.
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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.

    Code ( (Unknown Language)):
    1.  
    2.     list    p=12F675
    3.     radix    hex
    4.     title "Timelapse"
    5.         #include <p12f675.inc>
    6.     __config _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    7.  
    8.  
    9.     cblock 0x20
    10.     count
    11.     count2
    12.  
    13.     ADRESHcopy
    14.     W_TEMP
    15.     STATUS_TEMP
    16.     eetemp
    17.     GPIOtemp
    18.  
    19.     endc
    20. ;    
    21. ;----------------------------------------------------------------------
    22.  
    23.     org    0x000
    24.     goto init
    25.     org 0x004    ;interrupt goes here
    26.  
    27.     retfie
    28.      
    29. init    ;initialise stuff here
    30.     movlw B'00000111'
    31.     movwf CMCON                ;comparitors off    
    32.     BSF STATUS, RP0 ; Bank 1
    33.     movlw B'00010001'        
    34.     movwf ANSEL                ;AN0 analog and Fosc/8 for 4MHz
    35. ;    BSF STATUS, RP0 ; Bank 1    
    36.     clrf   OPTION_REG      ;  
    37.     bcf     STATUS, RP0    ; bank 0    
    38.     clrf   INTCON          ;  
    39.     CLRF GPIO                 ; Initialize GPIO by
    40.                             ; clearing output
    41.                             ; data latches
    42.     BSF STATUS, RP0         ; Select Bank 1
    43.     MOVLW B'00100001'         ; Value used to
    44.                             ; initialize data
    45.                             ; direction
    46.     MOVWF TRISIO             ; Set GP<4-1> as output, GP0 input
    47.     BCF STATUS, RP0         ;bank 0
    48.     movlw B'00000001'
    49.     movwf ADCON0            ;left justified, A/D on, AN0, ref VDD
    50.  
    51.  
    52. clrf count2
    53. main
    54.     bsf ADCON0, 1            ;start conversion
    55.  
    56.     movf ADRESH, W            ;most recent value not there yet
    57.     movwf ADRESHcopy
    58.     movf ADRESHcopy            ;test if ADRESH is zero
    59. bsf GPIO, 1                         ;assume it isn't
    60. btfss STATUS, Z
    61.  
    62.     bcf GPIO, 1
    63. m2
    64.     incfsz count2, F
    65.     goto m3
    66.     goto main
    67. m3
    68.     movf ADRESHcopy, W
    69.     subwf count2, W
    70.     btfsc STATUS,Z
    71.     bsf GPIO, 1
    72.     nop
    73.     nop
    74.     nop
    75.     goto m2
    76.      
    77.  
    78.  
    79.     goto main
    80.  
    81.  
    82.     end
     
  3. marktyers

    Thread Starter New Member

    Dec 29, 2010
    7
    0
    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)?
    Code ( (Unknown Language)):
    1. cblock 0x20
    2.         count
    3.         count2
    4.  
    5.         ADRESHcopy
    6.         W_TEMP
    7.         STATUS_TEMP
    8.         eetemp
    9.         GPIOtemp
    10.  
    11. endc
    Many thanks
    Mark.
     
  4. Markd77

    Senior Member

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

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    cblock is a way of declaring variables. You know that the GPRs in 12F675 are from 0x20. By stating
    Code ( (Unknown Language)):
    1. 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.
     
Loading...