PWM pulse generator

Status
Not open for further replies.

adamek

New member
hi all. I want to ask you for help.
I am using Teensy 3.2 and need to do pulse generator with PWM 1:10 and frequency 10Hz on one pin, and on second 100Hz (doesn't matter which one).
As far as I understand it needs to be done via interupts on timers. but can you guide me how?
thanks a lot.
 
thanks a lot. the link you sent answeared in 100%. :)
I also looked at the link to forum where guy needs to do generator with 2Hz. there was an example of analogWriteFrequencySlow...
but if i copy this code to my code, compiler throw this exception:

'FTM1_CH0_PIN' was not declared in this scope
if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {

what do I need to include the compiler knows this names correctly?
thanks
 
First, analogWriteFrequency does support slow speeds.

The thread is probably old info. But who can say, since you didn't give a link.

You also mentioned a compile error, but didn't post code. That's not how we work here. When you ask for help with any code issue, the expectation is a complete program which can be copied into Arduino to reproduce the problem. If you look at the many threads on this forum, you'll see we do very well at truly solving problems when complete code is posted, and when code is missing, usually the result is a lot of time-wasting guesswork.

Please, help us to help you. Post complete code if you ask these types of questions.
 
realy sorry for my bad english!!!
there wasn't problem in compiler but in my code. now I found solution on some forum.
I didn't post the code cause I didn't know there to start and everithing what I wrote was trash. but your link solved it all.
thanks a lot for your help.


my code:

#define FTM0_CH0_PIN 22
#define FTM0_CH1_PIN 23
#define FTM0_CH2_PIN 9
#define FTM0_CH3_PIN 10
#define FTM0_CH4_PIN 6
#define FTM0_CH5_PIN 20
#define FTM0_CH6_PIN 21
#define FTM0_CH7_PIN 5
#define FTM1_CH0_PIN 3
#define FTM1_CH1_PIN 4
#define FTM2_CH0_PIN 32
#define FTM2_CH1_PIN 25

const int ledPin = 13;

const int btn = 1;
const int FreqOutPin = 5;
bool bFreqState = true; //true=2Hz, false=100Hz

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(FreqOutPin, OUTPUT);
pinMode(btn, INPUT_PULLUP);

analogWriteFrequency(FreqOutPin, 2); // 2Hz
attachInterrupt(btn, isrBtn, FALLING);
}

void loop()
{
analogWrite(FreqOutPin, 25); //strieda 10% z 255
}

void isrBtn()
{
cli();
if( bFreqState )
{
bFreqState = false;
analogWriteFrequencySlow(FreqOutPin, 100); //switch to 100Hz
}
else
{
bFreqState = true;
analogWriteFrequencySlow(FreqOutPin, 2); //switch to 2Hz
}
delay(600);
sei();
}

void analogWriteFrequencySlow(uint8_t pin, float frequency)
{
uint32_t prescale, mod;
float minfreq;

for (prescale = 0; prescale < 7; prescale++) {
minfreq = (float)(31250 >> prescale) / 65536.0f;
if (frequency >= minfreq) break;
}
mod = (float)(31250 >> prescale) / frequency - 0.5f;
if (mod > 65535) mod = 65535;
if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
FTM1_SC = 0;
FTM1_CNT = 0;
FTM1_MOD = mod;
FTM1_SC = FTM_SC_CLKS(2) | FTM_SC_PS(prescale);
} else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
|| pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
|| pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
#ifdef FTM0_CH6_PIN
|| pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
#endif
) {
FTM0_SC = 0;
FTM0_CNT = 0;
FTM0_MOD = mod;
FTM0_SC = FTM_SC_CLKS(2) | FTM_SC_PS(prescale);
}
#ifdef FTM2_CH0_PIN
else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
FTM2_SC = 0;
FTM2_CNT = 0;
FTM2_MOD = mod;
FTM2_SC = FTM_SC_CLKS(2) | FTM_SC_PS(prescale);
}
#endif
}
 
Status
Not open for further replies.
Back
Top