Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Program to read A/D on firefly...

Status
Not open for further replies.

RobertD

New Member
Hi folks, I wrote this program to read the AD on pin 3 of the firefly, and I can't seem to get any results in the ADRESH/L registers. What am I doing wrong?

LIST p=16F88
include "P16F88.inc"
__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF


;_____________________________________________

count equ 0x29


BANKSEL TRISA
MOVLW B'00001000' ;MAKE RA3 INPUT
MOVWF TRISA
MOVWF ANSEL ;MAKE RA3 ANALOG INPUT
MOVLW B'10000000'
MOVWF ADCON1 ;set right justified

BANKSEL ADCON0
MOVLW B'01011001'
MOVWF ADCON0 ;enable A/D

BANKSEL count ;select bank
MOVLW 0x06
MOVWF count ;set count register

loop
DECFSZ count,F ;count 6 tocks
GOTO loop

BSF ADCON0,GO ; capture A/D
BTFSC ADCON0,GO
GOTO $-1 ; waiting for A/D to finish



END
 
First thing I do in a case like this is clean up the code so it's readable. :p
Code:
	list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

	cblock	0x20
	count
	endc

	banksel	TRISA		;bank 1
	movlw	B'00001000'	;MAKE RA3 INPUT
	movwf	TRISA
	movwf	ANSEL		;MAKE RA3 ANALOG INPUT
	movlw	B'10000000'
	movwf	ADCON1		;set right justified
	banksel	ADCON0		;bank 0
	movlw	B'01011001'
	movwf	ADCON0		;enable A/D
	movlw	0x06
	movwf	count		;set count register

loop	decfsz	count,F		;count 6 tocks
	goto	loop
	bsf	ADCON0,GO	;capture A/D
	btfsc	ADCON0,GO
	goto	$-1		;waiting for A/D to finish

	end
Then I look for trouble. Found some. Your code has nothing to catch it from falling out the bottom and blasting through empty memory (all NOPs) and restarting over and over. END does not stop code execution - it tells the assembler to stop assembling there. Put a continuous loop there to stop the program. Or jump back to loop to read A/D again. Read your ADRESH/L now.
 
Last edited:
OK did that, how can I read the memory of ADRESH/L while it's in the firefly? I put the switch off, and now the program is running, but I don't get any reading when I go into MPLAB view special function registers section.

And I modifed the code to get some LEDs to light up when I get something in the ADRESH register.


Code:
	list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

	cblock	0x20
	count
	endc

	banksel	TRISA		;bank 1
	movlw	B'11111111'
	movwf	TRISB		;make PORTB output
	movlw	B'00001000'	;MAKE RA3 INPUT
	movwf	TRISA
	movwf	ANSEL		;MAKE RA3 ANALOG INPUT
	movlw	B'10000000'
	movwf	ADCON1		;set right justified
	banksel	ADCON0		;bank 0
	movlw	B'01011001'
	movwf	ADCON0		;enable A/D
	movlw	B'00000110'
	movwf	count		;set count register

loop
	decfsz	count,F		;count 6 tocks
	goto	loop
	bsf		ADCON0,GO	;capture A/D
	btfsc	ADCON0,GO
	goto	$-1		;waiting for A/D to finish
	
	movfw	ADRESH
	movwf	PORTB

HANG
	GOTO HANG

	end
 
Last edited:
OK did that, how can I read the memory of ADRESH/L while it's in the firefly? I put the switch off, and now the program is running, but I don't get any reading when I go into MPLAB view special function registers section.
Use the debugger, since you don't have any display attached.

And I modifed the code to get some LEDs to light up when I get something in the ADRESH register.
There's always something in ADRESH/L, even if it's zero. You could code it to light up when ADRESH/L changes though.

Gotta take a shower. I'll hook up my Firefly when I get back and do some checking.
 
When I step through the debugger, it works OK, goes through the six tocks, then through the GO bit wait time, then goes to hang after about another six tocks. I did set the trimpot dip switch 2 on.

Shower time for me too... Canada day.
 
When I step through the debugger, it works OK, goes through the six tocks, then through the GO bit wait time, then goes to hang after about another six tocks. I did set the trimpot dip switch 2 on.

Shower time for me too... Canada day.
Heh. :D Nice lazy day. Need another coffee... EDIT: Silvia and Rocky have done their job. Nice espresso to drink. Yum! :p

This code:
Code:
	list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF
	ERRORLEVEL 0, -302

	cblock	0x20
	count
	endc

	banksel	TRISA		;bank 1
	movlw	B'00001000'	;MAKE RA3 INPUT
	movwf	TRISA
	movwf	ANSEL		;MAKE RA3 ANALOG INPUT
	movlw	B'10000000'
	movwf	ADCON1		;set right justified
	banksel	ADCON0		;bank 0
	movlw	B'01011001'
	movwf	ADCON0		;enable A/D
	movlw	0x06
	movwf	count		;set count register

loop	decfsz	count,F		;count 6 tocks
	goto	loop
	bsf	ADCON0,GO	;capture A/D
	btfsc	ADCON0,GO
	goto	$-1		;waiting for A/D to finish
	goto	loop

	end
works just fine. I use "run to cursor" in the debugger on the "goto loop" line over and over while adjusting VR2 and watching (in the Watch list) ADRESH and ADRESL. The numbers vary from $0000 to $03ff (0 to 1023) just as they should.

You DO have the VR2 dip switch in the ON position, don't you? My dip switch is set to 010111.
 
Last edited:
Thank you, I'm relieved to know my coding is adequate... :) I could not find anything wrong with it, so that's why I asked. Usually , since I'm very green I do errors all over the place, like maybe not reading the registers correctly or something like that. My dip switch setting is 010100, the 28/18 both off.

I don't have "run to cursor" and how do you read the registers?

When I run the debugger, I click "watch" and add the two AD registers, and they're flat line... :(

the ADCON0 does change when I watch special function registers. The GO pin changes form 0 to 1, so I should have a reading in AD regs...
 
Last edited:
I'll try running the prog with these on or off, tutor has to be off, when the prog is running, I just tested the trimpot, and there is power to RA3 2.4v, same as the trimpot output, though the trimpot seems erratic, I do get a voltage to work with.

I changed the 88 and now my leds light up... :) But the AD registers still read 00
 
Last edited:
I don't have "run to cursor"
Yes you do. Put your cursor on the line you want to run to and right-click the mouse. Select Run To Cursor.

and how do you read the registers?
Open a Watch window. In the left list at the top select ADRESH and hit Add SFR. Do the same for ADRESL. Then step through the code or use run to cursor and you'll see those SFR's change. They'll go red whenever a change happens.

When I run the debugger, I click "watch" and add the two AD registers, and they're flat line... :(
Probably because you're switching your tutor section off with the dip switch (see other post about this).
 
I did that, I changed the code to see if I would get any reading on ADRESH and toggle a value (FF) on and off. It changed on and off, but not with the input, with the software, when I tried to do the same thing with ADRESL I still got ADRESH to toggle, and can't get ADRESL to toggle.... ?!?!

Code:
	list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

	cblock	0x20
	count
	endc

start
	banksel	TRISA		;bank 1
	movlw	B'11111111'
	movwf	TRISB		;make PORTB output
	movlw	B'00001000'	;MAKE RA3 INPUT
	movwf	TRISA
	movwf	ANSEL		;MAKE RA3 ANALOG INPUT
	movlw	B'10000000'
	movwf	ADCON1		;set right justified
	banksel	ADCON0		;bank 0
	movlw	B'01011001'
	movwf	ADCON0		;enable A/D

	clrw

	call delay

	bsf		ADCON0,GO	;capture A/D
	btfsc	ADCON0,GO
	goto	$-1		;waiting for A/D to finish
	


	movlw	B'11111111'
	movwf	ADRESL
	
	call delay 
	
	movlw	B'00000000'
	movwf	ADRESL
	goto 	start	


delay
	movlw	B'00000110'
	movwf	count		;set count register
loop
	decfsz	count,F		;count 6 tocks
	goto	loop
	return

	end
 
Last edited:
Aha... I found a problem.

I isolated the register ADRESL for testing and found I still toggle ADRESH instead of ADRESL, so there is definitely a problem there..



Code:
	list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

cblock	0x20
	count
	endc

start
	movlw	B'11111111'
	movwf	ADRESL
	
	call delay 
	
	movlw	B'00000000'
	movwf	ADRESL
	
	call 	delay	
	goto 	start

delay
	movlw	B'00000110'
	movwf	count		;set count register
loop
	decfsz	count,F		;count 6 tocks
	goto	loop
	return

	end
 
Aha... I found a problem.

I isolated the register ADRESL for testing and found I still toggle ADRESH instead of ADRESL, so there is definitely a problem there..
I don't have a clue what you're trying to accomplish with that. Try my (your :p) tested and working code above. If that won't work for you, then your hardware is set wrong (tutor dip switch) or you're using the debugger incorrectly.
 
Last edited:
Can't seem to be able to write anything in any registry...
This code won't toggle PORTB, is there anything wrong with this?

Code:
	list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

cblock	0x20
	count
	endc

	banksel	TRISB
	movlw	0x00
	movwf	TRISB
	banksel	PORTB

start
	movlw	B'11111111'
	movwf	PORTB
	
	call delay 
	
	movlw	B'00000000'
	movwf	PORTB
	
	call 	delay	
	goto 	start

delay
	movlw	B'00000110'
	movwf	count		;set count register
loop
	decfsz	count,F		;count 6 tocks
	goto	loop
	return

	end
 
Last edited:
Can't seem to be able to write anything in any registry...
This code won't toggle PORTB, is there anything wrong with this?

Code:
	list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

cblock	0x20
	count
	endc

	banksel	TRISB
	movlw	0xFF
	movwf	TRISB
	banksel	PORTB
Umm... Your PortB is set to all inputs. That could be the problem. :p
 
Yes I noticed the error and changed it, still no response...

I'm beginning to think the compiler has a bug...
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top