|
|
View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
Strange problem with bit set in variable |
Posted: Sat Mar 01, 2014 4:11 pm |
|
|
I want to make a program that would read serial data on interrupt pin.
In this case the data is entered by me via button, therefore the BAUD rate is about 1. Here is the code:
Code: |
#INCLUDE <16F1508.h>
#FUSES INTRC_IO, NOMCLR, NOWDT, PUT, NOBROWNOUT
#USE delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C0, rcv=PIN_C2)
#define butt pin_b7
#DEFINE LED PIN_B4
#define IND PIN_C7
#BYTE my_byte=0x20
int i=0;
#INT_RB
void B7_isr(void) //sets serial bit
{
delay_ms(5);
if(input(butt))
output_toggle(LED);
bit_set(my_byte,i);
printf("\f%u",my_byte);
clear_interrupt(INT_RB7_L2H);
}
void main()
{
my_byte=0;
int1 s=1;
set_tris_b(0x80);
ENABLE_INTERRUPTS(INT_RB7_L2H);
ENABLE_INTERRUPTS(GLOBAL);
while(true)
{
for(i=0;s=1,i<8;i++)
{
ENABLE_INTERRUPTS(INT_RB7_L2H);
output_high(ind); //during high indicator led I can set the bit by pressing button
delay_ms(700);
output_low(ind);
DISABLE_INTERRUPTS(INT_RB7_L2H);
delay_ms(200);
}
s=0;
my_byte=0;
while(!input(butt))s=1; //if I press the button filling my_byte resets
}
} |
The problem is that my_byte can assume the following values: 1,2,4,8,16,32,64,128; I don't know why but if I set 1st and 2nd bits my_byte is dec. 2 instead of dec. 3. If I set 1st, 2nd and 5th bits, my_byte
is dec 32. Each less significant bit is being cleared when I set next one.
I know it is little bit confusing, but if you test the code you will see the outcomes. _________________ A person who never made a mistake never tried anything new. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sun Mar 02, 2014 11:59 am |
|
|
1- don't printf inside the ISR !!!!!
2- READ port b to clear the isr Inside the INT handler code
3- rethink the logic of what you coded , while you are at it.
a state machine can do this better - and maybe even without need of an ISR
then see where you are .... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Sun Mar 02, 2014 12:19 pm |
|
|
and, since you use delays in the ISR, interrupts will be disabled in all the external delays - the 700mSec...
The actual read will then occur after the LED goes out. |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Mon Mar 03, 2014 5:04 am |
|
|
Thank You both!
Quote: | READ port b to clear the isr Inside the INT handler code |
I think it is done by this line
Code: |
if(input(butt)) //B7
|
Isnt it enough to read a single bit of the port to clear the interrupt? _________________ A person who never made a mistake never tried anything new. |
|
|
|
|
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
|