How to access a port trough a C function using DEMO9RS08KA2 platform

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to access a port trough a C function using DEMO9RS08KA2 platform

2,606 Views
SLC
Contributor I
Hello everyone,
 
I am new one in the world of micro-controllers. I try to acquire some experience with the demo board DEMO9RS08KA2.
 
My first target is to fade a led using C language. I wrote a C function and I want to pass by argument the Ports that is connected to the led.
 
So the code is:
 
#define LED2 PTAD_PTAD5
 
void GlowLed(char *led)
{
.....
   .... *led = (cmpt<inc) ? ON:smileysurprised:FF;
....
}
 
void main () {
....
....
...
 GlowLed(&LED2)
....
 
}
 
when I compile the C code, the following masque is sent by the compiler
"Cannot take the addressee of this object "
 
SO !!??
 
My modesty impeach me to accept that I make a mistake or I miss something:smileywink:
 
Do you have the solutions or document that could help me
 
Thanks in advance for your help
 
Serge
 
Labels (1)
0 Kudos
8 Replies

685 Views
Navidad
Contributor III
The thing is that you cannot take the address of a bitfield (you cannot express that an object starts at bit x of byte y). If you want to pass the port's address by address, you will have to cast it to a pointer to char, but then you will have to do the bit arithmetic yourself. But is it really necessary to have a generic function here ? After all, you only have port A on RS08, so you will not be able to apply this function to any other port.
0 Kudos

685 Views
JimDon
Senior Contributor III
I understand you want to do things in an encapsulated manner, and not just hard code so it will  be easier to port your code later.

Here is one way that will work:

Code:
#define LED_PORT     (byte*) PTAD
#define LED_BIT          PTAD_PTAD5_MASK
 
 void SetLED(byte* port, byte bit)  {     *port |= bit;     // set the bit
     *port &= ~bit;    // Clear the bit
     *port ^= bit;     // Toggle the bit
 }....  SetLED(LED_PORT,LED_BIT);

 

0 Kudos

685 Views
bigmac
Specialist III
Hello,
 
Assuming the OP wishes to use a bit number, per the original definition of LED2, the expressions used would first need to convert the bit number to a mask value, and then do the bit manipulation.
 
*port |= (1<<bit);     // set the bit
 
*port &= ~(1<<bit);    // Clear the bit
    
*port ^= (1<<bit);     // Toggle the bit
 
A set_bit() function, that would incorporate the first expression, could then be called in the following manner -
 
#define LED_PORT PTAD
#define LED2     PTAD_PTAD5
 
set_bit( &LED_PORT, LED2)
 
Regards,
Mac
 
0 Kudos

685 Views
JimDon
Senior Contributor III
Or that.
But I was thinking that you might  want to use the standard bit names from the h file, so you don't even care what the bit number is.

0 Kudos

685 Views
bigmac
Specialist III
Hello Serge,
 
On second thoughts, the resources of the KA2 device are so limited, that if you wish to program in C, the coding will need to be very direct.  "Encapsulation" is probably something you cannot afford in this case, because of the extra resources it will consume.
 
It is debatable whether this device should even be coded using C.
 
#define LED2 PTAD_PTAD5
 
void main () {
....
....
...
  if (cmpt < inc)
    LED2 = ON;
  else
    LED2 = OFF;
....
}
 
This is likely to produce minimal code size, with minimum RAM usage.  Since the KA2 has no stack, and is limited to one nesting level for sub-routines, additional overheads will be required to allocate local variables, and for additional levels of nested C functions.  For sufficiently compact code, you will need to minimize the use of variables, and the nesting of functions.
 
Regards,
Mac
 
0 Kudos

685 Views
JimDon
Senior Contributor III
Well, it was a "C question, but you do have a point.
The KA2 is VERY minimal, and you may wish to use asm if you are going to do much with it.
In fact, if you go the KA Documentation tab, there are some pretty neat asm examples.

I think all the KS examples are asm.



0 Kudos

685 Views
SLC
Contributor I
Hello Navidad, JimDon and BigMac,
 
Thanks for your answers.
 
I am agree with the last remark. It will be better to write the code in asm in the case of MC9RS08KA2. But as JimDon said I want to port later this code on another Microcontroller which I don't know at this moment. The Microcontroller will be defined in the next weeks. This microcontroller must be have an USB interface. So I expect to be not limited with this.
 
I still trying your suggestions.
 
Regards
 
Serge
0 Kudos

685 Views
JimDon
Senior Contributor III
If you haven't already, I suugest looking at the JM family.

The demo board is available from mouser for 46.00.

It comes with sample code for many USB devices.
0 Kudos