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

UART own code not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lokeshbhatt



Joined: 16 Mar 2010
Posts: 11

View user's profile Send private message Send e-mail

UART own code not working
PostPosted: Sun Mar 28, 2010 2:39 am     Reply with quote

Hi,

As you know that if you don't collect data coming on receive pin of UART
UART will be shut down.
Same thing happening in my code because as I have interfaced uart with sim300 gsm modem. It gives initial string like

RDY

+CFUN: 1

+CPIN: READY

After that I'm sending command trying to collect response given by modem but it is not happening. Because of initial string it is getting shutdown.
My code size is increasing thats why I can't use library functions like puts, getc, putc, and other. I have written the code below but nothing it is showing.

If I would use library functions #use rs232 than there is ERRORS bit which just collect all errors.
How can I do without #use rs232 ?
Even I'm clearing CREN bit and checking OERR bit but it is continuous getting error. How to solve this.

I would be grateful to you.

Thanks in Anticipation
Lokesh
Code:

#include <16f690.h>
//#inlcude "string.h"
//#fuses NOWDT,NOPROTECT,CPD,NOMCLR,NOIESO,NOFCMEN  // preprocessor directive that defines fuses for the chip
#fuses INTRC_IO,NOWDT,NOPROTECT,CPD,NOMCLR,NOIESO,NOFCMEN

#use delay(clock=8000000)
//#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5,BITS = 8, PARITY= N,ERRORS )  // preprocessor directive that includes the rs232 libraries

#define IGT PIN_B4
#byte PIR1  = 0X0C
#byte PIE1  = 0X8C
#byte RCREG = 0x1A
#byte RCSTA = 0x18      
#byte TXSTA = 0x98
#byte TXREG = 0x19
#byte SPBRG = 0x99
#byte BAUDCTL = 0x9B
#byte ANSEL = 0x11E
#byte ANSELH = 0x11F

#bit RCIF = PIR1.5
#bit TXIF = PIR1.4
#bit CREN = RCSTA.4
#bit SPEN = RCSTA.5
#bit  OERR =  RCSTA.1

//#define PIR1  = 0X0C

void Serial_Init(void);

void ReadSerial(void);
void ReadWhile(unsigned char);
void WriteSerial(unsigned char *cByte );
void WriteSerial1(unsigned char );
unsigned int tCount1=0,bchknetwork=0,fTimeOver=0,tmpsize=0;
unsigned char ReadData=0;
//bit bOper,bOperStatus;
unsigned char *cByte;

/***************************************************************
**************          TIMER ISR FUNCTION      *********************
***************************************************************/
/*#INT_TIMER1
void my_timer()
{
//disable_interrupts(GLOBAL);   

//puts("It's Timer Routine");

   tCount1++;
      if(tCount1 == 50)      // IF 1SEC THAN CHECK FOR NETWORK
      {
         bchknetwork=1;
          tCount1=0;
          fTimeOver=1;
         if(bOperStatus != 1)
          puts("AT+COPS?\r");
         else if(bOperStatus == 1)
         puts("Operator is OK");
      putc('I');
      }

clear_interrupt(INT_TIMER1);
//enable_interrupts(GLOBAL);   
}*/

/***************************************************************
**************          MAIN FUNCTION      *********************
***************************************************************/
void main()
{
BYTE val;
unsigned char Reg_Arr[20],Command[5],Phone[14];
//unsigned char CMGR[]={{"AT+CMGR=1\r"}, {"AT+CMGR=2\r"}, {"AT+CMGR=3\r"},  {"AT+CMGR=4\r"}};
   
             
//unsigned char CMGD[]={{"AT+CMGD=1\r"},  {"AT+CMGD=2\r"}, {"AT+CMGD=3\r"},{"AT+CMGD=4\r"}};
                
unsigned char *arr[] = "AT\r";               
unsigned char Status=0,cCount,n,bReg=0,var=0,Msg_Index,Quote_Count=0,ph=0,cmd_flag=0,cmd=0;
unsigned char ComBuf1[30]={0},Oper[7]={0};//={"AT+CNMI=1,0,0,0,0\r"};

setup_oscillator(OSC_8MHZ);
setup_adc_ports(NO_ANALOGS | VSS_VDD);
//setup_adc(ADC_CLOCK_DIV_4);
//setup_adc_ports(sAN8 | VSS_VDD);

set_tris_c(0b00000000);
set_tris_b(0b00100000);
//SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_1);
//SET_TIMER1(25536);      // for 20msec 40000 count required

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
//enable_interrupts(INT_TIMER1);
//disable_interrupts(INT_RDA);
ANSEL  = 0X00;
ANSELH = 0X0000;
TXSTA = 0X20;   // TRANSMIT ENABLED
RCSTA = 0X90;   // RECEIVE ENABLED AND SERIAL PINS ENABLE
BAUDCTL = 0X00;
SPBRG = 12;
PIE1 = 0X30;
/************ LOW-HIGH-LOW PULSE ON PWRKEY PIN OF GSM */
output_low(IGT);
delay_ms(2000);
output_high(IGT);
delay_ms(2000);
//output_low(IGT);
//delay_ms(2000);
delay_ms(5000);

/******************************************************************
                CHECKING FOR NETWORK
   ******************************************************************/
    if(OERR == 1)
    {
   CREN = 0;
   WriteSerial1('C');
    CREN = 1;
    }

      WriteSerial(arr);
      do{
      ReadSerial();
      var = ReadData;
      WriteSerial1(var);
      //WriteSerial('\0');
      }while(var != 'K');
      WriteSerial1(var);   
      WriteSerial1('Y');
   
      delay_ms(5000);
   }   //WHILE1
 }  //MAIN
//}      

//**********************************************************
void ReadSerial(void)
{
      if(OERR == 1)
             {
         CREN = 0;
             CREN = 1;
             }
      while(RCIF == 0 ) //&& fTimeOver != 1);
      ReadData = RCREG;

       //delay_ms(5);
   
       
   //   RCIF = 0;
     //return ReadData;
     //}while(ReadData != char_2_recv);
}

//**********************************************************
/*void ReadWhile(unsigned char char_2_rcv)
{
   do{
      while(RCIF == 0 && fTimeOver != 1);
      ReadData = RCREG;
      RCIF = 0;
     }while(ReadData != char_2_rcv || fTimeOver != 1);
   }

/****************************************************************************************************************
                            Send string to GSM modem
****************************************************************************************************************/
void WriteSerial( unsigned char *cByte)
{
   //   unsigned char iCount ;
   //   for(iCount=0;iCount<strlen(cByte);iCount++)
   //      SendByte(cByte[iCount]) ;
      while(*cByte != '\0')      //while(*cByte != '\0')
      {
      TXREG = *cByte;
      while(TXIF == 0);
   //   TXIF = 0;
      cByte++;   
      //lenth--;
      delay_ms(50);
      }
}   

/**/

_________________
Thanks & Regards
Lokesh Bhatt
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Sun Mar 28, 2010 3:00 am     Reply with quote

Why re-invent the wheel ??
why set the bits in code, use the #use rs232 directive instead.

does the #use rs232 directive work at all ?? If your hardware and code are OK it will work alright.

When using the #use rs232 directive you do not have to set the bits in your code, the compiler does this automatically.

You can also use the printf, putc() , getc() commands as well, instead of making your own.

thanks
arunb
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Sun Mar 28, 2010 3:12 am     Reply with quote

The key thing is that like most things associated with hardware, you _must_ clear the hardware condition, as well as the flags associated with them.
So, to reset the 'OERR' condition, requires the following:
Read a character.
Turn off CREN
Turn CREN back on.

You need to empty the buffer _before_ clearing OERR fault, or it'll reset immediately. So something like:
Code:

#define your_kbhit() (RCIF)

int8 your_getc(void) {
   int8 rval=0;
   if (your_kbhit()) {
      rval=RCREG;
      if (OERR) {
         CREN=FALSE;
         CREN=TRUE;
      }
   }
   return rval;
}

As a comment, though you have it remmed out at the moment, _get rid of the line enabling the global interrupt in the timer_. On the PIC, you must _never_ enable the global interrupt inside an interrupt routine.
The PIC _hardware_, automatically enables the global interrupt, on the return from interrupt instruction, and _relies_ on global interrupts being disabled automatically inside the interrupt handlers. Enabling the global flag, inside an interrupt handler, is a 'sure way' to shoot yourself in the foot, with recursive calls occurring, and code locking....

Then there is the second 'no no'. Never enable an interrupt, without a handler being present. You have INT_RDA being enabled, but no handler shown for this. This will hang the code. This may actually be your main problem...

You don't need to enable the interrupt to use software based routines that read the interrupt flag. If an interrupt is enabled, a handler _must_ be present for it.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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