HELP!!!!!!!!!! A/D 12F675

Started by shaunundergrad August 8, 2003
hi guys,
please have a look at my code. what i am trying to achieve is to take
an analogue signal (2 pots,1k each, across + and - rails with wire to
AN0) turn the pots up and down and switch 2 led's on, 1 when above
preset level and the other when below (1 at a time). i know i could
do it with the comparator, but i'm newbie so just setting myself a
challenge. should just copy and paste in mplab to make look right. it
compiles ok and is sort of working, i.e., if turn pots down then
connect, then turn pots up led comes on, but stays on...please help
going mad!!!

regards

shaun

LIST PF675
#INCLUDE <P12F675.INC>

ERRORLEVEL -302 ;MICROCHIP RECOMMENDED (KNOWN
BUG)
;CONFIG SETTINGS
;CODE PROTECTION DISABLED
;WATCHDOG TIMER DISABLED
;MASTER CLEAR INTERNAL ENABLED
;POWER UP TIMER DISABLED
;BROWN OUT DETECT DISABLED
;INTERNAL RC OSCILLATOR ENABLED 4Mhz
__CONFIG _CP_OFF & _WDT_OFF & _MCLRE_OFF & _PWRTE_OFF
&_BODEN_OFF & _INTRC_OSC_NOCLKOUT
ORG 000
GOTO SETUP

;DELAY SUB ROUTINE

DELAY
MOVWF TEMP ;STORE MAIN PROG VALUE
MOVLW MAX_DELAY
MOVWF TIMER2 ;LOAD TIMER
STAGE1 MOVLW MAX_DELAY
MOVWF TIMER ;LOAD TIMER
STAGE2 DECFSZ TIMER,1 ;DECREMENT TIMER
GOTO STAGE2 ;SKIP IF ZERO
DECFSZ TIMER2,1 ;TIMER2 CASCADES TIMER
GOTO STAGE2 ;START FIRST TIMER AGAIN
MOVF TEMP,0 ;MOVE MAIN PROG INTO W REG
RETURN ;BACK TO MAIN

#DEFINE LED1_ON BSF GPIO,5
#DEFINE LED2_ON BSF GPIO,4
#DEFINE LED1_OFF BCF GPIO,5
#DEFINE LED2_OFF BCF GPIO,4

CONSTANT MAX_DELAY = 0xFF
CONSTANT GP_VAL = 0x7F ;JUST A RANDOM SETTING
;equates table

TIMER EQU 0x20 ;use gp reg as timer
TIMER2 EQU 0x21 ;use gp reg as timer2
TEMP EQU 0x23 ;stores value of w reg before call SETUP
BANKSEL TRISIO ;BANK 1
MOVLW B'00001111' ;MAKE GPIO,0,1,2,3 INPUTS
MOVWF TRISIO
BSF PIE1,ADIE ;BANK 1
;PERIPHERAL
INTERUPT ENABLE
;A/D
INTERUPT, 1=ENABLE, 0=DISABLE

BSF INTCON,PEIE ;BANK 0
;INTERUPT
CONTROL REGISTER
;PERIPHERAL
INTERUPT ENABLE,1=ENABLES UNMASKED INTERUPTS, 0=DISABLED
BANKSEL ADCON0
MOVLW B'00000001'
MOVWF ADCON0 ;BANK 0
;BIT 7, ADFM,
A/D RESULT JUSTIFICATION, 0=LEFT, 1=RIGHT
;BIT 6, VCFG,
VOLTAGE REF, 0=Vdd, 1=EXTERNAL REF
;BITS 5-4,
UNIMPLEMENTED
;BITS 3-2,
ANALOG CHANNEL SELECT 00=AN0,01=AN1,10=AN2,11=AN3
;BIT 1,
GO_DONE, A/D STATUS BIT, 1=START AQUISITION, AUTOMATICALLY CLEARED
WHEN FINISHED
;BIT 0, A/D
CONVERSION STATUS, 1=A/D ON, 0=A/D OFF
BANKSEL ANSEL
MOVLW B'01110111'
MOVWF ANSEL ;BANK 1
;ANSEL,
ANALOG SELECT REGISTER
;BIT 7,
UNIMPLEMENTED
;BITS 6-4,
A/D CONVERSION CLOCK SELECT, x11=FRC, DEDICATED INTERNAL CLOCK,
500KHz MAX
;BITS 3-0,
ANALOG SELECT BITS, 0=DIGITAL OR SPECIAL FUNCTIONS, 1=ANALOG INPUT
START
BANKSEL PIR1 ;BANK 0
BCF PIR1,ADIF ;A/D CONVERSION
COMPLETE FLAG, 1=COMPLETE, 0=INCOMPLETE, MUST CLEARED IN SOFTWARE
BANKSEL ADCON0 ;BANK 0
BSF ADCON0,GO ;SETS BIT 1,
START AQUISITION, AUTO CLEAR
TEST BTFSC ADCON0,GO_DONE ;CHECKS IF AQUISITION COMPLETE
GOTO TEST ;IF NOT, LOOP
MOVF ADRESH,W ;MOVE RESULTS INTO W
REG
IF W>GP_VAL ;RESULT IS
HIGHER THAN PRESET VALUE
LED1_ON
GOTO START ;IF TRUE LED1 ON
ENDIF ;IF NOT FALL
THRU
LED2_ON
GOTO START
END



I think I have seen this before. :-p You are using conditional
assembly as if it were assembly language. "IF" is not an opcode.

IF W>GP_VAL ;RESULT IS HIGHER THAN PRESET VALUE
LED1_ON
GOTO START ;IF TRUE LED1 ON
ENDIF
Chad

--- shaunundergrad <> wrote:
> hi guys,
> please have a look at my code. what i am trying to achieve is to take
>
> an analogue signal (2 pots,1k each, across + and - rails with wire to
>
> AN0) turn the pots up and down and switch 2 led's on, 1 when above
> preset level and the other when below (1 at a time). i know i could
> do it with the comparator, but i'm newbie so just setting myself a
> challenge. should just copy and paste in mplab to make look right. it
>
> compiles ok and is sort of working, i.e., if turn pots down then
> connect, then turn pots up led comes on, but stays on...please help
> going mad!!!
>
> regards
>
> shaun
>
> LIST PF675
> #INCLUDE <P12F675.INC>
>
> ERRORLEVEL -302 ;MICROCHIP RECOMMENDED (KNOWN
> BUG)
> ;CONFIG SETTINGS
> ;CODE PROTECTION DISABLED
> ;WATCHDOG TIMER DISABLED
> ;MASTER CLEAR INTERNAL ENABLED
> ;POWER UP TIMER DISABLED
> ;BROWN OUT DETECT DISABLED
> ;INTERNAL RC OSCILLATOR ENABLED 4Mhz
> __CONFIG _CP_OFF & _WDT_OFF & _MCLRE_OFF & _PWRTE_OFF
> &_BODEN_OFF & _INTRC_OSC_NOCLKOUT
> ORG 000
> GOTO SETUP
>
> ;DELAY SUB ROUTINE
>
> DELAY
> MOVWF TEMP ;STORE MAIN PROG VALUE
> MOVLW MAX_DELAY
> MOVWF TIMER2 ;LOAD TIMER
> STAGE1 MOVLW MAX_DELAY
> MOVWF TIMER ;LOAD TIMER
> STAGE2 DECFSZ TIMER,1 ;DECREMENT TIMER
> GOTO STAGE2 ;SKIP IF ZERO
> DECFSZ TIMER2,1 ;TIMER2 CASCADES TIMER
> GOTO STAGE2 ;START FIRST TIMER AGAIN
> MOVF TEMP,0 ;MOVE MAIN PROG INTO W REG
> RETURN ;BACK TO MAIN
>
> #DEFINE LED1_ON BSF GPIO,5
> #DEFINE LED2_ON BSF GPIO,4
> #DEFINE LED1_OFF BCF GPIO,5
> #DEFINE LED2_OFF BCF GPIO,4
>
> CONSTANT MAX_DELAY = 0xFF
> CONSTANT GP_VAL = 0x7F ;JUST A RANDOM SETTING
> ;equates table
>
> TIMER EQU 0x20 ;use gp reg as timer
> TIMER2 EQU 0x21 ;use gp reg as timer2
> TEMP EQU 0x23 ;stores value of w reg before call > SETUP
> BANKSEL TRISIO ;BANK 1
> MOVLW B'00001111' ;MAKE GPIO,0,1,2,3 INPUTS
> MOVWF TRISIO
> BSF PIE1,ADIE ;BANK 1
> ;PERIPHERAL
> INTERUPT ENABLE
> ;A/D
> INTERUPT, 1=ENABLE, 0=DISABLE
>
> BSF INTCON,PEIE ;BANK 0
> ;INTERUPT
> CONTROL REGISTER
> ;PERIPHERAL
> INTERUPT ENABLE,1=ENABLES UNMASKED INTERUPTS, 0=DISABLED
> BANKSEL ADCON0
> MOVLW B'00000001'
> MOVWF ADCON0 ;BANK 0
> ;BIT 7, ADFM,
> A/D RESULT JUSTIFICATION, 0=LEFT, 1=RIGHT
> ;BIT 6, VCFG,
> VOLTAGE REF, 0=Vdd, 1=EXTERNAL REF
> ;BITS 5-4,
> UNIMPLEMENTED
> ;BITS 3-2,
> ANALOG CHANNEL SELECT 00=AN0,01=AN1,10=AN2,11=AN3
> ;BIT 1,
> GO_DONE, A/D STATUS BIT, 1=START AQUISITION, AUTOMATICALLY CLEARED
> WHEN FINISHED
> ;BIT 0, A/D
> CONVERSION STATUS, 1=A/D ON, 0=A/D OFF
> BANKSEL ANSEL
> MOVLW B'01110111'
> MOVWF ANSEL ;BANK 1
> ;ANSEL,
> ANALOG SELECT REGISTER
> ;BIT 7,
> UNIMPLEMENTED
> ;BITS 6-4,
> A/D CONVERSION CLOCK SELECT, x11=FRC, DEDICATED INTERNAL CLOCK,
> 500KHz MAX
> ;BITS 3-0,
> ANALOG SELECT BITS, 0=DIGITAL OR SPECIAL FUNCTIONS, 1=ANALOG INPUT
> START
> BANKSEL PIR1 ;BANK 0
> BCF PIR1,ADIF ;A/D CONVERSION
> COMPLETE FLAG, 1=COMPLETE, 0=INCOMPLETE, MUST CLEARED IN SOFTWARE
> BANKSEL ADCON0 ;BANK 0
> BSF ADCON0,GO ;SETS BIT 1,
> START AQUISITION, AUTO CLEAR
> TEST BTFSC ADCON0,GO_DONE ;CHECKS IF AQUISITION COMPLETE
> GOTO TEST ;IF NOT, LOOP
> MOVF ADRESH,W ;MOVE RESULTS INTO W
> REG
> IF W>GP_VAL ;RESULT IS
> HIGHER THAN PRESET VALUE
> LED1_ON
> GOTO START ;IF TRUE LED1 ON
> ENDIF ;IF NOT FALL
> THRU
> LED2_ON
> GOTO START
> END


__________________________________



hi,
thanx for the reply, it says in the quick referance guide that "if" is an assembly directive...wot is it used for then??? how do you branch, i tried the while loop and that didn't seem to work either....any ideas??

regards


Attachment (not stored)
Sixties Sunflowers .jpg
Type: image/jpeg


Assembler directives tell the assembler how to assemble the code - often determining which code to assemble.  They are NOT executable statements.
 
Example:
 
#define DEBUG  ; remove this line for production code

#ifdef DEBUG
  movlw Result ; send the result to the PC
  call PCSend ; only during development
#endif
 
If the symbol DEBUG is defined in the files then the result will be sent via PCSend (some subroutine).
 
I have attached a file that may help you get going.  It is NOT for your chip but close enough.


Attachment (not stored)
test.asm
Type: application/octet-stream

On Saturday 09 Aug 2003 11:15 am, wrote:
> hi,
> thanx for the reply, it says in the quick referance guide that "if" is an
> assembly directive...wot is it used for then??? how do you branch, i tried
> the while loop and that didn't seem to work either....any ideas??
>
> regards

It is used to get the assembelr to do different things. For example you might
include special debug code whilst developing but want to leave it out when
building the production software. Conditional assembly allows you to have
one file with both sets of code in it but only assemble the one you want.
For more details RTM.

Ian