servomotor programming problem

I’m creating my own library to control servo (without Servo arduino library). But my program doesn’t really work when I upload program into atmega328p and arduino. My test code:

#include <avr/io.h>
#include <util/delay.h>
#define t_min 1000
#define t_max 2000
#define a_min 0
#define a_max 180

#define F_CPU 16000000UL

void initTimer1Servo(void) {
	TCCR1A |= (1 << WGM11);
	TCCR1B |= (1 << WGM12) | (1 << WGM13);
	TCCR1B |= (1 << CS10); 
	ICR1 = 20000;                          
	TCCR1A |= (1 << COM1A1);    
	DDRB |= (1 << PB1);                          
}

int main(){
	
	initTimer1Servo();
	while(1){
		OCR1A=1500;
		_delay_ms(1000);
		OCR1A=2000;
		_delay_ms(1000);
		OCR1A=1000;
		_delay_ms(1000);
	}
	return 0;
}

But my program doesn't really work

It does something. Read your post, and see if you can figure out what it does. I couldn't.

You expect it to do something. Read your post, and see if you can figure out what you expect it to do. I couldn't.

without Servo arduino library

Why?

With a main() function, the IDE does not add one. That means that YOU are responsible for setting up the hardware. Why didn't you?

Looks like you’ll never return from main()

You setup the timer registers by oring into them - don't do this, always overwrite
the registers completely so you have a well defined hardware state, or at least set the
registers to zero first.

MarkT:
You setup the timer registers by oring into them - don’t do this, always overwrite
the registers completely so you have a well defined hardware state, or at least set the
registers to zero first.

PaulS:
It does something. Read your post, and see if you can figure out what it does. I couldn’t.

You expect it to do something. Read your post, and see if you can figure out what you expect it to do. I couldn’t.
Why?

With a main() function, the IDE does not add one. That means that YOU are responsible for setting up the hardware. Why didn’t you?

I’m going to control servo motor like Servor arduino library. When i use Servo library in Arduino like this:

#include <Servo.h>

Servo myservo;  

void setup() {
  myservo.attach(9); 
}

void loop() {
    myservo.write(90);             
    delay(1000);                       
    myservo.write(0);              
    delay(1000);     
    myservo.write(180);              
    delay(1000);                   
  }
}

With above code, servo attached pin 9 of Arduino will rotate 90 degree, delay 1 second, next rotate 0 degree, delay 1 second, then rotate 180, delay 1 second, and infinitely repeate again. Intead of using Servo library, I want to write the code which operates the same by avr library, like this:

#include <avr/io.h>
#include <util/delay.h>


#define F_CPU 16000000UL

void initTimer1Servo(void) {
	TCCR1A |= (1 << WGM11); // fast pwd mode
	TCCR1B |= (1 << WGM12) | (1 << WGM13);
	TCCR1B |= (1 << CS10); // no prescaler
	ICR1 = 20000;                // top value = 20000= 20ms
	TCCR1A |= (1 << COM1A1);    // clear compare match, set over time ( = 20ms)
	DDRB |= (1 << PB1);             // set ouput
}

int main(){
	
	initTimer1Servo();
	while(1){
		OCR1A=1500; // rotate 0 degree
		_delay_ms(1000); // delay 1 second
		OCR1A=2000; // rotate 180 degree
		_delay_ms(1000);// delay 1 second
		OCR1A=1000;// rotate 90 degree
		_delay_ms(1000); // delay 1 second
	}
	return 0;
}

But it doesn’t work the same as the code with Servo arduino library. Sorry about my english.

The main() that the IDE generates, if you don't provide one, calls init(), to set up the hardware. Why doesn't your code?

PaulS:
The main() that the IDE generates, if you don’t provide one, calls init(), to set up the hardware. Why doesn’t your code?

Yes, but I uploaded code into atmega328p by avrdude, but it doesn’t work.

but it doesn't work.

You left out the call to init() to set up the hardware, and now the hardware doesn't work properly. Color me surprised.

PaulS:
You left out the call to init() to set up the hardware, and now the hardware doesn't work properly. Color me surprised.

Sorry about my english , I can misunderstand your idea, can you give me example (code) about what you said.

Sorry about my english , I can misunderstand your idea, can you give me example (code) about what you said.

No. If you really want to replace the main() function that the IDE creates, YOU must learn what the main() function that the IDE creates does, and YOU must make your main function do the things that need doing.

PaulS:
No. If you really want to replace the main() function that the IDE creates, YOU must learn what the main() function that the IDE creates does, and YOU must make your main function do the things that need doing.

Yes, i got your idea. I know in C, main function which is entry point to program. And in Arduino, main function like this:

void setup(){
// doing something..
}

void loop(){
// doing something..
}
int main(){
 setup();
 while(1){
  loop();
 }
 return 0;
}

I think Arduino IDE generates main() function by combining setup() function and loop() into main().
My problem here. I use pwd mode timer 1 (16bit) to control servo. I read and follow what i read, servo principle. Such as : Robot Platform | Knowledge | Servo Control tutorial. But my servo no rotation or rotate to random position and servo doesn't do any thing. I need a solution which use Timer and pwd mode ( another mode of timer) to control servo.

And in Arduino, main function like this:

No. The main() function looks like:

int main()
{
    init();
    setup();

    for(;;)
    {
       loop();
    }
}

You are missing the call to init() which gets the hardware ready to use (sets pin modes to INPUT, etc.).

PaulS:
No. The main() function looks like:

int main()

{
    init();
    setup();

for(;:wink:
    {
      loop();
    }
}




You are missing the call to init() which gets the hardware ready to use (sets pin modes to INPUT, etc.).

Yes, thank a lot. i’m missing init() in my arduino code. I will add init function when i writting for arduino. But in here i’m writting code for atmega328p (avr ) not arduino, so sample structure code for avr , maybe:

#include <.....> // avr/io.h ; avr/interrupt.h....

void name_function_1(){}
void name_function_2(){}...

int main(){
 name_function_1();
 name_function_2();
.....
 // doing something 
 // maybe while(1); to loop infinitely
 
 return 0;
}

Because my code which i’m writting for avr microcontroller (atmega328p), it doesn’t need init() function in main(). and my code for avr (atmega328p) is right. But I need help, which can help me how to control servo in avr(atmega328p) by timer.

But in here i'm writting code for atmega328p (avr ) not arduino,

How does the atmega328p that you are writing code for differ from the atmega328 used on the Arduino?

PaulS:
How does the atmega328p that you are writing code for differ from the atmega328 used on the Arduino?

Yes, both the same. But i use "avr-gcc" and "avrdude" to compile and upload program. Difference here , i use "avr-gcc" and "avrdude" ,no arduino IDE. so program structure is different.

I think you'll find that init() sets up the millis() and micros() timers for things like the servo code to use.