CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Function with xfer name of Stream

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
xtremeroboz



Joined: 15 Mar 2019
Posts: 2

View user's profile Send private message

Function with xfer name of Stream
PostPosted: Fri Mar 15, 2019 1:49 am     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Fri Mar 15, 2019 3:20 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Mar 15, 2019 8:05 am     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Fri Mar 15, 2019 8:53 am     Reply with quote

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: 9098
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 15, 2019 1:12 pm     Reply with quote

BTDT,, got stumped for quite awhile..... Sad
Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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