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.

pic16f877 AD conversion

Status
Not open for further replies.
Btw, i've got another problem with your code Pommie. When we enter for the second time the subroutine (but without calling it), the program stands still when reading the instruction 'return'. I think it's because we didn't called the subroutine.

Do you see what i mean ?

Thanks
 
The code sends the 4 MSB first as that is how it should be displayed - most significant nibble first. If it was decimal then the hundreds would have to be sent first.

Mike.
 
Btw, i've got another problem with your code Pommie. When we enter for the second time the subroutine (but without calling it), the program stands still when reading the instruction 'return'. I think it's because we didn't called the subroutine.

Do you see what i mean ?

Thanks

I don't understand what you are saying but maybe if I explain the code it may help you understand it.

Code:
SendHex		movwf	HexTemp		;save the value that was in W
		swapf	HexTemp,W	;move the high nibble into the low nibble
		call	WriteLoNibble	;send the nibble
		movfw	HexTemp		;get the value to be sent back
					;fall through to the write nibble routine
					;this is the same as calling it again
					;followed by a return
					;and sends the lower nibble
WriteLoNibble	andlw	0fh		;keep only the lower 4 bits (nibble)
		addlw	.256-.10	;add 246 >> this is the same as subtracting 10
		btfsc	STATUS,C	;if the original number was greater than 10
		addlw	7		;then add 7 to convert 10 to 17
		addlw	"0"+.10		;add ascii zero plus the 10 subtracted earlier
		goto	SendByte	;send it to the PC

BTW, a nibble is 4 bits.

Mike.
 
Ok, that's cool you commented it, thanks.
My problem appears when using the debug mode on MPLAB IC2. I really think sth goes wrong, because when i'm launching debug without breakpoints, then stops it, the program is at the beginning of its execution, whereas it should be in an infinite loop ! So that's the reason why i was wondering if the issue came or not from this subroutine.

Here is my code :

Code:
BANK1
	movlw	ADCON1VAL			; charger valeur ADCON1VAL du premier détecteur dans w
	movwf	ADCON1				; puis dans ADCON1
	BANK0




		

	movlw 	ADCON0VAL1			; charger le CAN pour la première valeur
	movwf	ADCON0				; dans ADCON0, pour démarrer la charge du condensateur	
	TEMP25						; temporisation pour que le condensateur se charge
	bsf		ADCON0,GO			; On démarre la conversion  numérique
waitconv1
	btfsc	ADCON0,GO			; la conversion est-elle terminée ?
	goto	waitconv1			; non, on attend qu'elle le soit
	movf	ADRESH,w			; on charge la 

SendHex		movwf	HexTemp
		swapf	HexTemp,W
		call	WriteLoNibble
		movfw	HexTemp
WriteLoNibble	andlw	0fh
		addlw	.256-.10 ; 	if a-f then carry set
		btfsc	STATUS,C
		addlw	7
		addlw	"0"+.10
		goto	SendByte


SendByte
	movwf	TXREG
	return

rien
		nop
		nop
		nop
		goto	rien
 
Your code is nearly correct but you need to test the interrupt flag and call the sendhex routine.

Try,
Code:
BANK1
		movlw	ADCON1VAL	; charger valeur ADCON1VAL du premier détecteur dans w
		movwf	ADCON1		; puis dans ADCON1
		BANK0






		movlw	ADCON0VAL1	; charger le CAN pour la première valeur
		movwf	ADCON0		; dans ADCON0, pour démarrer la charge du condensateur
		TEMP25			; temporisation pour que le condensateur se charge
		bsf	ADCON0,GO	; On démarre la conversion  numérique
waitconv1
		btfsc	ADCON0,GO	; la conversion est-elle terminée ?
		goto	waitconv1	; non, on attend qu'elle le soit
		movf	ADRESH,w	; on charge la 

		call	SendHex		;send the byte in W

rien		goto	rien		;wait forever


SendHex		movwf	HexTemp
		swapf	HexTemp,W
		call	WriteLoNibble
		movfw	HexTemp
WriteLoNibble	andlw	0fh
		addlw	.256-.10	; 	if a-f then carry set
		btfsc	STATUS,C
		addlw	7
		addlw	"0"+.10
		goto	SendByte


SendByte	btfss	PIR1,TXIF	;has previous byte been sent
		goto	SendByte	;no so wait
		movwf	TXREG		;yes so send next one.
		return

Mike.
Disclaimer, it's late, I've been out, It's midnight here, I've had lots to drink, the above could be completely wrong.:D
 
Ok, i finally got it ! My mistake was to consider SendHex as a Label only, and i didn't call it as a subroutine ! That's why when the debugger read for the second time the instruction 'return' he was lost !

Thanks pommie ! =)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top