7 segment counter Atmega328P

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

The program needs to display in a 7 segment display the numbers from 0 to 9, and then repeat the count (from 9 display again the number 0 and so on).

There is no need to put input buttons.

I must use lookup tables for this program.

 

I´m programming in assembler in Microchip Studio, using the atmega328p and simulating in proteus. Here's my schematic.

 

 

The program runs very well from 0 to 9 but after 9 the segments instead of displaying 0 (0x3F) the "f" and "d" segments are on (0x28), then the "f" and "g" segments are on (0X60).

Just not going back to 0 and starting again the sequence.

 

My program: 

.org $00
.cseg
ldi r16,$ff
out ddrb,r16

start:
ldi ZL, low(table*2)								
ldi ZH, high(table*2)
ldi r16,$A
clr r17

count:
lpm r17,Z+
dec r16
out portb,r17
call delay
cpi r16,0
breq start
jmp count

 table: 
.db 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F
;    0    1    2     3   4    5    6    7    8     9   

delay:
	ldi r18,$8
delay2:
	ldi r19,$7d
delay1:
	ldi r20,$fa
delay0:
	dec r20
	brne delay0
	dec r19
	brne delay1
	dec r18
	brne delay2
jmp count

 

I would really appreciate any help. 

This topic has a solution.
Last Edited: Fri. Apr 30, 2021 - 03:08 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For one thing your should replace jmp count with ret at the end on your relay routine as you are calling the routine so you must return or the stack goes crazy.

 

I have just loaded your code into Studio, see if I can see anything else.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I think that's the problem. You call delay, when delay ends it jumps to count and therefore you never execute the compare with 0 statement.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Funt3ck wrote:
simulating in proteus

So why not use that simulation to step through the code - and see what's happening?

 

That's one of the key reasons for using a simulator.

 

Funt3ck wrote:
atmega328p

The ATmega328P XPlained Mini board is only ~ $10 and comes complete with an on-board debugger - so you could step in the actual hardware

 

See this thread: https://www.avrfreaks.net/forum/xplained-mini-mega328pb for some examples of using  ATmega328PB & ATmega328P XPlained Mini boards ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It was! Thank you very much. I kinda feel embarrassed with such a basic thing but I´m very new to this whole world.

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

So why not use that simulation to step through the code - and see what's happening?

I was and I usually use it because it is very helpful and I thought that my code was right for what I saw in the simulation. But in proteus, I realize that something was clearly wrong.

The ATmega328P XPlained Mini board is only ~ $10 and comes complete with an on-board debugger

Thank you very much for the suggestion, but at the moment I can´t afford such a thing.  

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

I thought that my code was right for what I saw in the simulation

You must have not gone very far with the simulation. Do you know how to use breakpoints in Studio?

 

If you do then put a breakpoint at the end of the delay routine and from there single step (use F11) and you will see that you never get to the point where you test R16 for zero.

 

From then on you are collecting and displaying garbage.

I kinda feel embarrassed

Don't be too embarrassed, the code was reasonably good for a beginner and you learned a lesson the you will never forget: ALWAYS RETURN form a subroutine.

 

Also a couple of tips, JMP and CALL waste 1 byte each and for a small program you can use rcall and rjmp, in fact that's all you can use with chips of 8K or less.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
Do you know how to use breakpoints in Studio?

Or, since you're using Proteus, do you know how to do the equivalent in Proteus?

 

If you do then put a breakpoint at the end of the delay routine and from there single step (use F11) and you will see that you never get to the point where you test R16 for zero.

Again, Proteus should give you the same facility - with the added advantage that you should also be able to see the effects on the rest of the simulated circuit outside the AVR.

 

If you don't know how to use these facilities in Proteus (and/or Studio or whatever other IDE you use), you should certainly spend some time to learn & practice them

 

See: https://www.avrfreaks.net/commen... - in particular, note steps 2, 4, 5, ...

 

Proteus support is beyond the scope of this forum - you will need to study the Proteus documentation and seek help on the Proteus forums:

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

On debugging in general:

 

https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...