Atmega328p/Stepper Motor/ C Language

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

hi

Last Edited: Tue. Jun 27, 2017 - 01:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm confused. You've written a ton of words that collectively don't make much sense.
The gcc compiler is generic - if you want to use AVR, you use avr-gcc, if you want to use ARM, you use ARM-gcc. I could go on with mips, tensilica,x86,pdp11 and so on. arduino uses Avr-gcc,arm-gcc and a couple of others depending on the actual target cpu.
Why can't you upload the whole code to the microcontroller?
Sit down and think carefully about what you want to achieve. What you've written so far is chicken scratchings that don't make make much sense (to me anyways).

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

 

Since everything should be in pure C language I skipped the Arduino IDE

 

And what's stopping you doing that with the Arduino IDE?? You don't have to use anything Arduino if you don't want to and use "pure C".

 

I have to use GCC compiler and avoid using AVR-GCC

???

 

It seems that you are confused and so are we, please break your LOOOOONG post with paragraphs and spaces to make it readable.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 I just need to upload those lines of code which sets the pins of the microcontroller as outputs, 
 sending high and low signals and delay between the signals. 

For AVR,  you can refer to the registers to set I/O direction. For example:   DDRD != (1<<myIOpin);  makes the pin output;   DDRD &= ~(1<<myIOpin) makes it an input.

Delays are found in the delay.h file.   usually  _delay_ms(12);

 

Won't you be doing a lot of math for the stepper motor calculations, such as acceleration and deceleration curves?   How much weight is the stepper motor moving?  How fast will the item attached to the stepper be moving?

 

I suggest that you buy a clone Arduino UNO for $6 on eBay, or beg, borrow, steal one if you need to get the development process happening now.   Arduino programming is relatively easy to learn.  Then when everything is working OK with the new stepper motor controller, transfer the working .HEX file into an Arduino Pro-MINI.  The Pro-MINI is just a surface-mount AVR Mega328P on a little breakout board that costs about $3.  The on-board Mega328P has a bootloader that takes about 1-2K of flash.

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

I modified my original post a bit. 

 


 
Last Edited: Tue. Jun 27, 2017 - 01:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is getting very painful!  Are you using Atmel Studio at all or are you using command line stuff?

how can I upload those lines of code to the microcontroller once they are part of the bigger code. 

with the REST of the bigger code once it gets compiled.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

You've not adequately explained what relation the 'bigger' code has. If the microcontroller has no use for the 'bigger' code, why would you compile it with your avr specific code?

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

I'm using command line stuff. The microcontroller has use for the bigger code. As I said in my first post I'm trying to collect data. Let's say I'm hooking a sensor to the motor and the motor moves and the sensor gets some data and sends them to a function in the big C code and that function do some calculation and send the results to another function and so on. Also, I mentioned that the motor is controlled with the Arduino and the motor controller which means that the Atmega328p which is mounted on the Arduino board sends signals to the motor controller and the motor controller sends current to the coils of the motor and motor moves. So whenever the motor is need to be moved the Atmega328p microcontroller should communicate with the controller.

Last Edited: Tue. Jun 20, 2017 - 01:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm still yet to understand what the core problem is - you have 'big' code and you want to incorporate some 'small' code and load it into the avr using command line tools. Can you load parts of the program separately into the AVR? technically yes, but it might be more trouble than it is worth. Much easier to build the complete code at load it all at once.

Last Edited: Tue. Jun 20, 2017 - 03:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have read this thread twice and am still "non-the-wiser".

 

So...  please provide a sketch of your physical arrangement (motor, Uno, mega328, any other processor(s))

 

I have a sneaking suspicion that you have more than  one microcontroller in your design.

 

Ross McKenzie ValuSoft Melbourne Australia

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

You dont, under normal circumstances, upload just bits of what should be in the AVR flash program memory. You upload all of the contents, every time. This is normal an nothing to try to avoid.

Build your whole AVR app. Upload the whole thing. Then, even if a change is only a few lines, you build it all again , and upload it all again. Rinse, repeat until satisfied...

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Jun 20, 2017 - 02:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My guess is you are having problems controlling the motor, and having the "big C code", run as desired. The motor code is using delays, and they are blocking. Blocking means doing nothing waiting for time delay. Using timers for delays, and short ISRs, is a solution. Such a solution is well planned, and involves structure, to initiate tasks, and complete parts in available time, without interference.

It all starts with a mental vision.

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

Yes, I think the answer is "run the motor code in a timer interrupt". That is what I always do!
 

Bob.

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

edi.ptb wrote:

5) So here we have a main function and we upload the code and everything works fine. Now lets put those line into the bigger C code which has a main function and lots of other functions that talk to each other.

6) My question is that how can I upload those lines of code to the microcontroller once they are part of the bigger code. 

I think these two items are the core of the problem, but as everyone else I am not quite sure what you are actually asking for. I may be wrong, but I do get the feeling that you have not fully grasped all the fundamentals of programming C for AVR/Arduino. In general, you compile and link all the code into one single application/program/binary/HEX and then you program that into the AVR/Arduino or whatever MCU you are using (EDIT: one exception is boot loaders, but that is a whole different discussion).

 

So if you have "big C code" and want to incorporate "some lines", you inevitably need to modify the "big C code" so that your new lines are executed.

 

You say that the big C code has a main function. Is it a loop? Can you just insert those lines somewhere in the main loop, so that the lines get executed in each pass? That would be a start.

 

However, you need to consider that you now effectively have multiple tasks that are executed sequentially. Once this happens, you should consider writing code for each task without using calls to delay_us and similar. Each task should execute as quickly as possible, and most importantly without any blocking calls. If you need to incorporate delays in your task, you should implement this using a finite state machine in conjunction with a built-in hardware timer.

 

All this may sound complicated, but in fact it is not.

/Jakob Selbing

Last Edited: Wed. Jun 21, 2017 - 07:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jaksel wrote:
In general, you compile and link all the code into one single application/program/binary/HEX and then you program that into the AVR/Arduino or whatever MCU you are using (EDIT: one exception is boot loaders, but that is a whole different discussion).
 Thats what I'm doing. If you look at the commands that I wrote in earlier posts you can see that I have a C source code,then an .O file then a.HEX file, and this .Hex file is uploaded to the microcontroller. What I'm trying to say is that once I implemented these lines of code into the big C code I have no longer have a separate C source file that I can upload it to the microcontroller and it doesn't make sense if I upload a 2000 line code to the microcontroller. What I'm trying to accomplish is that how can I interface the big C code with the microcontroller, i.e, when the code reaches those lines that are for the microcontroller how can I talk to the microcontroller.

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

Again. You ALWAYS upload the entire program, in the form of the complete hex from a build, to the AVR. That's just how it works.

> and it doesn't make sense if I upload a 2000 line code to the microcontroller.
Making sense to you or not, that's the way it works. Change one line, just one line, of C code. Then you build the complete hex. Then you upload the complete hex. It takes a few seconds - is that a problem?

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Thu. Jun 22, 2017 - 03:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@edi.ptb: Where is the 2000 line code running?

Are you trying to interface a program executing on a PC with a program on the microcontroller?

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

It will be executed on a PC. I rather have one C source file. But If I can somehow create a separate c source code for the microcontroller and link it with the big C source code it's not a problem. 

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

edi.ptb wrote:

It will be executed on a PC. I rather have one C source file. But If I can somehow create a separate c source code for the microcontroller and link it with the big C source code it's not a problem. 

 

Now we're getting somewhere.

 

So, you have a program, written in C, which runs on a PC, either as a command line program or with a GUI (it doesn't matter which), and you want to send commands from that to a separate program which is running on an Arduino which in turn controls a motor. There are also some sensors connected to the Arduino and you want the readings from those to be sent back to the PC.

 

Am I correct?

 

Now to answer your question...those two pieces are separate pieces of code and need to remain that way. You will end up with two source code files.

pragmatic - dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

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

You will have to have some form of communication between the PC and the uC. When the PC gets to the place where the uC should do something, it would send a command over that communication channel to the uC to execute according to the received command from the PC.

 

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

You are right. The communication between the PC and the Arduino is done over the serial port. But I'm not sure if one can control individual pins over the serial port.

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

Rather than talking "big program" and "little program", lets get some terminology that folks can understand, a little better. From your most recent posts, it sounds like you have one program that runs on a PC. You have apparently been calling this "big program"; most, around here, would call that "host" or maybe "master".

 

Then, you have a so-far-undefined "communications link" that joins the host to a motor controller board that (apparently) has an AVR on it. 

 

The motor controller would logically be called "slave" (a few less common terms might be "client" or possibly just "controller"). 

 

Now, the host does not really care what is at the other end of the link, so long as it responds in expected ways to commands. It can have any compatible program and have any logic (it does not even have to be a microcontroller). It is programmed independently from the host. There is no need to change it when you change the host program, so long as they both use the same protocol over the link [nb: if you change something in the host that changes the protocol, THEN you do, indeed, need to change the slave program]. We might call all of this "system architecture", or a plan/design for how the system is supposed to work. This architecture (master, slave, and communication link) is not at all uncommon, being used for a wide range of things from In System Programmers to radio boards, display boards, and, yes, motor control boards. And, almost always, the programming of the slave is totally independent of programming for the master.

 

So, maybe it is time to step back for a minute and tell us, again, using some of the new terms just provided for you, what is the system architecture you are trying to achieve. How far are you? What are you using, now, to program the slave ("command line" tells us nearly nothing)? What problems do you have? 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Jun 22, 2017 - 07:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let me explain thoroughly what I'm trying to do. 


 
Last Edited: Tue. Jun 27, 2017 - 01:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I understand what you just wrote, is the Arduino the master and the motor driver, itself, the slave? What role, if any, does a PC have in this?

 

OK, so what is the question about programming and "big code" vs "little code"? Is big code in the Arduino? Are you objecting to updating the entire Arduino code when you change a subroutine? Hint: if you change a subroutine and it involves adding even (just) 1 byte, then the whole part of the program that follows that changed part has to be moved to make room for that new byte. Thus, the whole thing has to be recompiled and the whole thing has to be reloaded into its target.

 

By the way, words are important. It is very difficult if people who are trying to communicate use words that the other parties either don't understand or have different definitions for. You may be absolutely clear about what you are trying to convey, but that can be totally lost if others don't understand the words in the way you are trying to use them. This is not a criticism - it is only an attempt to encourage language that more of us can understand! For example, you could explain it in perfect and comprehensive Swahili, but if the others you communicate with don't know Swahili, the communication attempt is for naught. The same can be true (and, in this list, some times actually IS true) between those of us who use American English and those of us who use British English (or what ever the preferred name is). 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Jun 22, 2017 - 08:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
If I understand what you just wrote, is the Arduino the master and the motor driver, itself, the slave? What role, if any, does a PC have in this?

I think you know that the Arduino is connected to a PC via USB.

ka7ehk wrote:
OK, so what is the question about programming and "big code" vs "little code"? Is big code in the Arduino?

Imagine that you open a Microsoft visual Studio, Code:Blocks, or a text editor (and not the Arduino IDE) and you just start writing a C code and somewhere in your code you wish to move a motor and you have to talk to the arduino to tell it send these signals to the motor driver and when you are finished writing your code you just compile it and everything should be working as it should be. 

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

So I am still confused. You have a PC that communicates with an Arduino and the Arduino translates those commands into ones the motor controller can understand? If so, that makes the naming a bit more complex, but I think that we are finally getting closer to understanding.

 

Your response to the question about big code and little code is still a bit puzzling. Is the big code what runs on your PC? Or, is it what runs on the Arduino? You write about Code:Blocks and MS Visual Studio or a text editor (but not Arduino IDE). Are these used to create and modify the PC code or the Arduino code? Then you say "you just compile it and everything should be working as it should be". Compiling is different from the process to get it into the code's target (unless that target is the PC where it was compiled). In the embedded world, those are (almost always) two totally different processes. By design.

 

Again, which code, PC, or Arduino? And, again, please, clarify "big code" and "little code". 

 

Cheers,

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Jun 22, 2017 - 09:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It looks like the original code was for msdos as it bit bashes the printer port to generate the motor step signals.
Since we have no idea of the bigger picture, it is difficult to give a clear direction.
You might want to look at grbl (google that) and see if this might help.

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

Ok, i've had a better look at the code you posted. You seem to have to right idea on converting the printer port stuff to AVR ports. 

Your explanation is highly confusing to a number of us here. Currently my interpretation is:

1.There was a msdos system with some stepper motors and drivers that used the printer port.

2. the code was in C and you want to convert that to run on an Arduino

3. It looks like you've done some conversion for the code to use AVR ports rather than the PC printer port

 

So what is the specific problem?

There's some printf() and gotoXY which make sense on a PC in order to print and move the screen cursor, but on an Arduino you have no screen, so this doesn't make much sense.

Last Edited: Thu. Jun 22, 2017 - 11:15 PM