Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC problem with MPASM & PIC12F675

Status
Not open for further replies.

eem2am

Banned
Joined
Jun 22, 2008
Messages
1,179
Helped
37
Reputation
74
Reaction score
24
Trophy points
1,318
Activity points
0
intio pic12f675 gpio4

Hello,

I am writing in MPASM for PIC12F675.

My code builds fine but does not work properly.

I am using the ADC to read analog inputs on AN0 and AN1 in a loop.

Eg….Basically kind of like…..

-----------------------
Loop:

Read AN0
Read AN1

Goto Loop
-----------------------

Anyway, what actually should happen is that if the Analog conversion value in ADRESH is greater than 0x7F for the AN0 reading, then GPIO2 is taken high….if lower, GPIO2 is taken low

And if the analog conversion value in ADRESH is greater then 0x7F for the AN1 reading, then GPIO4 is taken high….if lower, GPIO4 is taken low
---------------------
However, the readings are always taken from AN1, and even though I change the channel to AN0 it still only reads AN1.

I know the AN0 channel works as I got it working in a different bit of code.

So does any reader know why I can’t change channels from AN1 to AN0 and read the analog input there in my loop ?

Here is my code…………….

Code:
;******************************************

;PIC12F675
;MPASM assembler
;MPLAB 8.10

;Programmer = Pickit2
;Application version 2.11.00
;Device File Version 1.12.00
;OS Firmware version 2.20.01

;Internal Oscillator at 4MHz
;--------------------------------------
;PIN USAGE
;AN0 = analog input;
;AN1 = analog input;
;RA2 = digital output;
;RA4 = digital output;
;RA5 = digital input;
;--------------------------------------

#include <p12f675.inc>
	LIST   P=PIC12F675

	__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BODEN_OFF & _CPD_OFF)

cnt0		equ		0x20
cnt1		equ		0x21
cnt2		equ		0x22
cnt3		equ		0x23

ccd0		equ		0x24

val0		equ		0x25
val1		equ		0x26

ded0		equ		0x27
ded1		equ		0x28


BANK0	macro
	bcf		STATUS,RP0
	endm

BANK1	macro
	bsf		STATUS,RP0
	endm

FET_ON	macro
	bsf		GPIO,2
	endm

FET_OFF	macro
	bcf		GPIO,2
	endm

ENABLE	macro
	bsf		GPIO,4
	endm

DISABLE	macro
	bcf		GPIO,4
	endm

ENABLE_ADC	macro
	bsf		ADCON0,0
	nop
	endm

DISABLE_ADC	macro
	bcf		ADCON0,0
	nop
	endm

ADC_CH0		macro
	bcf		ADCON0,3
	nop					;nop to avoid RMW problem
	bcf		ADCON0,2
	nop
	endm

ADC_CH1		macro
	bcf		ADCON0,3
	nop					;nop to avoid RMW problem
	bsf		ADCON0,2
	nop
	endm

DO_ADC		macro
	bsf		ADCON0,1
	nop
	endm


	org 0
	goto Start

Start:

	FET_OFF
	DISABLE

	BANK1

;Set up ANSEL register (set analog/digi ports
;..set up AD conversion clock)
	movlw	0x63
	movwf	ANSEL

;Sort comparator reference out even though 
;i dont want to use the comparator
	movlw	0x00
	movwf	VRCON

	movlw	0x2B
	movwf	TRISIO

	BANK0
;Disable interrupts as i don't want to use them
	bcf		INTCON,GIE
	bcf		PIR1,ADIF


;Sort comparator out even though i dont
;want to use it.
	movlw	0x06
	movwf	CMCON

;The comparator ref must still be enabled
;even though i dont want to use the comparator
	BANK1
	bsf	VRCON,VREN	;Enable comparator ref
	BANK0

;Set up ADCON0 = Ljust; Vdd = ref; xx; CHS = 00; GODONE; Disable
	movlw	0x00
	movwf	ADCON0

	ENABLE_ADC

	FET_OFF
	DISABLE
	

;VVVVVVVVVVVVVVVVVVVVV
;* MAIN PROGRAM LOOP *
loop:

;...........................
;Do ADC on channel 0
	bcf		PIR1,ADIF
	nop
	DISABLE_ADC
	ADC_CH0		;Change ADC channel
	ENABLE_ADC
	
	call	ADC_channel_changeover_delay
	D0_ADC
egg:
	btfsc	ADCON0,1
	goto	egg
;............................
	movf	ADRESH,0
	movwf	val0

	movlw	0x7F
	subwf	val0,0
	btfsc	STATUS,C
	FET_OFF
	movlw	0x7F
	subwf	val0,0
	btfss	STATUS,C
	FET_ON
;............................	

;............................

;----------------------------
;Do ADC on channel 1
	bcf		PIR1,ADIF
	nop
	DISABLE_ADC
	ADC_CH1		;Change ADC channel
	ENABLE_ADC
	call	ADC_channel_changeover_delay
	DO_ADC
dog:
	btfsc	ADCON0,1
	goto	dog
;............................
	movf	ADRESH,0
	movwf	val1

	movlw	0x7F
	subwf	val1,0
	btfsc	STATUS,C
	DISABLE
	movlw	0x7F
	subwf	val0,0
	btfss	STATUS,C
	ENABLE

;----------------------------
	goto	loop
;* END OF MAIN PROGRAM LOOP *
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^



ADC_channel_changeover_delay:
	movlw	0x0F
	movwf	ccd0
fff:
	decfsz	ccd0,1
	goto	fff
	retlw	0x00


	end
-------------------------------
I am very grateful for your attentions
 

pic12f675 examples

hi,

Only been able to look at your code, which is in a very small loop reading the two adc and switching your ports accordingly, but with the only delay being 50us, how can you 'see' what is actually happening ?
 

pic12f675 adc

thankyou wp100,

i can see what is happening with my voltmeter probing the ports GPIO2 and GPIO4.

The outputs on these ports depends on whether or not the ADC readings from channels 0 and 1 was greater or less than 0x7F (considers ADRESH only)

Unfortunately, the ADC module only reads from channel 1 and never switches over to channel 0
 

pic12f675 analog comparator

Yes, have followed your code logic again , looks fine but cannot spot any error.

Unable to actually run your code as my dev board is down at the moment.

Think going back a step and try running it with only adc 0 and then adc 1 to see if that works in this program code - think its just one of those frustrating silly one liner mistakes you just cannot see at the moment.

good luck
 

pic analog input assembly mpasm

thankyou wp100, actually i have a copy/paste error on the 13th line up from the bottom...."val0" should be "val1" but even with this corrected it still doesnt work.

basically all i need to do is to be able to switch adc channels.

here is the corrected code which doesnt work (wont switch adc channels)

Code:
;PIC12F675
;MPASM assembler
;MPLAB 8.10

;Programmer = Pickit2
;Application version 2.11.00
;Device File Version 1.12.00
;OS Firmware version 2.20.01

;Internal Oscillator at 4MHz
;--------------------------------------
;PIN USAGE
;AN0 = analog input;
;AN1 = analog input;
;RA2 = digital output;
;RA4 = digital output;
;RA5 = digital input;
;--------------------------------------

#include <p12f675.inc>
	LIST   P=PIC12F675

	__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BODEN_OFF & _CPD_OFF)

cnt0		equ		0x20
cnt1		equ		0x21
cnt2		equ		0x22
cnt3		equ		0x23

ccd0		equ		0x24

val0		equ		0x25
val1		equ		0x26

ded0		equ		0x27
ded1		equ		0x28

;   *** DECLARE MACRO'S ***
BANK0	macro
	bcf		STATUS,RP0
	endm

BANK1	macro
	bsf		STATUS,RP0
	endm

FET_ON	macro
	bsf		GPIO,2
	endm

FET_OFF	macro
	bcf		GPIO,2
	endm

ENABLE	macro
	bsf		GPIO,4
	endm

DISABLE	macro
	bcf		GPIO,4
	endm

ENABLE_ADC	macro
	bsf		ADCON0,ADON
	nop
	endm

DISABLE_ADC	macro
	bcf		ADCON0,ADON
	nop
	endm

ADC_CH0		macro
	bcf		ADCON0,CHS1
	nop					;nop to avoid RMW problem
	bcf		ADCON0,CHS0
	nop
	endm

ADC_CH1		macro
	bcf		ADCON0,	CHS1
	nop					;nop to avoid RMW problem
	bsf		ADCON0,	CHS0
	nop
	endm

DO_ADC		macro
	bsf		ADCON0,1
	nop
	endm
; *** MACRO'S NOW DECLARED ***

	org 0
	goto Start

Start:

	BANK1

;Set up ANSEL register (set analog/digi ports
;..set up AD conversion clock)
	movlw	0x63
	movwf	ANSEL

	movlw	0x2B
	movwf	TRISIO

	BANK0
;Disable interrupts as i don't want to use them
	bcf		INTCON,GIE
	bcf		PIR1,ADIF


;turn comparator off
	movlw	0x07
	movwf	CMCON


	BANK1
	bcf	VRCON,VREN	;disable comparator ref
	BANK0

;Set up ADCON0 = Ljust; Vdd = ref; xx; CHS = 00; GODONE; Disable
	movlw	0x00
	movwf	ADCON0
	
	nop

	ENABLE_ADC

	FET_OFF
	nop
	DISABLE
	

;VVVVVVVVVVVVVVVVVVVVV
;* MAIN PROGRAM LOOP *
loop:

;...........................
;Do ADC on channel 0
	bcf		PIR1,ADIF
	nop
	DISABLE_ADC
	ADC_CH0		;Change ADC channel
	ENABLE_ADC
	
	call	ADC_channel_changeover_delay
	D0_ADC
egg:
	btfsc	ADCON0,1
	goto	egg
;............................
	movf	ADRESH,0
	movwf	val0

	movlw	0x7F
	subwf	val0,0
	btfsc	STATUS,C
	FET_OFF
	movlw	0x7F
	subwf	val0,0
	btfss	STATUS,C
	FET_ON
;............................	

;............................

;----------------------------
;Do ADC on channel 1
	bcf		PIR1,ADIF
	nop
	DISABLE_ADC
	ADC_CH1	;Change ADC channel
	ENABLE_ADC
	call	ADC_channel_changeover_delay
	DO_ADC
dog:
	btfsc	ADCON0,1
	goto	dog
;............................
	movf	ADRESH,0
	movwf	val1

	movlw	0x7F
	subwf	val1,0
	btfsc	STATUS,C
	DISABLE
	movlw	0x7F
	subwf	val1,0
	btfss	STATUS,C
	ENABLE


;----------------------------
	goto	loop
;* END OF MAIN PROGRAM LOOP *
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ADC_channel_changeover_delay:
	movlw	0x0F
	movwf	ccd0
fff:
	decfsz	ccd0,1
	goto	fff
	retlw	0x00

delay:
	movlw	0xFF
	movwf	cnt1
mog:
	movlw	0xFF
	movwf	cnt1
hog:
	decfsz	cnt1,1
	goto	hog
	decfsz	cnt0,1
	goto	mog
	retlw	0x00

	end
[code*]
 

p=12f675 adcon adcon0

Hi,

Still not spotted any real error then .. think if you simply remove just the code below so if runs just on adc channel 0 and then see it that works, if ok then remove adc channel 0 code and then try on just channel 1 and see if that works.

Hopefully that should show up something -

Just a little thing might help with your coding, makes it easier to follow, when you have Subwf val0, 0 is easy to misread things - making it ( and similar instructions) .. val0,W or F, instead of 0 or 1, is less prone to errors.

;----------------------------
;Do ADC on channel 1
bcf PIR1,ADIF
nop
DISABLE_ADC
ADC_CH1 ;Change ADC channel
ENABLE_ADC
call ADC_channel_changeover_delay
DO_ADC
dog:
btfsc ADCON0,1
goto dog
;............................
movf ADRESH,0
movwf val1

movlw 0x7F
subwf val1,0
btfsc STATUS,C
DISABLE
movlw 0x7F
subwf val0,0
btfss STATUS,C
ENABLE
 

bug adc pic 12f675

If you look at your code in Mplab - you have three lines of " DO_ADC " the actual macro, and the two macro calls -

;...........................
;Do ADC on channel 0
bcf PIR1,ADIF
nop
DISABLE_ADC
ADC_CH0 ;Change ADC channel
ENABLE_ADC

call ADC_channel_changeover_delay
D0_ADC : **

Is this ** line in column 1 ? - bet it is - then it builds ok - but its only using it as a label not a macro call - will let you spot the error in your code ..


Don't worry -we all all do similar errors
:D
 

pic12f675 adc adresl

Hi all

I new member in this forum. i from malaysia. i new in pic12f675. i already have PICSTART plus, PIC12F675 & MPLAB IDE v8.3 software. I want to get knowledge from this furom. Hopefully i get it. So, now i searching simple assembly code for this PIC. Can u give me simple code. as example is LED blinking.

thanks
 

pic12f675 adcon0 c

Hi,

You will find lots of tutorials and code examples in this and other forums - many are based on the 16F chips but you should be able to use the methods on the 12F chip.

This site has a very good reputation and the guy is contactable
http://www.winpicprog.co.uk/pic_tutorial.htm
 

pic12f675 assembler

sasuke_ninchan said:
Hi all

I new member in this forum. i from malaysia. i new in pic12f675. i already have PICSTART plus, PIC12F675 & MPLAB IDE v8.3 software. I want to get knowledge from this furom. Hopefully i get it. So, now i searching simple assembly code for this PIC. Can u give me simple code. as example is LED blinking.

thanks

Hi,

You should try Gooligum electronics tutorial.
www.gooligum.com.au/tutorials.html
 

pic12f675 example assembler

eem2am said:
Hello,

I am writing in MPASM for PIC12F675.

My code builds fine but does not work properly.

I don't know assembly but had worked on a similar project on the same chip, in C. here is my code and hopefully it is of value to you.

Code:
#include <htc.h>

// hardware configuration

#define Vin_a		GPIO0	//analyg in on GPio0;
#define CSPin		GPIO1	//current sense pin on GPIO1;
#define OUTPUT		GPIO5	//led out on gpio5;
// end hardware configuration

#define GETBIT(var,bit) (((var)>>(bit))&1) 		/* read bit from var */
#define SETBIT(var,bit) ((var)|=(1<<(bit)))		/* set bit from var to 1 */
#define CLRBIT(var,bit) ((var)&=(~(1<<(bit))))	/* clear bit from var (set it to 0) */

#define Vcs_th		((long) 125*1023/5000)		//input voltage threshold (125mv) to turn on the LED. from 0-1023 (10bit adc) 
#define wait2		NOP();NOP()
#define wait4		wait2;wait2
#define wait8		wait4;wait4
#define wait16		wait8;wait8
#define wait32		wait16;wait16
#define wait64		wait32;wait32
#define wait128		wait64;wait64
#define wait256		wait128;wait128
#define adc_bits	7							//how many bits in pwm stepping
#define PWMLength	(1<<adc_bits)				//total length of pwm, should be consistent with sampling bits - 10 here
#define CSRemedy	OUTPUT=OFF
#define ON			0							//output high for ON
#define OFF			1							//output low for OFF
#define Tosc8		0b0010000					//ADC at 1/8th Tosc
#define Tosc16		0b1010000					//ADC at 1/16th Tosc
#define SEL_AN0		CHS1=0; CHS0=0				//select AN0 for ADC
#define SEL_AN1		CHS1=0; CHS0=1				//select AN1 for ADC

int ADC(void) {									//adc the currently selected channel
	
	GODONE=1;									//start the adc process on 	
	while (GODONE) 
		;										//wait for adc to complete
	
	return (ADRESH<<8)+(ADRESL);
}
__CONFIG(MCLRDIS & INTIO & WDTDIS & BORDIS);

void
main(void)
{
	int Vin_d;				//adc result for Vin_a
	int Vcs_d;				//adc results for Current Sensor
	int i=0;				//where we are in length?
	
	//OSCCAL=_READ_OSCCAL_VALUE();
	CMCON=7;				//turn off comparators;
	TRISIO=0b11;			//input on GPIO0 and GPIO1, all others digital output;
	
	//set up ANSEL register
	ANSEL=Tosc8;			//Adc speed setting
	ANS0=1;					//GPIO0 is analog input;
	ANS1=1;					//GPIO1 is analog input;

	//set up ADCON register
	ADFM=1;					//adc results right adjusted
	VCFG=0;					//use Vdd as reference voltage
	ADON=1;					//starts ADC

	OUTPUT=OFF;				//turn output off;
	while (1){
		//TODO Auto-generated main function
		SEL_AN1;						//select AN1 / GPIO1
		Vcs_d=ADC();					//Vcs stores the results;
		wait256;						//wait for the Chold capacitor to charge-up

		SEL_AN0;						//select GPIO0;
		Vin_d=ADC()>>(10-adc_bits);		//adc the current channel
		
		OUTPUT=ON; for (i=0; i<Vin_d; i++);
		OUTPUT=OFF; for (i=Vin_d; i<PWMLength; i++);

		//		wait8;							//wait to charge up Chold
	}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top