Jump to content

Recommended Posts

Perhaps you can help with a problem, as I am not a very good C programmer.

 

I have a PIC based program in which I have declared a flag in the header

file.

 

static bit signal;

 

In the C program I initialise the flag by-

 

signal=0;

 

Later in the main body I use the flag as-

 

signal = !signal; //simply to toggle the bit (?)

 

This last line is marked as a warning that conversion of unsigned char to

bit may lose data.

 

In fact, the program compiles correctly and runs properly, also confirmed

by examination of the listing file.

My question is: what am I doing wrong to cause this warning to appear?

 

Any help appreciated.

Link to post
Share on other sites

I've tryed it right now and I don't have any warning.

 

Are you sure you don't have another variable decalration for the same variable name?

 

Could you please add more info as Compiler version and target device?

 

Regards,

 

Wally

Edited by wally
Link to post
Share on other sites

I've tryed it right now and I don't have any warning.

 

>Are you sure you don't have another variable decalration for the same variable name?

No, this is the only variable of that name; wouldn't the compiler flag a duplicate symbol anyway?

 

 

>Could you please add more info as Compiler version and target device?

I am using BoostC version 6.6 for a PIC 16F876.

There are a number of single bit flags in the program, so it seems the fault (mine) lies in the assignment

signal=!signal

which might imply variable is treated as byte by compiler, but I can't find another way in C to perform toggle of a single bit, especially as the location of the bit is 'unknown' to me; (although it is revealed by looking into the listing file.)

 

Regards,

Mike W

Link to post
Share on other sites
...

signal = !signal;      //simply to toggle the bit (?)

 

This last line is marked as a warning that conversion of unsigned char to

bit may lose data.

 

In fact, the program compiles correctly and runs properly, also confirmed

by examination of the listing file.

My question is: what am I doing wrong to cause this warning to appear?

 

You see this warning because the ! operation produces an unsigned char intermediate result that is than used in assignment. You can safely ignore this warning.

 

Regards,

Pavel

Link to post
Share on other sites

Here are all the logical bit not fuctions I could think of in ten minutes:

#include <system.h>

bit signal;

void main()

{
 signal = 1; // Initialize the bit first
0003  1283 	 BCF STATUS, RP0
0004  1303 	 BCF STATUS, RP1
0005  1520 	 BSF gbl_signal,2

 signal = !signal;
0006  01A1 	 CLRF CompTempVar481
0007  1D20 	 BTFSS gbl_signal,2
0008  0AA1 	 INCF CompTempVar481, F
0009  1120 	 BCF gbl_signal,2
000A  1821 	 BTFSC CompTempVar481,0
000B  1520 	 BSF gbl_signal,2

 signal = ~signal;
000C  30FF 	 MOVLW 0xFF
000D  1920 	 BTFSC gbl_signal,2
000E  30FE 	 MOVLW 0xFE
000F  00A1 	 MOVWF CompTempVar482
0010  1120 	 BCF gbl_signal,2
0011  1821 	 BTFSC CompTempVar482,0
0012  1520 	 BSF gbl_signal,2

 signal ^= 1;
0013  0820 	 MOVF gbl_signal, W
0014  3A04 	 XORLW 0x04
0015  00A0 	 MOVWF gbl_signal

 signal = signal?0:1;
0016  1D20 	 BTFSS gbl_signal,2
0017  281A 	 GOTO	label268438736
0018  1021 	 BCF CompTempVar483,0
0019  281B 	 GOTO	label268438738
001A        label268438736
001A  1421 	 BSF CompTempVar483,0
001B        label268438738
001B  1120 	 BCF gbl_signal,2
001C  1821 	 BTFSC CompTempVar483,0
001D  1520 	 BSF gbl_signal,2

 ++signal;
001E  0820 	 MOVF gbl_signal, W
001F  3A04 	 XORLW 0x04
0020  00A0 	 MOVWF gbl_signal

 signal++;
0021  0820 	 MOVF gbl_signal, W
0022  3A04 	 XORLW 0x04
0023  00A0 	 MOVWF gbl_signal

 //--signal; //<- causes linker internal error
 //signal--; //<- causes linker internal error
}
0024  0008 	 RETURN

Just pick the one that generates the code you like best.

 

I did not get any warnings that conversion of unsigned char to bit may lose data using BoostC 6.60.

 

Did I miss an update?

Link to post
Share on other sites
...

I did not get any warnings that conversion of unsigned char to bit may lose data using BoostC 6.60.

 

Did I miss an update?

 

You need to set the warning option to show all warnings to see this one.

 

Regards,

Pavel

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...