Hello,
I have this code:
#define rb(NBIT, SFR) ((SFR & ( 1 << NBIT )) >> NBIT)
volatile char rx_buf[200], rx_byte;
volatile int rx_buf_len = 0;
volatile unsigned char tcnt2;
void setup() {
Serial.begin(57600);
pinMode(3, INPUT);
attachInterrupt(1, gsm_rx_int1, FALLING);
}
void (*gsm_rx_timer2)() = NULL;
void gsm_rx_int1 () {
detachInterrupt(1);
rx_byte = 0;
gsm_rx_timer2 = gsm_rx_bit0;
TIMSK2 &= ~(1<<TOIE2);
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
ASSR &= ~(1<<AS2);
TIMSK2 &= ~(1<<OCIE2A);
TCCR2B |= (1<<CS21);
TCCR2B &= ~((1<<CS22) | (1<<CS20));
float prescaler = 8.0;
tcnt2 = 256 - (int)((float)F_CPU * 0.0001045 / prescaler);
TCNT2 = tcnt2;
TIMSK2 |= (1<<TOIE2);
}
ISR(TIMER2_OVF_vect){
TCNT2 = tcnt2;
gsm_rx_timer2();
}
void gsm_rx_bit0 () {
rx_byte = rx_byte | (rb(3, PIND) << 0);
gsm_rx_timer2 = gsm_rx_bit1;
}
void gsm_rx_bit1 () {
rx_byte = rx_byte | (rb(3, PIND) << 1);
gsm_rx_timer2 = gsm_rx_bit2;
}
void gsm_rx_bit2 () {
rx_byte = rx_byte | (rb(3, PIND) << 2);
gsm_rx_timer2 = gsm_rx_bit3;
}
void gsm_rx_bit3 () {
rx_byte = rx_byte | (rb(3, PIND) << 3);
gsm_rx_timer2 = gsm_rx_bit4;
}
void gsm_rx_bit4 () {
rx_byte = rx_byte | (rb(3, PIND) << 4);
gsm_rx_timer2 = gsm_rx_bit5;
}
void gsm_rx_bit5 () {
rx_byte = rx_byte | (rb(3, PIND) << 5);
gsm_rx_timer2 = gsm_rx_bit6;
}
void gsm_rx_bit6 () {
rx_byte = rx_byte | (rb(3, PIND) << 6);
gsm_rx_timer2 = gsm_rx_bit7;
}
void gsm_rx_bit7 () {
rx_byte = rx_byte | (rb(3, PIND) << 7);
gsm_rx_timer2 = gsm_rx_end;
}
void gsm_rx_end () {
TIMSK2 &= ~(1<<TOIE2);
rx_buf [ rx_buf_len ++ ] = rx_byte;
attachInterrupt(1, gsm_rx_int1, FALLING);
}
void loop () {
delay (1000);
Serial.print("\r\n");
for (int i = 0; i < rx_buf_len; i++) {
Serial.print(rx_buf[i]);
}
Serial.print("\r\n");
}
I've copied the TIMER2 code from an URL that I have closed trying to copy the URL... lol.
I'm receiving strange chars in the arduino console, but I want to see human readable ones, as the gsm module sends to me, but i am not able to read well.
I implemented a delayed serial rx disabling interrupts, and everything read ok. but I need to do with interrupts, and the reason because I am doing "my own softwareserial" it's because I'm searching the way of making to work together my servo and my gsm module in my own robot, which uses an ATMEGA328 at 5V. So there's no extra hardware serial for working with gsm module. And the softwareserial with the servo library doesn't work fine at all... i had a lot of buzz in the servo while rx from gsm...
the true, is i could rx data from gsm and handle the servo at same time, but the reading functions were using delayMicroseconds(), and used to detect bit falling in a continous reading, so some data wen't to trash... now I want to try with interrupts, but I can detect bit falling and use delays again... but I don't really like, because I must disable interrupts, and then the servo has some spikes, and if I don't disable them, then the servo works fine, but reading sometimes is bad, so the sollution is the FALLING int1, but as I said, I also want to take profit of these delaytimes to handle the servo, so better performance if I find the sollution.
Hope that project helps others, and to get helped.
Thanks,
cheers.