|
|
View previous topic :: View next topic |
Author |
Message |
Aamir
Joined: 11 Apr 2010 Posts: 13
|
RS232 interrupt problem |
Posted: Sat Feb 25, 2012 11:20 am |
|
|
Hi everyone,
I have been trying this for a while but its not working, I know there will be very small mistake, but can't figure it out.
I am making a remote control lamp, that turns on, when I transmit a signal, off when I do it again. Same signal for on and off.
Code: |
#include <16F877.H>
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
int flag=2;
int q=0,a=0,b=0;
char Rcv;
ser(){
output_low(pin_e1);delay_ms(200);output_high(pin_e1);
delay_ms(500);
//if (Rcv=='Q'){
if(flag==1){ //new q==0
output_high(pin_b7);
a=1;}
if (flag==2) { //new q==1
output_low(pin_b7);
b=1;
}
//}
//if(a==1){
//q=1;a=0;}
//if(b==1){
//q=0;b=0;}
delay_ms(5000);
}
#INT_RDA //serial interrupt: when data available at serial pin of PIC
void SerialInt()
{
Rcv=getchar(); //a loop that stores data from serial
// output_low(pin_e1);delay_ms(200);output_high(pin_e1);
delay_ms(500);
if(flag==1){ //new
flag=2;} //new
else if(flag==2){ //new
flag=1;} //new
ser();
}
void main (){
enable_interrupts(global); //defines that all interrupts are global
enable_interrupts(int_rda); // serial data interrupt
//printf("hello");
//output_high(pin_e1);
output_low(pin_b7);
while(1){
output_high(pin_e1);
}
}
|
When I connect the hardware, it only turns on, never turns off. I checked the hardware, no faults, maybe code is the problem. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Sat Feb 25, 2012 11:53 am |
|
|
two items
1) add 'errors' to the use RS232(...) options....
2) get RID of all the delays inside the ISR ! You've got at least a 6 SECOND delay inside the ISR effectively killing the program.
Take a look at the examples CCS supplies in the examples folder( ex_sisr.c , I think..) for ISR type serial input.
If you're still 'lost' consider simple 'inline' code, without using ISR. |
|
|
Aamir
Joined: 11 Apr 2010 Posts: 13
|
|
Posted: Sun Feb 26, 2012 3:34 am |
|
|
Thanks, actually i added delay because i want it to take single input at a time, i will remove it anyway. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Sun Feb 26, 2012 5:27 am |
|
|
Just receive characters in the ISR.
Then in the main code, toggle your pin, wait, etc..
So:
Code: |
#include <16F877.H>
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
char Rcv=0;
#define OFF (0)
#define ON (1)
#INT_RDA //serial interrupt: when data available at serial pin of PIC
void SerialInt(void) {
Rcv=getchar(); //a loop that stores data from serial
}
void main(void ){
int1 state=OFF;
enable_interrupts(global); //defines that all interrupts are global
enable_interrupts(int_rda); // serial data interrupt
output_low(PIN_B7);
output_high(PIN_E1);
while(1){
switch (state) {
case OFF:
if (Rcv) {
//Here character has been received
output_high(PIN_B7)
delay_ms(5500); //delay before next change is allowed
Rcv=0; //any characters received will be thrown away
state=ON;
}
break;
case ON:
if (Rcv) {
//again character now received
output_low(PIN_B7);
delay_ms(5500);
Rcv=0;
state=OFF;
}
break;
}
}
}
|
All this does is sits looping, waiting for a character to arrive. When it does, it toggles pin B7, waits 5.5 seconds, then throws away any characters receives, and starts waiting again.
Hopefully gives an idea of how to handle what you want.
Best Wishes |
|
|
|
|
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
|