View previous topic :: View next topic |
Author |
Message |
cxiong
Joined: 09 Sep 2003 Posts: 52
|
#INT_EXT help |
Posted: Wed Oct 20, 2004 9:50 am |
|
|
I just want to test a simple external interrupt. I couldn't get it to work.
If a key is pressed, it start to lid up RB0, else print Normal on the RS 232 line. Please help....
Here is my code:
Code: |
#include <18f458.h>
#device ADC=10
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#int_ext
void display_isr()
{
if(kbhit())
{
output_high(PIN_B0);
}
}
void main()
{
output_b(0x00);
while(1)
{
if(!kbhit())
{
printf("Normal Mode\n\r");
}
else
{
enable_interrupts(int_ext);
enable_interrupts(global);
}
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Oct 20, 2004 10:29 am |
|
|
what do you mean with 'If a key is pressed' ?
Is this a key on your PC that is transmitted through RS232 or the key that is connected to your int_ext line?
Now your program is first waiting for a character from RS-232, only after that it will respond to a key press on int_ext. Is this how you want your program to work?
Also: The function kbhit() will check for a received character on the RS232 line. You are using the hardware UART which will buffer a maximum of three characters. kbhit() will return TRUE until you have read all received characters from this hardware buffer. You never read the received characters from the RS232 buffer, so kbhit() is never cleared.
Note that the software RS232 driver works very different from the hardware one. The software RS232 has a blocking kbhit() which will wait for the first character to be received over RS232 where the hardware based RS232 will just test a flag in the hardware and then continue without waiting. Another difference is that the software version of kbhit() will not remember whether a character was received but not read from the buffer. |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Wed Oct 20, 2004 5:33 pm |
|
|
This line:
Will change the PORTB pins to output because the compiler is in the "#use standard_io (B)" mode by default. Then when you enable the int_ext interrupt, RB0 is still set to output, so you won't receive any interrupts. Set it to input by using set_tris_b(0b00000001);
By the way, you may want to move this portion of the code:
Code: |
if(!kbhit())
{
printf("Normal Mode\n\r");
}
else
{
enable_interrupts(int_ext);
enable_interrupts(global);
}
|
out of the while loop. |
|
|
|