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.
I attached the original code below.
Please tell me how to use the lines below in original code.
The motor.txt file is using PWM on pins 9 and 10 (OCR1A & OCR1B registers corresponds to those PWM pins). It can be rewritten (and much clarified/simplified) using analogWrite () on those pins. The Arduino setup removes the need for a lot of grubby detail in code like this, use it!
What hardware are you using? Do you have a motor shield? or are you driving the motor using a transistor? You must NOT try to drive the motor directly from the micro controller!.
The code you have is not from an arduino sketch ( it contains the main function).
Yes i don't want it for arduino. I want to run it for atmega 8a 32pin tqfp. And the pin 14 of atmega 8a is connected to DIR pin of 10amp motor shield. Please modify motor.txt.
You should be able to get the Arduino software to compile for an ATmega8.
Can you provide more information such as schematic, more details on the difference in hardware setup - it shouldn't be hard to figure out the change for yourself if you can describe which pin does what (and used to do what). If you know Arduino you just need to understand the direct-port manipulation in terms of Arduino calls (PORTB, DDRB, PINB v. digitalRead/Write / pinMode etc etc).
#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;
I also attached the Schematic.I only want to control motor direction from only one pin 14.
That ok. So your trying to go from 1 pin to 2 pin.
The pins work as follows ( which I think you know) DIR gives the direction and PWM the speed.
Now pick two pins the first without the PWM facility to use for the DIR and one with PWM for PWM
Here's an outline of the code you need. Arduino sketches have two functions that you must have
setu() for inialisation and
a main loop called loop() which is called repeatedly.
main(){
init();
setup();
while(1==1){
loop();
}
}
void init(){
// do what ever standard things you need to for every program in here
}
void setup(){
// do the things that are just for this program
// in the case
set DIR pin as output
set PWM pin as a PWM output
}
void loop(){
// make the motor run
set DIR pin HIGH
set speed to Full speed
delay for 10 seconds
// stop the motor
set speed to stop
delay 1 second
// run the motor the other way
set DIR pin HIGH
set speed to Full speed
delay for 10 seconds
// stop the motor again don't go from full forward to full reverse it can't be good for the motor
set speed to stop
delay 1 second
}
You now need to figure out how to set DIR HIGH/LOW and how to write a value to PWM.
While you can do this by directly changing the registers its ugly, there should be some wrapper function such as the arduino's
digitalWrite(pin,value) sets the pin HIGH/LOW or analogWrite(pin,value) which sets the PWM value.