I believe I've made an accurate driver for the kit, however this is my first rodeo,
So if anyone has time to look it over to make sure ive done everything correctly, that would be much appreciated
I changed the pinout and pin masks to the correct pins for this kit,
updated the PWM initialization to include a 4th, as 4 digital PWM pins are used as analog outputs,
and inverted the inputs in updateInputBuffers()
#include <stdlib.h>
extern "C" {
#include "openplc.h"
}
#include "Arduino.h"
#include "../examples/Baremetal/defines.h"
#include "AVR_PWM.h"
//OpenPLC HAL for Arduino Nano with Canaduino PLC 100-12 DIY shield.
//Adapted from uno_leonardo_nano_micro_zero.cpp
//Removed Conditionals for other microcontrollers as this kit includes ATMEGA328 Arduino Nano
//Updated Pinouts, PWM Channels, and Inverted Digital Inputs
/******************PINOUT CONFIGURATION*******************
Digital In: 7, 8, 12, 13 (%IX0.0 - %IX0.3)
Digital Out: 2, 3, 4, 5, A2, A3 (%QX0.0 - %QX0.5)
Analog In: A0, A1, A6, A7 (%IW0 - %IW3)
Analog Out: 6, 9, 10, 11 (%QW0 - %QW3)
**********************************************************/
//Create the I/O pin masks
uint8_t pinMask_DIN[] = {7, 8, 12, 13};
uint8_t pinMask_AIN[] = {A0, A1, A6, A7};
uint8_t pinMask_DOUT[] = {2, 3, 4, 5, A2, A3};
uint8_t pinMask_AOUT[] = {6, 9, 10, 11};
// Define PWM instances directly without conditional compilation
#define PWM_DEFAULT_FREQ 490
#define NUM_OF_PWM_PINS 4
#define PWM_CHANNEL_0_PIN 6
#define PWM_CHANNEL_1_PIN 9
#define PWM_CHANNEL_2_PIN 10
#define PWM_CHANNEL_3_PIN 11
AVR_PWM *PWM_Instance[NUM_OF_PWM_PINS];
extern "C" uint8_t set_hardware_pwm(uint8_t, float, float); //this call is required for the C-based PWM block on the Editor
bool pwm_initialized = false;
void hardwareInit()
{
for (int i = 0; i < NUM_DISCRETE_INPUT; i++)
{
uint8_t pin = pinMask_DIN[i];
pinMode(pin, INPUT);
}
for (int i = 0; i < NUM_ANALOG_INPUT; i++)
{
uint8_t pin = pinMask_AIN[i];
pinMode(pin, INPUT);
}
for (int i = 0; i < NUM_DISCRETE_OUTPUT; i++)
{
uint8_t pin = pinMask_DOUT[i];
pinMode(pin, OUTPUT);
}
for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
{
uint8_t pin = pinMask_AOUT[i];
pinMode(pin, OUTPUT);
}
}
void init_pwm()
{
// If PWM_CONTROLLER block is being used, disable pins from regular analogWrite
const uint8_t pins[] = {PWM_CHANNEL_0_PIN, PWM_CHANNEL_1_PIN, PWM_CHANNEL_2_PIN, PWM_CHANNEL_3_PIN};
for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
{
for (int j = 0; j < NUM_OF_PWM_PINS; j++)
{
if (pinMask_AOUT[i] == pins[j])
{
pinMask_AOUT[i] = 255; //disable pin
}
}
}
// Initialize PWM pins
PWM_Instance[0] = new AVR_PWM(PWM_CHANNEL_0_PIN, PWM_DEFAULT_FREQ, 0);
PWM_Instance[1] = new AVR_PWM(PWM_CHANNEL_1_PIN, PWM_DEFAULT_FREQ, 0);
PWM_Instance[2] = new AVR_PWM(PWM_CHANNEL_2_PIN, PWM_DEFAULT_FREQ, 0);
PWM_Instance[3] = new AVR_PWM(PWM_CHANNEL_3_PIN, PWM_DEFAULT_FREQ, 0);
}
uint8_t set_hardware_pwm(uint8_t ch, float freq, float duty)
{
if (pwm_initialized == false)
{
init_pwm();
pwm_initialized = true;
}
const uint8_t pins[] = {PWM_CHANNEL_0_PIN, PWM_CHANNEL_1_PIN, PWM_CHANNEL_2_PIN, PWM_CHANNEL_3_PIN};
if (ch >= NUM_OF_PWM_PINS)
{
return 0;
}
if (PWM_Instance[ch]->setPWM(pins[ch], freq, duty))
{
return 1;
}
return 0;
}
void updateInputBuffers()
{
for (int i = 0; i < NUM_DISCRETE_INPUT; i++)
{
uint8_t pin = pinMask_DIN[i];
// Read the digital input value
bool inputValue = digitalRead(pin);
// Invert the logic
inputValue = !inputValue;
// Update the input buffer
if (bool_input[i/8][i%8] != NULL)
*bool_input[i/8][i%8] = inputValue;
}
for (int i = 0; i < NUM_ANALOG_INPUT; i++)
{
uint8_t pin = pinMask_AIN[i];
if (int_input[i] != NULL)
*int_input[i] = (analogRead(pin) * 64);
}
}
void updateOutputBuffers()
{
for (int i = 0; i < NUM_DISCRETE_OUTPUT; i++)
{
uint8_t pin = pinMask_DOUT[i];
if (bool_output[i/8][i%8] != NULL)
digitalWrite(pin, *bool_output[i/8][i%8]);
}
for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
{
uint8_t pin = pinMask_AOUT[i];
if (int_output[i] != NULL)
analogWrite(pin, (*int_output[i] / 256));
}
}