How to generate a different PWM’s signals with Arduino based PLC

How to generate a different PWM’s signals with Arduino based PLC

On this example is explained how to generate a PWM with M-DUINO  and ARDBOX 

In our equipment’s you can generate a PWM on Digital/Analog Ports. But most of these ports have a limitation of frequency (500 Hz approx.). PIN3 for ARDBOX and PIN3 and PIN2 for M-DUINO don’t have limitation and they can generate a PWM between 100Hz (even less) and 8MHz.

PIN3 for ARDBOX can work as an external output for the Timer/Counter0 Output Compare B (OC0B). And the same on M-DUINO: PIN3 can work as an external output for the Timer/Counter3 Output Compare C (OC3C) and PIN2 can work as an external output for the Timer/Counter3 Output Compare B (OC3B).

But how can we set up the frequency needed?

The microcontroller has different registers to set up the PWM signals on OCnX. In this example is showed how to work with Clear Timer on Compare (CTC mode).

ARDBOX:

Register TCCR0A controls the Output Compare pin (OC0A) behavior and the Timer/Counter0 mode of operation.

Register TCCR0B controls the Output Compare pin (OC0A) behavior, the Timer/Counter0 mode of operation and the prescaler of Timer/Counter0.

IMPORTANT: With ARDBOX if you change the prescaler of default (clk/64) functionality it also changes the time of delay functions.

CTC mode compares OCR0A register with TCNT0 (Timer/Counter), when the value is equal the uC toggles the output signal OC0A to generate the PWM.

For more information see Atmel ATmega32U4 datasheet between pages 94 and 110.

Next is showed the implemented code to generate different frequencies on PIN3:

void setup() {
  pinMode(3, OUTPUT);
  setFrequency(2000); // Value is the wished frequency, in this case 2kHz
}

void loop() {
}

void setFrequency(unsigned long freq) {
  TCCR0A &= 0b00001100; //Clear TCCR0A protecting the reserved bits for uC
  TCCR0A |= 0B00010010; // Configure OC0B (PIN3) 
  TCCR0B &= 0b00110000; // Clear TCCR0B protecting the reserved bits for uC
  TCCR0B |= 0b11000011; // Select prescaler of 64 and configure Mode operation (CTC)
  OCR0A = (8000000 / (64 * freq)) - 1;
} 

 

M-DUINO:

Register TCCR3A controls the Output Compare pin (OC3B and OC3C) behavior and the Timer/Counter3 mode of operation.

Register TCCR0B controls the Output Compare pin (OC3B and OC3C) behavior, the Timer/Counter0 mode of operation and the prescaler of Timer/Counter0.

CTC mode compares OCR3A register with TCNT3 (Timer/Counter), when the value is equal the uC toggles the output signal OC3A to generate the PWM.

For more information see Atmel ATmega2560 datasheet between pages 133 and 160.

Next is showed the implemented code to generate different frequencies on PIN3:

void setup() {
  pinMode(3, OUTPUT);
  setFrequency(2000); // Value is the wished frequency, in this case 2kHz
}


void loop() {
}

void setFrequency(unsigned long freq) {
  TCCR3A &= 0b00000000; //Clear TCCR3A
  TCCR3A |= 0B01010100; // Configure OC3C (PIN3) and OC3B (PIN2) as toggle mode 
  TCCR3B &= 0b00100000; // Clear TCCR3B protecting the reserved bit for uC
  TCCR3B |= 0b11001011; // Select prescaler of 64 and configure mode operation (CTC)
  OCR3A = (8000000 / (64 * freq)) - 1;
}

 

See also:

HOW TO TOUCH UP DATA FROM THE EEPROM,  HOW TO CONNECT AND CONFIGURE A LOAD CELL USING HX711 TRANSMITTER WITH OUR EQUIPMENT’S,  HOW TO USE STEPPER MOTOR WITH ARDBOX ANALOG,  HOW TO USE THE WATCHDOGRS-485 COMMUNICATION EXAMPLE USING OUR LIBRARIES,  HOW TO CREATE A WEB SERVER TO READ A SD CARD WITH OUR PLC’S,  HOW TO CONNECT A SD CARD TO A PLC,  HOW TO UPLOAD A BOOTLOADER ON A ARDBOX/M-DUINO PLC

Comments are closed.