why is this not working ?

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

hello,

 

i'm busy to make a stepper motor acc. on a simple way whit ussing array's.

its working correctly on the simmulator, but i don't see the pulse on a logic analyzer witch i expected.

do someone see wat is going wrong ?     thank you

#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>

#define F_CPU 1000000UL // 1 MHz int osc

int count;

ISR(TIMER0_OVF_vect)
{
    count = count + 1; // = 256 uS
}

int acc_stepper_time [10] = {400, 800, 1200, 1600, 2000, 2400, 2800, 3200, 3600, 4000,}; // 10 steps, every step = about 100 mS
int acc_stepper_speed  [10] = {64536, 64636, 64736, 64836, 64936, 65036, 65136, 65236, 65336, 65436}; // just numbers for example
int acc_stepper = 0;

int pulse = 0;
int temp1 = 0;

int x = 0;

int main(void)
{
    DDRA = 0b0000011;
    
    DDRB = 0b00100000;
    
    DDRF = 0b00000111;
    PORTF = 0b00000000;
    
    PORTF |= (1 << PINF0); // DM556 = 1 = disable
    
    sei();
    
    while(1)
    {
        PORTF &= ~(1 << PINF0); // DM556 = 0 = enable

        TCCR0 |=  1<<(CS00); // prescaler = 1
        TIMSK |= 1<<(TOIE0); // over flow interrupt enable        
                
        for (pulse = 0; pulse < 1000; pulse ++) // number of pulses i need
        {
            x = acc_stepper_time [temp1];
            
            if (count < x)
            {
                TIFR|= (1 << TOV1); // make overflow flag "0" yes you have to write a "1"
                
                PORTB |= (1 << PINB5); // pulse to stepper driver = 1
                _delay_us(5);
                PORTB &= ~(1 << PINB5);    // pulse to stepper driver = 0
                
                TCNT1 = acc_stepper_speed [temp1];
                TCCR1B |= (1<<CS10);  // prescaler = 1 = start timer
                
                
                while ((TIFR & (1<<TOV1))==0);
                TCCR1B &= ~(1<<CS10);  //prescaler = 1 = stop timer

            }

            else
            {
                if (temp1 < 10) // to check ore we stay in the range
                {
                    temp1 = temp1 + 1;
                }
                else
                {
                    temp1 = 0; // if we are not in the range
                }                            
            }
            
        }
    }
}
Last Edited: Sun. Oct 20, 2019 - 10:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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: 1

The issue here is volatile. "count" MUST be. (so I've no idea how this worked in simulation?)

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

yes i have already a thread, but i'm still struggle a with the code. i like the set up that i made, its short en simple. 

i don't want a complicate stepless code, it's overkill for my application.

and i want to know why this is not working, its a principle case for me now smiley

 

i tried it whit the volatile, but that was also not working, but i gonna add it any way.

 

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

You wrote:

    DDRA = 0b0000011;

It may not actually matter (or be the bug) but I expect to see 8 bits to make up a byte not just 7.

 

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

i'm busy to make a stepper motor acc

WHAT?  Are you just accumulating how far your motor has traveled?  

You don't bother anywhere to explaining what you are wanting to do:

 

i'm busy to make a stepper motor acc. on a simple way whit ussing array's.

its working correctly on the simmulator, but i don't see the pulse on a logic analyzer witch i expected.

do someone see wat is going wrong ?     thank you

     Yes, it is evident!

 

Why do you have an ISR that increments count, that seems to be used nowhere?  Before writing a bunch of twisted code, put on paper what you want to do & how it will work.  Then post it.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Oct 20, 2019 - 03:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes that is a silly mistake 7 bits in a byte blush , strange that its past the compiler, thanks.

 

count is using: 

if (count < x)

count is increase in the overflow interrupt from TIMER0, every count = 256 uS lets say 250 uS.

the first value in the acc_stepper_time array = 400. 400x250 us = 100 mS. 

in that 100 mS i want to made pulses for the stepper driver.

so output pin "1" and outputpin "0"

and than i wait a time who is depending on the value of array acc_stepper_speed.

 

thats it

 

edit: i see now that i have to make some were count 0

Last Edited: Sun. Oct 20, 2019 - 04:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

trixo wrote:
strange that its past the compiler

Not at all - leading bits will be assumed to be zero.

 

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

trixo wrote:
strange that its past the compiler, thanks.
That's a bit like it's strange the compiler accepts 123 as well as 123456. 0b1 is perfectly valid for example. So is 00000037.

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

Do you have 2 hardware timers available?  That would make your life much easier.  You need 2 timings:

 The high speed stepping pulses at various rates

 The much slower timing to ramp up the rates of the high speed pulses.

While it is possible to combine all these on one timer, it might mean timer 2 to be based off a changing timer 1 timebase...not as simple, especially for debugging.

 

Another approach is to use one constant high speed timer & base both needed timings (use software timers) off that one by looking at the counts for both (either by tcnt comparison or a match detect irq).

 

However, using two hardware timers gives you the ultimate flexibility...you can completely readjust one & not worry about affecting the other.

 

First, put on paper your scheme/method/idea (NOT CODING) & check it thoroughly for weakness & troubles...then code it.

  

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Oct 20, 2019 - 07:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
WHAT?  Are you just accumulating how far your motor has traveled?

 

I read acc to mean accelerate - the clue is in the companion thread (linked in #2) where:

trixo wrote:
i'm trying to make a stepper motor acc & dec.

 

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

clawson wrote:
So is 00000037.

But beware of 037 ...

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

awneil wrote:
But beware of 037 ...
Sorry but how are the octal constants 0000037 or 037 actually any different? They are both 31 or 0x1F or 0b11111.

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

The catch is not to realise that 037 is an octal constant - not decimal - but 37 is decimal.

 

It's a (the?) case where a leading zero does make a difference.

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

With hindsight I wonder if 0x000037 would have been a better example? I was just trying to point out that leading 0's are never important so the

0b0000011;

in question here might just as well have been:

0b11;

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

leading 0's are never important so the

Well they are, as someone tooted, if you are denoting octal (Octal is spoken around here)....wonder what happens when sent through a system that "helps" by stripping out leading zeros!!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

clawson wrote:
I was just trying to point out that leading 0's are never (sic) important

I was just pointing out the one case where they are important.

 

Yes, I have been caught by that one!

 

But, of course, when you've got a 0x or 0b prefix there is no ambiguity - so leading zeros are entirely insignificant.

 

EDIT

 

Although, in the Octal case, the number of leading zeros - one or more - is insignificant.

 

Which, I guess, is Cliff's point in #13 ?

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...
Last Edited: Mon. Oct 21, 2019 - 09:38 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Whatever.

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

laugh

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

avrcandies wrote:

Do you have 2 hardware timers available?  That would make your life much easier.  You need 2 timings:

 The high speed stepping pulses at various rates

 The much slower timing to ramp up the rates of the high speed pulses.

t is a atmega128, whit more than 2 hardware timers, that's no problem.

 

avrcandies wrote:

While it is possible to combine all these on one timer, it might mean timer 2 to be based off a changing timer 1 timebase...not as simple, especially for debugging.

 

i don't now wat you mean here, my english is not that good, thank you.

 

 

 

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

t is a atmega128, whit more than 2 hardware timers, that's no problem.

Good then keep it simple....use first timer to generate the stepping pulses, the stepping rate being adjustable by a variable (call it cat)

 

Use the second timer to adjust the variable (cat) at so many cat counts per each second timer tick....Or use a lookup table here to set cat if it is not a linear ramp.

 

Now you can relax and have a hot dog.   

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

but i'm, already using 2 timers, TIMER0 and TIMER1 smiley