Pages: [1]   Go Down
Author Topic: Change PWM frequency into 10Khz  (Read 2900 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code below shows two pwm signals. Pin5 gives the original signal and Pin6 gives the inverted signal, which means that the signals are behaving in anti-parallel manor. The PWM frequency equals to 7.8 Khz. How can I increase it up to 10 Khz and keep both signals behaving in the same way. I tried to play with the prescaller but I could not get the desired frequency. Thanks alot guys. smiley

//=====setting of timer0 for fast PWM method====
 //frequency PWM is fosc/(8*256) (by 16MHz oscil. = 7812,5)
 //timer 0, 8-bit timer, pins 5,6(+)   128us 
 TCCR0A=0b10110011;   // generate inverted PWM signals in output
 TCCR0B=0b00000010;   // set of source of clock signal + prescaller
 TCCR0A = TCCR0A ^ 0b01010000;

 //setting of PWM ports 5 and 6 to output (one is inverted - frequency is the same all the time)
 pinMode(5,OUTPUT);
 pinMode(6,OUTPUT);
« Last Edit: April 18, 2011, 05:08:57 am by Arman » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12911
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How can I increase it up to 10 Khz and keep both signals behaving in the same way

Does the frequency have to be 10 KHz?  Or can it be higher?

Does timer 0 have to be used?

Which board are you using?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The board is Arduino Uno.

The desired frequency is 10 khz.

The only timer used is timer0. Whatever the timer is, I would like to understand the code. I took it from the net and i applied it in the lab, and it gave me v good result of two opposite pulses at 7.8 khz.

I understood from (TCCR0B=0b00000010) that the code is using 8-bits, if i am right :S
and (TCCR0A = TCCR0A ^ 0b01010000) means that it is inverting the original (TCCR0A=0b10110011).

So, I would like to use any timers (0 , 1 or 2) in order to get 10 khz if possible.

Thank you
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12911
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


It is possible to get exactly 10 KHz PWM frequency on an 8 bit timer (timer 0 and timer 2).  You will give up resolution.  Instead of 0-255, the range becomes 0-199.  Is this acceptable?

I cannot tell from the datasheet if dual outputs work when the frequency is set to 10 KHz.  Are you willing to create an inverted output with hardware?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am controlling the duty cycle of the signals by an incremental ratio (15% at a time). (i.e Duty1=15%, Duty2=25%...........Duty9=95%). Do u mean that the poor resolution will affect or will not give me the desired duty cycle control?

I am applying the two signals as an input into two switches (one is normal and the other one is inverted) in the lab.

The original and the inverted signals are taken from the two pins (pin5 and 6) to feed two mosfet swicthes. I got v good results with 7.8 khz but i am willing to increase it up to 10 khz.

P.S. I am using pins 2, 7, 8, and 9 for a connection with push buttons to control the duty cycle of each pwm signal (p2 and p7 to increase and decrease the duty cycle, respectivley. And p8 and 9 are doing the same thing with the other signal (inverted one). The push button network is working fine.
« Last Edit: April 18, 2011, 05:13:41 am by Arman » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12911
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am controlling the duty cycle of the signals by an incremental ratio (15% at a time). (i.e Duty1=15%, Duty2=25%...........Duty9=95%). Do u mean that the poor resolution will affect or will not give me the desired duty cycle control?

0 through 199 gives 200 steps.  1 / 200 = 0.5% per step.  You're fine.

Quote
but i am willing to increase it up to 10 khz.

After some testing I've determined that you can have one (inverted output) or the other (10 KHz frequency) but not both.

Are you willing to give up the inverted output to have the frequency at 10 KHz?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmmm. I think I need both signals to be synchronized at the same frequency. After re-considering my case study, I might be able to increase it up to 20khz for both signals (I think this is my extreme limit :S). Is it possible ?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12911
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


It's time to give up on the 8 bit timers and consider the 16 bit timer.  Are you using timer 1 for anything?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, i am not using timer 1 or 2 at all. I am using timer 0 only

you are free to use any timer
« Last Edit: April 19, 2011, 02:41:06 am by Arman » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I read some info. about arduino timers, So using the  16 bit means that you will use timer1 in writing the code, which is alright for me. i am not using any timer in other applications
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12911
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Give this a spin...

Code:
void setup( void )
{
  // Turn off the timer while we make changes
  TCCR1B = TCCR1B & ~ ((1 << CS12) | (1 << CS11) | (1 << CS10));
 
  // Ensure the Timer 1 output pins are configured for output
  pinMode( 9, OUTPUT );
  pinMode( 10, OUTPUT );
 
  // Set Compare Output Mode and part of the Waveform Generation Mode (mode 14)
  TCCR1A =
      (1 << COM1A1) | (0 << COM1A0)  // Clear OC1A on Compare Match, set OC1A at BOTTOM (non-inverting mode)
      |
      (1 << COM1B1) | (1 << COM1B0)  // Set OC1B on Compare Match, clear OC1B at BOTTOM (inverting mode)
      |
      (1 << WGM11) | (0 << WGM10);  // Mode 14: Fast PWM, TOP = ICR1, Update of OCR1x at BOTTOM, TOV1 Flag Set on TOP

  // Set the other half of the Waveform Generation Mode (mode 14) and ensure a few things are disabled
  TCCR1B =
      (0 << ICNC1)  // Input Capture Noise Canceler disabled
      |
      (0 << ICES1)  // Input Capture Edge Select don't care
      |
      (1 << WGM13) | (1 << WGM12)  // Mode 14: Fast PWM, TOP = ICR1, Update of OCR1x at BOTTOM, TOV1 Flag Set on TOP
      |
      (0 << CS12) | (0 << CS11) | (0 << CS10);  // Clock continues to be disabled.  Not yet finished configuring.

  // Set the output frequency
  // fOCnxPWM = fclk_I/O / (N * (1 + TOP))
  // fOCnxPWM = 16000000 / (8 * (1 + 199))
  // fOCnxPWM = 10000 Hz
  ICR1 = 199;
 
  // Start with both outputs turned off
  OCR1A = 0;
  OCR1B = 199;
 
  // Start the clock
  TCCR1B =
      TCCR1B
      |
      (0 << CS12) | (1 << CS11) | (0 << CS10);  // clkI/O/8 (From prescaler)
}

void loop( void )
{
  delay( 1000 );

  // A full on
  OCR1A = 199;

  delay( 1000 );
 
  // B full on
  OCR1B = 0;

  delay( 1000 );

  // A twinkling
  OCR1A = 1;

  delay( 1000 );

  // B twinkling
  OCR1B = 198;

  delay( 1000 );

  // A off
  OCR1A = 0;

  delay( 1000 );

  // B off
  OCR1B = 199;
}
Logged

Pages: [1]   Go Up
Jump to: