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.

Nigel's I2C serial EEPROM routines and interrupts

Status
Not open for further replies.

blackman

New Member
I am wondering how safe it is to use Nigel's bit-banging I2C serial EEPROM routines and interrupts.

The interrupt service routine would not be terribly long; literally on the order of a dozen instructions or so.

I am just worried that if the uC is in the middle of writing something to the serial EEPROM and then an interrupt occured, if the time spent servicing the interrupt would screw up the write.

Any help would be appreciated.

P.S. Great site, Nigel!
 
yup, i2c is a static bus design, it will be just fine being interrupted as long as you don't disturb the bus during the interrupt servicing...

I haven't used Nigel's routines, so I can't testify to them, but he is a well respected programmer, so I assume they are good to go as it were...
 
I see no problems with using interrupts, I2C is a syncronous protocol, if an interrupt call makes a particular bit take longer than usual it will make no difference at all.
 
I agree with all replay.
but you have to carefull with regi`s that you are using in iic routines and interrupt routines.
if you can use one or two bits from bit addressble area
for making separetion between these routines as indicator (ex, in_iic_routine ,not_in_iicroutine), it is somewhat difficult but easy for both routines.
 
No 'care' required, you should always save and restore any common registers used (or affected) by your interrupt rourine, this is standard practice, and there's nothing different required for I2C routines.
 
exactly so, the interrupt service routine should always preserve critical register files. Usually referred to as context saving.
 
im still new with pic's, just wondering about the file registers that i should restore upon a innterrupt on the 16F876A, are they only WReg, Status and Option, or should i restore others, eg:

Interrupt:

Temp_Option = Option
Temp_Status = Status
Temp_WReg = WReg

.
.
. 'interrupt routine..
.
.

Interrupt_Exit:

Option = Temp_Option
Status = Temp_Status
WReg = Temp_WReg

Return

EDIT: Just reading some other forums on restoring registers, and it seems the SWAPF is crucial for restoring the STATUS register, How do you go about this in Proton+ PICBasic, or just picbasic in general?
 
Last edited:
see this thread

https://www.electro-tech-online.com/threads/timer-0-interrupt-on-pic-16f629.24672/


The quick reply is that any appropriate instruction that does not alter the contents of the status register could be used, it doesn't have to be the swapf instruction.
As for only context saving/restoring the critical registers, this extends to any register file that may be in current use in the main body of code, but may be inadvertantly altered in the interrupt service routine.
 
Last edited:
tunedwolf said:
As for only context saving/restoring the critical registers, this extends to any register file that may be in current use in the main body of code, but may be inadvertantly altered in the interrupt service routine.


thanks wolf, i cant view attachments as im just using an internet cafè atm, could you or someone else just give a list of the file registers that i need to restore that would be used by other routines/functions in proton+(picbasic), just so that i dont alter values while a dword mathamatic equation is being carried out and the interrupt triggers for example..
 
Obviously it depends entirely on the compiler, but I would assume that the compiler itself takes care of the context saving - but why are we discussing interrupt routines and my I2C routines if you're only using BASIC?.
 
it does seem a little strange Nigel, I admit, I thought that pretty much all of the PIC basic type packages these days had built in functions for all of these things...i2c, uarts, lcd's etc, maybe I'm misguided...I don't use basic

As for what registers to save, I have no idea what you are using in terms of register files Gramo so anything else you are using in the main code, and in your interrupt routine, the obvious critical ones would be, W, Status, PCLath, Fsr

I would look to your basic compiler as Nigel suggests, I'm sure most of them handle all this sort of stuff for you...
 
Thanks guys, it was something Nigel had said earlier:


Nigel Goodwin said:
No 'care' required, you should always save and restore any common registers used (or affected) by your interrupt rourine, this is standard practice, and there's nothing different required for I2C routines.

Sorry to sway away from your original thread, but in response too:

Nigel Goodwin said:
why are we discussing interrupt routines and my I2C routines if you're only using BASIC?.

Got me thinking about that damn problem i had with a basic alarm clock i had made, using TMR0. Every now and then a problem arrised; when I updated the the data on the lcd, the interrupt routine would trigger during a print statement, and the result would be complete garbage on the lcd screen.

I assumed the system registers like status etc.. were not being saved and restored correctly. In proton+, the command Context Save and Context Restore are used before and after interrupt routines, but when compiling the program with these commands, it gives a warning saying that this is done automatically on the 16 series pics, and is therefore not req.

But it must be missing something, or some register, as my timer interrput routine is really basic, just incrementing integers..?. Why the garbage on the lcd screen..?.

EDIT: The only way i could correct this/stop it from happening was to disable interrupts before a print command, then enable them afterwards, but that really bugged me as everytime i did this, i was decreasing the accuracy of the clock.
 
Last edited:
The most likely causes I can think of are that you are either altering the port pins driving the lcd during the interrupt service routine inadvertently, or perhaps you are changing the file registers contents with the updated time etc in the interrupt service routine, but trying to send data, from the same registers in your main body of code. If you do that, then part way through sending your data, the file register contents will change, and the rest will either be be seen as gibberish to the lcd or at least certainly not what was intended. Update the lcd with the new time register data in your interrupt service routine and that problem should disappear.
 
That would depend largely on what you are writing your program with, Basic, Asm, C, jal, etc, also what routines you are using to do it, because there are as many of them freely available on the web. See Nigels routines for i2c if you need routines, also read his tutorials.
 
mabauti said:
OK, so what's the simplest code for writing a byte from a PIC to a 24C08?

The code in my I2C tutorial is fairly simple, because it's designed to be easily understood - to that end it's split into many subroutines, probably more than you would normally use, but it makes it easier to understand.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top