PIC16F877A串口發送,查詢方式,就為了調出這個費了我一天時間,原來是串口芯片電壓問題,現總結如下:
1、注意232串口芯片供電電壓,有5V和3.3V的
2、注意TXD、RXD接線,單片機的TXD接232芯片的R2OUT,單片機的RXD接232芯片的T2IN
3、注意ESP8266的UTXD接MCU的RX,URXD接MCU的TX
4、注意ESP8266的UTXD接232的T2IN,URXD接232的R2OUT
5、串口波特率設置成9600
6、注意連接線的可靠性,如果用線連的話一定要檢查線和插座是否穩定連接
7、系統連線如下圖所示
//以下是串口發送程序
#include《pic.h》
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B32);
void delay2ms(uint x)
{
uint a,b;
for(a=x; a》0; a--)
for(b=152; b》0; b--);
}
void UART_SendByte(unsigned char dat)
{
TXREG=dat;
while(TRMT==0);
}
void serial_init()
{
TRISC=0xbf;//設置RX為輸入,TX為輸出
TXSTA=0x24;
RCSTA=0x80;
SPBRG=0x33;//本人使用的是8MHz晶振,9600=8000000/(16*(X+1))-》X=51-》X=0x33
GIE=1;
PEIE=1;
RCIE=1;
}
void main()
{
serial_init();
while(1)
{
UART_SendByte(0x31);
UART_SendByte(0x32);
UART_SendByte(0x33);
delay2ms(500);
}
}
//以下是MCU發送AT命名給ESP8266的程序
#include《pic.h》
#define uchar unsigned char
#define uint unsigned int
#define LEDRD1
__CONFIG(0x3B32);
#define LED_OPEN() (LED=1)
#define LED_CLOSE() (LED=0)
#defineCPU_FOSC8000000
#defineBAUD9600
unsigned char* LYMS=“AT+CWMODE=3\r”;
unsigned char* SZLY=“AT+CWSAP=\”ESP8277_CYT\“\,\”1234567890\“\,11\,3\r”;
unsigned char* RST=“AT+RST\r”;
unsigned char* LCAP=“AT+CWLAP\r”;
unsigned char* SZDLJ=“AT+CIPMUX=1\r”;
unsigned char* KQFU=“AT+CIPSERVER=1\,5000\r”;
unsigned char* FSSJ=“AT+CIFSR\r”;
void delay2ms(uint x)
{
uint a,b;
for(a=x; a》0; a--)
for(b=152; b》0; b--);
}
void sys_init()
{
TRISD=0x00;//設置PORTD口為輸出
}
void usart_init()
{
TRISC=0xbf;//設置RX為輸入,TX為輸出
TXSTA=0x24;
RCSTA=0x80;
SPBRG=0x33;//本人使用的是8MHz晶振,9600=8000000/(16*(X+1))-》X=51-》X=0x33
GIE=1;
PEIE=1;
RCIE=1;
}
void usart0_putchar(unsigned char data)
{
while (!TRMT);
TXREG = data;
}
void print(unsigned char* p_string)
{
while (*p_string)
{
if (*p_string != ‘\r’)
{
usart0_putchar(*p_string);
}
else
{
usart0_putchar(‘\r’);
usart0_putchar(‘\n’);
}
p_string++;
}
}
void send_command()
{
print(LYMS);delay2ms(500);delay2ms(500);
print(SZLY);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(RST);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(LCAP);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(SZDLJ);delay2ms(500);delay2ms(500);
print(KQFU);delay2ms(500);delay2ms(500);
print(FSSJ);delay2ms(500);delay2ms(500);
}
void main()
{
sys_init();
usart_init();
send_command();
while(1)
{
PORTD = 0xff;
delay2ms(500);
PORTD = 0x00;
delay2ms(500);
}
}
//以下是MCU發送AT命名給ESP8266的程序,并接收命令、解析并作出反應
#include《pic.h》
#define uchar unsigned char
#define uint unsigned int
unsigned char RX_Buffer[32];
unsigned char RX_CNT=0;
#define LED1RD1
#define LED2RD2
#define LED3RD3
__CONFIG(0x3B32);
#define LED1_OPEN() (LED1=1)
#define LED1_CLOSE() (LED1=0)
#define LED2_OPEN() (LED2=1)
#define LED2_CLOSE() (LED2=0)
#define LED3_OPEN() (LED3=1)
#define LED3_CLOSE() (LED3=0)
#defineCPU_FOSC8000000
#defineBAUD9600
unsigned char* LYMS=“AT+CWMODE=3\r”;
unsigned char* SZLY=“AT+CWSAP=\”ESP8277_TYC\“\,\”1234567890\“\,11\,3\r”;
unsigned char* RST=“AT+RST\r”;
unsigned char* LCAP=“AT+CWLAP\r”;
unsigned char* SZDLJ=“AT+CIPMUX=1\r”;
unsigned char* KQFU=“AT+CIPSERVER=1\,5000\r”;
unsigned char* FSSJ=“AT+CIFSR\r”;
void delay2ms(uint x)
{
uint a,b;
for(a=x; a》0; a--)
for(b=152; b》0; b--);
}
void sys_init()
{
TRISD=0x00;//設置PORTD口為輸出
}
void usart_init()
{
TRISC=0x80;//設置RX為輸入,TX為輸出
TXSTA=0x24;
RCSTA=0x90;
SPBRG=0x33;//本人使用的是8MHz晶振,9600=8000000/(16*(X+1))-》X=51-》X=0x33
RCIE=0x01;
TXEN=0x01;
TXIE=0x01;
PEIE=0x01; //外部中斷使能
GIE=1;//總中斷使能
}
void usart0_putchar(unsigned char data)
{
while (!TRMT);
TXREG = data;
}
void print(unsigned char* p_string)
{
while (*p_string)
{
if (*p_string != ‘\r’)
{
usart0_putchar(*p_string);
}
else
{
usart0_putchar(‘\r’);
usart0_putchar(‘\n’);
}
p_string++;
}
}
void send_command()
{
print(LYMS);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(SZLY);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(RST);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(LCAP);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(SZDLJ);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(KQFU);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
print(FSSJ);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);delay2ms(500);
}
void interrupt usart(void)
{
if(RCIE&&RCIF)
{
//TXREG = RCREG;
RX_Buffer[RX_CNT] = RCREG;
if(RX_Buffer[0]==0x45)
RX_CNT++;
else
RX_CNT=0;
if(RX_CNT》=10)
{
if(RX_Buffer[0]==0x45&&RX_Buffer[1]==0x53&&RX_Buffer[2]==0x50)
{
if(RX_Buffer[4]==0x4C&&RX_Buffer[5]==0x45&&RX_Buffer[6]==0x44)
{
if(RX_Buffer[7]==0x31)
{
if(RX_Buffer[3]==0x4B)
LED1_OPEN();
if(RX_Buffer[3]==0x47)
LED1_CLOSE();
}
}
}
RX_CNT=0;
}
}
}
void main()
{
sys_init();
usart_init();
send_command();
LED1_OPEN();
LED2_OPEN();
LED3_OPEN();
while(1);
}
評論
查看更多