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] [Moved] Want help to use the counter to count the pulses for AT MEGA 2560.

Status
Not open for further replies.

ismbn

Full Member level 3
Joined
Feb 11, 2012
Messages
160
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,308
Location
Mumbai. india
Activity points
2,444
Hi All
I am working on AT MEGA 2560.
In My application I need to count the Pulses for the machine cycle use the 16 bit counter in the AVR.
the problem is that I am not able to configure the timer as well as counter. tried to write the code but I failed.
attaching the code so please have a look in this and tell where i am wrong

also please help we with some hint to configure the counter also........

Here is the code for timer using interrupt:

Code:
#include<avr/io.h>
#include<avr/interrupt.h>
int main (void)
{
// Prescaler = FCPU/1024
   TCCR0B|=(1<<CS02)|(1<<CS00);

 //Enable Overflow Interrupt Enable
   TIMSK0|=(1<<TOIE0);
   sei ();
    //Initialize Counter
   TCNT0=0;	
	DDRH=0xff;
	DDRG=0xff;
	PORTG=1;
	while(1);;
}
char count=0;

ISR(TIMER0_OVF_vect)
{
PORTG= 0xf;

//Increment our variable

count++;
  	if(count==60)
   	{
   	PORTH=~PORTH; //Invert the Value of PORTC
   	count=0;
   	a==0;
	}
}


Thanks Regards
Ismail
 

Re: Want help to use the counter to count the pulses for AT MEGA 2560.

Your code is using timer0 which is a 8bit counter.
Use timer1,3,4,5 which are 16bit counters
 
  • Like
Reactions: ismbn

    ismbn

    Points: 2
    Helpful Answer Positive Rating
Re: Want help to use the counter to count the pulses for AT MEGA 2560.

Oh Thanks for the answer sir;
i will do this:
can you tell me how to count the pulses
 

Re: Want help to use the counter to count the pulses for AT MEGA 2560.

May I ask what you are trying to do with this count?
You said "count the Pulses for the machine cycle", what do you mean?

- - - Updated - - -

Are these external source pulses?
Are they at a level appropriate for the AVR input?
 
  • Like
Reactions: ismbn

    ismbn

    Points: 2
    Helpful Answer Positive Rating
Re: Want help to use the counter to count the pulses for AT MEGA 2560.

sorry for the stupid way of questioning but i am new.

actually I am working on a machine in that there r 4 to 5 motors. considering the motor by which the arm of the machine is moving.
so if i want to move the arm to 5mm. they are using some calculation. when i asked they (my seniors) are saying that it counts the pulses...
I asked that what types of pulses it is ? Then they start waking in some garden where my mind is not reaches.

So sir is there any types of pulses that we can count to use in this application????;
they are taking the reading of full scale and then dividing it by the value to get the correct result

ismail
 

Re: Want help to use the counter to count the pulses for AT MEGA 2560.

If we assume that there is a device used with the motor that gives digital pulses (for example per rotation) then I think you can use one of the timers set to count on pulses so instead of using the AVR clock as the clock source you set it to T1 rising or falling edge and use the Tn pin, for each pulse the timer counter will increase by 1.

If I understand correctly then time is not important , all you want is to know when the pulses have reached a predefined number (for example 100 pulses), if so then you can set the compare match of the timer to that value and wait for the interrupt.

Another method is to use external interrupt, each pulse will give an interrupt and you can increment a variable by 1 until the time you reach the pulse count you expected.

Alex
 
  • Like
Reactions: ismbn

    ismbn

    Points: 2
    Helpful Answer Positive Rating
Re: Want help to use the counter to count the pulses for AT MEGA 2560.

sir exactly this is the thing i want to know. thanks for the precious help..
I will try to do this now. If got any trouble i will get back to... can I ?
because my coding is not so good....
 

its working gr8 sir i am using interrupt method for counting the pulses... but i wont be able to check practically right now.. i cheked on protious only.
for now i thing it will work and moving forward...

Thank You for the gr8 Help

Ismail
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top