Expecting LVALUE such as variable name or *expression ----error - Page 1
Forum section: electronics / general

author: radhika
author_rank: Regular Contributor
timestamp: December 08, 2018, 08:12:45 am
content: Hello,I am working on speaker code where i am using MCP4131 digital potentiometer to increase or decrease the volume. This volume display asVOLUME: xx (xx= volume in digit). I am getting some error. Here is the code:Code:#define SSD1306_RST PIN_D4#include <18F4520.h>#fuses HS,NOWDT,NOPROTECT,NOLVP#use delay(clock = 20MHz)#use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // Initialize I2C// Include SSD1306 OLED driver source code#include <SSD1306.c>//#include <SSD1306OLED.c>#define Chip PIN_B4 //Chip Select#define Clock PIN_B5 //Serial Clock#define Data PIN_B6 //Serial Data In// End specification for pinsvolatile unsigned long button_select, button_up, button_down;void SendPotValue (unsigned short num);void SelectArrow();//POT values;unsigned short aa, count1, i;unsigned int t, Flag, Mask;const char MESSAGE[] = "VOLUME:";void SendPotValue(unsigned short num1){Mask = 0x80;Data = 1;Clock = 1;Chip = 1;delay_ms(5);Clock = 0;// Shift in 8-bit wiper position for POTAfor (t=0; t<8; t++){Flag = num1 & Mask;if(Flag==0)Data = 0;Chip = 0;elseDI = 1;Clock = 1;Chip = 1;delay_ms(1);Clock = 0;Mask = Mask >> 1;}SSD1306_GotoXY(1, 6); //XCugas PowerSSD1306_PutC(MESSAGE);SelectArrow();}void SelectArrow(){for (i=0; i<count1; i++) {SSD1306_GotoXY(1, 6); //XCugas PowerSSD1306_PutC(MESSAGE: count1);}}void Debounce_Key(){delay_ms(200);}void main() {CMCON = 0x07; // Disable comparatorsADCON1 = 0x0F; // Disable Analog functionsSSD1306_GotoXY(1, 6); //XCugas PowerSSD1306_PutC(MESSAGE);aa = 50;count1 = 2;SendPotValue(aa);do {if (!Up) {Debounce_Key();if (aa < 250) {aa = aa + 25;count1 = aa/25;}SendPotValue(aa);}if (!Down) {Debounce_Key();if (aa > 0) {aa = aa - 25;count1 = aa/25;}SendPotValue(aa);}if (!Select) {Debounce_Key();SelectArrow();}} while (1);}I am using MPLAB IDE and CCS C compiler.Here is the error i am getting :https://postimg.cc/TKwfVwMnThe code may have some logic issue too. I am new at this. I can't figure out how to make it. Please guide. How do I make this code run.ThanksGood Day

author: Fred27
author_rank: Unknown
timestamp: December 08, 2018, 10:28:21 am
content: I think the compiler has assumed that Data is a constant as you've not defined its type. Then you try to change the value. (That image site doesn't work well on a phone, so hard to tell.)

author: radhika
author_rank: Regular Contributor
timestamp: December 08, 2018, 10:36:51 am
content: Quote from: Fred27 on December 08, 2018, 10:28:21 amThanks,Am I need to define it's type as int?And here is the error:Error 49 "speaker.c" Line 37(1,6): Expecting LVALUE such as a variable name or * expression*** Error 49 "speaker.c" Line 38(1,7): Expecting LVALUE such as a variable name or * expression*** Error 49 "speaker.c" Line 39(1,6): Expecting LVALUE such as a variable name or * expression*** Error 49 "speaker.c" Line 41(1,7): Expecting LVALUE such as a variable name or * expression*** Error 49 "speaker.c" Line 47(4,9): Expecting LVALUE such as a variable name or * expression*** Error 49 "speaker.c" Line 48(4,9): Expecting LVALUE such as a variable name or * expression*** Error 51 "speaker.c" Line 49(4,8): A numeric expression must appear here*** Error 49 "speaker.c" Line 51(4,10): Expecting LVALUE such as a variable name or * expression*** Error 49 "speaker.c" Line 52(4,9): Expecting LVALUE such as a variable name or * expression*** Error 49 "speaker.c" Line 54(4,10): Expecting LVALUE such as a variable name or * expression*** Error 58 "speaker.c" Line 66(23,24): Expecting a close paren*** Error 12 "speaker.c" Line 80(1,6): Undefined identifier CMCON*** Error 12 "speaker.c" Line 81(1,7): Undefined identifier ADCON1*** Error 103 "speaker.c" Line 86(7,9): Constant out of the valid range 50 is not 0..1*** Error 103 "speaker.c" Line 87(11,12): Constant out of the valid range 2 is not 0..1*** Error 12 "speaker.c" Line 90(8,10): Undefined identifier Up*** Error 12 "speaker.c" Line 101(8,12): Undefined identifier Down*** Error 12 "speaker.c" Line 110(8,14): Undefined identifier Select18 Errors, 0 Warnings.Build Failed.

author: newbrain
author_rank: Super Contributor
timestamp: December 08, 2018, 10:44:14 am
content: I do not do PICs so I will only briefly comment on the specific part.The error you are getting refers to something called an LVALUE.As I seem to understand you are a beginner, I'll keep it simple and say that an "LVALUE" is something that can stand on the left side of an assignment operator, i.e. a "left value"*.In this case the offending element isData, defined in your code asPIN_B6.Unless PIN_B6 is:Defined in one of the include files (possibly 18F4520.h)ANDDefined in a way it can be assigned to,You will not be able to use it as the target of an assignment operator.As I don't know what's inside those file, this is all I can sayon the specific problem.But, being old and and grumpy I can say a lot on other stuff:Using images for error message is not convenient for people willing to help, I hope your IDE supports copy & paste also in that window!Having the code correctly formatted is not only pleasant to the eye, it also help finding error before the compiler can, and sometimes, errors that the compiler cannot identify, i.e. correct code, doing something you did not intend.Your IDE should be able to do the formatting automatically while you type or on request from a menu or a key combination. If not, you should use a real one.Why all of the above? Look at what you're trying to do in the loop inside SendPotValue, something is not right.Some identifier is not declared, e.g. DI (should that be Data?), Up, Down (a good IDE would also show this!).This does not look like C:SSD1306_PutC(MESSAGE: count1);I think it was supposed to be:SSD1306_PutC(MESSAGE[ count1]);Again, a decent IDE would look like a Xmas tree with error indicators!I would suggest getting a copy of K&R, and going through all the exercises, you'll have a simpler life afterwards!EtA: That debounce strategy is a bit primitive, there are much better ways...but this might be the topic for another time.*For more complete (and correct) information, refer to the e.g. theC99 standard, chapter 6.3.2.1

author: newbrain
author_rank: Super Contributor
timestamp: December 08, 2018, 11:04:38 am
content: Quote from: radhika on December 08, 2018, 10:36:51 amIn fact, you need to do quite a lot more than that.I understand you are trying to send a value to a digital potentiometer, bit banging an SPI-like interface.I don't know if the MCU you are using has got an SPI (probably yes), but still it might be good do it manually for learning purposes.Defining Data as an integer is not going to help: you have to find how to manipulate the GPIO pins on you MCU, and use the right (= suggested by the vendor) way to do it.In most cases, it is simply a matter of using the right identifier found the in the MCU include file.If you simply redefine Data as "unsigned int Data;" or something similar, nothing will happen on you output ports.The same applies for the pins used as clock and chip select.

author: tsman
author_rank: Frequent Contributor
timestamp: December 08, 2018, 01:06:49 pm
content: You need to work on this project a single step at a time and get each individual part working before adding more features. It looks like you're pasting together portions of multiple programs you found online that aren't compatible with CCS. Your previous thread had the same issues about code that isn't designed for the version of CCS you're using and you're getting similar errors again about missing register identifiers like ADCON1.

author: rstofer
author_rank: Super Contributor
timestamp: December 08, 2018, 05:04:19 pm
content: Are you using Google? Try a search for something like 'ccs c pic pin io'One response is this:https://www.ccsinfo.com/faq.php?page=map_io_to_variableYou can't talk to individual pins without defining them in a struct. You can read/write an entire 8 bit port and that's about it. However, if Data is one field in a structure, you can read PORTB.DataI don't know where you are getting your code. It simply won't work. Embedded programming is just a lot more complex than some high level application.You have 3 problems:1) Getting started with the vagaries of PIC. In the early days I would use these and in particular the 16F877A and a couple of others. I didn't like them but there wasn't a lot of choice. Then I moved to AVR and now I stay with either AVR (Arduino) or ARM.2) Learning C. Kernighan & Ritchie "The C Programming Language" is a 40 year old book that is considered the holy grail of C. The newer edition complies with ANSI C. I would search on Aibris.com for a used copy of either or both.3) Understanding how to beat C into submission for handling embedded programming. How to declare structs, how to define bitfields, understanding the dreaded "read-modify-write" operation on a PIC. Here's something fun: The layout of bit fields is not defined in the standard. Sometimes a struct will lay down left to right, sometimes right to left and it will depend on the compiler and the chip.When you understand every line of the link I posted above, you will understand why Data isn't an lvalue and, even if it were, it would do what you want.

author: rstofer
author_rank: Super Contributor
timestamp: December 08, 2018, 05:19:53 pm
content: One more thing: When you get something like PORTB_struct.Data = 1 to work, look at the emitted assembler code and see if it looks right.I would assume it would read PORTB and OR in the value of Data (shifted into position) then write back the value of PORTB. Something like that.If you had PORTB_struct.Data = 0, it would read PORTB and AND in the complement of Data (shifted into position) then write back the value of PORTB.You need to understand the code emitted by the compiler.The 18F apparently has bit-banding so it is possible to write directly to an individual bit but you would need to read the documentation to see how that works. CCS documentation to see how the compiler handles it, PIC documentation to see how the hardware handles it and then find some examples. I have never used bit banding so I can't point you in a direction. Given that the Reference Manual is 976 pages (more or less), I don't suppose I'll be reading it any time soon.

author: JPortici
author_rank: Super Contributor
timestamp: December 08, 2018, 07:19:14 pm
content: PIC18F doesn't have bit banding. it does have however atomic BSF and BCF instructions that can work on any register in addressable memoryso if it doesn't compile into a single instruction, throw that POS CCS away. i would throw it away anyway, learn C, learn how to write my own code to stop copying things around for different compilers and bodging them together.... and learn how to use the official tools

author: rstofer
author_rank: Super Contributor
timestamp: December 08, 2018, 08:04:51 pm
content: Quote from: JPortici on December 08, 2018, 07:19:14 pmI didn't think it had bit banding either but I ran across something that confused me into believing it did. With BSF and BCF, the code would indeed be simplified.Nevertheless, the OP should expect to have to look at the assembly code emitted by the compiler. I don't know anything about the 18F series but C for the 16F series was pretty grim. Then again, so was the architecture.

author: richardman
author_rank: Frequent Contributor
timestamp: December 09, 2018, 12:11:48 am
content: The if/else fragments make me think that you are missing a bunch of { } as well to make things into legal C.Sorry to say this, but you need to take some good introductory lessons in C. There are plenty on the web.

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 09, 2018, 03:41:56 am
content: yes. try to solve that out first. C will spit miscellaneous errors if you break the { } rule... try thisCode:// Shift in 8-bit wiper position for POTAfor (t=0; t<8; t++){Flag = num1 & Mask;if(Flag==0) {Data = 0;Chip = 0;} else {DI = 1;Clock = 1;Chip = 1;delay_ms(1);Clock = 0;Mask = Mask >> 1;}}and recompile see what will happen.

author: rjjj™
author_rank: Newbie
timestamp: December 09, 2018, 02:20:24 pm
content: It is simple. Those are constants according to the C language:Code:#define Chip PIN_B4 //Chip Select#define Clock PIN_B5 //Serial Clock#define Data PIN_B6 //Serial Data InThis means you may not use the assignment operator (=) to give them new values, for example:Code:Data = 1;Clock = 1;Chip = 1;Using the library 18F4520.h, just applyoutput_highandoutput_lowto change the logic levels of an output pin:Code:output_high(Data);output_high(Clock);output_high(Chip);I hope that I have helped.

author: Jeroen3
author_rank: Super Contributor
timestamp: December 09, 2018, 03:50:20 pm
content: Quote from: rjjj™ on December 09, 2018, 02:20:24 pmLet's go all the way.Your code:Code:#define Data PIN_B6 //Serial Data In18F4520.h:Code:#define PIN_B6 31758That means that this line:Code:Data = 1;Is preprocessed to (remember, #define is apreprocessorkeyword):Code:31758 = 1;Which is obviously ridiculous. You cannot assign an integer literal to an integer literal.

author: radhika
author_rank: Regular Contributor
timestamp: December 10, 2018, 03:30:03 pm
content: Quote from: newbrain on December 08, 2018, 10:44:14 amThis is great. I am a beginner and yes I did lot much mistake. But, your reply seem so good :0 And this picture.. That's look more Front valueI will take care for the next time and compile again in proper format. Good Day

author: radhika
author_rank: Regular Contributor
timestamp: December 10, 2018, 03:39:02 pm
content: Quote from: tsman on December 08, 2018, 01:06:49 pmI worked on MPLAB X IDE using C18 compiler.But, because of some issue i have to use CCS Compiler which I never used before. And hence the code which i developed earlier for other compiler I have to convert it to CCS and this is the only reason Mam/Sir I am facing problemThanksGood Day

author: radhika
author_rank: Regular Contributor
timestamp: December 10, 2018, 04:33:02 pm
content: Quote from: rjjj™ on December 09, 2018, 02:20:24 pmThank you so much. I did the required change and things work

author: radhika
author_rank: Regular Contributor
timestamp: December 10, 2018, 04:37:09 pm
content: Thank you so much every oneI did required changes suggested by you all.I am facing this 3 error now. How do I solve this error?*** Error 58 "speaker.c" Line 72(24,30): Expecting a close paren ---------------------------- SSD1306_PutC(MESSAGE count1);*** Error 103 "speaker.c" Line 88(7,9): Constant out of the valid range 50 is not 0..1------- aa = 50;*** Error 103 "speaker.c" Line 89(11,12): Constant out of the valid range 2 is not 0..1----- count1 = 2;3 Errors, 0 Warnings.Build Failed.The 50 is the initial value of the digital potentiometer.

author: newbrain
author_rank: Super Contributor
timestamp: December 10, 2018, 07:13:51 pm
content: Quote from: radhika on December 10, 2018, 04:37:09 pmThe first one is easy. That routine (supposedly) emits a single character on the display and expects a (possibly const) 'char' argument.MESSAGE is an array of const char, and to select just one item you need to use the [] operator.To quote myself:Quote from: newbrain on December 08, 2018, 10:44:14 amAbout the other two:That's a strange error, how are those two variables declared? Still 'unsigned short '?Even if they were _Bool or bitfields I would not expect this diagnostic.I checked that compiler manual, and it's not even remotely standard compliant.Iwould really, really, hate to use it.In CCS,shortare a single bit (according to the standard they should be at least 2 bytes), so with your current declarationunsigned short aa, count1;it's impossible to store more than 0..1 into them. Try changing them to 'unsigned int'...

author: rstofer
author_rank: Super Contributor
timestamp: December 11, 2018, 02:11:10 am
content: Quote from: newbrain on December 10, 2018, 07:13:51 pmThere should be a LAW! A short is supposed to be 16 bits according to the Wiki. I didn't actually dig out the standard but I think that is correct:https://en.wikipedia.org/wiki/C_data_types

author: glarsson
author_rank: Frequent Contributor
timestamp: December 11, 2018, 07:45:58 am
content: Quote from: rstofer on December 11, 2018, 02:11:10 amNo. Both the C standard and the Wikipedia page says that a short is at least 16 bits. It usually is 16 bits, but can use more bits on some processor/compiler combinations.

author: newbrain
author_rank: Super Contributor
timestamp: December 11, 2018, 08:17:24 am
content: Quote from: glarsson on December 11, 2018, 07:45:58 amI also checked the relevant chapter when writing my post, then forg...Look! A squirrel!It's clearly stated in "5.2.4.2.1 Sizes of integer types <limits.h>":QuoteAnd, moreover, that brain-dead short definition also breaks the conversion rank as defined in 6.3.1.1:QuoteIt is a really silly thing to do, if one considers that the standard (since C99, IIRC) make explicit provision for extended signed and unsigned integer types (6.2.5, clause 4 and 6).They could have called it 'unsigned __bit' and kept at least a semblance of compliance.Better yet, they could just optimize the code when flipping single bits.

author: radhika
author_rank: Regular Contributor
timestamp: December 12, 2018, 03:32:34 pm
content: Thank you so much everyone. The unsigned int solve my problem

author: Nerull
author_rank: Frequent Contributor
timestamp: December 12, 2018, 06:38:17 pm
content: Quote from: rstofer on December 08, 2018, 05:04:19 pmCCS includes functions for reading and writing to single pins. input, output_high, output_low.