程序写了,但无法实现通讯,不知道什么原因。
#include <iom128v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#define baud 9600
#define focs 8000000
uchar rdata,flag;
void delay(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
void uart0_init(void)
{
UCSR0B=0x00;
UCSR0A=0x00; //控制寄存器清零
UCSR0C=(1<<UCSZ01)|(1<<UCSZ00);
//选择UCSRC,异步模式,禁止
// 校验,1位停止位,8位数据位
UBRR0L=(focs/16/(baud+1))%256;
UBRR0H=(focs/16/(baud+1))/256; //设置波特率
UCSR0B=(1<<TXEN0)|(1<<RXEN0);
//接收、发送使能,接收中断使能
}
void putchar0(uchar c)
{
while(!(UCSR0A&(1<<UDRE0)));
UDR0=c;
}
uchar getchar0(void)
{
while(!(UCSR0A&(1<<RXC0)));
return UDR0;
}
void puts0(char *s)
{
while(*s)
{
putchar0(*s);
s++;
}
putchar0(0x0a);
}
void connect_wifi(void)
{ int flag=1;
uart0_init();
puts0("AT+CWMODE=1");
delay(200);
puts0("AT+RST");
delay(2000);
while(flag)
{
puts0("AT+CWJAP=CMCC,312312312");
delay(4000);
while(rdata>0)
{
if (rdata == 0x4F)
{
if (rdata == 0x4B)
flag = 0;
break;
}
delay(4);
}
delay(2000);
}
flag = 1;
while (flag)
{
puts0("AT+CIPSTART=TCP,192.168.1.141,80");
delay(5000);
while (rdata > 0)
{
if (rdata == 0x4F)
{
if (rdata == 0x4B)
flag = 0;
break;
}
delay(4);
}
delay(2000);
}
puts0("AT+CIPMODE=1");
delay(1000);
puts0("AT+CIPSEND");
}
void main(void)
{
uchar flag=1;
while(1)
{
if(flag)
{
connect_wifi();
flag=0;
}
}
}
#pragma interrupt_handler uart0_rx:19
void uart0_rx(void)
{
UCSR0B&=~BIT(RXCIE0);
rdata=UDR0;
flag=1;
UCSR0B|=BIT(RXCIE0);
} |