PDA

View Full Version : Format is data stored in?



jmgelba
- 2nd March 2004, 21:34
When taking an ADC reading, is the data stored as hex, binary, decimal?? How is data stored in eeprom and how would i convert one type of system to to another.
I am trying to display a decimal reading of an ADC reading on 3 seven segment displays using a F818.
See the thread "New Member" for more details of the program.


Regards,

James.

AndyP
- 2nd March 2004, 22:49
I'm not quite sure what you mean by how is the data stored.. but basically it is stored as 1s and 0s in the PICs RAM registers, so Binary I guess.

With PBP however you can refer to the data stored in binary, hex or decimal, -its up to you.

eg.

VALUE VAR BYTE
VALUE=%00101000

is the same as

VALUE=40

or

VALUE=$28

jmgelba
- 3rd March 2004, 00:39
Ok, what Im trying to say is, I have my program take an ADC reading. This value gets stored to a varible, in my case I call it "RAW". Now, has the program (PIC), stored this ADC read value as a hex number, a binary number or a decimal number?

The F818 I am using has a 10 bit ADC therefore any reading will be 0 - 1023. So at a max reading, is the data stored as "1023" in the varible "RAW" or is the Hex, or binary equilvalent of 1023 stored in "RAW"
If its hex or binary, how do I convert it to decimal?

I wrote a small program that took an ADC reading and stored it in the eeprom. I can read the data back in my programmer and its read back as Hex. Hence the question, does the pic store data as a hex number, or is this just my programmer converting the stored data to Hex?
Here is the math I want to do with my ADC value: (All credit to Ingvar)

ADCIN 0, RAW 'READ ADC store value to RAW
millivolts = RAW */ 1250 'convert ADC to millivolts
psitimes100 = millivolts ** 47527 'convert millivolts to psi*100
psitimes100 = psitimes100 + 145 ' add sensor offset

I would like to display a decimal reading of psitimes100 on 3 seven segment displays.

I tried this following code just to experiment:

DEFINE OSC 20
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 10
TRISA = %11000001 'set portA
ADCON0 = %11000101 'read as analog
ADCON1 = %00001110
RAW VAR WORD 'Set ADC value to name of RAW
millivolts VAR WORD
psitimes100 var word
B0 VAR WORD
B1 VAR WORD
B2 VAR WORD
B3 VAR WORD
B4 VAR WORD
B5 VAR WORD
B6 VAR WORD
TRISB = %00000000 'set portB all outputs
PortA.2 = 1 'Blanks displays at startup
PortA.3 = 1
PortA.4 = 1
PortB.0 = 1 'A
PortB.1 = 1 'B
PortB.2 = 1 'C
PortB.3 = 1 'D
PortB.4 = 1 'E
PortB.5 = 1 'F
PortB.6 = 1 'G
PortB.7 = 1 'DP

loop:

ADCIN 0, RAW 'READ ADC store value to RAW
millivolts = RAW */ 1250 'convert ADC to millivolts
psitimes100 = millivolts ** 47527 'convert millivolts to psi*100
psitimes100 = psitimes100 + 145 ' add sensor offset

B2 = PSITIMES100 DIG 0 'TENTHS
B3 = PSITIMES100 DIG 1 'UNITS
B4 = PSITIMES100 DIG 2 'TENS

B0 = B2 ' Pass the count to the conversion subroutine
Gosub bin2seg ' Convert to segment value
PortB = B0 ' Put segment values out to LED
PortA.2 = 0 ' Turn on the digit
Pause 1 ' Display it for 1MS second
PortA.2 = 1
GOTO LOOP1
' Convert binary number in B0 to segments for LED
bin2seg: Lookup B0,[%11111001, %11000100, %11110000, %10011001, %10010010, %10000010, %11111000, %10000000, %10011000, %11000000],B0
Return

LOOP1
B5 = B3 ' Pass the count to the conversion subroutine
Gosub bin2seg2 ' Convert to segment value
PortB = B0 ' Put segment values out to LED
PortA.3 = 0 ' Turn on the digit
Pause 1 ' Display it for 1MS second
PortA.3 = 1
GOTO LOOP2
' Convert binary number in B0 to segments for LED
bin2seg2: Lookup B5,[%11111001, %11000100, %11110000, %10011001, %10010010, %10000010, %11111000, %10000000, %10011000, %11000000],B5
Return

LOOP2
B6 = B4 ' Pass the count to the conversion subroutine
Gosub bin2seg3 ' Convert to segment value
PortB = B0 ' Put segment values out to LED
PortA.4 = 0 ' Turn on the digit
Pause 1 ' Display it for 1MS second
PortA.4 = 1
Goto loop

' Convert binary number in B0 to segments for LED
bin2seg3: Lookup B6,[%11111001, %11000100, %11110000, %10011001, %10010010, %10000010, %11111000, %10000000, %10011000, %11000000],B6
Return

Goto loop


Im trying not to be a burden to the group, I apologise for all the questions.

Regards,

James.

AndyP
- 3rd March 2004, 08:51
The data is stored as binary because that is how all computers work. However because Pic Basic is a high level language, it gives you the opportunity to refer to that variable as a decimal integer, or a hex value. In other words, your code above should be fine.

Andy P

Melanie
- 3rd March 2004, 10:22
As a rider to atomski's thread, it should be remembered that EEPROM storage is a BYTE (8-bits) at a time. If you want to store your 10-but ADC value, you will have to do that as two bytes. EEPROM Storage is always in BYTES (8-bits). How you (as a human) relates to those eight bits by displaying or quoting them in BINARY, HEX or DECIMAL is irrelevant, the crux is there are still eight bits of Data.

Take for eample the Decimal number 65. In Hex it is $41, in Binary it is %01000001. If you print it to an LCD in RAW form it becomes the ASCII Character 'A'. As far as the PIC is concerned, it's just eight bits. How you want to see it is up to you. Remember everythings a number... even alphabetic characters are just numbers that are remapped for your (human) benefit.

Remember the cult TV series "The Prisoner"? "I am not a name but a number..." or something like that... *smiles* No, I'm not that old... I saw the reruns not the original series!

jmgelba
- 4th March 2004, 01:32
I do remember "The Prisoner" I too watched the re-runs. They were usually on late though, like 3am.
What the heck where those giant beach ball thingies that got you every time you swam out into the sea? Wierd. And why didnt he just take a knife with him next time?

Anyway...(!) Forgetting all the beach balls, math and data storage for a second. Maybe you could explain this to me.

Even though my connections are correct, and when i write a program that cycles through 0 - 9 on portb ( the program does nothing else), i get numbers perfectly every time. However, if i do anything using those same port settings, ie binary or hex numbers in a lookup table, i get some ok charcters and some garbage. Same port settings, just in a lookup table.
I searched for errors in the data sheet and silicon and found a portb issue whereby if you drive rb3 low, or place an rc network on it, it will reset the device. I removed my 10k pulldown resistor and no difference. Still cycles perfectly, but craps out if the data is in a lookup table.
Could the garbage be because im using a var word and not assigning a high and low variable?
Im very confused by this. This is one of the three problems im having thats stopping me finishing this project. I just dont get it. Everyone seems to think the code should work.

Thanks for your reply by the way. And every one else. A great bunch of people.

Regards,

James.