📄 main.c
字号:
//#include <avr/iom32.h>
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
// Standard Input/Output functions
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
//#include "sms_comm.h"
#define CPU_freq 6000 KHz
#define FT232RL_RST PB2
#define BUTTON_DEAD_TIME 50
int s,button_filter;
int butflag=0;
// Declare your global variables here
char sms_read[]={'A','T','+','C','M','G','R','='};
char status[9]={'G','E','T','S','T','A','T','U','S'};
char getrelay1[9]={'G','E','T','R','E','L','A','Y','1'};
char setrelay1[9]={'S','E','T','R','E','L','A','Y','1'};
char getrelay2[9]={'G','E','T','R','E','L','A','Y','2'};
char setrelay2[9]={'S','E','T','R','E','L','A','Y','2'};
char gettemp[7]={'G','E','T','T','E','M','P'};
char nocarrier[10]={'N','O',' ','C','A','R','R','I','E','R'};
char callready[10]={'C','a','l ','l',' ','R','e','a','d','y'};
//char dial[14]={'A','T','D','0','8','7','6','0','4','1','1','0','1',';'};
unsigned long a;
unsigned char i=0;
unsigned char j,k;
unsigned char proba;
unsigned char temp_array [64];
double tempcalc,tempcalc2;
int index1;
int colcount=0;
unsigned const RING [6] = {'R', 'I', 'N', 'G', '\r'};
char sms_rec[12]={'C','M','T','I',':',' ','"','S','M','"',','};
char sms_send[10]={'A','T','+','C','M','G','S','=','"','+'};
//char sms_send2[]={'A','T','+','C','M','G','S','=','"','0','8','7','6','0','4','1','1','0','0','"'};
char sms_send_number[30];
char message[160];
char smsind[5];
char sms_number[15];
char sms_read_comm[9];
int l=0;
int flag=0;
int com_out=0;
int ring_state=0;
int t=0;
int stopbuzzer=0;
char ok[2]={'O','K'};
#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 256
char rx_buffer[RX_BUFFER_SIZE];
int rx_wr_index,rx_rd_index;
volatile int rx_counter;//unsigned char
// This flag is set on USART Receiver buffer overflow
unsigned char rx_buffer_overflow;
unsigned char conf_pointer;
unsigned char Ta_pointer;
unsigned char TCN_Conf;
unsigned int TCN_Data;
unsigned char TCN_raddr,TCN_waddr;
int er;
//////////////////////////////////////////////////////////////////////////////
void Init()
{// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=In Func5=Out Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=T State5=0 State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B 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
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=Out Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=0 State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xC0;
// Port D initialization
// Func7=Out Func6=In Func5=In Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x92;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 6000,000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Toggle
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x10;//enable Timer/Counter 1
TCCR1B=0x01;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
//proba
OCR1BH=0x07;
OCR1BL=0x2A;
//OCR1BH=0x05;
//OCR1BL=0x52;
// External Interrupt(s) initialization
// INT0: Off
// INT1:
// INT1 Mode: Falling Edge
// INT2: Off
GICR=0x00;//disable external interrupts
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x80;
/*
GICR|=0x80;
MCUCR=0x08;
MCUCSR=0x00;
GIFR=0x80;*/
// Timer(s)/Counter(s) Interrupt(s) initialization
//TIMSK=0x02;
//--------interrupt--------
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 115.2 kbps
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00; //0x00;5F
UBRRL=0x03; //2F->>9600 at 7.3278 ;0x26;-->baudrate 9600 at clock=6 MHz
// Timer(s)/Counter(s) Interrupt(s) initialization
//TIMSK=0x00;//timer interrupt disable
TIMSK=0x08;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
PORTB &= ~(1<<PB2);//hold FT232RL in reset
DDRB |= 0x04;
PORTA &= 0b01011111;//disable SLEEP mode with RTS=0; DTR =0
DDRA |= 0b10100000;
// Global enable interrupts
asm volatile("sei");
//#asm("sei")
}
////////////////////////////////////////////////////////////////
static void putchr(char c)
{
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
}
///////////////////////////////////////////////////////////////
void Send_UART(unsigned char * byte)
{
for (;*byte;++byte) putchr (*byte);
putchr(13);
}
///////////////////////////////////////////////////
void Send_UART2(unsigned char * byte)
{
for (;*byte;++byte) putchr (*byte);
putchr(26);
}
////////////////////////////////////////////////////////////////////////
char getchar_UART(unsigned long timeout)
{
char data;
//wait given timeout
while ((rx_counter==0) && (timeout >0))
timeout--;
if(timeout==0)// (rx_counter==0)
{
com_out=1;
return;
}
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
asm volatile("cli");
--rx_counter;
asm volatile("sei");
return data;
}
/////////////////////////////////////////////////////////////////////
// USART Receiver interrupt service routine
ISR(USART_RXC_vect)
//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;
};
};
}
///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// Timer 1 output compare interrupt service routine
ISR(TIMER1_COMPB_vect)
{
t++;
TCNT1=0; //clear TCNT1 value(to imitate CTC mode on Timer 1 B)
//1.check whether to toggle or not(buzzer_enable);
if (stopbuzzer || (t==4000))
TCCR1A=0x0;//disconnect Timer/Counter 1 A/B Output pin
}
//////////////////////////////////////////////////////////////////////////////
unsigned char Compare (void)
{
unsigned char a, b;// temp;
b=0;
for (a=0; a<5; a++)
{
// temp = getchar_UART();
if (temp_array[b++] != RING[a]) return 0;
}
return 1;
}
///////////////////////////////////////////////////////////////////
unsigned char Compare2 (void)
{
unsigned char a, b;
b=0;
for (a=0; a!=11; a++)
{
if (temp_array[b++] != sms_rec[a]) return 0;
}
return 1;
}
////////////////////////////////////////////////////////////////////
unsigned char Comparereq (char* request)
{
unsigned char a, b;// temp;
b=0;
for (a=0; a!=9; a++)
{
// temp = getchar_UART();
if (temp_array[b++] != request[a]) return 0;
}
return 1;
}
//////////////////////////////////////////////////////////////////////
void gsmconfig()
{
Send_UART("ATE0");//disable ECHO characters
a=65535;
while(--a!=0);
if(!waitOK())
return 0;
com_out=0;
Send_UART("AT+CMGF=1");//set text SMS format
a=65535;
while(--a!=0);
if(!waitOK())
return 0;
com_out=0;
}
////////////////////////////////////////////////////////////////////
int waitOK()
{
//Feature:can exit by timeout
int l=0;
int flag=0;
char ok[2]={'O','K'};
while ((getchar_UART(100000)!=10)&& (!com_out))
{
l=l;
}
if(com_out)
return 0; //here exits by timeout
while(ok[l++]==getchar_UART(100000))
{
flag=flag+1;
}
while ((getchar_UART(100000)!=10)&& (!com_out) )
{
l=l;
}
return flag;
}
///////////////////////////////////////////////////////////
int waitplus()
{
com_out=0;
int l=0;
int flag=0;
while ( (getchar_UART(100000)!=10) && (!com_out))
{
l=l;
}
if(!com_out)
proba=getchar_UART(100000);
else return 0; //here exits by timeout
if(proba==43)
{
flag=flag+1;
}
return flag;
}
/////////////////////////////////////////////////////////////
void waitcarrier(void)
{
int l=0;
int flag=0;
com_out=0;
while ((getchar_UART(100000)!=10) && (!com_out))
{
l=l;
}
while(nocarrier[l++]==getchar_UART(100000)&& (!com_out))
{
flag=flag+1;
}
while ((getchar_UART(100000)!=10)&& (!com_out) )
{
l=l;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
int call_ready(void)
{
//Feature: can exit by timeout
int l=0;
int flag=0;
int match=0;
//wait for a new line
while ((getchar_UART(100000)!=10) && (!com_out))
{
l=l;
}
while(!match)
{
if(rx_wr_index)
{
proba=getchar_UART(100000);
}
else return; //here exits by timeout
if (callready[l]==proba) //first symbol
{
l=l+1;
if (callready[l]==getchar_UART(100000)) //second symbol
{
match=match+1;
while(callready[++l]==getchar_UART(100000))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -