You are on page 1of 7

/*****************************************************

This program was produced by the


CodeWizardAVR V1.25.3 Standard
Automatic Program Generator
© Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 4/10/2011
Author : F4CG
Company : F4CG
Comments:

Chip type : ATmega8


Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <stdio.h>
#include <delay.h>
void set_noecho(void);
void report1(void);
void report2(void);
void cek_new_SMS(void);
void compare_sms(void);
void ready_gsm(void);
void delete(void);
void scan_number(void);
void cek_number1(void);
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];
#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
{
--tx_counter;
UDR=tx_buffer[tx_rd_index];
if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
{
tx_buffer[tx_wr_index]=c;
if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
++tx_counter;
}
else
UDR=c;
#asm("sei")
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
bit flag_no,xpsn;
int no_hp;
#define enter putchar(13)
#define buka PORTB.0
#define tutup PORTB.1
#define xbuka PORTB.2
#define xtutup PORTB.3
flash unsigned char
no1[13]={"085260913631"},psn1[13]={"6F78D95D2603"},psn2[13]={"63F67B5E2603"};
unsigned char rx_no[13],rx_psn[13],i,hasil;
void set_noecho()
{
printf("ate0"); enter;
while(getchar()!='O')
{
printf("ate0"); enter;};
delay_ms(100);
}
void ready_gsm()
{
printf("at");enter;
while(getchar()!='O'){printf("at");enter;};
delay_ms(500);
}
void delete()
{
PORTB.1=1;
printf("at+cmgd=1");putchar(13);
//
while(getchar()!='O')
{
printf("at+cmgd=1");enter;};
delay_ms(300);
PORTB.1=0;
}
void scan_number() //manual scan number is easier to compare with reference Numb
er........!
{
rx_no[0]='0';
rx_no[2]=getchar();rx_no[1]=getchar();rx_no[4]=getchar();rx_no[3]=getchar();
rx_no[6]=getchar();rx_no[5]=getchar();rx_no[8]=getchar();rx_no[7]=getchar();
rx_no[10]=getchar();rx_no[9]=getchar();getchar();rx_no[11]=getchar();
}
void cek_number1()
{
unsigned char hasil;
for(i=0;i<12;i++) //cek no 1
{
hasil=rx_no[i]-no1[i];
if(i==11 && hasil==0)
{
flag_no=1;
no_hp=1;
return flag_no;
}
if(hasil!=0)
{ break;
}
}
}
void report1()
{
printf("at"); putchar(13);
while (getchar()!='O');
if (getchar()=='K')
PORTB.2=1;
printf("at+cmgs=28");putchar(13);
while (getchar()!=62);
if (getchar()==32)
{
printf("07912618010000F001000D91265862903136F100000F");
printf("E8F01C242E97DDA037BCEC2E9301");// "has been opened"
putchar(26);
PORTB.2=0;
}
void report2()
{
printf("at");putchar(13);
while (getchar()!='O');
if (getchar()=='K')
PORTB.2=1;
printf("at+cmgs=24");putchar(13);
while (getchar()!=62);
if (getchar()==32)
{
printf("07912618010000F001000D91265862903136F100000F");
printf("E8F01C242E97DDA031FB3D2F9301");//"has been closed"
putchar(26);
PORTB.2=0;}
}
void compare_sms()
{
if(flag_no==1) //command bellow will be accesed if SMS from number .............
...
{
xpsn=0;
for(i=0;i<=11;i++) //bukA
{
hasil=rx_psn[i]-psn1[i];
if(i==11&& hasil==0)
{
xpsn=1;tutup=1; buka=0;report1();
}
if(hasil!=0)
break;
}
hasil=0;
if(!xpsn)
{
for(i=0;i<=11;i++) //tutp
{
hasil=rx_psn[i]-psn2[i];
if(i==11&& hasil==0)
{
xpsn=1;buka=1;tutup=0;report2();
}if(hasil!=0)
break;
}hasil=0;
}
}}
void cek_new_SMS()
{
unsigned char dat;
PORTB.0=1;
ready_gsm();//.....................................................be sure that
GSM is ready
printf("at+cmgl=0");enter;dat=getchar();//............AT+CMGL=0 to read UNREAD S
MS (New SMS)
//.....................................+ as header New SMS and E indicate ERROR
/ No New SMS
while(dat!='+'&& dat!='O')
{
printf("at+cmgl=0");enter;dat=getchar();};
if(dat=='+') //............................... !=R (ERROR) indicate recieve New
SMS
{
for(i=0;i<=25;i++) getchar();//43
scan_number();
for(i=0;i<=19;i++) getchar();
for(i=0;i<12;i++) rx_psn[i]=getchar();
//...............................masukkan psn
cek_number1();//....................................................Cek no USER
cek=cek_number1();
if cek==1
delay_ms(1000);
compare_sms(); //....................................compare SMS with reference
data
delay_ms(1000);
for(i=0;i<10;i++){ rx_no[i]=0;}//................................CLEAR NO in SRA
M uC
for(i=0;i<=5;i++){ rx_psn[i]=0;}//..............................CLEARm PSN in SR
AM uC
}
else
{}
delete();//..........................................DELETE SMS in HP
delay_ms(2000);
PORTB.0=0;
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=O
ut
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=1
PORTB=0x01;
DDRB=0xFF;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 19200
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x0C;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
ready_gsm();
set_noecho();//.............................................Turn off GSM ECHO */
//delay_ms(1000);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
cek_new_SMS();
delay_ms(1000);
};
}

You might also like