CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Fri Jul 28, 2017 4:00 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu May 18, 2017 12:23 am 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 38
Good evening,
I use this code to control modbus control
I use the digital outputs to control 6 on-board relays and analog inputs to check the status of relay relay-controlled relays to know their status.
I do not understand why when I pass an analog input into codesys i go high two inputs at a time.

I do not know if I made the problem clear but in case you could give me a hand?

Thank you, Alberto

Code:
#include <Controllino.h>  /* Usage of CONTROLLINO library allows you to use CONTROLLINO_xx aliases in your sketch. */
#include <ModbusRtu.h>
#define ID   2

Modbus slave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDI
int8_t state = 0;
unsigned long tempus;

// data array for modbus network sharing
uint16_t au16data[9];

void setup() {
  io_setup(); // I/O settings
  // start communication
  slave.begin( 9600 );
}

void loop() {
  // poll messages
  state = slave.poll( au16data, 9 );
  io_poll();
}
void io_setup() {
  pinMode(CONTROLLINO_D0, OUTPUT);
  pinMode(CONTROLLINO_D1, OUTPUT);  // note that we are using CONTROLLINO aliases for the digital outputs
  pinMode(CONTROLLINO_D2, OUTPUT);
  pinMode(CONTROLLINO_D3, OUTPUT);  // the alias is always like CONTROLLINO_
  pinMode(CONTROLLINO_D4, OUTPUT);  // and the digital output label as you can see at the CONTROLLINO device
  pinMode(CONTROLLINO_D5, OUTPUT);  // next to the digital output screw terminal

  pinMode(CONTROLLINO_A0, INPUT);  // next to the digital output screw terminal
  pinMode(CONTROLLINO_A1, INPUT);  // next to the digital output screw terminal
  pinMode(CONTROLLINO_A2, INPUT);  // next to the digital output screw terminal
  pinMode(CONTROLLINO_A3, INPUT);  // next to the digital output screw terminal
  pinMode(CONTROLLINO_A4, INPUT);  // next to the digital output screw terminal
  pinMode(CONTROLLINO_A5, INPUT);  // next to the digital output screw terminal
}

void io_poll() {

  // set digital outputs -> au16data[1]
  digitalWrite( CONTROLLINO_D0, bitRead( au16data[1], 0 ));
  digitalWrite( CONTROLLINO_D1, bitRead( au16data[1], 1 ));
  digitalWrite( CONTROLLINO_D2, bitRead( au16data[1], 2 ));
  digitalWrite( CONTROLLINO_D3, bitRead( au16data[1], 3 ));
  digitalWrite( CONTROLLINO_D4, bitRead( au16data[1], 4 ));
  digitalWrite( CONTROLLINO_D5, bitRead( au16data[1], 5 ));

  // get digital inputs -> au16data[0]
  bitWrite( au16data[0], 0, analogRead(CONTROLLINO_A0));
  bitWrite( au16data[0], 1, analogRead(CONTROLLINO_A1));
  bitWrite( au16data[0], 2, analogRead(CONTROLLINO_A2));
  bitWrite( au16data[0], 3, analogRead(CONTROLLINO_A3));
  bitWrite( au16data[0], 4, analogRead(CONTROLLINO_A4));
  bitWrite( au16data[0], 5, analogRead(CONTROLLINO_A5));

  // diagnose communication
  //au16data[6] = slave.getInCnt();
  //au16data[7] = slave.getOutCnt();
  //au16data[8] = slave.getErrCnt();
}


Top
   
PostPosted: Thu May 18, 2017 12:39 am 
Offline

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 33
Don't know anything about this but I'll take a guess:
Any chance it has to do with passing an INT value int a bit when you do bitWrite( au16data[0], 0, analogRead(CONTROLLINO_A0));


Top
   
PostPosted: Thu May 18, 2017 9:18 am 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 38
Hi Comingback4u,
You've caught the problem !!
If I put to 1 input A0 I also activate bit 1
If I put to 1 A1 and I put to 0 A0 I activates bit 1 and bit 2 and so on.

In the picture I put A0 to 1 !!

How can I solve on Arduino code?

Thank you, Alberto


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Thu May 18, 2017 2:38 pm 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 38
Solved !!
I hve onlychanged analogRead with digitalRead :)

Thanks for advice.

Alberto


Top
   
PostPosted: Fri May 19, 2017 12:20 am 
Offline

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 33
Glad I could help :D
Glad you got it working :D


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited