View previous topic :: View next topic |
Author |
Message |
Alanis
Joined: 28 Sep 2007 Posts: 13
|
signal and interrupt detection on 12F |
Posted: Sun Dec 16, 2007 2:01 pm |
|
|
After some time offline...
I write this small code to test the interrupt that i want to use as signal detector input. I dont know if this is the wright way to do this, so i need your suggestions.Please let's take this one step at time so i ( and others ) can follow this for future use.
Code: |
#include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#byte ADCON0 = 0x1F
#byte ANSEL = 0x9F
#byte CMCON = 0x19
void init(){
set_tris_a( 0b11111101 ); // GP1 OUTPUT, rest = INPUT
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
ADCON0 = 0; // ADC off
ANSEL = 0; // 0 - 4 = digital
setup_comparator( NC_NC_NC_NC ); // disable comparators
ext_int_edge(L_TO_H); // positive edge
enable_interrupts(INT_EXT); // run interrupt
enable_interrupts(GLOBAL); // run global
}
void main(){
init();
while ( TRUE ){
output_high( PIN_A1 ); delay_ms( 350 );
output_low ( PIN_A1 ); delay_ms( 350 );
}
}
|
Situation:
Incoming signal is generated in one external device with smartcard.
The signal from this device is passed to GP2 at speed of 9600 b/s.
OK.
The idea was to set "read speed of the GP2" per rs232, so that this speed would be equal to speed passed from external device.
On this way, i should be able to get all correct data from external device. What do you think about that ?
So, GP2 should be able to detect interrupts ( signals ) at speed of 9600 b/s. If this signals are 100101010110101010101 this should be detected.
Or ?
With the code above i can detect interrupts but i dont know if this could be useful in my case or at speed of 9600 b/s. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 16, 2007 2:06 pm |
|
|
Quote: |
enable_interrupts(INT_EXT); // run interrupt
enable_interrupts(GLOBAL); // run global
With the code above i can detect interrupts |
You don't have an #INT_EXT routine. Because of this, if you get an
interrupt, the program will crash. |
|
|
Alanis
Joined: 28 Sep 2007 Posts: 13
|
|
Posted: Wed Dec 19, 2007 3:35 pm |
|
|
Hi.
Am not sure how to use the interrupt routine for such problem.
To be true i dont know if this is the right way to do something like this, because i dont think that external interrupt can detect characters, bits , bytes etc. on such speeds. Maybe wrong thinking ?
Code: | #include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#byte ADCON0 = 0x1F
#byte ANSEL = 0x9F
#byte CMCON = 0x19
#define LOW_LEVEL
#define HIGH_LEVEL
#define SPEED_CHECK
#define MS_counter
void init(){
set_tris_a( 0b11111101 ); // GP1 OUTPUT, rest = INPUT
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
ADCON0 = 0; // ADC off
ANSEL = 0; // 0 - 4 = digital
setup_comparator( NC_NC_NC_NC ); // disable comparators
ext_int_edge(L_TO_H); // positive edge
enable_interrupts(INT_EXT); // run interrupt
enable_interrupts(GLOBAL); // run global
}
#INT_EXT
void ext_isr(void)
{
if() { // how to define that this is the place where program should read the first bit
output_low(PIN_A1);
}
else{
output_high(PIN_A1);
}
}
void main(){
init();
output_low( PIN_A2 );
output_low( PIN_A3 );
output_low( PIN_A4 );
while (1){}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 19, 2007 3:39 pm |
|
|
What is the external device ? |
|
|
Alanis
Joined: 28 Sep 2007 Posts: 13
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Dec 24, 2007 10:57 am |
|
|
Quote: |
To be true i dont know if this is the right way to do something like this, because i dont
think that external interrupt can detect characters, bits , bytes etc. on such speeds.
Maybe wrong thinking ?
|
Yes you are.
The external interrupt in most MCU are edge triggered. Having the choice to select with
which edge the external interrupt would be triggered, is is easily configurable to
capture the beginning of an upcoming character. For an RS232 signal level, in the TTL
side, an idle line means a steady H level, hence any action regarding a char reception
should start with a going down signal.
This is what we select with the H_to_L option in the ext_int_edge() function to
capture the front edge, the very first event.
You should know that in a Serial Data Transmition (RS232 is one of the most common
methods of sending data from one device to other) the first event that announce that
a data is coming is a START bit, a going down signal that is 1/10 of the used speed.
Setting up properly the edge to trigger the External Interrupt we can easily detect the
beginning of a data stream. But doing this, we will only get that the MCU falls immediately
in its vector interrupt when it "see" the going down of the START bit pulse, nothing more.
But remember that our goal is to catch a full char, then to get a full char, we must code
the External Interrupt handler, because actually the internal actions of the MCU was
hardware redirected here.
Fortunately the C language give us the getc() function that makes our life easier:
Code: |
#INT_EXT
void EXT_isr( void )
{
RcvdChar = getc();
OneCharHasArrived = TRUE;
}
|
In this way we already captured the incoming character and copied it in the variable RcvdChar.
The variable OneCharHasArrived is used only as a flag to indicate that a new char
was received and is available.
In main() we only need to watch if such flag was setted. The receiving process was done in background.
The following is very straight forward.
Code: |
char RcvdChar;
unsigned int OneCharHasArrived;
void main()
{
init();
setup_comparator( NC_NC_NC_NC );
ext_int_edge(H_to_L);
enable_interrupts(EXT_INT);
enable_interrupts(GLOBAL);
while(1)
{
// your code
........
........
if(OneCharHasArrived)
{
putc(RcvdChar);
OneCharHasArrived = FALSE;
}
}
}
|
Humberto |
|
|
Alanis
Joined: 28 Sep 2007 Posts: 13
|
|
Posted: Mon Feb 25, 2008 1:16 pm |
|
|
Hi friends!
I got myself the 16F877 chip and am testing this on demo board.
Now i want to continue with my project in this enviroment.
What would be the first step, should i go with external interrupt or maybe some other idea ?
Any suggestion is welcome and my goal is still the same.
Alan |
|
|
Alanis
Joined: 28 Sep 2007 Posts: 13
|
|
Posted: Wed Feb 27, 2008 11:50 am |
|
|
Any idea about my signal detection problem ?
If someone knows how to solve this please give a tip where to start.
Thank you. |
|
|
|