PIC16F877A串口发送字符串问题
扫描二维码
随时随地手机看文章
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、系统连线如下图所示
PIC16F877A串口发送 - allegro_tyc - allegro_tyc的博客
//以下是串口发送程序
#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;//设置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
#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=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;//设置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
#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=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;//设置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);
}