Type Specifier

General discussion on mikroPascal PRO for AVR.
Post Reply
Author
Message
elmar.faber
Posts: 15
Joined: 03 Jul 2014 11:43

Type Specifier

#1 Post by elmar.faber » 13 Jul 2014 12:06

Hello,

is there a possibility to declare something like

Code: Select all

Type
  TPWM0_MODE = (PWM0_MODE_00, PWM0_MODE_01);
...

Procedure PWM0_Init(wave_mode : TPWM0_MODE; prescaler : Byte);

or something equivalent?

Thank you

best regards

Elmar

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

Re: Type Specifier

#2 Post by filip » 14 Jul 2014 14:29

Hi,

You can use this piece code, for example :

Code: Select all

Program MyProject;

type TPWM0_MODE = record
  PWM0_MODE_00 : byte;
  PWM0_MODE_01 : byte;
end;

var mode : TPWM0_MODE;
var test0, test1 : byte;

Procedure PWM0_Init(var wave_mode : TPWM0_MODE; prescaler : Byte);
begin
  test0 := mode.PWM0_MODE_00;
  test1 := mode.PWM0_MODE_01;
end;

begin
  mode.PWM0_MODE_00 := 0xAA;
  mode.PWM0_MODE_01 := 0xBB;
  PWM0_Init(mode, 1);
end.
Regards,
Filip.

elmar.faber
Posts: 15
Joined: 03 Jul 2014 11:43

Re: Type Specifier

#3 Post by elmar.faber » 16 Jul 2014 09:53

Thank you Filip for your reply,

but I think it isn't what I mean. In your case ther are no posibillity to define
a construction like:

Code: Select all

Procedure PWM0_Init(var wave_mode : TPWM0_MODE; prescaler : Byte);
begin
   Case wavemode Of
     ... : Begin
           End;
   End;
end;
In this way I can define one wave mode for the PWM Channel.

Best regards

Elmar

Olso
Posts: 20
Joined: 22 Feb 2013 20:49

Re: Type Specifier

#4 Post by Olso » 16 Jul 2014 16:52

elmar.faber wrote:Thank you Filip for your reply,

but I think it isn't what I mean. In your case ther are no posibillity to define
a construction like:

Code: Select all

Type
  TPWM0_MODE = (PWM0_MODE_00, PWM0_MODE_01);
...
Procedure PWM0_Init(var wave_mode : TPWM0_MODE; prescaler : Byte);
begin
   Case wavemode Of
     ... : Begin
           End;
   End;
end;
In this way I can define one wave mode for the PWM Channel.
Hello Elmar,

Why don't you declare something like this:

Code: Select all

type
   TPWM0_MODE = byte;

const
   PWM0_MODE_00 = 0;
   PWM0_MODE_01 = 1;

Procedure PWM0_Init(var wave_mode : TPWM0_MODE; prescaler : Byte);
begin
   Case wave_mode Of
      PWM0_MODE_00 : Begin
           End;
   End;
end;
Regards,

Olso

elmar.faber
Posts: 15
Joined: 03 Jul 2014 11:43

Re: Type Specifier

#5 Post by elmar.faber » 17 Jul 2014 09:32

Hello Olso,

yes it's another way thats right but all this don't avoid that
I could change the params of the procedure.
This declaration would avoid that I take the wrong params:

Type
TPWM0_MODE = (PWM0_MODE_00, PWM0_MODE_01);
TPWM0_PRESC = (PWM0_PRESCALER_0, PWM0_PRESCALER_1, ...);

Procedure PWM0_Init(var wave_mode : TPWM0_MODE; TPWM0_prescale : Byte);

Sample:

PWM0_Init(PWM0_MODE_00, PWM0_PRESCALER_1);

Otherwise this is possible:

PWM0_Init(PWM0_PRESCALER_1, PWM0_MODE_00);

That's my intension for my question :-)

Best regards

Elmar

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Type Specifier

#6 Post by Dany » 17 Jul 2014 12:50

There is no enumeration type in mP, so you should use the method Olso suggested. mP is no so strong typed as it could be...
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Olso
Posts: 20
Joined: 22 Feb 2013 20:49

Re: Type Specifier

#7 Post by Olso » 19 Jul 2014 11:03

Hello Elmart,
elmar.faber wrote:yes it's another way thats right but all this don't avoid that
I could change the params of the procedure.
This declaration would avoid that I take the wrong params:
[...]
That's my intension for my question :-)
Yes, of course...

To avoid a wrong value when running, you can force it with a mask:
PWM0_MODE_00 := PWM0_MODE_00 and 1;

If you want the compiler to react when compiling, you must define some different types for the different variables, but it is not an easy task with mPP...
Dany wrote:There is no enumeration type in mP, so you should use the method Olso suggested. mP is no so strong typed as it could be...
Yes. Here's one way to force compiler to cry at compile time with an inversion of parameters:

Code: Select all

//Type TPWM0_MODE = (PWM0_MODE_00, PWM0_MODE_01);
//Procedure PWM0_Init(wave_mode : TPWM0_MODE; prescaler : Byte);
const
  PWM0_PRESCALER_0 = 0;
  PWM0_PRESCALER_1 = 1;

type
  TPWM0_MODE = function: byte;
  pTPWM0_MODE = ^TPWM0_MODE;

var
  prescaler: byte;
  
 function PWM0_MODE_00: byte; begin result := 0 end;
 function PWM0_MODE_01: byte; begin result := 1 end;

 procedure proc(wave_mode: pTPWM0_MODE; prescaler: byte);
 begin
    ...
 end;


begin
  ...
  proc(@PWM0_MODE_00, PWM0_PRESCALER_1);         // accepted
  proc(@PWM0_PRESCALER_1, PWM0_MODE_00);         // compiler error: 'Operator "@" not applicable to these operands'
  ...
end;
Operational, but not a good solution, naturally.

Best regards,

Post Reply

Return to “mikroPascal PRO for AVR General”