Is this supposed to work?

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Is this supposed to work?

#1 Post by LGR » 29 Feb 2012 01:23

I've got an application that uses Modbus, and so several bits are assigned to an array of byte called "coils". I'd like to give those bits aliases, so that they make more sense in the program, but the compiler doesn't like this:

Code: Select all

var out1c   : sbit at coils[0].0; out2c   : sbit at coils[0].1;
    out3c   : sbit at coils[0].2; updownc : sbit at coils[0].3;
etc..

Shouldn't that syntax be valid?
If you know what you're doing, you're not learning anything.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Is this supposed to work?

#2 Post by filip » 29 Feb 2012 11:35

Hi,

From the help file
The mikroPascal PRO for PIC32 compiler has sbit data type which provides access to bit-addressable SFRs.
You can declare a sbit varible in a unit in such way that it points to a specific bit in SFR register.
As your structure is not a SFR, this cannot be achieved using sbit.

Regards,
Filip.

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

Re: Is this supposed to work?

#3 Post by jpc » 29 Feb 2012 13:57

maybe you can store your coils in a few 32bit vars as this seems to work

Code: Select all

program sbit;

var coils : dword;
    test : sbit at coils.27;
begin
   coils := 0;
   test := 1;
   test := 0;
end.
The help does not limit the sbit to SFR's and gives as example

Code: Select all

program MyProgram;
...
var MyVar: byte;
var Abit: sbit at MyVar.0; // this is where Abit is fully defined
...
begin
...
end.

Au royaume des aveugles, les borgnes sont rois.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Is this supposed to work?

#4 Post by janni » 29 Feb 2012 14:19

Looks like sbits scope extended to GPR arrays is a popular expectation - similar post may be found here.
Or could the bit type have an option to point at specific bits?

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Is this supposed to work?

#5 Post by janni » 29 Feb 2012 15:07

jpc wrote:maybe you can store your coils in a few 32bit vars as this seems to work

Code: Select all

program sbit;

var coils : dword;
    test : sbit at coils.27;
begin
   coils := 0;
   test := 1;
   test := 0;
end.
Indeed :) . For small arrays one may even map the variable over array and have both ways of accessing the bits

Code: Select all

program sbit;

var coils : array[4] of byte;
    coilsb : dword at coils;
    out1c : sbit at coilsb.0;
    updownc : sbit at coilsb.16;

begin
   coils[2]:=0;
   coils[2].0 :=1;  // equivalent
   updownc:=1;      // assignments
   
end.
Pity it's not possible in mP for PIC :( .

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: Is this supposed to work?

#6 Post by LGR » 29 Feb 2012 16:39

Thanks for the suggestions, but the natural definitional structure for Modbus coils is bits packed into an array of byte, and the array can potentially be large. This is what makes such a cumbersome expression necessary.

I'll experiment with some other ideas.
If you know what you're doing, you're not learning anything.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Is this supposed to work?

#7 Post by janni » 29 Feb 2012 19:02

MODBUS interface is cheap compared to newer ones, like Profibus/Fieldbus or Profinet, so devices rarely have more than 16 digital outputs. Truth is, I also never assumed less than 64 coils or inputs when developing MODBUS interfaces, but, naturally, never came close to this in final devices :wink: . On the other hand, even if I had to use lots of input/outputs, I would probably never attempt to name in code more than few of them - I'd left that for the user :lol: .

Post Reply

Return to “mikroPascal PRO for PIC32 General”