# Function Read and Write EEPROM

Status
Not open for further replies.

#### Winch

##### Member
I have some problems with the processing of the code in the program.

Why do these simple code below not work properly?

Code:
'DEVICE PIC16F628A

Define CONFIG = 0x3f50

Define CLOCK_FREQUENCY = 4

AllDigital  'alle ports digital

'Poortnamen
Symbol led = PORTA.1
Symbol s1 = PORTB.0

'76543210
PORTA = %00000000
TRISA = %11111011
TRISB = %11111111

OPTION_REG.7 = 0

main:
While s1 = 0
Wend
WaitMs 10

Toggle led
Write 0, led

While s1 = 1
Wend
WaitMs 10

Goto main

End

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Ok never mind.... You can't compile it..

led is defined as a bit.... Read and write require bytes.. Use a port buffer to access the bits..

Code:
Define CONFIG = 0x3f50

Define CLOCK_FREQUENCY = 4

AllDigital  'alle ports digital

'Poortnamen
Symbol led = PORTA.1
Symbol s1 = PORTB.0

'76543210
PORTA = %00000000
TRISA = %11111011
TRISB = %11111111

Dim portbuf As Byte
OPTION_REG.7 = 0

led = portbuf.1

main:
While s1 = 0
Wend
WaitMs 10

Toggle led
portbuf.1 = led
Write 0, portbuf

While s1 = 1
Wend
WaitMs 10

Goto main

End

#### Winch

##### Member
Oke, I did try this.
It looks to work. But I am not sure?

The intention was to make the LED high (make the output high) write to the "0" address of the EEprom en read it again out with the result the LED high output. And visa versa.
Now it looks like to work just the other way the LED is high if you take the power supply of and back again then the LED is Low. And visa versa.

What I don't understand in this code is you write "portbuf.1" what is that ".1" exactly doing?
I use this solution earlier but without that ".1" now I am wondering what does that?

#### Winch

##### Member
I suppose that I can also use bit 2 or 3? With the same result.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Yes...
The dot operator can also be used with word sized variables to access hi byte and low byte.

Code:
dim x as word
dim y as byte

y = x.HB

#### Winch

##### Member
Thanks for the explanation!

Status
Not open for further replies.