PIC16F877A串口發(fā)送,查詢方式,就為了調(diào)出這個(gè)費(fèi)了我一天時(shí)間,原來是串口芯片電壓?jiǎn)栴},現(xiàn)總結(jié)如下:1、注意232串口芯片供電電壓,有5V和3.3V的2、注意TXD、RXD接線,單片機(jī)的TXD接232芯片的R2OUT,單片機(jī)的RXD接232芯片的T2IN3、注意ESP8266的UTXD接MCU的RX,URXD接MCU的TX4、注意ESP8266的UTXD接232的T2IN,URXD接232的R2OUT5、串口波特率設(shè)置成96006、注意連接線的可靠性,如果用線連的話一定要檢查線和插座是否穩(wěn)定連接7、系統(tǒng)連線如下圖所示//以下是串口發(fā)送程序#include#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;//設(shè)置RX為輸入,TX為輸出TXSTA=0x24;RCSTA=0x80;SPBRG=0x33;//本人使用的是8MHz晶振,9600=8000000/(16*(X+1))->X=51->X=0x33GIE=1;PEIE=1;RCIE=1;}void main(){serial_init();while(1){UART_SendByte(0x31);UART_SendByte(0x32);UART_SendByte(0x33);delay2ms(500);}}
//以下是MCU發(fā)送AT命名給ESP8266的程序#include#define uchar unsigned char#define uint unsigned int#define LED RD1__CONFIG(0x3B32);#define LED_OPEN() (LED=1)#define LED_CLOSE() (LED=0)#define CPU_FOSC 8000000#define BAUD 9600unsigned char* LYMS="AT+CWMODE=3r";unsigned char* SZLY="AT+CWSAP="ESP8277_CYT","1234567890",11,3r";unsigned char* RST="AT+RSTr";unsigned char* LCAP="AT+CWLAPr";unsigned char* SZDLJ="AT+CIPMUX=1r";unsigned char* KQFU="AT+CIPSERVER=1,5000r";unsigned char* FSSJ="AT+CIFSRr";void delay2ms(uint x){uint a,b;for(a=x; a>0; a--)for(b=152; b>0; b--);}void sys_init(){TRISD=0x00; //設(shè)置PORTD口為輸出}void usart_init(){TRISC=0xbf; //設(shè)置RX為輸入,TX為輸出TXSTA=0x24;RCSTA=0x80;SPBRG=0x33; //本人使用的是8MHz晶振,9600=8000000/(16*(X+1))->X=51->X=0x33GIE=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發(fā)送AT命名給ESP8266的程序,并接收命令、解析并作出反應(yīng)#include#define uchar unsigned char#define uint unsigned intunsigned char RX_Buffer[32];unsigned char RX_CNT=0;#define LED1 RD1#define LED2 RD2#define LED3 RD3__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)#define CPU_FOSC 8000000#define BAUD 9600unsigned char* LYMS="AT+CWMODE=3r";unsigned char* SZLY="AT+CWSAP="ESP8277_TYC","1234567890",11,3r";unsigned char* RST="AT+RSTr";unsigned char* LCAP="AT+CWLAPr";unsigned char* SZDLJ="AT+CIPMUX=1r";unsigned char* KQFU="AT+CIPSERVER=1,5000r";unsigned char* FSSJ="AT+CIFSRr";void delay2ms(uint x){uint a,b;for(a=x; a>0; a--)for(b=152; b>0; b--);}void sys_init(){TRISD=0x00; //設(shè)置PORTD口為輸出}void usart_init(){TRISC=0x80; //設(shè)置RX為輸入,TX為輸出TXSTA=0x24;RCSTA=0x90;SPBRG=0x33; //本人使用的是8MHz晶振,9600=8000000/(16*(X+1))->X=51->X=0x33RCIE=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++;elseRX_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);}