need help in timer1

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi i want to use TCNT1 in Timer1 of mega32 in 8 bit mode. Actually it's 16 bit counter. is it possible?? How??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Set compare for 255?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use CTC mode with TOP=255.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
Set compare for 255?

ya

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another question, can i use both hardware and software pwm simultaneously??

I used OC1A & OC1B pins by Timer1 using fast pwm,
and also used OC0 and OC2 pins using software pwm.
but when I connect these 4 pins to 4 control signals of servo motors, two of them vibrates quite annoyingly.I want to make a robotic arm using 4 servo but i can't because it is vibrating continuously.

I have given 20 ms time period to OC1A,OC1B,OC0 and OC2 as servo requires. But still problem persists.
Is it problem of power? I have given power from laptop and adaptor which has max current 350ma
Here is the code


#include
#include
#include

ISR(TIMER0_COMP_vect) {
        // clear the output pin on OCR0 match
   PORTC &= ~(1<<PC0);
}
ISR(TIMER0_OVF_vect) {
        // set the output pin at timer overflow
   PORTC |= (1<<PC0);
}


ISR(TIMER2_COMP_vect) {
        // clear the output pin on OCR2 match
   PORTC &= ~(1<<PC1);
}
ISR(TIMER2_OVF_vect) {
        // set the output pin at timer overflow
   PORTC |= (1<<PC1);
}

int main(void) {

   
   DDRC |= (1<<PC0)|(1<<PC1);
   TIMSK |= (1<<TOIE2)|(1<<OCIE2)|(1<<OCIE0)|(1<<TOIE0); // use both interrupts
   TCNT0=0;
   TCNT2=0;
  
    
   TCCR0 = (1<<CS00)|(1<<CS01);

   TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);        //NON Inverted PWM
   TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); //PRESCALER=64 MODE 14(FAST PWM)

   TCCR2|=(1<<CS22);
   
   sei();

   ICR1=312;  //fPWM=50Hz (Period = 20ms Standard).
   

   DDRD|=(1<<PD4)|(1<<PD5);  //PWM Pins as Out



   while(1){


   
   OCR1A=6;  
   _delay_ms(2000);
        


    OCR1B=6;  
    _delay_ms(2000);
            
       
    OCR0 = 6;
   _delay_ms(2000);

  
    OCR2 = 6;
   _delay_ms(2000);

  
      
     


	  //2

     OCR1A=19;  
     _delay_ms(2000);
	

     OCR1B=19;  
     _delay_ms(2000);
            
     OCR0 = 19;
    _delay_ms(2000);      
  
    OCR2= 19;
   _delay_ms(2000);

  
	  
	
   
   }
   
 }
Last Edited: Sun. Apr 28, 2013 - 07:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mechaboy wrote:
Hi i want to use TCNT1 in Timer1 of mega32 in 8 bit mode. Actually it's 16 bit counter. is it possible?? How??
How about one of the 8-bit modes? Mode 1 is Phase Correct 8-bit PWM, and mode 5 is Fast 8-bit PWM.

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OCR1A=6 means 0 degree, 19 is 90degree

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Another question, can i use both hardware and software pwm simultaneously??

I used OC1A & OC1B pins by Timer1 using fast pwm,
and also used OC0 and OC2 pins using software pwm.
but when I connect these 4 pins to 4 control signals of servo motors, two of them vibrates quite annoyingly.I want to make a robotic arm using 4 servo but i can't because it is vibrating continuously.

Maybe you should check your other thread on the matter, where an answer to this was already given.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i checked but that didn't help

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

well did you actually read the document ? I know for a fact that it is possible. I've driven 24 rc servo's with one avr without jitter. It's also considered polite to acknowledge answers given in threads. People go out of their way and take the time to help you and then you don't even bother to reply.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I have given power from laptop and adaptor which has max current 350ma
This may be enough for the micro, but for servos use extra battery, 3 or 4 dry cells.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm a little puzzled. If you have a 16bit timer available then, depending on F_CPU, you can have as much as 65536 steps to represent 20ms. So the 1ms..2ms active range of the servo is 3277 steps. That gives you very fine positional control. In fact it may be more than is needed and typical choice of crystals might actually find you using the counter to just count 20,000 steps so that the servo position can be expressed as 1,000 of those.

If you switch to a an 8bit timer (or a 16bit used as 8bit) with 256 steps to represent 20ms. Then the 1ms..2ms range is limited to 13 steps. That is very coarse movement for a servo.

Why on earth would you do that? (the usual reason to limit TOP is that the PWM frequency needs to be higher but for a servo you have the luxury of 50Hz/20ms).