您的位置 首页 系统

PIC16F877A串口发送字符串问题

PIC16F877A串口发送,查询方式,就为了调出这个费了我一天时间,原来是串口芯片电压问题,现总结如下:1、注意232串口芯片供电电压,有5V和3.3V的2、注

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(RCIERCIF)

{

//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]==0x45RX_Buffer[1]==0x53RX_Buffer[2]==0x50)

{

if(RX_Buffer[4]==0x4CRX_Buffer[5]==0x45RX_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);

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/141591.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部