View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
Data transformation problem |
Posted: Fri Jul 05, 2013 11:26 am |
|
|
Hi! I'm using MPlab v8.91, ccs v 4.134 and mdd stack v2012-06-22. In the beginning I thought I have problem with the port direction but it seems to be a data format problem. I don't know why but when I load the mdd stack it starts to add sign to usually unsigned variables. For example:
Code: |
char dataBuff[10];
dataBuff[0]=0;
if(input(PIN..)) dataBuff[0]+=10;
if(input(PIN..)) dataBuff[0]+=50;
|
and after that I have -60 into dataBuff[0] which disturbs my program.
I tried to use unsigned char - no effect!
How can I fix this? How can I remove the sign?
Thanks! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Fri Jul 05, 2013 11:32 am |
|
|
silly question but how are you reading/displaying the data? |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Fri Jul 05, 2013 12:07 pm |
|
|
I'm using simple add function to read the data :
Code: |
char data=0;
if(input(PIN1)) data+=1;
if(input(PIN2)) data+=2;
if(input(PIN3)) data+=4;
|
This is because I'm not using port, but single pins.
I'm displaying data this way:
Code: |
if(data>=128)
{
output_high(PIN1);
data-=128;
}
else
{
output_low(PIN1);
}
if(data>=64)
{
output_high(PIN1);
data-=64;
}
else
{
output_low(PIN1);
}
|
The problem is when data is <0 this function doesn't works.
May be I should add a second function in case data<0?! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Jul 10, 2013 12:37 pm |
|
|
You do realise that an unsigned value can never be <0....
Characters are unsigned by default.
In CCS, all integers default to unsigned.
The defaults can be changed by a #type statement.
The MDD stack is Microchip code, and this, uses signed as the default for many types, so there will be a type statement setting this up.
You should switch to being explicit about your types. Load stdint.h, and use int8_t for a signed int8, and uint8_t for unsigned etc..
Best Wishes |
|
|
|