Help with ADC code M48

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have this bit of code sort of working but it doesn't do what it says on the tin....
I have 100K from battery (12V) to AIN6 and 51K from there to a port pin, which I ground to take a reading.
That all works ok and I get 0x64/65 for Vin = 8V and 0x94/95 for Vin = 12V.
So I set the compare to 0x70 but the software fails to branch to battery ok with 12V i/p and ADCH = 94.
Can someone put me out of my misery please...


        cbi portc,4				;put batt gnd = 0v

	LDI RTEMP,0X0			;adc fre running mode
	STS ADCSRB,RTEMP

	ldi rtemp,0x66			;ADC6 + vref = vcc + leftjustify = 8 bits in ADCH
	sts admux,rtemp

	ldi rtemp,0xd7			;start etc
	sts adcsra,rtemp

;*********************************************************************

waitforadc:
	lds rtemp,adcsra
	sbrs rtemp,adif
	rjmp waitforadc			;SETTLING TIME FOR adc

	ldi rtemp,0xd7			;read again
	sts adcsra,rtemp
waitforadc2:
	lds rtemp,adcsra
	sbrs rtemp,adif
	rjmp waitforadc2			;SETTLING TIME FOR adc

	lds rtemp,adcl
	lds rtemp,adch
sts UDR0,rtemp            ;debug
	cpi rtemp,0x70

	brge voltsok
batteryfail:
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This kind of thing:

   ldi rtemp,0xd7         ;start etc 

makes your code very hard to follow. After digging out a mega48 datasheet I see it really means:

   ldi rtemp,(1<<ADEN)|(1<<ADSC)|(1<<ADIF)|(7<<ADPS0)     ;start etc 

Anyway you are reading ADCL even though you are using ADLAR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Anyway you are reading ADCL even though you are using ADLAR.


... which isn't a cause for a problem.

Let's consider this fragment...

Quote:

cpi rtemp,0x70

brge voltsok


... and what BRGE is ...

Quote:
BRGE – Branch if Greater or Equal (Signed)

See the problem?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahhhh it's signed. :(
Quick check suggests BRSH... :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That works just fine :)Many thanks....
Old age is not a nice place to be sometimes :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Old age is not a nice place to be sometimes

Well, I don't do much ASM on AVRs anymore. But you are in a minefield area here. In the Instruction Set document, see the Conditional Branch Summary section, just before the discussion of all the op codes.

Whenever I get to one of the BRSH/BRLO pairs that might need operand swapping, I get confused (age related?) and I have to figure it out anew each time.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.