Hi friends,
I want to generate a high frequency PWM on pin 9 (timer 1) near to 120kHz, I am ready to sacrifice resolution of waveform. I need it for bug converter.
I know how to make 62 khz as it doesnt need to use OCRA1 but not understanding how to achive 120khz!
At best you can use 7 bit resolution. Use the appropriate timer mode and set the timer1 TOP value to 127 (see the processor data sheet for details).
I was trying following code but not getting any output on my pin what am I doing wrong in this code?
void setup() {
// put your setup code here, to run once:
pinMode (9, OUTPUT) ;
TCCR1A = 0xA3 ;
TCCR1B = 0x19 ; // mode 15, clock prescale by 1
OCR1A = 128-1 ; // 160 clock periods = 10us per cycle
OCR1B =0 ;
TCNT1 =0 ;
}
void loop() {
// put your main code here, to run repeatedly:
OCR1A=80;
}
#include <TimerOne.h>
//UNO only
void setup()
{
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
Timer1.initialize(8); // Frequency, 8us = 125KHz
Timer1.pwm(9,512); // 50% DC on pin 9
//Timer1.pwm(10,255); // 25% DC on pin 10
// D.C.
// 10KHz
// You can use 2 to 1023
// 0 & 1 gives a constant LOW
// 1024 gives a constant HIGH
// 2 gives ~125ns HIGH pulses
// 1023 gives ~125ns low pulses
// 512 gives 50us
}
void loop()
{
}
Thanks a lot, I am now getting that frequency but can you point out what I was doing wrong In my code?
I want to develop it in fast PWM mode so I can get atleast 7 bit resolution with this phase corrected mode I am getting 6 bit resolution.
You are using the wrong mode for what you are trying to achieve. You should use mode 14 (Fast PWM to ICR1).
With ICR1 = 127 you will have a frequency of 125KHz. Use OCR1A to set the duty cycle.
When you use mode 15 (Fast PWM to OCR1A), you can only achieve a toggle at the top value, for a 50% duty cycle and no ability to change it.
Thanks a lot now getting what I wanted 7 bit resolution with 125khz!
This is final code so some one could get help from it.
void setup() {
// put your setup code here, to run once:
pinMode (9, OUTPUT) ;
TCCR1A = 0xA2 ;
TCCR1B = 0x19 ; // mode 15, clock prescale by 1
ICR1 = 128-1 ; // 160 clock periods = 10us per cycle
TCNT1 =0 ;
}
void loop() {
// put your main code here, to run repeatedly:
OCR1A=80;
}
I also facing one odd problem when I use fast PWM mode I do get waveform of 125 khz but if I increase time per devisions I can see some patches where waveform doesnt get generated!
Should I create new thread for this issue?
You may be seeing the effect of the Timer0 overflow interrupt used for the millis(). What do you see when you turn off that timer?
The comments in your code are incorrect. Why do you think that an incorrect comment help other people to understand your code? Do you not realize they are wrong? They may help us understand you better than they help us understand your code.
sorry for my wrong commenting I kept it as it was, didn't noticed that sorry for it!
void setup() {
pinMode (9, OUTPUT) ;
TCCR1A = 0xA2 ;
TCCR1B = 0x19 ; // mode 14, clock prescale by 1
ICR1 = 127;
TCNT1 =0 ;
}
void loop() {
OCR1A=80;
}
This was my code and as I didn't used timer0 do I still need to off it? and how can I turn it off?
Also if we see the first image, the on period is occuring nearly after every 6 ms but if I change mode to phase corrected instead of fast pwm and create same frequency this problem disappears, not getting what is the reason behind it!
and how can I turn it off?
See the data sheet entry for Timer0
I tried to switch of timer zero but no effect on output!
Here is my code
void setup() {
pinMode (9, OUTPUT) ;
TCCR1A = 0xA2 ;
TCCR1B = 0x19 ; // mode 14, clock prescale by 1
ICR1 = 127;
TCCR0A=0x00; //Normal port operation, OC0x disconnected.
TCCR0B=0x00; //No clock source (Timer/Counter stopped)
TCNT1 =0 ;
}
void loop() {
OCR1A=80;
}
And this is the output Image on DSO
This only happens when I go for fast PWM mode not with phase corrected one so again not understanding the problem!
can any one could guide me for it? I need this for SMPS and cant afford that pin keeping high and making my MosFET on for that much of time!
I don't have a scope, but I can't confirm any gaps in the count when I use a simple pulse counting external interrupt and a sample period triggered from Timer2 (using the MSTimer2 library). I'm not exactly sure of the frequency of your gap, but I've sampled from 20ms to 1 second and don't see any variation from the expected count. You may be seeing an artifact of your scope.
#include <MsTimer2.h>
volatile boolean timeFlag = false;
volatile unsigned long count = 0;
unsigned long copyCount = 0;
int samplePeriod = 1000;//Timer2 interrupt period
void setup()
{
MsTimer2::set(samplePeriod, flag);
MsTimer2::start();
pinMode (9, OUTPUT) ;
TCCR1A = 0xA2 ;
TCCR1B = 0x19 ; // mode 14, clock prescale by 1
ICR1 = 127;
TCCR0A = 0x00; //Normal port operation, OC0x disconnected.
TCCR0B = 0x00; //No clock source (Timer/Counter stopped)
TCNT1 = 0 ;
Serial.begin(115200);
Serial.println("start...");
attachInterrupt(0, isrCount, RISING);//jumper pin 9 to pin 2
}
void loop()
{
OCR1A = 80;
//take count readings at Timer2 interrupt
if (timeFlag)
{
timeFlag = false;
// make copy of count, disable interrupts while copying, then reenable
noInterrupts();
copyCount = count;
count = 0; // reset counter in isr
interrupts();
Serial.println(copyCount);
copyCount = 0;
}
}
//external interrupt ISR
void isrCount()
{
count++;
}
//Timer2 ISR
boolean flag() {
timeFlag = true;
}
TCCR0A=0x00; //Normal port operation, OC0x disconnected.
This is a serious mistake. The timer can output directly to a pin and cannot be interrupted by the processor. Use that mode instead.
This is a serious mistake. The timer can output directly to a pin and cannot be interrupted by the processor. Use that mode instead.
Which MODE?
I don't have a scope, but I can't confirm any gaps in the count when I use a simple pulse counting external interrupt and a sample period triggered from Timer2 (using the MSTimer2 library). I'm not exactly sure of the frequency of your gap, but I've sampled from 20ms to 1 second and don't see any variation from the expected count. You may be seeing an artifact of your scope.
I already tested it on 2 different DSO 1 of tectronix and other of RIGOL. also as I said before if I use phase corrected mode insted of fast PWM mode to generate 120 kHZ my same dso dont show those gaps!
Which MODE?
Do not disconnect the timer output from the output pin.
Please study the timer section of the data sheet more carefully.
I'm not sure how to interpret the scope image. Can you please explain the frequency and duration of the gaps.
PWM frequency is 125khz, and in every 10ms of period there is 8ms PWM signal and 2ms of patch of pin is at 5v