سلام برنامه زیر رو به کمک کدویزارد کدویژن برای کنترل رله توسط پیامک نوشتم. میکروی استفاده شده atmega16 هستش. portb.0 به رله متصل هست و با ارسال on# فعال و off# غیر فعال میشه. portb.1 رو هم گذاشتم برای وضعیت که به صورت پیشفرض روشنه و در صورت برقرار شدن ارتباط میکرو با sim800 خاموش میشه. از کریستال خارجی 16 مگاهرتز برای اسیلاتور استفاده شده. میکرو بعد روشن شدن پیامک sim 800 was turned on به شماره ای که از قبل تعیین شده میفرسته.
اطلاعات بیشتر رو انشاالله در پست های بعدی میگم.
به این نکته توجه کنید که بعد روشن شدن مدار باید یه 30 ثانیه ای صبر کنید،بعد پیام بفرستید.این عمل فقط برای اولین بار بعد روشن شدن ماژول باید رعایت بشه و بعد اون محدودیتی برای ارسال پیامک وجود نداره.
آپدیت: 96/08/13
اینم لینک برنامه کدویژن(آپدیت شده)
اینم فیلم عملکرد مدار
اطلاعات بیشتر رو انشاالله در پست های بعدی میگم.
به این نکته توجه کنید که بعد روشن شدن مدار باید یه 30 ثانیه ای صبر کنید،بعد پیام بفرستید.این عمل فقط برای اولین بار بعد روشن شدن ماژول باید رعایت بشه و بعد اون محدودیتی برای ارسال پیامک وجود نداره.
آپدیت: 96/08/13
کد:
#include <mega32a.h>
#include <delay.h>
#include <string.h>
char c=0;
char i=0;
char * pch;
char a;
char ctrlz=0x1A;
char at[]={"AT\r"};
char ATE0[]={"ATE0\r"};
char text_mode[]={"AT+CMGF=1\r"};
char cpbs[]={"AT+CPBS=\"ME\"\r"};
char csmp[]={"AT+CSMP=17,167,0,0\r"};
char cpms[]={"AT+CPMS=\"ME\",\"ME\",\"ME\"\r"};
char cnmi[]={"AT+CNMI=2,1,0,0,0\r"};
char gsm[]={"AT+CSCS=\"GSM\"\r"};
char save_on_sim[]={"AT+CSAS=0\r"};
char ATW[]={"AT&W\r"};
char send_sms[]={"AT+CMGS=\"+98911213----\"\r"};
char sms_ok[]={"sim800 was turned on"};
char cmgda[]= {"AT+CMGDA=\"DEL ALL\"\r"};
char cmgr[]={"AT+CMGR="};
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
// USART Receiver buffer
#define RX_BUFFER_SIZE 128
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE <= 256
unsigned char rx_wr_index=0,rx_rd_index=0;
#else
unsigned int rx_wr_index=0,rx_rd_index=0;
#endif
#if RX_BUFFER_SIZE < 256
unsigned char rx_counter=0;
#else
unsigned int rx_counter=0;
#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_BUFFER_SIZE == 256
// special case for receiver buffer size=256
if (++rx_counter == 0) rx_buffer_overflow=1;
#else
if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
}
#endif
}
}
#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_BUFFER_SIZE != 256
if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#endif
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// USART Transmitter buffer
#define TX_BUFFER_SIZE 24
char tx_buffer[TX_BUFFER_SIZE];
#if TX_BUFFER_SIZE <= 256
unsigned char tx_wr_index=0,tx_rd_index=0;
#else
unsigned int tx_wr_index=0,tx_rd_index=0;
#endif
#if TX_BUFFER_SIZE < 256
unsigned char tx_counter=0;
#else
unsigned int tx_counter=0;
#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_BUFFER_SIZE != 256
if (tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
#endif
}
}
#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_BUFFER_SIZE != 256
if (tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
#endif
++tx_counter;
}
else
UDR=c;
#asm("sei")
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
// State: Bit7=P Bit6=P Bit5=P Bit4=P Bit3=P Bit2=P Bit1=P Bit0=P
PORTA=(1<<PORTA7) | (1<<PORTA6) | (1<<PORTA5) | (1<<PORTA4) | (1<<PORTA3) | (1<<PORTA2) | (1<<PORTA1) | (1<<PORTA0);
// Port B initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=Out Bit0=Out
DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (1<<DDB1) | (1<<DDB0);
// State: Bit7=P Bit6=P Bit5=P Bit4=P Bit3=P Bit2=P Bit1=0 Bit0=0
PORTB=(1<<PORTB7) | (1<<PORTB6) | (1<<PORTB5) | (1<<PORTB4) | (1<<PORTB3) | (1<<PORTB2) | (1<<PORTB1) | (0<<PORTB0);
// Port C initialization
// Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=In Bit2=Out Bit1=Out Bit0=Out
DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
// State: Bit7=0 Bit6=0 Bit5=0 Bit4=0 Bit3=P Bit2=0 Bit1=0 Bit0=0
PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (1<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
// Port D initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=Out Bit0=In
DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (1<<DDD1) | (0<<DDD0);
// State: Bit7=P Bit6=P Bit5=P Bit4=P Bit3=P Bit2=P Bit1=0 Bit0=T
PORTD=(1<<PORTD7) | (1<<PORTD6) | (1<<PORTD5) | (1<<PORTD4) | (1<<PORTD3) | (1<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) | (0<<MPCM);
UCSRB=(1<<RXCIE) | (1<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
UBRRL=0x67;
//analogue comparator power off to reduce power consumption
ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
SFIOR=(0<<ACME);
//wait for initiallizing sim800
delay_ms(10000);
UCSRB=(1<<RXCIE) | (1<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
//checking serial connection
check:
for ( i=0 ; i<=2 ; i++)
{
putchar(at[i]);
}
delay_ms(1000);
pch=strstr(rx_buffer,"OK");
if(pch==NULL) goto check;
PORTB.1=0;
delay_ms(1000);
//echo off
for ( i=0 ; i<=4 ; i++)
{
putchar(ATE0[i]);
}
delay_ms(1000);
#asm("cli")
// delete receive buffer
for(i=0;i<=128;i++) {
rx_buffer[i]=0;
}
#asm("sei")
// Global enable interrupts
#asm("sei")
pch=NULL;
rx_wr_index=0;
// text mode sms
for ( i=0 ; i<=9 ; i++)
{
putchar(text_mode[i]);
}
//checking ok
while(pch==NULL) {
pch=strstr(rx_buffer,"OK");
}
//"ME"
for ( i=0 ; i<=12 ; i++)
{
putchar(cpbs[i]);
}
delay_ms(500);
//17,167,0,0
for ( i=0 ; i<=18 ; i++)
{
putchar(csmp[i]);
}
delay_ms(500);
//"ME","ME","ME"
for ( i=0 ; i<=22 ; i++)
{
putchar(cpms[i]);
}
delay_ms(500);
//2,1,0,0,0
for ( i=0 ; i<=17 ; i++)
{
putchar(cnmi[i]);
}
delay_ms(500);
//gsm
for ( i=0 ; i<=13 ; i++)
{
putchar(gsm[i]);
}
delay_ms(500);
//save_on_sim
for ( i=0 ; i<=9 ; i++)
{
putchar(save_on_sim[i]);
}
delay_ms(1000);
//save all setting
for ( i=0 ; i<=4 ; i++)
{
putchar(ATW[i]);
}
delay_ms(1500);
#asm("cli")
// delete receive buffer
for(i=0;i<=128;i++) {
rx_buffer[i]=0;
}
#asm("sei")
//send turning on sms
for ( i=0 ; i<=23 ; i++)
{
putchar(send_sms[i]);
}
delay_ms(1000);
for ( i=0 ; i<=19 ; i++)
{
putchar(sms_ok[i]);
}
putchar('\r');
putchar(ctrlz);
delay_ms(1000);
#asm("cli")
// delete receive buffer
for(i=0;i<=128;i++) {
rx_buffer[i]=0;
}
#asm("sei")
pch=NULL;
rx_wr_index=0;
a=0;
//delete all messege
for ( i=0 ; i<=18 ; i++)
{
putchar(cmgda[i]);
}
delay_ms(5000);
i=0;
while (1) {
rx_wr_index=0;
if(c == 1) {
if(a>=52){ //if a>='4'
c=0;
//delete all messege
for ( i=0 ; i<=18 ; i++)
{
putchar(cmgda[i]);
}
i=0;
delay_ms(10000);
}
}
pch=NULL;
rx_wr_index=0;
//waiting for +CMTI: from sim800
while(pch==NULL) {
pch=strstr(rx_buffer,"+CMTI:");
}
delay_ms(1000);
//show messege request
for ( i=0 ; i<=7 ; i++)
{
putchar(cmgr[i]);
}
putchar(pch[12]);
if(pch[13]>47 && pch[13]<58){
putchar(pch[13]);
c=1;
}
putchar('\r');
a=pch[12];
pch=NULL;
rx_wr_index=0;
delay_ms(1500);
//checking on
pch=strstr(rx_buffer,"#on");
if(pch != NULL){
PORTB.0=1;
pch=NULL;
rx_wr_index=0;
}
//checking off
pch=strstr(rx_buffer,"#off");
if(pch != NULL) {
PORTB.0=0;
pch=NULL;
rx_wr_index=0;
}
#asm("cli")
// delete receive buffer
for(i=0;i<=128;i++) {
rx_buffer[i]=0;
}
pch=NULL;
#asm("sei")
}
}
اینم فیلم عملکرد مدار













دیدگاه