Convert C into Arduino Uno 3

Hi I am a newbie. I have a DC Servo motor code in C. I want to run it in Uno 3. Please telll me how to convert\change it so that I can run on Uno 3.

/*************************************************

ATMega8 processor at 16MHz

*************************************************/


#include "Motor.h"

extern uint16_t  PWM=1000;

int8_t SetPWM(uint16_t pwm)
{
	uint8_t sreg;

	if(pwm!=PWM)
	{
		
		Motor_Port &= ~(Motor_Clockwise|Motor_Anticlockwise);
		/* Save Global Interrupt Flag */
		sreg = SREG;
		/* Disable interrupts */
		cli();
		//TCCR1A =(bit(WGM10)|bit(WGM11));
		TCCR1A=0;
		if(pwm>1000)
		{
			OCR1A = (pwm-1000);
			TCCR1A |= bit(COM1A1);
			Motor_Port |= Motor_Anticlockwise;
		}
		else
		if(pwm<1000)
		{
			OCR1B = (1000-pwm);
			TCCR1A |= bit(COM1B1);
			Motor_Port |= Motor_Clockwise;
		}

		sei();
		SREG = sreg;

		PWM=pwm;
	}
	return 1;

}





void InitMotor(void)
{
	//Init Timer 1 for PWM

	Motor_Port&= ~(Motor_Clockwise|Motor_Anticlockwise);	

	TCNT1 = 0;
    TCCR1A = 0;
    TCCR1B = 0;

	ICR1=1000; 
	
	TCCR1B = (bit(WGM13)|/*bit(CS10)*/bit(CS11)); // CS10=10KHz PWM   CS11 = 1.2 KHz

	//TCCR1A = (bit(WGM10)|bit(WGM11));
	//TCCR1B = (bit(WGM12)|/*bit(CS10)*/bit(CS11)); // CS10=10KHz PWM   CS11 = 1.2 KHz
	// Set the PWM duty cycle to zero.
	OCR1A = 0;
    OCR1B = 0;
	SetPWM(1000);
}

I will apost motor.h if you like.

Arduino is C++, which means it can quite happily compile C code.

Just copy your code there into a new sketch. Add a loop() and setup() function. Add a new tab in the sketch file and call it Motor.h, and paste your motor.h code in there. Press compile and see what happens.

Ok thanks Tom. Actuallty I want to hack a Servo motor and make my own Servo.
Please read my another c code program and tell me the changes to make in that. I attached it as .txt. Please read :grin:

ServoMotor.txt (12.9 KB)

Please read

Nearly 600 lines and hardly any comments?
You've got to be kidding.

// CalibrationInit - initialize calibration variables

And even when there are comments, they're of the "repeat-the-obvious" variety.

return = (index == 0) ? 16 : index - 1;

Interesting construct - what does it do?

I am not a programmer. I don't know what is a construct. Please tell me how i can convert it in uno 3. I only want to use this code.

I am not a programmer.

That's another one of those "stating the obvious" comments.

I don't know what is a construct.

Get out a dictionary and look up the word. You have constructed a statement that is complete nonsense.

Please tell me how i can convert it in uno 3.

As has been pointed out, the Arduino is programmed using C++, which is a superset of C. There is no conversion required. Although the code that is to be compiled needs to use names that make sense on the Arduino. The code you posted appears to. So, just compile it and see what happens. If there are errors, we can possibly explain them, and help you fix them.

I only want to use this code.

Why? Is that a homework assignment?

Hahaha this is not homework assignment. I want to make dc servo motor myself. If i use any other code then i have to change the Servo motor controller board.

If i use any other code then i have to change the Servo motor controller board.

Or, just pitch it in the wastebasket. Servo motors can be directly controlled by the Arduino.

I know we can control servos. I want to make servo. I want to use uno 3 as a replacement of MCU like atmega 8. To control dc motor with encoder.

char	ControlTable[];
	for(int i=24; i<56; i++) 
          ControlTable[i] = 0;

That's not going to go well.

Where did you get this code?

SlopeTable[] = {0, 1, 2, 5, 11, 22, 45, 90, 181};

Datatype?

TempTable[] = {125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 121,

Again.

I attached the motor with Motor Driver and Uno 3. Can you please complete the code below ? I want to add the starting lines. Please define pins.

void setup() {
  
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  
}

void loop(){
  
  //forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed
  
  delay(3000);
  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A

  delay(1000);
  
  //backward @ half speed
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);   //Spins the motor on Channel A at half speed
  
  delay(3000);
  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A
  
  delay(1000);
  
}

I connected GND to UNO GND
DIR to pin 12
PWM to pin 3 of UNo 3.
Break to pin 8

Brake to pin 8 or pin 9?

Brake is connected to pin 8 on Uno 3. :grin:

Well, do you see a problem here?

  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

Or do you just want us to write the code for you?

I want the starting lines. Define all 4 pins so that the code will become correct.

Define "correct".

The code, as far as I can see, will compile, and if you just move one pin, will do...something.

do you just want us to write the code for you?

Your answer seems "yes", but I think you did not get the rhetorical nature of Nick's comment.
(I fear it was not meant to be a yes/no question with free choice of answer)

Well, do you see a problem here?

If "no" is the answer to this question, even after AWOL's hint, you should explain why you need to overload volunteers trying to help you.
( "I'm not a programmer" is no valid excuse, rather a reason to not deal with a µC )

Ok but how can i define Dir and pwm pin ?

const byte directionPin = <your preferred value here>;

The PWM pin is left as an exercise for the reader.

MohitJindal:
Brake is connected to pin 8 on Uno 3. :grin:

If you are not going to answer this question I am going to lock this thread as you are just wasting everyone's time.