Convert 2 pin code into 1 pin code.

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.

#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);
}
}

Motor.txt (1.21 KB)

port B bit 2 is called digital pin 10 in the Arduino world, so that main() is prettymuch equivalent to

void setup()
{
  pinMode (10, OUTPUT) ;
}

void loop ()
{
  digitalWrite (10, HIGH) ;
  delay (200) ;
  digitalWrite (10, LOW) ;
  delay (200) ;
}

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!

Lets start at the beginning.

  1. 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!.

  2. The code you have is not from an arduino sketch ( it contains the main function).

Mark

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).

Here is the full motor.h file.

#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.

Processor.pdf (161 KB)

As far as I can see that code does just play with one pin!

Mark

Hi Mark, the original code motor.txt don't work with one pin. I want to modify it.

Next question, which motor shield are you useing? (give us a link to it).

Mark

Holmes4. There is no link. I purchased it from electronics shop. It has 5 pins. 5Volt, PWM, DIR, BRAKE, GND.

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

  1. setu() for inialisation and

  2. 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.

Good luck

Mark

Here is a link to the code that the arduino uses to write to its pin

http://code.google.com/p/arduino/source/browse/#svn%2Ftrunk%2Fhardware%2Farduino%2Fcores%2Farduino

You need the wiring libs

Mark

sorry Holmes4, i only want c program to be modified. I don't want arduino codes. I have more hints for you if you want to help me.

I have more hints for you if you want to help me.

People aren't here to waste time playing games. Post ALL the information you have, if you want people to help you.

MohitJindal:
sorry Holmes4, i only want c program to be modified. I don't want arduino codes. I have more hints for you if you want to help me.

You want a C program but not code? I see a problem here.

The arduino uses C the link gives you the code in c.

Mark

Here are some hints:-

To set pin high:

PORTB |= (1<<2);

To set pin low:

PORTB &= ~(1<<2);

pin14 is PB2 (ie BIT2).

I want BIT2 to go to 0 instead of 1.

instead of

else
if(pwm<1000)
{
OCR1B = (1000-pwm);
TCCR1A |= bit(COM1B1);
Motor_Port |= Motor_Clockwise;
}

just have :

else {
OCR1B = (1000-pwm);
TCCR1A |= bit(COM1B1);
Motor_Port &= ~Motor_Anticlockwise;
}


add the line:

uint16_t PWM;

to Motor.c as well