📄 protocol.c
字号:
/* ATmega128 serial.c File
Author : Hawk shang
Company: Proteam
Date of Creation : 23 Oct 2003
*/
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/signal.h>
#include <avr/eeprom.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "logic.h"
/*External Variable List*/
extern unsigned char ErrorType;
extern unsigned char smsDataCnt;
extern unsigned int _1msCounter;
extern unsigned char rs232DataCnt;
extern unsigned char UART1Index;
extern unsigned char UART0Index;
extern unsigned char uart1Data[255]; /*only save the data from slave slave*/
extern unsigned char rs232Data[255];
extern unsigned char uart1_rcvDATA[1024];
extern unsigned char Uart1SendFlag;
extern unsigned char Uart0SendFlag;
extern unsigned char ReConn_Cnt;
extern unsigned char GPRS_Status;
extern unsigned char GPRS_Working_Status;
extern unsigned int TimeOut;
extern unsigned char GPRSOKFlag;
extern unsigned char ATSENDFlag;
extern unsigned char _10msFlag;
extern unsigned char _10msCnt;
extern unsigned char Read_Pointer;
extern unsigned char Write_Pointer;
extern unsigned char Read_Pointer1;
extern unsigned char Write_Pointer1;
/*Internal Variable List*/
static char rx_buf[10]; /*receive data buffer*/
static char rx_cmd_buf[10]; /*receive command data buffer from slave*/
static unsigned char rs232_rx_buf[100];
extern void InitSerial(void); /* UART0 serial port initialise*/
extern void RS232Receive(void); /* UART0 receive data process*/
extern void puts0(unsigned char *str,unsigned char length); /* transmit data to pc serial port*/
extern void Receive(void); /* UART1 receive data process*/
void puts1(unsigned char *str,unsigned char length); /*transmit data to slave during UART1*/
/****************************************************
Name: InitSerial
Input:none
Output:none
Function: 1.set the parameter of UART0~1
2.clear the data
3.enable interrupt
3.open the UART
4. baudrate 19200,8 databit,1 stopbit,No Parity
*****************************************************/
void InitUART1(void)
{
//unsigned char i;
//open UART0 transmit and receive, and enable recevie complete interrupt
UBRR1H=00;
UBRR1L=0x33;
UCSR1C=0X86;
UCSR1B=0XD8;
UCSR1A=0X02;
PORTD = 0xff;
DDRD = 0xfb;
Uart1SendFlag=0x00;
}
/*******************************************
Name: RuntimeInitialise
Input:
none
Output: none
Fucntion:
timer init
*******************************************/
void RuntimeInitialise( void )
{
//timer/counter1
TCCR1B = 0; /* Stop T1 */
TCCR1A=0;
TCCR1C=0;
TCNT1 = 0; /* Clear T1 */
TCNT1 = _1MS_INTERRUPT; /* Set Compare A - 1ms */
TIMSK|= BIT(TOIE1); /* Compare A interrupt enabled */
TCCR1B = BIT(CS10);
}
/************************************************
Function :call this routine to initialise all peripherals
***********************************************/
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
InitUART1();
InitSerial();
RuntimeInitialise();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
//TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
PORTB=0x00;
DDRB|=0xC0;
//start GPRS
PORTB=0x80;
DDRB|=0xC0;
PORTB&=0x7F;
delayms(20);
PORTB|=0x80;
delayms(1000);
PORTB&=0x7F;
// delay and wait gprs power on
//delayms(10000);
/**/
PORTG=0xF7;
DDRG|=0xFF;
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
/*********************************************
Name: putchar puts1
Function:
send data to serial port Uart1
***********************************************/
void putchar(unsigned char c)
{
while (!(UCSR1A&(1<<UDRE0))); /*Wait for the data buffer is empty*/
UDR1=c;
//while( !( UCSR1A & (1<<TXC1)));
//UCSR1A &= ~(1<<TXC1);
}
void puts1(unsigned char *str,unsigned char length)
{
if(Uart1SendFlag==0x00) return;
//CLI();
PORTE^=0x80;
while(length!=0) /*if length is 0, stop transmit */
{
putchar(*str); /*put one byte to Uart1 */
str++;
length--;
}
Uart1SendFlag=0x00;
//SEI();
}
/*///////////////////////////
Main Routine
//////////////////////////*/
void main(void)
{
unsigned char ConnFlag;
unsigned char *contype;
unsigned char *user;
unsigned char *password;
unsigned char *apnMODE;
unsigned char *srvtype;
unsigned char *conid;
unsigned char *address;
unsigned char *con_open;
unsigned char *con_AT;
unsigned char *CHECK_CARD;
unsigned char *GET_SIMID;
unsigned char *GPRS_Csq;
unsigned char *GPRS_CMEE;
unsigned char *GPRS_Write;
contype=(unsigned char*)"at^sics=0,contype,GPRS0";
user=(unsigned char*)"at^sics=0,user,gprs";
password=(unsigned char*)"at^sics=0,passwd,gprs";
apnMODE=(unsigned char*)"at^sics=0,apn,CMNET";
srvtype=(unsigned char*)"at^siss=0,srvtype,socket";
conid=(unsigned char*)"at^siss=0,conid,0";
address=(unsigned char*)"at^siss=0,address,\"socktcp://121.xxx.xxx.xxx:15000\"";// replace the IP address with yourself IP address
con_open=(unsigned char*)"at^siso=0";
con_AT=(unsigned char*)"AT";
GPRS_CMEE=(unsigned char*)"at+cmee=2";
GPRS_Csq=(unsigned char*)"at+csq";
CHECK_CARD=(unsigned char*)"at^scks?";
GET_SIMID=(unsigned char*)"at^scid";
GPRS_Write=(unsigned char*)"at^sisw=0,20";
GPRSOKFlag=0;
ATSENDFlag=0;
_10msFlag=1;
_10msCnt=10;
ConnFlag=0;
Read_Pointer=0;
Write_Pointer=0;
GPRS_Status=STATE_DEVICE_INIT;
while(1)
{
if(_10msFlag==1)
{
_10msFlag=0;
RS232Receive();
Receive();
switch(GPRS_Status)
{
case STATE_DEVICE_INIT:
//INIT DEVICE
if(ATSENDFlag==0)
{
init_devices();
SEI();
ATSENDFlag=1;
ReConn_Cnt=20;
TimeOut=1000;
}
if(TimeOut==0)
{
GPRS_Status=STATE_GPRS_START;
ATSENDFlag=0;
GPRS_Working_Status=STATE_GPRS_WORKIDLE;
}
break;
case STATE_GPRS_START:
//sent AT
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(con_AT,2);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_CPRS_SET_CMEE:
//SEND GPRS CMEE
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(GPRS_CMEE,9);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_TEST_CSQ:
//SEND TEST CSQ
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(GPRS_Csq,6);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GRPS_CHECKCARD:
//SEND CHECK CARD
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(CHECK_CARD,8);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_CPRS_GET_SIMID:
//SEND CHECK CARD
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(GET_SIMID,7);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_SET_CONTYPE:
//SEND CONTYPE
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(contype,23);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_SET_USER:
//SEND USER
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(user,19);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_SET_PWD:
//SEND PASSWORD
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(password,21);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_SET_APN:
//SEND APN MODE
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(apnMODE,19);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_SET_SRVTYPE:
//SEDN SRVTYPE
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(srvtype,24);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_SET_SRVCONID:
//SEND CONID
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(conid,17);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_SET_ADDRESS:
//SEND ADDRESS
if(ATSENDFlag==0&&TimeOut==0)
{
Uart0SendFlag=0x01;
puts0(address,48);
Send_Return_NewLine();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_CONN_OPEN:
if(ATSENDFlag==0&&TimeOut==0)
{
GPRSConnect();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
ReConn_Cnt--;
ATSENDFlag=0;
TimeOut=1000;
}
break;
case STATE_GPRS_CONNECT:
PORTG|=0x08;
break;
case STATE_GPRS_DISCONNECT:
if(ATSENDFlag==0&&TimeOut==0)
{
GPRSDisConnect();
ATSENDFlag=0;
TimeOut=1000;
PORTG&=0xF7;
}
if(TimeOut==0)
{
GPRS_Status=STATE_GPRS_CONN_OPEN;
GPRS_Working_Status=STATE_GPRS_WORKIDLE;
}
break;
case STATE_GPRS_SHUTDOWN:
if(ATSENDFlag==0&&TimeOut==0)
{
GPRS_ShutDown();
ATSENDFlag=1;
TimeOut=1000;
}
if(TimeOut==0)
{
GPRS_Status=STATE_DEVICE_INIT;
ATSENDFlag=0;
ReConn_Cnt=20;
}
break;
}
if(ReConn_Cnt==0)GPRS_Status=STATE_GPRS_SHUTDOWN;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -