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 CCS Technical Support

Pointers in 16F690

 
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

Pointers in 16F690
PostPosted: Fri Mar 19, 2010 10:44 pm     Reply with quote

Hi,

Can we use pointers in 16f690 c code ?
One situation in my project is I have to send string to sim300 like "AT+CNMI\r". So I don't understand how to accomplish it.
I have used below pointer but it is giving error like "attempt to create a pointer".
If any suggestion to send string are appreciated.
I can't use array because I have to send so many different string commands so that I don't have much space.
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=115200, xmit=PIN_B7, rcv=PIN_B5 ) // preprocessor directive that includes the rs232 libraries

#define IGT PIN_C7
#byte PIR1  = 0X0C
#byte RCREG = 0x1A
#byte RCSTA = 0x18      
#byte TXSTA = 0x98
#byte TXREG = 0x19
#byte SPBRG = 0x99
#byte BAUDCTL = 0X9B

#bit RCIF = PIR1.5
#bit TXIF = PIR1.4
//#define PIR1  = 0X0C

void Serial_Init(void);

void WriteSerial(unsigned char );
void WriteSerialS(unsigned char *sByte);

void ReadSerial(void);
unsigned int tCount1=0,bchknetwork=0,fTimeOver=0,tmpsize=0;
unsigned char ReadData=0;
//bit bOper,bOperStatus;
//const char *Byte;

/********************************************************
************          MAIN FUNCTION      *******************
********************************************************/
void main()
{
BYTE val;
unsigned char Status=0,cCount,n,bReg=0,var=0,Msg_Index,Quote_Count=0,ph=0,cmd_flag=0,cmd=0;

setup_oscillator(OSC_8MHZ);
setup_adc_ports(NO_ANALOGS | VSS_VDD);

set_tris_c(0b00000000);
set_tris_b(0b00100000);
TXSTA = 0X20;      //ONLY TRANSMIT ENABLED
RCSTA = 0X90;      
BAUDCTL = 0X00;
SPBRG = 12;
//enable_interrupts(GLOBAL);

/************ 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(35000);

   while(1)
   {
   //   puts("AT\r");
   WriteSerial('A');
   WriteSerial('T');
   WriteSerial('\r');
   WriteSerialS("AT+CNMI=1,0,0,0,0\r");
   
      while(1)
      {   ReadSerial();
         WriteSerial('M');
         WriteSerial(ReadData);
         delay_ms(200);
      }
   }   //WHILE1
 }  //MAIN

//*****************************************************
void ReadSerial()
{
   
      while(RCIF == 0 );
      ReadData = RCREG;
      RCIF = 0;
     //return ReadData;
     //}while(ReadData != char_2_recv);
}
//*****************************************************

/******************************************************
                            Send string to GSM modem
******************************************************/
void WriteSerial(unsigned char wByte)
{
   //   while(*Byte != '\0')
   //   {
      TXREG = wByte;
      while(TXIF == 0);
      TXIF = 0;
   //   Byte++ ;   
//      }
}   


/******************************************************
                            Send string to GSM modem
******************************************************/
void WriteSerialS(unsigned char *sByte)
{
      while(*sByte != '\0')
      {
      TXREG = sByte;
      while(TXIF == 0);
      TXIF = 0;
      sByte++ ;   
      }
}   


/*****************************************************                               Sending Byte by UART
*****************************************************/
/*void SendByte(unsigned char cByte)
{
      TXREG = cByte;
      while(TXIF == 0);
      TXIF = 0;
      //val++;   
      }   
   
}8*/
/*******************************************************
      CHECKING FOR SMS
*******************************************************/
/*   Quote_Count=0;
   for(Msg_Index = 0; Msg_Index < 4; Msg_Index++)
   {
      puts("AT+COPS?\r");
      delay_ms(20);
      puts("AT+CPAS\r");
      delay_ms(20);
      puts("AT+CMGR=0\r");
            do{
            var=getc();
            if(var == 'K')
            break;
            else if(var == '"')
            {   
            Quote_Count++;
            cmd_flag=1;
            }
            
             if(Quote_Count == 2)
            {
               do{
                  Phone[ph++] = getc();
                  }while(ph!=13);   
               Quote_Count++;   
            }
            }while(Quote_Count != 5 );
         cmd=0;
      if(cmd_flag ==1)
       {
         do{
           var = getc();
           puts("waiting for O");
           Command[cmd++] = var;
         }while(var != 'O');
         cmd_flag=0;
       }
       delay_ms(2000);
//   puts("AT+CMGD=0\r");*/
//33333
/*
puts("AT+CMGS=\"+919967753968\"\r");
do{
var=getc();
}while(var!='>');
puts("NARAYAN NARAYAN NARAYAN OM !!!!!!!!\r");
putc(0x1A);
do{
ar=getc();
/***************************************************
*********                GSM SETTING                  ************
****************************************************/

/****************************************************************************************************************
                            Check for SMS.
****************************************************************************************************************/
/***************************************************************
**************         UART ISR FUNCTION      *********************
***************************************************************/

/*#int_rda
void rda_isr(void)
{
//   disable_interrupts(GLOBAL);   

   unsigned char tmpByte=0, tmp1=0;
      //if( bOper ==1 )   
      tmpByte = getc();
      putc('U');
      if(bOper == 1 && tmpByte == '"')
      bOperStatus = 1;
//   disable_interrupts(GLOBAL);   
   clear_interrupts(INT_RDA);
}
*/

/***************************************************************
**************          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);   
}*/


//***********************************************************
/*********** 
*************************************************************/
/************ 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(35000);

//   Byte = "AT+CNMI=1,0,0,0,0\r";
   puts("AT+CNMI=1,0,0,0,0\r");
//    WriteSerial(arr);
   delay_ms(100);
   
   puts("AT+CMGF=1\r");
     
   delay_ms(100);

   puts("AT+IPR=9600\r");
   
   delay_ms(100);

   puts("AT&W\r");
   
   delay_ms(100);

   puts("AT+CLIP=1\r");
   
   delay_ms(100);
   
//   enable_interrupts(INT_RDA);
//   enable_interrupts(INT_TIMER1);

   bchknetwork=0;
   bOperStatus = 0;
   bOper = 0;
   */

_________________
Thanks & Regards
Lokesh Bhatt
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Mar 20, 2010 7:03 am     Reply with quote

This is covered in many posts here. A search should find a lot about it.

However, a series of basic things:
This comes about because of the architecture of the PIC. Historically, most 'commonly used processors, have the RAM, and ROM, sitting in the same address space. On the PIC, a different architecture is used. The Harvard architecture. This has separate address spaces for ROM, and RAM. Now the problem this introduces, is that 'address 100', in a normal (Von Neumann) architecture processor, always refers to just one location. On the Harvard architecture, there are two locations with this address - one in ROM, and one in RAM. Then, on the simpler PICs (most of the earlier ones), there is no method of actually reading data 'from' a specific location in the program memory. On these to provide 'constant' storage, a program has to be written, that returns the required byte, when given a particular number, with no ability to select data from a specific address directly.

There are a number of ways of getting (at least in part) the effect of using pointers, or of actually using them:

First one:
Have a single RAM buffer, use strcpy, to copy a ROM constant into the buffer, then use this with a standard pointer based function. Since the copied data is now in RAM, normal pointer functions can be used. Downside, a RAM buffer is needed.

Second:
The modern compilers have the ability to do this for you, with the option to pass strings in RAM. Same downside.

Third:
To give some of the same ability, with the least cost in RAM, and ROM, CCS has a 'shortcut' with regards to ROM constant strings.
Code:


void function(int8 chr) {
    //Do something, with just one character
}

//Call with
function("Test string");


Now, the call looks strange, since the function accepts a single character, but is being called with a string. What CCS have done, is if you hand a constant string to a function accepting a single integer, it _automaticall generates the code to call the function repeatedly with each character in turn from the string. This is the most 'code compact' solution. This is in fact what the string copy function does. It is an overloaded function, able to accept either a string address, or a single integer, and if called with the latter, automatically puts the characters passed 'in turn', into the output string.

Fourth:
CCS now allows a 'ROM' construct, that directly supports pointers (on chips with the abilities this needs). Downside, bulkier, and only works on reasonably recent compilers.

A search for 'create pointer to constant', asking for 'all' terms to be found, will return a lot more.

Best Wishes
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Sat Mar 20, 2010 7:34 am     Reply with quote

There is yet another method, but it only works on certain processors. It is available on the PIC16F690. That's what I call a "wormhole" between the ROM and data spaces, which you can access by using the read_program_eeprom command, or by writing your own routine. If space is really tight, you can load each ROM location with 14 bits rather than 8.

I've never found (never needed to find) a really elegant way to make this work, but it definitely is usable. It's not really a pointer operation, but you could write a function to make it look more normal.

Sorry if this method is actually involved in one of TTelmah's suggestions, but I don't think it was. It's not likely that the compiler would use it, given that it's not available on all the processors.
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