View previous topic :: View next topic |
Author |
Message |
ashrafkhatri
Joined: 06 May 2008 Posts: 14
|
printf does not work |
Posted: Tue May 06, 2008 9:05 am |
|
|
Below is my code. My code work fine and print "Pic controller" on serial port on portb pin4 interupt. But when i add serial interupt #INT_RDA, printf stop printing on portb pin4 interupt.
I want to know why.
Can any one explain the problem, why serial port interupt stop sending data on serial pin on portb interupt
Code: |
#include <16f870.h>
#fuses HS, NOWDT, PUT, NOBROWNOUT, PROTECT, NOLVP, NOCPD
#use delay(clock=20000000)
#byte PORTA = 5
#byte PORTB = 6
#byte PORTC = 7
#define SET 1
#define RESET 0
struct VARIABLES
{
unsigned char data;
unsigned int serial ;
unsigned int ON_OFF;
}
ABC;
#USE RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7)
#INT_RDA
RDA_isr()
{
ABC.data = getch();
}
void main() {
enable_interrupts(INT_RDA);
enable_interrupts( INT_RB);
enable_interrupts(GLOBAL);
SET_TRIS_A(0x00);
SET_TRIS_C(0xBF);
SET_TRIS_B(0XFF);
while(1)
{
output_low(PIN_A1);
delay_ms(1000);
output_high(PIN_A1);
delay_ms(1000);
}
}
#int_rb
rb_isr() {
byte ch, last_b;
ch = last_b ^ portb;
last_b = portb;
if (bit_test(ch,4 ) && !bit_test(last_b,4)){
//b4 went low
delay_ms(50);
printf("Pic controller");
output_low(PIN_A2);
delay_ms(1000);
output_high(PIN_A2);
delay_ms(1000);
}
delay_ms (100); //debounce
}
|
|
|
|
Matro Guest
|
|
Posted: Tue May 06, 2008 9:14 am |
|
|
Try :
Code: |
#USE RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ERRORS)
|
You have to be aware that when an ISR is executed, all interrupts are disabled.
Your #INT_RB ISR is very very long to execute and that's very dangerous.
Moreover, having a printf() in an ISR is never good.
Matro. |
|
|
ashrafkhatri
Joined: 06 May 2008 Posts: 14
|
|
Posted: Tue May 06, 2008 9:24 am |
|
|
then how can i print on serial pin , while pressing portb pin4. and also include #int_RDA in my code as wel for recieving data |
|
|
Matro Guest
|
|
Posted: Tue May 06, 2008 9:29 am |
|
|
The best is to only set a flag in the #INT_RB ISR.
In your main() endless loop you check this flag. And if it is set you process the data (printf(), ...) and finally clear the flag.
Matro |
|
|
ashrafkhatri
Joined: 06 May 2008 Posts: 14
|
|
Posted: Tue May 06, 2008 9:46 am |
|
|
Thanks for your replying
Do you mean i use
#int_rb noclear in my code
how can i set flags and reset it in main loop in C language
could you post a sample code for my better understanding |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Tue May 06, 2008 10:53 am |
|
|
In your interrupt isr set a global flag myisr_got_achar=true declare it as global (Ex int8 myglobal_flag int8 myisr_got_achar) to indicate a specific condition as having occured ( Ex interrupt so a keypress or interrupt received a char) . In the main code initialize the global Ex flag myisr_got_achar=false and and in the main loop check to see if it was set by the isr if so do the appropriate action in the main routine ( Ex if( myisr_gat_achar ==true) printf(" char recvd"))
then clear the flag Ex flag myisr_got_achar=false so a future event if it happens can re-trigger it).
Stay with byte size vars don't use word size since they take more than one instruction to assign them allowing for a rare situation in which an interrupt could happen between instructions. |
|
|
|