Define Atmega 8A pins in C programming

Discussion in 'Programmer's Corner' started by mohitjindal, Sep 16, 2012.

  1. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    Hi;I have a motor.c code in which pin 13 and 14 are set\used for clockwise and anti-clockwise direcion as original firmware. I want to use only one pin 14 for DIR and then set the values to HIGH and LOW for clockwise and anti-clockwise direction. :rolleyes:
    I attached the original code below.
    Please tell me how to use the lines below in original code.
    Code ( (Unknown Language)):
    1. #undef F_CPU
    2. #define F_CPU 1000000UL
    3. #include <avr/io.h>
    4. #include <util/delay.h>
    5. int main(void) {
    6.   DDRB = (1 << 2);
    7.   while(1) {
    8.     PORTB |= (1 <<2);
    9.     _delay_ms(200);
    10.     PORTB &= ~(1 << 2);
    11.     _delay_ms(200);
    12.   }
    13. }
    14.  
     
    Last edited: Sep 18, 2012
  2. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    Please someone help me. I want to fix the 10 lines above in the original motor.c code.So that the motor can rotate in both directions from only one Pin 14.
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Depends on the compiler you are using. What are you using?
     
  4. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    I am using avr studio 5.1
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    It still depends on which C compiler you are using. Some compilers allow you to create bit fields to address individual bits. Others have a predefined header that defines bit fields.
    Check the header files for things like PORTB.0 or PB0.

    Rather than messing around it is just as easy to address single bits as you have done.
    I would keep it simple.

    If pin-14 is PB2, here is your code modified to explicitly identify BIT2
    (but I think pin-14 is PB0).

    Code ( (Unknown Language)):
    1.  
    2. #undef F_CPU
    3. #define F_CPU 1000000UL
    4. #include <avr/io.h>
    5. #include <util/delay.h>
    6.  
    7. #define BIT2 2
    8.  
    9. int main(void) {
    10.   DDRB = (1 << BIT2);
    11.   while(1) {
    12.     PORTB |= (1 << BIT2);
    13.     _delay_ms(200);
    14.     PORTB &= ~(1 <<  BIT2);
    15.     _delay_ms(200);
    16.   }
    17. }
    18.  
    If pin-14 is PB0 and that is all you want to control, then do this:

    Code ( (Unknown Language)):
    1.  
    2. #undef F_CPU
    3. #define F_CPU 1000000UL
    4. #include <avr/io.h>
    5. #include <util/delay.h>
    6.  
    7. int main(void) {
    8.   DDRB = 1;
    9.   while(1) {
    10.     PORTB |= 1;
    11.     _delay_ms(200);
    12.     PORTB &= ~1;
    13.     _delay_ms(200);
    14.   }
    15. }
    16.  
     
    Last edited: Sep 18, 2012
  6. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    Thanks but i want to modify motor.c code. Yes the pin 14 is PB2 SS/OC1B.
    Can you please modify motor.txt ?
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    You have to define what is Motor_Port, Motor_Clockwise and Motor_Anticlockwise.
    They are controlling two bits to define clockwise and anticlockwise.
     
  8. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    Here is the full motor.h file:-
    Code ( (Unknown Language)):
    1. #include <avr/delay.h>
    2. #include <avr/io.h>
    3. #include <avr/iom8.h>
    4. #include <avr/interrupt.h>
    5. #include <ctype.h>
    6. #include "UtilsAndDefines.h"
    7. #define Motor_Clockwise  BIT2
    8. #define Motor_Anticlockwise BIT1
    9. #define Motor_Port    PORTB
    10. #define Motor_Dir    DDRB
    11.  
    12. /**
    13. [EMAIL="*@brief"]*@brief[/EMAIL] simply control PWM for the motor.\n
    14. The pwm must be givem with values between 0 and 2000.\n
    15. The 1000 is the motor stopped. The 0 is the 100% PWM rotating Anticlockwise and the 2000 is the 100% PWM rotating Clockwise
    16. The PWM has increments of one step with 1000 steps of resolution.
    17. [EMAIL="*@var"]*@var[/EMAIL] pwm This is the pwm to set on the motor
    18. [EMAIL="*@return"]*@return[/EMAIL] 2 if rotating clockwise.\n
    19. 1 if stopped.\n
    20. 0 if anticlockwise.\n
    21. -1 if fail or bad value or limit reached.
    22. */
    23. int8_t SetPWM(uint16_t pwm);
    24.  
    25. /**
    26. [EMAIL="*@brief"]*@brief[/EMAIL] Init the motor low level control, on this case pins, timers, pwm variables, etc.
    27. */
    28. void InitMotor(void);
    29. uint16_t PWM;
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    There you go. So they are using BIT2 and BIT1. You can modify that to whatever you wish.
     
  10. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    Please modify it to use pin 14. OC1B PB2.
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    You need two pins, not one.
     
  12. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    I want only 1 pin 14 because my dc motor shield has only 1 DIR pin.
     
  13. mohitjindal

    Thread Starter Active Member

    May 25, 2009
    49
    0
    pin14 is PB2 (ie BIT2)

    I want BIT2 to go to 0 instead of 1
     
  14. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    the way i read it, you need to set value to pwm, interrupt handler is controlling direction pins so your code should NOT touch them.

    Code ( (Unknown Language)):
    1. @brief simply control PWM for the motor.\n
    2. The pwm must be givem with values between 0 and 2000.\n
    3. The 1000 is the motor stopped. The 0 is the 100% PWM rotating Anticlockwise and the 2000 is the 100% PWM rotating Clockwise
    4. The PWM has increments of one step with 1000 steps of resolution.
    5.  
    6.  
    so you need to assign value to pwm that is in 0-2000 range, something like

    Code ( (Unknown Language)):
    1. #undef F_CPU
    2. #define F_CPU 1000000UL
    3. #include <avr/io.h>
    4. #include <util/delay.h>
    5. int main(void) {
    6.   // DDRB = (1 << 2);
    7.   while(1) {
    8.    pwm=500;
    9.     _delay_ms(2000);
    10.     pwm=1500;
    11.     _delay_ms(2000);
    12.   }
    13. }
    also, you don't really expect motor to change direction 5 times in a second, do you? 200ms is way too short to get motor running, it would only twitch and hum in one place, so i just changed it to 2 seconds forward and 2 second reverse.

    as for using one bit to control direction, that should not be problem (unless you are not telling something) because only one of the two bits is high. if the shield expects one bit, and ignores other, you are all set.
     
Loading...