|
|
View previous topic :: View next topic |
Author |
Message |
xtremeroboz
Joined: 15 Mar 2019 Posts: 2
|
Function with xfer name of Stream |
Posted: Fri Mar 15, 2019 1:49 am |
|
|
Hi Everyone
I try to create a function with xfer, this is part of my code:
Code: |
#use spi (MASTER, CLK=SPI_CLOCK, DI=SPI_DATA, LOAD=SPI_LOAD, LOAD_ACTIVE=1, MODE=2, BITS=8, STREAM=SHIFT_REG_A, MSB_FIRST)
#use spi (MASTER, CLK=SPI_CLOCK, DI=SPI_DATA, LOAD=SPI_LOAD, LOAD_ACTIVE=1, MODE=1, BITS=8, STREAM=SHIFT_REG_B, MSB_FIRST)
#use spi (MASTER, CLK=SPI_CLOCK, DI=SPI_DATA, LOAD=SPI_LOAD, LOAD_ACTIVE=1, MODE=0, BITS=8, STREAM=SHIFT_REG_C, MSB_FIRST)
//This is My Function
unsigned int ReadShiftRegister(char NUM_SHIFT_REGISTER)
{
unsigned int VAL_SHIFT_REGISTER;
VAL_SHIFT_REGISTER = spi_xfer(NUM_SHIFT_REGISTER, 0x00);
return VAL_SHIFT_REGISTER;
}
|
I have trouble with variable NUM_SHIFT_REGISTER
*** Error 130 "shifts.c" Line 315(51,55): Stream must be a constant in the valid range ::
Thanks for your help |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Fri Mar 15, 2019 3:20 am |
|
|
The point is that stream names _have to be constants_.
If you want to use a variable, you have to code this yourself.
Understand that when a stream name is used for a function like SPI_XFER,
the code to physically access the named stream is substituted.
So if you want to use a variable to access three different streams, you
would have to just use a variable, and a switch statement to call the
three different output routines. So:
Code: |
//This is My Function
unsigned int ReadShiftRegister(char NUM_SHIFT_REGISTER)
{
unsigned int VAL_SHIFT_REGISTER;
switch (NUM_SHIFT_REGISTER) {
case 0:
VAL_SHIFT_REGISTER = spi_xfer(SHIFT_REG_A_REGISTER, 0x00);
break;
case 1:
VAL_SHIFT_REGISTER = spi_xfer(SHIFT_REG_B_REGISTER, 0x00);
break;
case 2:
VAL_SHIFT_REGISTER = spi_xfer(SHIFT_REG_B_REGISTER, 0x00);
break;
}
return VAL_SHIFT_REGISTER;
}
|
As a general 'style comment', in C, it is standard to reserve 'ALL CAPITALS'
for things that are #defined, or otherwise fixed (so typedefs for example).
So better to call a variable that accesses a shift register number
'Val_Shift_Register'.
As a standard, it helps when debugging code, to quickly be able to
distinguish things that are variables from things that are not.
Just a useful 'tip' to help code legibility. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Mar 15, 2019 8:05 am |
|
|
Ttelmah wrote: | As a general 'style comment', in C, it is standard to reserve 'ALL CAPITALS'
for things that are #defined, or otherwise fixed (so typedefs for example).
|
This is important, especially when #case is used. I had a bunch of stream names stop working suddenly when I added #case. Apparently the compiler capitalized the stream names. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Fri Mar 15, 2019 8:53 am |
|
|
Yes, the one for streams is very important. CCS defaults internally to
capitalising all stream names, and if you implement #case, and use a
stream name that is not capitalised, it won't work... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Mar 15, 2019 1:12 pm |
|
|
BTDT,, got stumped for quite awhile.....
Jay |
|
|
|
|
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 vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|