CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Strange problem with bit set in variable

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

Strange problem with bit set in variable
PostPosted: Sat Mar 01, 2014 4:11 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Sun Mar 02, 2014 11:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Mar 02, 2014 12:19 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Mar 03, 2014 5:04 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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