CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

[SOLVED] dsPIC33EP64MC204 RDA Interrupt
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

[SOLVED] dsPIC33EP64MC204 RDA Interrupt
PostPosted: Mon Oct 21, 2019 2:37 am     Reply with quote

Hello all,

I'm working on a project with the dspic and I need to get data from SIMCOM800L with RDA interrupt but I can't get data from the SIM. I checked RDA inits and I think they are true and so I don't understand why it is not working acordingly. I want consult you. Here is my code. (I don't use crystal and I'm using internal oscillator.)
Code:

//MAIN.H header-----------------------------------------------------------------

#include <./33EP64MC204.h>
#include <stdlib.h>
#include <./STRING.H>
#include <./MATH.H>

#use delay(internal=80MHz)     
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES FRC                     
#use i2c(master,I2C1)
#pin_select U1TX=PIN_B9
#pin_select U1RX=PIN_C6
#use rs232(UART1, baud=115200,parity=N,stop=1,ERRORS, stream=GSM)
#use rs232(baud=9600,parity=N,stop=1,xmit=PIN_B6 ,rcv=PIN_B7,enable =PIN_B8, stream = RS485)

char String[20];
char    gsm_rx_arr[200];
int   buffer_ind=0;
char    id[5],imei[25];
unsigned int    check_sum;
unsigned int signal_q=0;

char    timed_getc_gsm();
int     gsm_response_ctrl(char* arry,char* resp1,char* resp2,int repeat,int16 time);
int     open_gsm();
void    close_gsm();
int     open_gprs();
int     get_gprs_connection();
int     sendcommand2server(char* arry1);
void    gsm_first_connection();

char    gsmAT[9] = "AT+CSQ\r\n\r";
char    gsmOK[3]="OK\0";
char    gsmCREG[12]="AT+CGREG?\r\n\r";
char    gsm10[2]=",1\0";
--------------------------------------------------------------------------------
//MAIN.C------------------------------------------
#include <main.h>
#include <stdio.h>
#include "DS1307.h"
#include "I2C_Functions.h"
#include "printBig.h"
#include "i2c_scanner.h"
#include "Button.h"
#include "Date.h"
#include "Identity.h"
#include "gsm.h"

#INT_RDA HIGH
void serial_interrupt(void)
{   
    gsm_rx_arr[buffer_ind] = fgetc(GSM);
    buffer_ind++;
}


void main() {
    setup_oscillator(OSC_INTERNAL, 80000000);
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);
while (1) {
       
        open_gsm();
       delay_ms(200);
   }
}

//gsm.h--------------------------------------------------------------------------
int open_gsm()
{
   
    if(!gsm_response_ctrl(gsmAT,gsmOK,gsmOK,5,2000))
   
    {   
        fprintf(RS485,"Return 0");
        return(0);
    }
    return(gsm_response_ctrl(&gsmAT,&gsm10,&gsm10,10,2000));
}
int gsm_response_ctrl(char* arry,char* resp1,char* resp2,int repeat,int time)
{
    int i=0,j=0;
    for(i=0;i<repeat;i++)
    {
     
        clear_rx_arr();
        fprintf(RS485,"\r\n");
        for(j=0;j<100;j++)
        {
            fputc(arry[j],GSM);
            if(arry[j] == '\n' && arry[j+1] == '\r') break;
            fputc(arry[j],RS485);
           
        }
   
        delay_ms(1000);
        fprintf(RS485,"+++++");
        for(j=0;j<200;j++)
        {
         
           if(gsm_rx_arr[j] == '\0') continue;
           fprintf(RS485,"%c",gsm_rx_arr[j]);
           
           
        }
      /
         
 
        if(strstr(gsm_rx_arr,resp1) != 0 || strstr(gsm_rx_arr,resp2) != 0) return(1);
        delay_ms(time);
    }
    return(0);
}

Why can't I get strings ?


Last edited by camleot23 on Tue Oct 22, 2019 9:30 am; edited 2 times in total
PrinceNai



Joined: 31 Oct 2016
Posts: 462
Location: Montenegro

View user's profile Send private message

PostPosted: Mon Oct 21, 2019 3:05 am     Reply with quote

Do you reach your RDA interrupt routine? Maybe toggle a LED any time you come there, to at least see if it fires.

One of the possible problems is baud rate. Are you sure SIM is set to your speed? I had a few of those and they were set anywhere from 9600 to 115,200 when new.

Regards
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

PostPosted: Mon Oct 21, 2019 3:08 am     Reply with quote

Yes it is working on 115.200 baudrate. I know this because I can control outputs with rs232 probe and can see the incoming data from my computer. For your led suggestion, yes I was doing it and now I tried it again, I see the led toggling. It means interrupt working but my array is empty!

EDIT: Actually, It seems like it is interrupting just for once. It doesn't seem like getting interrupted for many times. For example, If I am getting "abcdefg" from SIMCOM, my interrupt is working and just for once makes an interrupt and tries to get "a" but it can't get, then it does nothing, I mean, it does not try to get the bcd... I could conclude this from toggling speed of led. It is slooow.
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Mon Oct 21, 2019 7:24 am     Reply with quote

Comments:

On the DSPIC, you can have multiple characters waiting when you arrive
in the ISR. Unlike the PIC16/18, where the interrupt cannot be cleared till
the buffer is empty - so if a couple of characters are waiting the ISR
will be called again. On the DsPIC's your ISR needs to loop reading all the
characters. So:

Code:

#INT_RDA HIGH
void serial_interrupt(void)
{
   do
   {
         gsm_rx_arr[buffer_ind] = fgetc(GSM);
         buffer_ind++;
   } while (kbhit(GSM));
}


Then there is a second issue, that nothing resets buffer_ind, and nothing
stops it potentially getting to the end of the array. You need code to
control this index.

Then get rid of 'HIGH'. If you look, though 'high' is mentioned, for the
DsPIC's, no information on it is given. If you want an ISR that has a higher
priority than others, you need the preprocessor command:

#DEVICE NESTED_INTERRUPTS=TRUE

and then set the interrupt to a higher priority with:

#INT_RDA LEVEL=5

Choose a level that suits. This then allows the ISR to interrupt standard
ISR's.

Then your string declarations are wrong. Remember a string has a
terminating NULL. The string "AT+CSQ\r\n\r", needs _10_ characters
of storage, not 9. Your gsmOK string is OK, but then all the others
do not allocate enough storage to actually hold the strings.
You don't need to add extra NULL's these are always there, and just get
rid of the sizes and let the compiler automatically size the arrays.

Don't include libraries till after the fuses and configuration is done. The
compiler has got friendlier about this in recent years, but stdlib in
particular can malfunction if the STDIO is not declared when it is loaded.

Then is the string from the modem actually null terminated?. Generally
I'd suspect not. A string in C, is a null terminated array of characters.
Your receive code is going to have to look for the line feed character,
and automatically add a null terminator after this is received, or all the
'string' functions cannot work with the received array.
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

PostPosted: Mon Oct 21, 2019 9:42 am     Reply with quote

Ttelmah, thank you very much for your valuable responses and information. I've applied your codes and suggestions into my code. However, I've concluded interesting results. I have an UART to USB converter. I look for the data in both sides. Modem can receive AT command and can send "OK". I can see them from the buses via UART to USB converter in my PC. Also, here is the interesting thing, When I send data from the converter to dsPIC, it can get the data !. But dsPIC can't get the data from the Modem, it just gets full of Nulls. I suspect that the problem is in hardware.
In addition, I wonder, is there any logic level that my converter can read logic level but dsPIC can't read the same logic level?.

(converter is something like that: https://www.amazon.co.uk/PL-2303HX-PL-2303-Drivers-available-Windows/dp/B00AVRIDB0 )
PrinceNai



Joined: 31 Oct 2016
Posts: 462
Location: Montenegro

View user's profile Send private message

PostPosted: Mon Oct 21, 2019 11:35 am     Reply with quote

Maybe a stupid question. Is it possible you have Rx and Tx on the modem switched? With labels on those modules I'm never sure which side they are referring to, MCU or the module. I had a SIM900 module where MCU's Rx went to the pin labeled Rx on the module.
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Mon Oct 21, 2019 1:58 pm     Reply with quote

Are you using a 'bare' sim800, or a board carrying it?. If a board, what was
it designed to interface to?. (Arduino, etc..).
Many devices like the Arduino, are 5v logic, and the boards made for them
have 5v conversions in the connections. These won't work with a 3.3v
PIC....
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 1:23 am     Reply with quote

I'm using it on a board with dsPIC33EP64MC204 for communication between
each other. So, did I understand correctly that you said with 3.3V pic(it means
that it is my dsPIC) my GSM module won't work?In short, won't I read incoming data ?

EDIT: PrinceNai, no it is not a stupid question, actually thank you for your comment. But, I connected them correctly.


Last edited by camleot23 on Tue Oct 22, 2019 1:41 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 1:33 am     Reply with quote

No. If you are talking directly to the SIM chip it should work fine. I was asking
if you were perhaps connecting to a modem 'module' built to run with
another processor like an Arduino?. If so, these modules contain interfaces
that make them incompatible with the PIC.
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 1:45 am     Reply with quote

Ha okay, I see. No, I have just a PIC.
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 2:12 am     Reply with quote

How is the modem chip physically connected?. Is the board you are using
one built for the PIC24, or one that is more 'universal' for PIC18's as
well?. Part number/link for it?.
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 2:16 am     Reply with quote

It is designed for PIC24 not universal and I'm sorry I don't have a link or something. So, you are suspecting from the hardware?
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 2:48 am     Reply with quote

No, not really.
There are just so many faults in the code, that this is the most likely
problem. However the continuous receipt of data when nothing is being
sent 'smells' of a hardware issue. Hence the questions.
PrinceNai



Joined: 31 Oct 2016
Posts: 462
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 2:52 am     Reply with quote

I soldered a second row of pins to my TTL-USB module. My hookup for testing those gadgets is always like this: from PIC to TTL module and from TTL module to GSM. Only 3 wires, Rx, Tx and GND, positive voltage for the GSM comes from the board with PIC on it. That way I can spy on the conversation between PIC and GSM. I don't send anything from PC, just listening. If you send "AT" from PIC, does the GSM reply with "OK"? I'd make a small test program that just sends AT every second or so to test that.

Do you happen to have a 5V PIC laying around? That way you could test if voltage levels are causing your problem.
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

PostPosted: Tue Oct 22, 2019 3:25 am     Reply with quote

Thank you both for your help.I really appreciate. I've solved the problem.
There was a hardware issue that decreases the SIMCOM Tx pin's logic level
to 1.6V, so when I solved this problem I could read the data that sent from
SIMCOM. For people who wonders my RDA interrupt code, is Ttelmah's code
that has been sent above.

#INT_RDA HIGH
void serial_interrupt(void)
{
do
{
gsm_rx_arr[buffer_ind] = fgetc(GSM);
buffer_ind++;
} while (kbhit(GSM));
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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