Connect with us

how to write c code for motor

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Apr 9, 2012.

Scroll to continue with content
  1. vead

    vead

    473
    14
    Nov 27, 2011
    Hello,
    How to write c code for motor
    if motor move forward
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    11,009
    2,506
    Nov 17, 2011
    main()
    {
    motor_on();
    }

    This could work. Depends on how motor_on() is implemented.

    O.k., that was the fun part. You have to supply a lot more information on your project:
    - What kind of controller do you use?
    - What kind of motor do you use?
    - How is the motor attached to the controller?
    - Do you have any libraries for motor control you might use?

    Harald
     
  3. jackorocko

    jackorocko

    1,284
    1
    Apr 4, 2010
    Code:
    main()
    {
    motor_on();
    }
    That didn't work, thought you where smarter then that? :D
     
  4. vead

    vead

    473
    14
    Nov 27, 2011
    thanks everybody
     
  5. jackorocko

    jackorocko

    1,284
    1
    Apr 4, 2010
    vead, on a serious note, do you understand what harald is trying to say? You really left us with no way to help you. Did you get it figured out or are you still stuck? If you still need help we will need you to answer haralds questions.
     
  6. vead

    vead

    473
    14
    Nov 27, 2011
    hi jackorocko i am really sorry if i did make any mistake . i am new in embedded c . i have not join c class i want to learn myself. so first thing came in my mind how to make c program for
    microcontroller. if i should get basic of embedded c then i can make small program for system so first i decide to learn simple program as motor move forward ,LED turn on off

    actually i am searching platform of embedded c which is use in every embedded system

    and again sorry for my bad English


    thanks
     
  7. jackorocko

    jackorocko

    1,284
    1
    Apr 4, 2010
    Well my best advice is to search google for code in the language you want to learn. Reading and playing around with code that already works is a great way to learn the language.

    what type of micro-controllers have you looked at? If you are interested in the PIC's. Sorry, it is in english. http://www.gooligum.com.au/tutorials/baseline/PIC_Base_C_1.pdf

    edit: I had to remove the first link, the author has decided to charge for his material and advertising for commercial profit is prohibited on these boards.
     
    Last edited: Apr 11, 2012
  8. Harald Kapp

    Harald Kapp Moderator Moderator

    11,009
    2,506
    Nov 17, 2011
    jackorocko is right.
    My personal opinion: Programming in embedded C is structurally not too different from programming in C on a PC. The BIG differences are:
    - in the embedded world your ressources are very limited (little memory, slow CPU, mostly no floating point unit, little mass memory etc.). Mind that this is a very general statement. I know very well that there are embedded CPUs with floating point and even with lots of memory etc.
    - in the embedded world you have direct access to a lot of low level periphery. For example you can read a single input pin of the CPU or you can set a single output pin of the CPU. Both possible in the PC world only with lots of effort.
    - in the embedded world you often have to deal with real-time requirement´s, menaing that you may have to guarantee a rsponse of the CPU to an input event within a limited (and mostly well defined´) time frame.

    So if you want to control a motor, before you start writing your first line of code you need to know:
    - How is the motor connected to the CPU?
    - Which pins of the CPU (or maybe a peripheral chip) control the motor? What logic levels are required for "forward"?
    - How is the program informed to start and stop the motor (pushbuttons? other events?)

    For starters you may have a look at the arduino platform (which doesn't mean that jackorocko's suggestion of a PIC is not a good idea). The arduino comes with lots of libraries and so called "shields" for different I/O purposes. Unfortunately that's about all i know about the arduino.

    Harald
     
  9. vead

    vead

    473
    14
    Nov 27, 2011
    hi Harald and jackorocko
    you have both done very great work for me thank you very much its very big help for me

    i have tried to write some code please check me

    The robot move over pipe. The IR sensor is used to sense The pipe when infrared signal fall on white surface its gets reflected and if it fall on black surface it is not reflected

    #include<pic.h>
    #define sensor_3
    #define motor1_4
    #define motor2_5
    main()
    {
    DDRA=OXFF; //port A as input
    DDRB=OXFF; // port B as output
    if(sensor==1); //sensor on
    {
    (motor1=motor2=1); //motor run forward
    }
    if (sensor==0); //sensor off
    {
    (motor1=motor2=0); //motor stop
    }
    }


    i don't know how to set low and high pin
    i hope you will reply

    thanks
     
    Last edited: Apr 12, 2012
  10. Harald Kapp

    Harald Kapp Moderator Moderator

    11,009
    2,506
    Nov 17, 2011
    I'm not versed in PIC programming. However, there should be a command simiar to
    PORTB = <value>;
    where <value> is an 8 bit data that defines the states of the port bits B0..B7.
    For example:
    PORTB= 0x01;
    will set B0 = high, B1...B7 = Low

    This seems easy, but beware: there is a pitfall here: The command as written above will set all port bits to low or high according to the bit pattern of <value>, disregarding your intent that you probably may want to set a single port bit only. To achieve this, you have to first read the current state of the port bits, change the one bit you want to be set or reset and write the modified port bits.
    To set a single bit you use the OR function, ORing the bit pattern of the port with a bit maskt that sets the single bit you want to change, but leaves the rest unchanged:
    TMP = PORTA; /* read port bits */
    TMP = TMP | 0x01; /* set bit 0 to 1, note: unchanged bits are 0 in the bit mask*/
    PORTA = TMP; /* write new port bits */
    To reset a single bit you use the AND function. ANDing the bit pattern of the port with a bit mask that resets the single bit you want to change, but leaves the rest unchanged:
    TMP = PORTA; /* read port bits */
    TMP = AND | 0xFE; /* reset bit 0 to 0, note: unchanged bits are 1 in the bit mask*/
    PORTA = TMP; /* write new port bits */

    You have to know which port pins your motor is attached to and what logic levels are required to start and stop the motor.

    Harald
     
  11. vead

    vead

    473
    14
    Nov 27, 2011
    for example
    i have two port
    port A as input
    port B as output
    port A pin A0 A1 A2 A3 A4 A5 A6 A7
    port B pin B0 B1 B2 B3 B4 B5 B6 B7

    output of sensor connected to A4
    motor1 connected to pin B2 B3
    motor2 connected to pin B4 B5

    logic level
    sensor motor1 motor2
    low low low
    high high high

    if sensor low motor stop
    if sensor high motor move forward

    how to write statement
    DDRA=OXFF; // port A as input
    DDRB=OXFF; // port B as output
    PORTA=...?; //set sensor low or high

    i am confused here how to write statement for input output port and to set high and low pin
    how to write hexa code
     
  12. Harald Kapp

    Harald Kapp Moderator Moderator

    11,009
    2,506
    Nov 17, 2011
    You're not supposed to "set sensor low or high". The sensor is an input and should be read by a statement like
    Sensor = PORTA & 0x08 /hex 08 = 0000 1000 -> only bit A4 is relevant, all other bits in "Sensor" are set to 0.
    Similarly setting port pins on PORTB follows the scheme I wrote in my previous answer. Please re-read.

    About Hex-Code you can read up here: http://www.theproblemsite.com/codes/hex.asp

    May I suggest you first get some experience with writing C code at all before you tackle embedded programming? To me it looks like your fighting at two fronts at the same time. Get a grip on C-programming first, then apply your knowledge to embedded programming and learn the specifics.

    Otherwise you may have a look at this course: http://www.swarthmore.edu/NatSci/echeeve1/Ref/C for PIC/C_Intro.html

    Harald
     
  13. timothy48342

    timothy48342

    218
    1
    Nov 28, 2011
    vead, make sure your not mixing up the upper case letter "O" and the number "0". (zero) Even though they look similar on the screen, the compiler treats them different. 0xff is number. Oxff doesn't mean anything.

    The define command doesn't use an equal sign, so:
    #define PORTB 0x01
    ... will set that value at compile-time. (The pre-compiler swaps out "PORTB" with "0x01" in one of the first compiler passes.)

    When setting variables at run-time, code like this:
    motor1=motor2=1; //or even like this (motor1=motor2=1);
    will set both variables motor1 and also motor2 to the value 1.
    A lot of people won't know that, so it good to seperate those out for clarity.
    motor1=1;
    motor2=1;

    When posting c-code it is good to put some spacing before some lines to make it easier to read and put [ code] before your code and [/code] after so that the message board keep the spacing, like this:
    Code:
    #define DO_IT 1
    #include <hello.h>
    void main()
    {
         if( DO_IT > 0 )
         {
              //it will always do my function
              Do_My_Function();
         }
         //thats' all
    }
    

    Since I don't know much about microcontrollers. Which one is this that you guys are talking about? (So I can follow along and learn.) Would I look up "PIC microcontroller" or something else?

    --tim
     
  14. jackorocko

    jackorocko

    1,284
    1
    Apr 4, 2010
    to add to what harald has said. Also, I take it you are using a AVR chip, Or don't you have a clue?

    Code:
    DDRA = 0x00; //set all porta to input
    DDRB = 0xFF; //set all portb to output
    
    sensor = 0;
    
    while(1) //infinite loop because it is always true
    {
        sensor = PORTA & 0b00000100;
    
        if(sensor == 0b00000100) //if sensor is 'HIGH' turn on motor 1
            PORTB = 0b00001000;
    }
    
     
  15. Harald Kapp

    Harald Kapp Moderator Moderator

    11,009
    2,506
    Nov 17, 2011
    From
    #include<pic.h>
    I assumed vead is using a PIC microcontroller. The DDRA and PORTA statements, on the other hand, look a lot like AVR.

    I like that binary notion 0b00001000 instead of 0x08 because in this case it is a much more obvious solution for representing the bit mask. I didn't know that this notion is available in C (I use 0x notation all the time).

    Since the motors are connected to 2 port pins each, both port pins need to be set and reset, not just one. Depending on the exact circuit, which vead hasn't disclosed to us yet, this could look like:

    void motor_1_forward()
    {
    int temp;

    temp = PORTB;
    temp = temp & 0b11110111; /* B3 = 0 */
    temp = temp | 0b00100000; /* B5 = 1 */
    PORTB = temp;
    }
    void motor_1_backward()
    {
    int temp;

    temp = PORTB;
    temp = temp | 0b00001000; /* B3 = 1 */
    temp = temp & 0b11011111; /* B5 = 0 */
    PORTB = temp;
    }
    void motor_1_stop()
    {
    int temp;

    temp = PORTB;
    temp = temp | 0b00101000; /* B3 = B5 = 1 -> same logic level on both pins means no voltage differential on motor */
    PORTB = temp;
    }

    Thes eroutines can be modified accordingly using other port pins for motor 2. From within the main program it is sufficient to call these routines.

    By the way: It is good coding styöe to "hide" the binary representations by using #define statements for the preprocessor. This also eases maintenance. If, for example, you connect the motor(s) to other pins you just redefine the bit pattern in the #define statement.

    Harald
     
  16. vead

    vead

    473
    14
    Nov 27, 2011
    hi everybody
    thanks giving long response
    I have tried again please check
    I am using avr


    algorithm
    If sensor is High -both motor move forward
    If sensor is Low- stop both motor

    PORT A A0 A1 A2 A3 A4 A5 A6 A7 output of sensor is connected to A4 pin
    0 0 0 0 1 0 0 0

    PORT B B0 B1 B2 B3 B4 B5 B6 B7 motor1 and motor2 connected pin 2345
    0 0 1 1 1 1 0 0

    [#include
    main()
    while(1)
    {
    DDRA=OXOO; //port A as input
    DDRB=OXFF; //port B as output
    PORTA=0b00001000; // set as high pin
    PORTA=0b00000000; //set as low pin
    {
    If (port A=00001000); //If sensor is high
    {
    (port B=0b00111100); // both motor move forward
    }
    If (port A=00000000); //If sensor is Low
    {
    (port B=00000000); //stop both motor
    }
    }
    }
    ]
     
  17. Harald Kapp

    Harald Kapp Moderator Moderator

    11,009
    2,506
    Nov 17, 2011
    I told you before: PORTA is an input, so the statement
    PORTA=0b00001000; // set as high pin
    doesn't make sense. You can read from PORTA, but not write to it.

    And nobody can tell you whether tis staement
    (port B=0b00111100); // both motor move forward
    works unless you post the schematic circuit how the motor is attached to the port pins.

    Harald

    Harald
     
  18. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,477
    2,820
    Jan 21, 2010
    It may do something though. It could tun on the pull-up resistors for that single input.

    But yeah, it certainly won't set a high value to an input (because that's kind of a meaningless assertion)
     
  19. Harald Kapp

    Harald Kapp Moderator Moderator

    11,009
    2,506
    Nov 17, 2011
    What I completely miss in the C-code presented is the inizialization of the AVR.
    You need to:
    - initialize the stack pointer
    - initialize the clock system (whick clock: internal / external, which frequency etc.)
    - initialize an other function blocks

    Maybe you just left this out of the code you showed us to keep it short. But it is required, See here http://www.jarkonkotisivu.org/AVRcoder/ for a little tool to assist in generating the necessayr settings (and it even generates C-code!).

    By the way, here http://iamsuhasm.wordpress.com/tutsproj/avr-gcc-tutorial/ is a tutorial on writing C-Code for the AVR controllers.

    Harald
     
  20. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    On the AVR, PORTA is an output register. If you want to READ the port, use PINA.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-