Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to store values in Mikrobasic?

Status
Not open for further replies.

bigal_scorpio

Active Member
Hi to all,

Could anyone tell me how to store a value eg. if RB2 received an input high, and keep that value after power off and restart?

The MB manual is a bit vague in some areas and I can't find the info on the datasheets either, but that may be because I don't know what I'm looking for.

Thanks for looking...........Al :confused:
 
Bigal

You need to use the EEPROM library. Have a look at the sample code, and please let me know if you still have problems.
 
Hi mate,

The EEprom part of the manual is very small. Took me all of a minute to read, but not understand! This is the example.
Example

for i = 0 to 20
EEPROM_Write(i, i + 6)
next i

What it doesn't tell me is the position or value of i, does it mean that whenever I use this command on ANY Pic that it will use the first available byte of EEprom?

Or is there a specific address that the manual assumes I know about.

I learn best from example but the manuals example does not give me enough info to sink in. I often think the wife is right and I do get thicker every day! hehehe

Regards............Al
 
Hi mate,

The EEprom part of the manual is very small. Took me all of a minute to read, but not understand! This is the example.
Example

for i = 0 to 20
EEPROM_Write(i, i + 6)
next i

What it doesn't tell me is the position or value of i, does it mean that whenever I use this command on ANY Pic that it will use the first available byte of EEprom?

Or is there a specific address that the manual assumes I know about.

I learn best from example but the manuals example does not give me enough info to sink in. I often think the wife is right and I do get thicker every day! hehehe

Regards............Al

hi Al,:)
The PIC's internal EEPROM address start at 00 and inc upwards.

EDIT: whats the PIC type?
 
Last edited:
Al

See if this makes more sense, please let me know if I can help more.
This code is for MBPro, I am not sure what version you are using

Code:
program Eeprom
dim counter as byte       ' loop variable
main:
ANSEL =   0               ' Configure AN pins as digital I/O
ANSELH =  0
C1ON_bit = 0             ' Disable comparators
C2ON_bit = 0
PORTB = 0
PORTC = 0
PORTD = 0
TRISB = 0
TRISC = 0
TRISD = 0
for counter = 0 to 31               ' Fill data buffer
EEPROM_Write(0x80+counter, counter) 'Write data to address 0x80+ii
next counter
EEPROM_Write(0x02,0xAA)          ' Write some data at address 2
EEPROM_Write(0x50,0x55)           ' Write some data at address 0150
  Delay_ms(1000)                    ' Blink PORTB and PORTC diodes
  PORTB = 0xFF                     ' to indicate reading start
  PORTC = 0xFF
  Delay_ms(1000)
  PORTB = 0x00
  PORTC = 0x00
  Delay_ms(1000)
  PORTB = EEPROM_Read(0x02)     ' Read data from address 2 and dis
  play
  it on PORTB
                               ' Read data from address 0x50 and dis
  PORTC = EEPROM_Read(0x50)
  play it on PORTC
  Delay_ms(1000)
  for counter = 0 to 31           ' Read 32 bytes block from address
  0x100
  PORTD = EEPROM_Read(0x80+counter) ' and display data on PORTC
  Delay_ms(100)
  next counter
end.
 
Hi Eric and Gaspode,

Thanks for the code Gaspode, it does make a bit more sense when there is a larger example than the manual's, starting to get my haed round it I think.

Eric, I don't have a specific Pic in mind yet but will probably opt for one of the 40 pinners 16F877 or 887 as I will need a good few inputs and outputs.

The thing I am trying to do is use momentary push buttons to switch things on or off with relays, but as I would be using the same switch for on and off I need the Pic to remember the state of the port when I turn off the ignition - you probably guessed its a car project by now - and keep the settings so I can leave things in the same state until I use the switch again.

I was thinking of just storing the whole port (s) that went to relays and writing them back on power up, but is it as easy as it sounds?

Regards...........Al
 
The thing I am trying to do is use momentary push buttons to switch things on or off with relays, but as I would be using the same switch for on and off I need the Pic to remember the state of the port when I turn off the ignition - you probably guessed its a car project by now - and keep the settings so I can leave things in the same state until I use the switch again.

I was thinking of just storing the whole port (s) that went to relays and writing them back on power up, but is it as easy as it sounds?

Regards...........Al

hi Al,
Storing the state of switches etc in EEPROM is fairly easy to do.

Using basic.
i= PORTB.0
;The command
EEPROM_Write(0,i); will save the state of PORTB.0, only 1 bit!
but its more efficient if you write the complete port state to the EEPROM
i=PORTB
EEPROM_Write(0,i); all 8 bits, if thats where your switches are.
;
When you do a EEPROM READ, you can do a bit test on the individual bits
i= EEPROM_Read(0)

The 16F877 has 256 bytes of EEPROM addr 0x00 thru 0xff
 
Last edited:
Hi again Eric,

That seems to be just the job!

Would have replied sooner but my mate has just come out of hospital today after 14 weeks (stroke and brain bleed) only to find when his wife put the kettle on that the fuse box tripped out! So I have just spent 2 hours looking for the fault and failed. He had some mobility work done in the house while he was in hospital as he has one side not working yet and can't walk, and I wonder if they have pierced a cable somewhere and not noticed? So its floorboards up tomorrow. Doh!

But trust it to happen on his first day home eh! Sods law rules OK!

Anyway Eric I hope you are well, at least better that us in rotherham, we all seem to be going down hill fast ;)

Al
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top