Atmega16 as a counter using timers0

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

hello freaks,

I just got into Timer/counter programming, and i'm trying to make a counter circuit using atmega16 and 7-segment.

but i reached to a point where the counting on 7-segment displays doesn't go well.

 

Here is the code I used and the circuit is attached too:

 

#define  F_CPU 1000000ul
#include <avr/io.h>
#include <util/delay.h>
#define SegOne   0x01
#define SegTwo   0x02
#define SegThree 0x04	
int main(void)
{
	TCCR0 |= (1<<CS02) | (1<<CS01);
	TCCR0 &= ~(1<<CS00);
	DDRC = 0xff; // Data lines
	DDRD = 0xff; // Control signal PORTD0-PORTD3
	int num,temp;
	char seg_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
    while (1) 
    {
		num = TCNT0;
		temp = num / 100;
		num = num % 100;
		PORTD = SegOne;
		PORTC = seg_code[temp];
		_delay_ms(1);

		temp = num / 10;
		num = num % 10;
		PORTD = SegTwo;
		PORTC = seg_code[temp];
		_delay_ms(1);

		temp = num / 1;
		PORTD = SegThree;
		PORTC = seg_code[temp];
		_delay_ms(1);
    }
}

Can anyone please tell me where is my mistakes?

Attachment(s): 

Last Edited: Sat. Oct 6, 2018 - 04:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What do you mean by ‘doesn’t go well’?

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

like the first digit counts to 9 then it is starts again from 0 till it reaches 9 again then 0 then the next digit becomes 8 and it stays like that for ever.

 

Last Edited: Sat. Oct 6, 2018 - 09:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What have you done to narrow down the problem?

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

i'm still trying to modify the code.

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

I wrote the right code, thank you for you interests.

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

I just changed the delay time form 1ms to 10ms and it worked properly.

but i still don't know what is the explanation.

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

How fast can the human eye see and interpret what is displayed? Updating 300 times per second is beyond what the human can decode. 30 times per second is more likely.

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

Stop opening more threads asking the same question!
I will lock this one and keep thr other one open

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Topic locked