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、注意连接线的可靠性,如果用线连的话一定要检查线和插座是否稳定连接
PIC16F877A串口发送
//以下是串口发送程序
#include
#define ucharunsigned 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
#defineLEDRD1
__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);
}
上一篇:怎样解决pic单片机读修改写问题
下一篇:PIC单片机模拟异步串行通讯UART源程序
推荐阅读最新更新时间:2024-11-20 10:54
设计资源 培训 开发板 精华推荐
- AD8048AR-EBZ,AD8048AR 高速运算放大器评估板,采用 6 引脚 SOT-23 封装
- AM2G-1212DH30Z ±12V 2 瓦 DC-DC 转换器的典型应用
- 用于卤素灯和低功耗灯的数字墙调光器
- LT3990EDD 1.21V 降压转换器的典型应用
- 使用 Microchip Technology 的 LXE1810-100 的参考设计
- LED 上的 ESD 电容器 (CPx =10 nF) 的典型应用,A8511 2 MHz、4 通道 x 150 mA WLED/RGB 驱动器与输出断开的热降额
- OM13058,用于 LPC11U68 系列 MCUS 的 LPCXpresso 评估板
- 具有PWM电流控制器的L6227 DMOS双路全桥驱动器评估板
- EVAL-AD7715-3EB,使用 AD7715 的评估板,16 位 Sigma Delta 模数转换器
- zvs双管