#include <mega32.h>
#include <stdio.h>
#include <delay.h>
#include <lcd.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B
#endasm
//************
unsigned int e,x,y;
int meysam[2];
//************
unsigned char sp[10];
unsigned char rep1,rep2,d,m,b,b1,c,z;
unsigned char sensor;
unsigned char result[200],str[20],load,str3[20],str4[20];
//--------------------------- mobile
eeprom unsigned char mobile_num1[12];
eeprom unsigned char mobile_num2[12];
eeprom unsigned char mobile_num3[12];
eeprom unsigned char mobile_num4[12];
unsigned char string_mobile_1[12],string_mobile_2[12],string_mobile_3[12],string_mobile_4[12];
unsigned char rsp[200],rsp5[200],rsp2[10];
//------------- keypad ---------
unsigned char clumn,ROW,KEY,L;
unsigned char K[16]={10,11,12,13,1,2,3,4,5,6,7,8,9,0,14,15};
// 10='cancel' 11='ok' 12='up' 13='down' 14='menu' 15='F1'
//------------------password
eeprom unsigned char pass[5]={1,2,3,4,5};
eeprom unsigned long int sms_pass=12345;
unsigned long int sms_pass_temp_1,sms_pass_temp_2;
unsigned char temp[20],temp1[20];
unsigned int a;
unsigned int j,i,s,w,v;
unsigned char flag_pakage;
//-------------- temperature varb ------------
float temp_t;
unsigned char temperature_flag,prosses_flag,calc_flag,shab1_flag;
eeprom float t[2];
float DC,temperature,T1,T2;
char final_temperature[4];
unsigned char str1[10],str2[10];
//------------------- time sending sms
unsigned char time_temp;
eeprom unsigned char time;
unsigned long int shab,cnt_0,cnt_1;
//------------- flags
eeprom unsigned char mode_flag=3;
unsigned char sms_flag_0,sms_flag_1;
unsigned char if_flag,ex_finish;
//-------------------- functions -------------
unsigned char Echo_off(unsigned char show);
void Test(void);
void manage_panel(unsigned char L);
unsigned char getkey (void);
void calc_temp(void);
unsigned char check_send(unsigned char show);
unsigned char check_modem(unsigned char show);
//--------------------------------------------
interrupt [EXT_INT0] void ext_int0_isr(void)
{
PORTC=0x0F;
DDRC=0xF0;
if(PINC.0==0) {ROW=0; goto soton;}
else if(PINC.1==0) {ROW=4;goto soton;}
else if(PINC.2==0) {ROW=8;goto soton;}
else if(PINC.3==0) {ROW=12;goto soton;}
soton:
PORTC=0xF0;
DDRC=0x0F;
delay_ms(150);
if(PINC.4==0) {clumn=0; goto out;}
else if(PINC.5==0) {clumn=1;goto out;}
else if(PINC.6==0) {clumn=2;goto out;}
else if(PINC.7==0) {clumn=3;goto out;}
out:
KEY=ROW+clumn;
L=K[KEY];
PORTC=0x0F;
DDRC=0xF0;
manage_panel(L);
}
// sensor #1
interrupt [EXT_INT1] void ext_int1_isr(void)
{
switch (temperature_flag)
{
case 0:
ex_finish=0;
TCCR0=0x02;
TCCR2=0x02;
TCNT0=TCNT2=w=v=0;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Falling Edge
// INT2: Off
GICR|=0xC0;
MCUCR=0x08;
MCUCSR=0x00;
GIFR=0xC0;
temperature_flag=1;
break;
case 1:
TCCR2=0x00; //DC
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Rising Edge
// INT2: Off
GICR|=0xC0;
MCUCR=0x0C;
MCUCSR=0x00;
GIFR=0xC0;
temperature_flag=2;
break;
case 2:
TCCR0=0x00; // T
calc_temp();
temperature_flag=0;
break;
}
}
// sensor #0
interrupt [EXT_INT2] void ext_int2_isr(void)
{
switch (temperature_flag)
{
case 0:
ex_finish=0;
TCCR0=0x02;
TCCR2=0x02;
TCNT0=TCNT2=w=v=0;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: On
// INT2 Mode: Falling Edge
GICR|=0x60;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x60;
temperature_flag=1;
break;
case 1:
TCCR2=0x00; //DC
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: On
// INT2 Mode: Rising Edge
GICR|=0x60;
MCUCR=0x00;
MCUCSR=0x40;
GIFR=0x60;
temperature_flag=2;
break;
case 2:
TCCR0=0x00; // T
calc_temp();
temperature_flag=0;
break;
}
}
//---------------------------------
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
v++;
}
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
w++;
}
void InitMicro(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0x0F;
DDRC=0xF0;
PORTD=0x00;
DDRD=0xC0; //PORTD.6 ---->output
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Rising Edge
// INT2: On
// INT2 Mode: Rising Edge
GICR|=0x00;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x00;
TIMSK=0x41;
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x05;
ACSR=0x80;
SFIOR=0x00;
lcd_init(20);
//------------------------------ in this part we change mobile number to string for sending sms aim eeprom var. can not string
sprintf(string_mobile_1,"%d%d%d%d%d%d%d%d%d%d%d",mobile_num1[0],mobile_num1[1],mobile_num1[2],mobile_num1[3],mobile_num1[4],mobile_num1[5],mobile_num1[6],mobile_num1[7],mobile_num1[8],mobile_num1[9],mobile_num1[10]);
sprintf(string_mobile_2,"%d%d%d%d%d%d%d%d%d%d%d",mobile_num2[0],mobile_num2[1],mobile_num2[2],mobile_num2[3],mobile_num2[4],mobile_num2[5],mobile_num2[6],mobile_num2[7],mobile_num2[8],mobile_num2[9],mobile_num2[10]);
sprintf(string_mobile_3,"%d%d%d%d%d%d%d%d%d%d%d",mobile_num3[0],mobile_num3[1],mobile_num3[2],mobile_num3[3],mobile_num3[4],mobile_num3[5],mobile_num3[6],mobile_num3[7],mobile_num3[8],mobile_num3[9],mobile_num3[10]);
sprintf(string_mobile_4,"%d%d%d%d%d%d%d%d%d%d%d",mobile_num4[0],mobile_num4[1],mobile_num4[2],mobile_num4[3],mobile_num4[4],mobile_num4[5],mobile_num4[6],mobile_num4[7],mobile_num4[8],mobile_num4[9],mobile_num4[10]);
//---------------------------------------------------------------------------
}
void main (void)
{
InitMicro();
delay_us(200);
//-------------------------------------------------------------
//************ check modem
delay_ms(1000);
lcd_clear();
Test();
rep1=check_modem(0);
lcd_gotoxy(0,3);
if (rep1==1)
{
lcd_putsf("GSM MODEM IS OK");
}
else {lcd_putsf("PLEASE CALL BFG Co.");}
delay_ms(2500);
lcd_clear();
sensor = 0;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: On
// INT2 Mode: Rising Edge
GICR|=0x60;
MCUCR=0x00;
MCUCSR=0x40;
GIFR=0x60;
#asm("sei")
//delay_ms(3000);
while (1)
{
// ------- this part is waiting for coming an sms
//when we receive sms, get \r\n+CMTI: "MT",21\r\n
//for find out that we receive a complete sms package we should receiv tow \n that in decimal is equal 10
j=flag_pakage=0;
do
{
rsp[j]=getchar();
if (rsp[j]=='\n' && rsp[j-1]=='\r') {flag_pakage++;}
j++;
}while (flag_pakage < 2);
//------- just show what we receive from GSM
/*for (i=0;i<j;i++)
{
sprintf(result,"%c",rsp[i]);
lcd_puts(result);
delay_ms(200);
}*/
#asm ("cli")
lcd_gotoxy(0,3);
sprintf (result,"NEW MESSAGE:%c%c ",rsp[14],rsp[15]); // number of sms on memory of GSM
lcd_puts(result);
delay_ms(500);
printf("AT+CMGR=%c%c%c",rsp[14],rsp[15],0x0D); // read sms from memory
j=flag_pakage=0;
do
{
rsp[j]=getchar();
if (rsp[j]=='\n' && rsp[j-1]=='\r') {flag_pakage++;}
j++;
}while (flag_pakage < 5);
//---------- just show what we receive from GSM
/*for (i=0;i<j;i++)
{
lcd_clear();
sprintf(result,"%c",rsp[i]);
lcd_puts(result);
delay_ms(300);
}*/
//---- mobile number of sms sender
lcd_gotoxy(0,3);
lcd_putsf(" ");
lcd_gotoxy(4,3);
for (i=23;i<36;i++)
{
sprintf(result,"%c",rsp[i]);
lcd_puts(result);
}
//---------------------------------
delay_ms(1500);
lcd_gotoxy(0,3);
lcd_putsf(" ");
lcd_gotoxy(0,3);
//---- text of sms , I set 5 digit for password of system this part show first 5 digit of sms
//-----------------------------------------------
// in this part I check sms password
ltoa(sms_pass,str4);
sprintf(str3,"%c%c%c%c%c",rsp[63],rsp[64],rsp[65],rsp[66],rsp[67]);
// just show sms
//lcd_clear();
//lcd_puts(str3);
rep2=strcmp(str3,str4);
if (rep2==0) // correct password
{
lcd_gotoxy(0,3);
lcd_putsf(" CORRECT PASSWORD ");
printf("AT+CMGS=%c%c%c%c%c%c%c%c%c%c%c%c%c%c",rsp[23],rsp[24],rsp[25],rsp[26],rsp[27],rsp[28],rsp[29],rsp[30],rsp[31],rsp[32],rsp[33],rsp[34],rsp[35],0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("TEMP=%d%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf("REPLAY SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
}
else // wrong password
{
lcd_gotoxy(0,3);
lcd_putsf(" WRONG PASSWORD ");
printf("AT+CMGS=%c%c%c%c%c%c%c%c%c%c%c%c%c%c",rsp[23],rsp[24],rsp[25],rsp[26],rsp[27],rsp[28],rsp[29],rsp[30],rsp[31],rsp[32],rsp[33],rsp[34],rsp[35],0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("Your Password is Wrong.%c----------%cBFG Co. %cwww.bfgq.ir %c",0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" REPLAY SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
}
delay_ms(2000);
printf("AT+CMGD=32%c",0x0D);
do
{
rsp2[j]=getchar();
j++;
}while (j < 6);
// just show ok
/*lcd_clear();
for (i=0;i<j;i++)
{
sprintf(result,"%c",rsp2[i]);
lcd_puts(result);
delay_ms(200);
} */
lcd_gotoxy(0,3);
lcd_putsf(" ");
#asm("sei")
}
}
//-------------- Echo off -------------------------------------------
// Echo ra khamosh mikonad in ommand
unsigned char Echo_off(unsigned char show)
{
unsigned char rsp[10];
unsigned char result[10];
printf("ATE0%c",0x0D);
rsp[0]=getchar(); //\r\nok\r\n
rsp[1]=getchar();
rsp[2]=getchar();
rsp[3]=getchar();
rsp[4]=getchar();
rsp[5]=getchar();
if(show==1)
{
sprintf(result,"%c%c",rsp[2],rsp[3]);
lcd_puts(result);
}
if(rsp[2]=='O' && rsp[3]=='K') {return 1;} // yani function dorost anjam shode va 1 ra barmigardanad
else {return 0;}
}
//------- calculation of temperature
void calc_temp(void)
{
#asm ("cli")
//T1=T1+((w*256)+TCNT2)*0.0904;
//T2=T2+((v*256)+TCNT0)*0.0904;
T1=T1+((w*256)+TCNT2)*0.7233;
T2=T2+((v*256)+TCNT0)*0.7233;
#asm ("sei")
e++;
if (e==300)
{
#asm ("cli")
//************************************
//************************************
e=0;
T1/=300;
T2/=300;
DC=(T1/T2);
temperature=(DC-0.32)/0.0047;
//ftoa(temperature,1,str2);
//ftoa(DC,2,str1);
if (sensor == 0) {meysam[0]=floor(temperature);}
else if (sensor == 1) {meysam[1]=floor(temperature);}
lcd_gotoxy(0,0);
lcd_putsf(" TEMPERATURE ");
lcd_gotoxy(0,1);
sprintf(result," S1= %d S2= %d ",meysam[0],meysam[1]);
lcd_puts(result);
lcd_gotoxy(0,2);
lcd_putsf("--------------------");
if (abs (meysam [0]) > (abs (t[0]) + 3 ))
{
lcd_gotoxy(0,3);
lcd_putsf(" ");
lcd_gotoxy(0,3);
lcd_putsf("WARNING SENSOR 1 ");
}
else
{
lcd_gotoxy(0,3);
lcd_putsf(" ");
lcd_gotoxy(0,3);
lcd_putsf("NORMAL TEMPERATURE");
//sms_flag_0=0;
}
if (abs (meysam [1]) > (abs (t[1]) + 3 ))
{
lcd_gotoxy(0,3);
lcd_putsf(" ");
lcd_gotoxy(0,3);
lcd_putsf("WARNING SENSOR 2 ");
}
else
{
lcd_gotoxy(0,3);
lcd_putsf(" ");
lcd_gotoxy(0,3);
lcd_putsf("NORMAL TEMPERATURE");
//sms_flag_1=0;
}
T1=T2=0;
if (mode_flag == 1)
{
if (abs (meysam[0]) > (abs (t[0]) + 3 ))
{
if (sms_flag_0 < 2)
{
printf("AT+CMGS=%s%c",string_mobile_1,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
//-----------------
printf("AT+CMGS=%s%c",string_mobile_2,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
//----------------
printf("AT+CMGS=%s%c",string_mobile_3,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
//-------------------
printf("AT+CMGS=%s%c",string_mobile_4,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
sms_flag_0 ++ ;
}
else
{
cnt_0++;
if (cnt_0==3600)
{
cnt_0=0;
sms_flag_0=0;
}
}
//------------
if (abs (meysam[1]) > (abs (t[1]) + 3 ))
{
if (sms_flag_1 < 2)
{
printf("AT+CMGS=%s%c",string_mobile_1,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
//-----------------
printf("AT+CMGS=%s%c",string_mobile_2,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
//----------------
printf("AT+CMGS=%s%c",string_mobile_3,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
//-------------------
printf("AT+CMGS=%s%c",string_mobile_4,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%csms number=1%c----------%cBFG Co. %cwww.bfgq.ir%c",meysam,0x0D,0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" TIMING SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
sms_flag_1 ++ ;
}
else
{
cnt_1++;
if (cnt_1==3600)
{
cnt_1=0;
sms_flag_1=0;
}
}
}
}
}
else if (mode_flag==2)
{
shab ++;
if (shab == (500)*time)
{
printf("AT+CMGS=%s%c",string_mobile_1,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%cS2=%d%c----------%cBFG Co. %cwww.bfgq.ir %c",meysam[0],0x0D,meysam[1],0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" REPLAY SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
//-----------------------
printf("AT+CMGS=%s%c",string_mobile_2,0x0D);
for (j=0;j<3;j++) {rsp5[j]=getchar();}
printf("S1=%d%cS2=%d%c----------%cBFG Co. %cwww.bfgq.ir %c",meysam[0],0x0D,meysam[1],0x0D,0x0D,0x0D,26);
rep1=check_send(0);
if (rep1==1)
{
lcd_gotoxy(0,3);
lcd_putsf(" REPLAY SMS SENT ");
}
else if (rep1==0)
{
lcd_gotoxy(0,3);
lcd_putsf(" ERROR ");
}
delay_ms(1000);
shab=0;
}
}
}
#asm ("sei")
}
//--------------------------------------------------------------------------
PORTC=0x0F;
DDRC=0xF0;
if(PINC.0==0) {
ROW=0;
goto soton;
}
else if(PINC.1==0) {
ROW=4;
goto soton;
}
else if(PINC.2==0) {
ROW=8;
goto soton;
}
else if(PINC.3==0) {
ROW=12;
goto soton;
}
soton:
PORTC=0xF0;
DDRC=0x0F;
PORTC=0x0F;
DDRC=0xF0;
if(PINC.0==0) {
ROW=0;
}
else if(PINC.1==0) {
ROW=4;
}
else if(PINC.2==0) {
ROW=8;
}
else if(PINC.3==0) {
ROW=12;
}
PORTC=0xF0;
DDRC=0x0F;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?