PDA

View Full Version : Internal Eeprom/flash , what are you doing ?



ultiblade
- 15th October 2008, 14:47
Hi there,

New problem. Eeprom doesn't seem to 'store' values. What is going on ?
Due to the fact that i could'nt get the eeprom part of my code to work, i changed the program to read value, increment, write value and flash led 'value' times. Every time i apply Vdd the value in eeprom should be 1 more then before. Unfortunately, this code always flashes twice, what's going on ?



'==== Set fuses =============================================
@ device pic12F519, intrc_osc, wdt_off, mclr_off, ioscfs_off, protect_off, cpdf_off

'==== Set defines ============================================
DEFINE OSC 8 'Int OSC @ 8 Mhz
OPTION_REG = %10000000

'==== Set variables ===========================================
Y VAR word '
TMP var BYTE 'Temp variable
tmp2 var byte 'Temp variable


'==== Set IO ================================================
TRISB = %00111100 'Set TRIS register input's & output's
PORTB = %00000000 'Set all digital low
led var PORTB.0 'led pin

'==== Main program ===========================================
MAIN:
pause 400

read 1,tmp 'read previously written value
pauseus 100

tmp = tmp + 1 'increment value

write 1,tmp 'store new value
pauseus 100

tmp2 = 0
for tmp2 = 0 to tmp
led = 1
pause 300
led = 0
pause 700
next tmp2

pause 500

theend:
goto theend


Anybody ??

skimask
- 15th October 2008, 14:57
Increase the pause time after the writes to about 10ms, not 100us...just like the book says...

ultiblade
- 15th October 2008, 15:42
Skimask, hi !

I tried the 10ms delays after read and write, but still no results, keep getting those 2 flashes.
When i read back the hex location 0x400 always reads 0001 (1 flash one 0, and one on 1, two flashes)

I have changed the code a little bit to let the variable increment whilst the power is on to be sure it increments, but after power off, power on, again two flashes ...

Here's the code:


'==== Set fuses ============================================
@ device pic12F519, intrc_osc, wdt_off, mclr_off, ioscfs_on, protect_off, cpdf_off

'==== Set defines =========================================
OPTION_REG = %10000000

'==== Set variables ============================================
Y VAR word 'To Hold the 12-bit RC5 code
TMP var BYTE 'Temp variable
tmp2 var byte


'==== Set IO =================================================
TRISB = %00111100 'Set TRIS register input's & output's
PORTB = %00000000 'Set all digital low
led var PORTB.0 'led pin

'==== Main program ============================================
MAIN:
pause 400
tmp = 0

read 0,tmp
pause 10
if tmp > 50 then tmp = 1
if tmp < 1 then tmp = 1

again:
tmp = tmp + 1
write 0,tmp
pause 10
tmp2 = 0

for tmp2 = 1 to tmp
led = 1
pause 200
led = 0
pause 400
next tmp2

pause 2000
goto again
end


What is wrong, here ?

skimask
- 15th October 2008, 16:33
@ device pic12F519, intrc_osc, wdt_off, mclr_off, ioscfs_on, protect_off, cpdf_off
option_reg=$80 : tmp var byte : tmp2 var byte : ledtmp var byte
badflag var bit : trisb = $3c : portb=0 : led var portb.0
main: badflag = 0 : for tmp=0 to 63 : write tmp,tmp : pause 10 : read tmp,tmp2
if tmp<>tmp2 then gosub flashbad
next tmp : if badflag = 0 then gosub flashgood
goto main
flashbad: badflag = 1 : for ledtmp=0 to 9 : led=1 : pause 50 : led=0
pause 50 : next ledtmp : return
flashgood: for ledtmp=0 to 9 : led=1 : pause 500 : led=0 : pause 500
next ledtmp : return
end

Code will write the value into the eeprom location, then read it back, stepping thru each eeprom location in the '519.
If the readback doesn't match what was written, the led will flash fast 10 times.
If the readback is good, it'll flash slowly 10 times after the loop is complete, then start over...

peterdeco1
- 15th October 2008, 18:01
I don't know if this has anything to do with it but it looks like the PIC doesn't have portb. It has GPIO's. Try changing portb.0 to gpio.0 and trisb to trisio.

skimask
- 15th October 2008, 18:06
I don't know if this has anything to do with it but it looks like the PIC doesn't have portb. It has GPIO's. Try changing portb.0 to gpio.0 and trisb to trisio.

That was discussed in another thread earlier today...
Apparently, PBP aliases GPIO to PORTB and TRISIO to TRISB for the 12F519...
Go figure eh? I wouldn't have found that... Handy though...

Darrel Taylor
- 15th October 2008, 22:42
In general, writes to internal EEPROM are self timed. No pauses are needed.

However, on the 12F519, there isn't any EEPROM.
There's 64 bytes of Flash memory instead.

That memory is erased in blocks of 8 bytes at a time, and can't just be overwritten byte by byte like you can with EEPROM.

For what you want to do, it'll be a lot easier to change chips.
<br>

ultiblade
- 16th October 2008, 14:11
Thanks for your replies guys !

I could'nt get back to you any sooner, cause the forum was offline.

The reason i would like to use the 519 is because it's the cheapest and smallest
pic micro that has memory on it. I did some projects on the 12F635, and that works
like a charm, however, it's more expensive and isn't available in a MSOP package ...

OK, so read and write is not going to work then ?

Is there anyway i can make this work Darrel or Skimask ?

The project is very small step up dimmer for small led lights. It uses a i2c digital potentiometer to influence a sense resistor. Each time the pic get Vdd it reads the previous value and adds 1 to it and applies the setting to the digital pot. So, by switching the lights on and off a few times the dimmer state can be influenced ...

skimask
- 17th October 2008, 00:18
Is there anyway i can make this work Darrel or Skimask ?
The project is very small step up dimmer for small led lights. It uses a i2c digital potentiometer to influence a sense resistor. Each time the pic get Vdd it reads the previous value and adds 1 to it and applies the setting to the digital pot. So, by switching the lights on and off a few times the dimmer state can be influenced ...

Using a digital pot for LEDs (i.e. varying the current) isn't the way to go with LEDs. Sure, it'll work, but the results won't be very linear at the lower light levels. Best bet with an LED is to use PWM of some sort of another. If you look at the datasheet for whatever LED you've got, you'll notice that the color/intensity/etc are all referenced to a normalized current drive (or at least a range of current). Go outside of that range and the performance drops. If you use PWM, you drive the LED at an efficient level, with a varying duty cycle. And it saves power too...

As far as the FLASH in the 12F519? I didn't realize it was 64 bytes of FLASH either, I thought it was EEPROM. I don't have a 519 handy, and I haven't tried it either, so I don't know if PBP supports it in the same way it supports the other PICs with EEPROMs or not. You might end up writing an assembly subroutine to handle read, modifying, and re-writing the FLASH in 8 byte rows.

Darrel Taylor
- 17th October 2008, 03:39
Is there anyway i can make this work Darrel or Skimask ?
I think so.

Been looking through the 12F library, and it looks like the READ and WRITE commands still do the right thing. But there's no way (in PBP) to erase a block before writing.

This might work. It's a macro that will erase 1 block at a time using a Constant for the address.

To erase the first block (bytes 0-7), you can ...
@ EraseFlash 0

To erase the second block
@ EraseFlash 8

Put it right before the WRITE command.

And put this macro somewhere near the top of the program.

ASM
EraseFlash macro Addr
local EraseLoop
MOVE?CB (Addr & 0x38), EEADR
BSF EECON,FREE ; SELECT ERASE
BSF EECON,WREN ; ENABLE WRITES
BSF EECON,WR ; INITITATE ERASE
EraseLoop
btfsc EECON, WR ; Wait for the erase to complete
goto EraseLoop
endm
ENDASM

ultiblade
- 17th October 2008, 08:55
You're both absolutely right.

I forgot to mention I'm using Cree powerled's, that are driven by a supertex led driver. This driver uses a sense resistor to measure the current, if we influence the current ( by adding a resistor parallel to the sense resistor), we can influence the intensity of the powerled's.

Darrel, i'm going to try the erase macro immediately, will post my findings ....

Thanks Experts !

ultiblade
- 20th October 2008, 11:06
Hi Guys,

I have tried your code Darrel, and erasing the blocks before writing does the trick !!! Thank you !
Skimask, thank you for your quick replies and expert information !!

http://home.concepts.nl/~rgrndaal/upload/201008darrel.jpg

Best regards,

Ultiblade

Darrel Taylor
- 20th October 2008, 21:42
Woohoo!

Hope she's my prize. :D
<br>

ultiblade
- 21st October 2008, 07:51
[loud laughter]

I will ask her to contact you !

[/loud laughter]