Define Atmega 8A pins in C programming

Thread Starter

mohitjindal

Joined May 25, 2009
49
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.
Rich (BB code):
#undef F_CPU 
#define F_CPU 1000000UL 
#include <avr/io.h> 
#include <util/delay.h> 
int main(void) { 
  DDRB = (1 << 2); 
  while(1) { 
    PORTB |= (1 <<2); 
    _delay_ms(200); 
    PORTB &= ~(1 << 2); 
    _delay_ms(200); 
  } 
}
 

Attachments

Last edited:

Thread Starter

mohitjindal

Joined May 25, 2009
49
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.
 

MrChips

Joined Oct 2, 2009
30,821
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).

Rich (BB code):
#undef F_CPU 
#define F_CPU 1000000UL 
#include <avr/io.h> 
#include <util/delay.h> 

#define BIT2 2

int main(void) { 
  DDRB = (1 << BIT2); 
  while(1) { 
    PORTB |= (1 << BIT2); 
    _delay_ms(200); 
    PORTB &= ~(1 <<  BIT2); 
    _delay_ms(200); 
  } 
}
If pin-14 is PB0 and that is all you want to control, then do this:

Rich (BB code):
#undef F_CPU 
#define F_CPU 1000000UL 
#include <avr/io.h> 
#include <util/delay.h> 

int main(void) { 
  DDRB = 1; 
  while(1) { 
    PORTB |= 1; 
    _delay_ms(200); 
    PORTB &= ~1; 
    _delay_ms(200); 
  } 
}
 
Last edited:

MrChips

Joined Oct 2, 2009
30,821
You have to define what is Motor_Port, Motor_Clockwise and Motor_Anticlockwise.
They are controlling two bits to define clockwise and anticlockwise.
 

Thread Starter

mohitjindal

Joined May 25, 2009
49
Here is the full motor.h file:-
Rich (BB code):
#include <avr/delay.h>
#include <avr/io.h>
#include <avr/iom8.h>
#include <avr/interrupt.h>
#include <ctype.h>
#include "UtilsAndDefines.h"
#define Motor_Clockwise  BIT2
#define Motor_Anticlockwise BIT1
#define Motor_Port    PORTB
#define Motor_Dir    DDRB
 
/**
*@brief simply control PWM for the motor.\n
The pwm must be givem with values between 0 and 2000.\n
The 1000 is the motor stopped. The 0 is the 100% PWM rotating Anticlockwise and the 2000 is the 100% PWM rotating Clockwise
The PWM has increments of one step with 1000 steps of resolution.
*@var pwm This is the pwm to set on the motor
*@return 2 if rotating clockwise.\n
1 if stopped.\n
0 if anticlockwise.\n
-1 if fail or bad value or limit reached.
*/
int8_t SetPWM(uint16_t pwm);

/**
*@brief Init the motor low level control, on this case pins, timers, pwm variables, etc.
*/
void InitMotor(void);
uint16_t PWM;
 

panic mode

Joined Oct 10, 2011
2,754
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.

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

Rich (BB code):
#undef F_CPU 
#define F_CPU 1000000UL 
#include <avr/io.h> 
#include <util/delay.h> 
int main(void) { 
  // DDRB = (1 << 2); 
  while(1) { 
   pwm=500; 
    _delay_ms(2000); 
    pwm=1500; 
    _delay_ms(2000); 
  } 
}
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.
 
Top