PIC16F877A serial port transmission

Publisher:脑力激荡Latest update time:2016-11-03 Source: eefocusKeywords:PIC16F877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
PIC16F877A serial port sending and querying methods. It took me a day to find out this. It turned out to be a problem with the serial port chip voltage. The summary is as follows:
1. Pay attention to the power supply voltage of the 232 serial port chip, there are 5V and 3.3V
2. Pay attention to the TXD and RXD wiring. The TXD of the microcontroller is connected to the R2OUT of the 232 chip, and the RXD of the microcontroller is connected to the T2IN of the 232 chip.
3. Note that the UTXD of ESP8266 is connected to the RX of MCU, and the URXD is connected to the TX of MCU.
4. Note that the UTXD of ESP8266 is connected to the T2IN of 232, and the URXD is connected to the R2OUT of 232
5. Set the serial port baud rate to 9600
6. Pay attention to the reliability of the connection line. If you use a wire connection, be sure to check whether the wire and the socket are stably connected.
7. System connection is shown in the figure below
PIC16F877A serial port transmission - allegro_tyc - allegro_tyc's blog
 //The following is the serial port sending program
#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=that;
    while(TRMT==0);
}
 
void serial_init()
{
TRISC=0xbf; //Set RX as input and TX as output
TXSTA=0x24;
RCSTA=0x80;
SPBRG=0x33;//I use 8MHz crystal, 9600=8000000/(16*(X+1))->X=51->X=0x33
GIE=1;
LIKE=1;
RCIE=1;
}
 
void main()
{
serial_init();
while(1)
{
UART_SendByte(0x31);
UART_SendByte(0x32);
UART_SendByte(0x33);
delay2ms(500);
}
}

//The following is the program for MCU to send AT naming to 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 9600
 
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; //Set PORTD to output
}
 
void usart_init()
{
TRISC=0xbf; //Set RX as input and TX as output
TXSTA=0x24;
RCSTA=0x80;
SPBRG=0x33; //I use 8MHz crystal oscillator, 9600=8000000/(16*(X+1))->X=51->X=0x33
GIE=1;
LIKE=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);
}
}
 
//The following is the program for MCU to send AT command to ESP8266, receive command, parse and respond
#include
 
#define uchar unsigned char
#define uint  unsigned int
 
unsigned 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 9600
 
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; //Set PORTD to output
}
 
void usart_init()
{
TRISC=0x80; //Set RX as input and TX as output
TXSTA=0x24;
RCSTA=0x90;
SPBRG=0x33; //I use 8MHz crystal oscillator, 9600=8000000/(16*(X+1))->X=51->X=0x33
RCIE=0x01;
TXEN=0x01;
TXIE=0x01;
PEIE=0x01; //External interrupt enable
GIE=1; //General interrupt enable
}
 
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);
}
Keywords:PIC16F877A Reference address:PIC16F877A serial port transmission

Previous article:Simple example of PIC16F84A reading clock chip DS1302
Next article:Explanation of I/O--lat registers of PIC16 and PIC18

Recommended ReadingLatest update time:2024-11-16 13:06

PIC16F877A TMR0 Timer Experiment 2
/*********PIC16F877A TMR0 timer experiment 2*******  Use the prescaler. To achieve accurate timing, TMR0 should not be written repeatedly.  PS2 PS1 PS0   0 0 0 2 division  0 0 1 4 division  0 1 0 8 division  0 1 1 16 division 1 0  0 32 division 1 0  1 64 division  1 1 0 128 division  1 1 1 256 division  When TMR0 is u
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号