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.

[SOLVED] PIC ADC Code Timer Question

Status
Not open for further replies.

hactic

Newbie level 5
Joined
Aug 26, 2009
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,335
Hi, I have a basic question about the use of timer 0 in an ADC example code from Microchip.

Here's the code:

Code:
;**********************************************************
;*  TUT452.ASM
;**********************************************************
;*  Microchip Technology Incorporated
;*  17 March 2003
;*  Assembled with MPASM V3.20 and MPLINK v3.20
;**********************************************************
;*  This program configures the A/D Module to convert on
;*  A/D channel 0 (the potentiometer) and display the
;*  results on the LEDS on PORTB.  
;**********************************************************

	list p=18f452

	; Include file, change directory if needed
	include "p18f452.inc"


	; Start at the reset vector
Reset_Vector  code 0x000
	goto Start

	; Start application beyond vector area

	code	0x002A
Start
	clrf	PORTB		;Clear PORTB
	clrf	TRISB		;PORTB all outputs, display 4 MSB's
				;of A/D result on LEDs

	movlw	B'01000001'	;Fosc/8, A/D enabled
	movwf	ADCON0
	movlw	B'00001110'	;Left justify,1 analog channel
	movwf	ADCON1		;VDD and VSS references

	movlw	B'11000111'	;TMR0 prescaler, 1:256
	movwf	T0CON

Main
	btfss	INTCON,TMR0IF	;Wait for Timer0 to timeout
	goto	Main
	bcf	INTCON,TMR0IF

	bsf	ADCON0,GO	;Start A/D conversion
Wait
	btfss	PIR1,ADIF	;Wait for conversion to complete
	goto	Wait

	swapf	ADRESH,W	;Swap A/D result nibbles
	andlw	0x0f		;Mask off lower 4 bits
	movwf	PORTB		;Write A/D result to PORTB

	clrf	PORTB
WaitPush			;Pause while switch is pressed
	btfss	PORTA,4
	goto	WaitPush

	movwf	PORTB
	goto	Main		;Do it again

	end

My Question:

1. What is the timer 0 used for in here?

Code:
Main
	btfss	INTCON,TMR0IF	;Wait for Timer0 to timeout
	goto	Main
	bcf	INTCON,TMR0IF

2. Why clear PORTB immediately after loading it with some data?

Code:
	movwf	PORTB		;Write A/D result to PORTB

	clrf	PORTB

Thanks for your help.
 

Hi,

Timer0 is just used as a simple small Delay.

Agree, the CLRF PORTB makes no sense, unless they had a Timer0 delay between MOVWF PORTB and CLRF PORTB.

Likewise the following MOVWF PORTB after the wait push makes little sense as is.

It highlights the point that you should always add plenty of ;comments to explain your code, however obvious to you it may seem.
You only have to look in this forum to see pages of code posted without a single ;comment, yet they expect you to be able to understand and even debug their errors.
 

Hi, its evident that its used for a small delay but I need to understand why is that delay needed. What would happen if the timer code was removed? Would the rest of the ADC code still do what it's supposed to?
 

Hi,


The chip has just been powered on and setting the Ports takes 6 millionths of a second ( 4mhz osc), the power rails barely having chance to settle.
Then just 1 millionth of a second later you would go straight into the ADC routine which needs all voltages etc to be settled to give accurate results so a small delay is good practice.

The routine will work without the delay and in your conditons probably return the same result.
 
  • Like
Reactions: hactic

    hactic

    Points: 2
    Helpful Answer Positive Rating
Ok, makes sense. So it's the settle down period. Thanks for humbly answering my basic questions. Appreciate it.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top