Hi all I am trying to interface GSM SIM900 with atmega32. I have a written a code in which if a user press a push button, a call will be made to a pre defined number. If we want to change the pre defined number, we can send message like CHANGE1234567890 where 1234567890 is the new number so that if anyone presses the push button, call will be made to this new number.
In my code I have made serial_Tx
function which send string serially. serial_Rx
which receive data and uart_put
which transmit single character.
CODE:
//serial initialize
void serial_Init()
{
UCSRB = (1<<TXEN) | (1<<RXEN);
UCSRC = (1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
UBRRL = 51;
}
//serial transmission function
void serial_Tx(char *str)
{
for (unsigned int i=0;str[i]!=0;i++)
{
UDR=str[i];
while(!(UCSRA&(1<<UDRE)));
}
}
//serial transmission single char
void uart_put(char data)
{
while(!(UCSRA & (1<<UDRE)))
{
}
UDR=data;
}
//serial receive function
char serial_Rx()
{
while(!(UCSRA & (1<<RXC)))
{
}
return UDR;
}
char newnum[10];
char smscontent[200];
int len=0; ;
unsigned char slot;
char smscmd[16];
char delcmd[16];
char cfmcall[16];
char call[20];
char smsindi[14];
char data[15];
char calldata[10];
char confirmdata[10];
char deldata[10];
int main(void)
{
DDRA = 0x00;
DDRC = 0xFF;
PORTA = 0xFF;
PORTC = 0x00;
serial_Init();
PORTA=0xFF;
while(1)
{
//********FUNCTION TO GET SLOT NUMBER of received msg***********//
if (serial_Rx() != NULL)
{
//serial_Tx("done\n");
PORTC = 0x01;
for (int i=0;i<=14;i++)
{
data[i] = serial_Rx();
if(isdigit(data[i]))
{
//serial_Tx("enter\n");
PORTC = 0x02;
slot = data[i];
}
}
}
else
{
serial_Tx("nothing to read \n");
}
//**************LOOP END***********************//
//************FUNCTION TO SEND SMS COMMAND**********************//
uart_put(slot); //printing the SMS slot number
sprintf(smscmd,"AT+CMGR=%c\r\n",slot);
serial_Tx(smscmd);
for(int k =0; k<=200;k++)
{
smscontent[k] = serial_Rx();
uart_put(smscontent[k]);
}
//*****************LOOP END*******************************//
//***************FUNCTION TO EXTRACT NEW NUMBER*************//
for(int k =0;smscontent[k]!=NULL;k++) //smscontent[k]!=NULL
{
if (smscontent[k] == 'C')
{
if (smscontent[k+1] == 'H')
{
if (smscontent[k+2] == 'A')
{
if (smscontent[k+3] == 'N')
{
if (smscontent[k+4] == 'G')
{
if (smscontent[k+5] == 'E')
{
if(isdigit(smscontent[k+6]))
{
k=k+6;
serial_Tx("MATCH\n");
for (int i=0;i<=9;i++,k++)
{
newnum[i] = smscontent[k];
}
serial_Tx(newnum); // new number is stored in newnum
}
}
}
}
}
}
}
}
//*****************LOOP END******************************//
//*******************CALL function **********************//
if(PINA==0xFE) // if switch pressed then:
{
sprintf(call,"ATD%s;\r\n",newnum);
serial_Tx(call);
for(int j=0;j<=9;j++)
{
calldata[j] = serial_Rx();
}
if(strstr(calldata,"OK") != NULL)
{
for (int i = 0;i<=20;i++)
{
PORTC = 0x01; // led blink
_delay_ms(150);
PORTC = 0x00;
_delay_ms(150);
}
}
else
{
PORTC = 0x02; // led 2 on
}
}
//*************************LOOP END***********************************//
}
}
I am able to get the slot number of received message and I have also extracted the number from my msg. The problem is that now I have to replace the old number with new number in call function
but its not working properly and even when I press the push button it doesnt function. Please help.
PINA==0xFE
is the proper way to check a button, you should check that as atleast something should've happened? \$\endgroup\$