Engmarcio
Bom dia.
 
Existe algum bloco pwm no open plc?
 
poderiam me ajudar.
 
preciso gerar 400 pulsos a partir de uma sw1 - digital imput
Reply 0 0
Grz
Hello Engmarcio,
You could find block in forum "Custom Function Blocks".
Best regard 
Pierre
Reply 1 0
Engmarcio
Thank's
Reply 0 0
gfine
This Program is a Servo PWM program for the Arduino Uno R3 but should work for the Nano. It uses the PWM instruction in Ladder Diagram to move the servo -90 to zero to +90. It uses the S90 or S51 series.
There are comments as to how it is set up. Please refer to attached pdf for the program and picture of the Data Table.
PWM_Servo_Data_Table.png
Reply 3 0
damoney27
This Program is a Servo PWM program for the Arduino Uno R3 but should work for the Nano. It uses the PWM instruction in Ladder Diagram to move the servo -90 to zero to +90. It uses the S90 or S51 series.
There are comments as to how it is set up. Please refer to attached pdf for the program and picture of the Data Table.
PWM_Servo_Data_Table.png
Thanks, it works well. I was just about to post to ask how and I searched first. 
Reply 1 0
Praveen Venugopal
It work well when I write this program via editor, but I get compilation error while trying to upload this program to runtime.
 
Please help to resolve
This Program is a Servo PWM program for the Arduino Uno R3 but should work for the Nano. It uses the PWM instruction in Ladder Diagram to move the servo -90 to zero to +90. It uses the S90 or S51 series.
There are comments as to how it is set up. Please refer to attached pdf for the program and picture of the Data Table.
PWM_Servo_Data_Table.png
Reply 0 0
thiagoralves
If you’re trying to run it on a Raspberry Pi it won’t work. PWM_CONTROLLER block only works with some Arduino boards
Reply 0 0
Praveen Venugopal
If you’re trying to run it on a Raspberry Pi it won’t work. PWM_CONTROLLER block only works with some Arduino boards
Am trying with Windows PC as master and Arduino UNO as slave.
 
It shows the following compilation error
 
cat: ethercat: No such file or directory
Generating C files...
./st_files/91979.st:3-23..3-36: error: invalid specification in variable declaration.
./st_files/91979.st:9-18..9-26: error: invalid variable before ':=' in ST assignment statement.
./st_files/91979.st:9-31..9-33: error: ';' missing at the end of statement in ST statement.
./st_files/91979.st:9-33..9-38: error: invalid variable before ':=' in ST assignment statement.
./st_files/91979.st:9-46..9-48: error: ';' missing at the end of statement in ST statement.
./st_files/91979.st:9-48..9-53: error: invalid variable before ':=' in ST assignment statement.
./st_files/91979.st:9-58..9-59: error: ';' missing at the end of statement in ST statement.
./st_files/91979.st:9-59..9-59: error: invalid statement in ST statement.
8 error(s) found. Bailing out!
Error generating C files
Compilation finished with errors!
 
 
Reply 0 0
Praveen Venugopal
This is the corresponding ST code
 
PROGRAM PWM
VAR
PWM_CONTROLLER0 : PWM_CONTROLLER;
Freq1 : REAL := 250.0;
C1 : SINT := 1;
D1 : REAL := 50.0;
END_VAR
PWM_CONTROLLER0(CHANNEL := C1, FREQ := Freq1, DUTY := D1);
END_PROGRAM

CONFIGURATION Config0
RESOURCE Res0 ON PLC
TASK task0(INTERVAL := T#20ms,PRIORITY := 0);
PROGRAM instance0 WITH task0 : PWM;
END_RESOURCE
END_CONFIGURATION
(*DBG:char md5[] = "d8f08e320e804b30c2c489a1c33de060";*)
(*DBG:/**)
(*DBG: * This file is part of OpenPLC Runtime*)
(*DBG: **)
(*DBG: * Copyright (C) 2023 Autonomy, GP Orcullo*)
(*DBG: * Based on the work by GP Orcullo on Beremiz for uC*)
(*DBG: **)
(*DBG: * This program is free software; you can redistribute it and/or*)
(*DBG: * modify it under the terms of the GNU General Public License*)
(*DBG: * as published by the Free Software Foundation; either version 2*)
(*DBG: * of the License, or (at your option) any later version.*)
(*DBG: **)
(*DBG: * This program is distributed in the hope that it will be useful,*)
(*DBG: * but WITHOUT ANY WARRANTY; without even the implied warranty of*)
(*DBG: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*)
(*DBG: * GNU General Public License for more details.*)
(*DBG: **)
(*DBG: * You should have received a copy of the GNU General Public License*)
(*DBG: * along with this program; If not, see <http://www.gnu.org/licenses/>.*)
(*DBG: **)
(*DBG: */*)
(*DBG:*)
(*DBG:#include <stdbool.h>*)
(*DBG:*)
(*DBG:#include "iec_types_all.h"*)
(*DBG:#include "POUS.h"*)
(*DBG:*)
(*DBG:#define SAME_ENDIANNESS 0*)
(*DBG:#define REVERSE_ENDIANNESS 1*)
(*DBG:*)
(*DBG:uint8_t endianness;*)
(*DBG:*)
(*DBG:*)
(*DBG:extern PWM RES0__INSTANCE0;*)
(*DBG:*)
(*DBG:static const struct {*)
(*DBG: void *ptr;*)
(*DBG: __IEC_types_enum type;*)
(*DBG:} debug_vars[] = {*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.EN), BOOL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.ENO), BOOL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.CHANNEL), SINT_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.FREQ), REAL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.DUTY), REAL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.SUCCESS), BOOL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.INTERNAL_CH), SINT_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.INTERNAL_FREQ), REAL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.PWM_CONTROLLER0.INTERNAL_DUTY), REAL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.FREQ1), REAL_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.C1), SINT_ENUM},*)
(*DBG: {&(RES0__INSTANCE0.D1), REAL_ENUM},*)
(*DBG:};*)
(*DBG:*)
(*DBG:#define VAR_COUNT 12*)
(*DBG:*)
(*DBG:uint16_t get_var_count(void)*)
(*DBG:{*)
(*DBG: return VAR_COUNT;*)
(*DBG:}*)
(*DBG:*)
(*DBG:size_t get_var_size(size_t idx)*)
(*DBG:{*)
(*DBG: if (idx >= VAR_COUNT)*)
(*DBG: {*)
(*DBG: return 0;*)
(*DBG: }*)
(*DBG: switch (debug_vars[idx].type) {*)
(*DBG: case SINT_ENUM:*)
(*DBG: return sizeof(SINT);*)
(*DBG: case REAL_ENUM:*)
(*DBG: return sizeof(REAL);*)
(*DBG: case BOOL_ENUM:*)
(*DBG: return sizeof(BOOL);*)
(*DBG: default:*)
(*DBG: return 0;*)
(*DBG: }*)
(*DBG:}*)
(*DBG:*)
(*DBG:void *get_var_addr(size_t idx)*)
(*DBG:{*)
(*DBG: void *ptr = debug_vars[idx].ptr;*)
(*DBG:*)
(*DBG: switch (debug_vars[idx].type) {*)
(*DBG: case SINT_ENUM:*)
(*DBG: return (void *)&((__IEC_SINT_t *) ptr)->value;*)
(*DBG: case REAL_ENUM:*)
(*DBG: return (void *)&((__IEC_REAL_t *) ptr)->value;*)
(*DBG: case BOOL_ENUM:*)
(*DBG: return (void *)&((__IEC_BOOL_t *) ptr)->value;*)
(*DBG: default:*)
(*DBG: return 0;*)
(*DBG: }*)
(*DBG:}*)
(*DBG:*)
(*DBG:void force_var(size_t idx, bool forced, void *val)*)
(*DBG:{*)
(*DBG: void *ptr = debug_vars[idx].ptr;*)
(*DBG:*)
(*DBG: if (forced) {*)
(*DBG: size_t var_size = get_var_size(idx);*)
(*DBG: switch (debug_vars[idx].type) {*)
(*DBG: case SINT_ENUM: {*)
(*DBG: memcpy(&((__IEC_SINT_t *) ptr)->value, val, var_size);*)
(*DBG: //((__IEC_SINT_t *) ptr)->value = *((SINT *) val);*)
(*DBG: ((__IEC_SINT_t *) ptr)->flags |= __IEC_FORCE_FLAG;*)
(*DBG: break;*)
(*DBG: }*)
(*DBG: case REAL_ENUM: {*)
(*DBG: memcpy(&((__IEC_REAL_t *) ptr)->value, val, var_size);*)
(*DBG: //((__IEC_REAL_t *) ptr)->value = *((REAL *) val);*)
(*DBG: ((__IEC_REAL_t *) ptr)->flags |= __IEC_FORCE_FLAG;*)
(*DBG: break;*)
(*DBG: }*)
(*DBG: case BOOL_ENUM: {*)
(*DBG: memcpy(&((__IEC_BOOL_t *) ptr)->value, val, var_size);*)
(*DBG: //((__IEC_BOOL_t *) ptr)->value = *((BOOL *) val);*)
(*DBG: ((__IEC_BOOL_t *) ptr)->flags |= __IEC_FORCE_FLAG;*)
(*DBG: break;*)
(*DBG: }*)
(*DBG: default:*)
(*DBG: break;*)
(*DBG: }*)
(*DBG: } else {*)
(*DBG: switch (debug_vars[idx].type) {*)
(*DBG: case SINT_ENUM:*)
(*DBG: ((__IEC_SINT_t *) ptr)->flags &= ~__IEC_FORCE_FLAG;*)
(*DBG: break;*)
(*DBG: case REAL_ENUM:*)
(*DBG: ((__IEC_REAL_t *) ptr)->flags &= ~__IEC_FORCE_FLAG;*)
(*DBG: break;*)
(*DBG: case BOOL_ENUM:*)
(*DBG: ((__IEC_BOOL_t *) ptr)->flags &= ~__IEC_FORCE_FLAG;*)
(*DBG: break;*)
(*DBG: default:*)
(*DBG: break;*)
(*DBG: }*)
(*DBG: }*)
(*DBG:}*)
(*DBG:*)
(*DBG:void swap_bytes(void *ptr, size_t size) *)
(*DBG:{*)
(*DBG: uint8_t *bytePtr = (uint8_t *)ptr;*)
(*DBG: size_t i;*)
(*DBG: for (i = 0; i < size / 2; ++i) *)
(*DBG: {*)
(*DBG: uint8_t temp = bytePtr[i];*)
(*DBG: bytePtr[i] = bytePtr[size - 1 - i];*)
(*DBG: bytePtr[size - 1 - i] = temp;*)
(*DBG: }*)
(*DBG:}*)
(*DBG:*)
(*DBG:void trace_reset(void)*)
(*DBG:{*)
(*DBG: for (size_t i=0; i < VAR_COUNT; i++) *)
(*DBG: {*)
(*DBG: force_var(i, false, 0);*)
(*DBG: }*)
(*DBG:}*)
(*DBG:*)
(*DBG:void set_trace(size_t idx, bool forced, void *val)*)
(*DBG:{*)
(*DBG: if (idx >= 0 && idx < VAR_COUNT) *)
(*DBG: {*)
(*DBG: if (endianness == REVERSE_ENDIANNESS)*)
(*DBG: {*)
(*DBG: // Aaaaarghhhh... Stupid AVR is Big Endian.*)
(*DBG: swap_bytes(val, get_var_size(idx));*)
(*DBG: }*)
(*DBG:*)
(*DBG: force_var(idx, forced, val);*)
(*DBG: }*)
(*DBG:}*)
(*DBG:*)
(*DBG:void set_endianness(uint8_t value)*)
(*DBG:{*)
(*DBG: if (value == SAME_ENDIANNESS || value == REVERSE_ENDIANNESS)*)
(*DBG: {*)
(*DBG: endianness = value;*)
(*DBG: }*)
(*DBG:}*)
(*DBG:*)
Reply 0 0
thiagoralves
As I said, PWM_CONTROLLER will only work on Arduino boards. You would need to upload the code directly to the Arduino board and not use it as a slave.
Reply 0 0
jbe
If you’re trying to run it on a Raspberry Pi it won’t work. PWM_CONTROLLER block only works with some Arduino boards
ok kool did you have the list of arduino board tanks
 
jf
Reply 0 0
Dzahien
This Program is a Servo PWM program for the Arduino Uno R3 but should work for the Nano. It uses the PWM instruction in Ladder Diagram to move the servo -90 to zero to +90. It uses the S90 or S51 series.
There are comments as to how it is set up. Please refer to attached pdf for the program and picture of the Data Table.
PWM_Servo_Data_Table.png
Dear gfine, this is absolutely helpful. I have a question regarding the pin addressing.
 
Why is C1 = 1 but is at pin 10 on the board? Would you mind clarifying this for me or are there any documentation that I should be aware of this addressing?
 
Thank you
Reply 0 0
fg71
The pin 10 on Uno r3 is PWM pin and I believe the reason why the user set C1 = 1 would be to make the channel to be active for PWM Controller.
Reply 0 0
thiagoralves
Dear gfine, this is absolutely helpful. I have a question regarding the pin addressing.
 
Why is C1 = 1 but is at pin 10 on the board? Would you mind clarifying this for me or are there any documentation that I should be aware of this addressing?
 
Thank you
The Arduino Uno only has 3 PWM channels, that are available on pins 9,10 and 11
Reply 0 0
gfine
The Arduino Uno only has 3 PWM channels, that are available on pins 9,10 and 11
It has been awhile since I wrote this post. I believe that for this PWM instruction to reference the PWM at pin 10 the top line of the PWM instruction has to equal 1 ( C1 = 1) which is Channel 1 on pin 10. If you wanted to reference Channel 0 on pin 9 you would set C1 to 0 and if you wanted to reference Channel 2 on pin 11 you would set C1 to 2.
Reply 0 0
Reply