#include <iom128v.h>
#include<macros.h>
//#define com1
//#define fosc 16000000 //晶振16MHZ
//#define baud 9600 //波特率
#pragma interrupt_handler uart1_rx_isr:iv_USART1_RXC
#pragma interrupt_handler uart1_udre_isr:iv_USART1_UDRE
#define uchar unsigned char
#define uint unsigned int
#define RXC_BUFF_SIZE 16
#define TXC_BUFF_SIZE 64
unsigned char RXC_BUFF[RXC_BUFF_SIZE];
unsigned char TXC_BUFF[TXC_BUFF_SIZE];
unsigned char RXC_RD;//接受缓冲区读指针
unsigned char RXC_WR;//接受缓冲区写指针
unsigned char TXC_RD;//发送缓冲区读指针
unsigned char TXC_WR;//发送缓冲区写指针
uchar at[]={"AT"};
uchar ati[]={"ATI"};
uchar sicscontype[]={"at^sics=0,contype,gprs0"};
uchar sicsapn[]={"at^sics=0,apn,cmnet"};
uchar sisssrvtype[]={"at^siss=1,srvtype,socket"};
uchar sissconid[]={"at^siss=1,conid,0"};
uchar sissaddress[]={"at^siss=1,address,socktcp://***.**.***.***:****"}; //IP地址及端口号
uchar siso[]={"at^siso=1"};
uchar sisc[]={"at^sisc=1"};
uchar sisw[]={"at^sisw=1,21"};
uchar write[]={"Hello,This is a test."};
uchar read[]={"at^sisr=1,16"};
//延时focs
void s_ms(uint ms)
{
for(;ms>1;ms--)
{ }
}
//延时ms
void ms(uint ms)
{uint i;
while(ms--)
{
for(i=0;i<2666;i++)
{ }
}
}
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UBRR1L=0x67; //osc=16M ,U2X=0,bit/s=9600
UBRR1H=0x00;
UCSR1B=0x98; //RX Enable,TX Enable,RX interrupt Enable
UCSR1C=0x06; //8bit data ,1bit stop
//UCSR1C =(1<<UCSZ01)|(1<<UCSZ00);//8bit+1bit stop
//UBRR1L=(fosc/16/(baud+1))%256;
//UBRR1H=(fosc/16/(baud+1))/256;
//UCSR1B =(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);//RXCEN TXCEN RXCIE
}
void putchar(unsigned char c)
{
TXC_BUFF[TXC_WR]=c;
if(TXC_WR<(TXC_BUFF_SIZE-1))
TXC_WR++;
else
TXC_WR=0;
UCSR1B|=(1<<UDRIE1);
}
unsigned char getchar(void)
{
unsigned temp;
while(RXC_RD==RXC_WR)
;
temp=RXC_BUFF[RXC_RD];
if(RXC_RD<(RXC_BUFF_SIZE-1))
RXC_RD++;
else
RXC_RD=0;
return temp;
}
void puts(char *s)
{
while (*s)
{
putchar(*s);
s++;
}
putchar(0x0D);
putchar(0x0A);
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
// XDIV = 0x00; //xtal divider
// XMCRA = 0x00; //external memory
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
//all peripherals are now initialised
}
void main(void)
{
unsigned char j=0;
TXC_RD=0;
TXC_WR=0;
RXC_RD=0;
RXC_WR=0;
init_devices();
uart1_init();
SEI();
ms(3);
puts(at);
ms(1000);
puts(ati);
ms(2000);
puts(sicscontype);
ms(2000);
puts(sicsapn);
ms(2000);
puts(sisssrvtype);
ms(2000);
puts(sissconid);
ms(2000);
puts(sissaddress);
ms(5000);
puts(siso);
ms(5000);
puts(sisw);
ms(5000);
puts(write);
ms(1000);
}
//中断例程
//USART1_RXC中断
void uart1_rx_isr(void)
{
RXC_BUFF[RXC_WR]=UDR1;
if(RXC_WR<(RXC_BUFF_SIZE-1))
RXC_WR++;
else
RXC_WR=0; //uart has received a character in UDR
}
//USART1_UDRE中断
void uart1_udre_isr(void)
{
UDR1=TXC_BUFF[TXC_RD];//character transferred to shift register so UDR is now empty
if(TXC_RD<(TXC_BUFF_SIZE-1))
TXC_RD++;
else
TXC_RD=0;
if(TXC_RD==TXC_WR)
UCSR1B&=~(1<<UDRIE1);
}
|