|
|
View previous topic :: View next topic |
Author |
Message |
ChicoDaRave
Joined: 21 Nov 2014 Posts: 10
|
12F675 + write_eeprom + INT_EXT |
Posted: Thu May 21, 2015 3:12 am |
|
|
Hello people.
I'm doing a small project here, all were going ok until here.
The device has a button connected to GP2, this should be a mode button, each time button is pressed INT_EXT is called, inside it Mode++; and write_eeprom(254,Mode);
In the setup routine there is Mode=read_eeprom(254);
So, each time the device is turned on should always in same mode, but it is not!
Each time I full turn off the device then turn it on again Mode is zero, I dont know what is happening but I believe the INT_EXT routine is being called on the startup, without a button press. *update, I made INT_EXT toggle a LED in another pin and it was activated in the startup.
GP2 is pulled up with a resistor, button ties it to ground, config is ext_int_edge(H_TO_L);
I dont know how to fix this, so I'm asking help here.
Thank you very much. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Thu May 21, 2015 3:50 am |
|
|
With any of the interrupts, when you 'reprogram' the interrupt (so change a counter, enable a serial, or change the edge being used for the EXT interrupt for example), the sequence needs to be:
1) Do the setup.
2) Make sure the hardware condition if applicable is cleared (so for INT_RB, read the port, for the serial make sure no characters are waiting etc.).
3) Clear the interrupt.
4) Enable the interrupt.
Microchip have a warning in some data sheets, and in some of their application notes for external interrupts, that setting the INTEDG bit (which selects which edge the interrupt is on), can cause the interrupt to trigger, and also that the interrupt may be set on power-on.
So basically:
Code: |
ext_int_edge(howyouwant); //set the edge how you want
//for things like INT_RB, read the port here
clear_interrupts(INT_EXT); //clear the interrupt
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
|
As a comment though, even if it is triggering the result should not be '0'. It sounds as if you are enabling the interrupt before reading the 'mode', so then the default value in the variable (typically 0xFF), gets incremented by one, and written, to give the zero result. Your reading should be before you enable the interrupt. |
|
|
ChicoDaRave
Joined: 21 Nov 2014 Posts: 10
|
|
Posted: Thu May 21, 2015 4:44 am |
|
|
Precise answer Ttelmah!
Just "clear_interrupt(INT_EXT);" right before enabling it has solved!
Thank you very much!
I was doing everything right as you said, except for this.
Everything was BEFORE the "enable_interrupts(GLOBAL);" except for the "Mode=read_eeprom(254);" which was later.
Yes, probably 255 then incremented in the external interrupt and then writed to eeprom as zero.
Thank you again!
PS: Each time I readed the code from the microcontroller (in Pickit2 software) the value in address 254 was incremented, now it is all time the same, as expected |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|