Really weird counter glitch

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

Hallo all.

I am having a lot of trouble with timer/counter 0\

I have configured it as a counter:

 .include "m328Pdef.inc"

 .def temp = R16

  .org 0x0000

 maint:
	ldi temp, high(ramend)
	out sph, temp
	ldi temp, low(ramend)
	out spl, temp

port_setup:
	ldi temp, 0x00
	out DDRD, temp
	
	ldi temp, 0x0F
	out DDRB, temp

;=============================

RPM_Calc2:
	clr R27
	clr R28
	
	ldi Temp, 0x06
	out TCCR0B, Temp

	rcall delay_short
	
	ldi Temp, 0
	out TCCR0B, Temp

problem:
	ldi Temp, TCNT0

	out TCNT0, Temp

	out PortB, Temp

here:rjmp RPM_Calc2
;==========================

delay:
	nop
	nop
	ret

;=========================

delay_short:
	push R21
	ldi R21, 200
DQ:	call delay
	dec R21
	brne DQ
	pop R21
	ret

;========================

Now when I run this code in AVR studio 5 I get the value of 38 loaded into "Temp" at the label "problem".
I have no idea what is causing this and it is causing havoc with my larger code.
Please help

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

You're actually loading the SRAM address for TCNT0 into Temp doing this

problem:
   ldi Temp, TCNT0

Don't know what number you intend to load into Temp, but probably not SRAM address.

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

He probably meant

lds Temp, TCNT0

except that would not work either as TCNT0 is almost certainly defined as an IO not RAM address and should be read with:

in Temp, TCNT0

If that was the intention it does make you wonder what the purpose of:

   in Temp, TCNT0

   out TCNT0, Temp

   out PortB, Temp

might be - why would one read it then immediately write it back?

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

Btw, writing comments is an essential part of writing code.
As it look now, no one else but you have a clue what you really want to happen in that code line.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
   in Temp, TCNT0
   out TCNT0, Temp
   out PortB, Temp

maybe should be

   in Temp, TCNT0
   out PortB, Temp

Which would load the value of TCNT0 on PortB.

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

Thank you everyone so much... Its so simple I can't believe I missed it!