PIC microcontroller example 9: PC and microcontroller serial full-duplex communication

Publisher:liliukanLatest update time:2016-11-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Purpose of this example:

This example is completely based on computer software. It uses PROTEUS software to build a single-chip microcomputer system, and uses VB to write a computer control software to communicate serially with the single-chip microcomputer system. The control software on the computer can send a number between 0 and 999, which is displayed on the single-chip microcomputer system. The single-chip microcomputer system can also input a number with the same value range and display it on the computer control software. This example is mainly to show the simulation of virtual serial communication.

2. System Configuration

Although this example is simple, it is necessary to establish a virtual serial port connection in order to see the desired simulation results. To simulate serial communication, the serial port on the microcontroller system must be connected to the serial port on the computer so that data can be transmitted between them. However, since it is a simulation, there is no actual connection, so it is necessary to create a pair of virtual serial ports and connect them. Here I use the software Virtual Serial Ports Driver XP 5.1 (VSPD XP), and the interface after opening is shown in Figure 1. In the drop-down options next to the "ADD PAIR" button on the right, select "COM3" and select "COM3".

PIC microcontroller example 9: PC and microcontroller serial full-duplex communication - wujieflash - Universal Electronics Network

"COM4", and then click the "ADD PAIR" button to complete the connection between the virtual serial ports "COM3" and "COM4". The completion is shown in Figure 1. Simple, right? !

       In addition, you also need to choose a serial port debugging software as your debugging software, because when you have completed the MCU system and the control software on the computer has not been compiled, without serial port debugging software, you will not be able to know whether your MCU system is correct or not. There are many such software on the Internet, just find one. My software is SSCOM32, and the interface is shown in Figure 2.

PIC microcontroller example 9: PC and microcontroller serial full-duplex communication - wujieflash - Universal Electronics Network

I won't introduce the functions and usage one by one here. Anyway, it can both receive and send, and it can debug the functions required by the instance.

3. Single chip microcomputer system

MCU hardware:

The circuit is shown in Figure 3.

PIC microcontroller example 9: PC and microcontroller serial full-duplex communication - wujieflash - Universal Electronics Network

The microcontroller used in this example is still PIC16F877, which contains the UASRT module, which is required for serial communication. The display part uses a 3-digit digital tube common anode output. The keyboard uses a matrix keyboard. It should be noted here that since it is a simulation, the level conversion chip MAX232 is not used to convert the TTL level into the RS232 level. In fact, this step is necessary. Special attention should be paid to the setting of the serial port. Since the baud rate set in the microcontroller program of this example is 9600, 8-bit data, and no parity check, the serial port must also be set in the same way. Here, since a virtual connection between "COM3" and "COM4" is established, the port number must be set to "COM3" or "COM4". This is the key! As shown in Figure 4.

PIC microcontroller example 9: PC and microcontroller serial full-duplex communication - wujieflash - Universal Electronics Network

MCU software:

The microcontroller software mainly consists of the following parts: initialization program, display program, keyboard input program, serial receiving program, and serial sending program.

The program uses C language, which is simple and clear, and has been compiled by PICC.

The entire procedure is as follows:

/****************************************************** ***********

* Title: PCtoPIC *

* Purpose: used for communication between the host computer and the MCU (in this case, the slave computer software) *

* Date: January 4, 2009 *

* Author: WUJIEFLASH *

*************************************************** **********/                      

#include

//Predefined

#define uch unsigned char

//Display code table

const uch shu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98,0xff};

//variable

int data,temp1,temp2,temp3,temp;

uch flag,keylock;

uch point,buffer[3];      

//initialization

void init()

{

       TRISB=0;

       PORTB=0;

       TRISC=0;

       PORTC=0;

       keylock=1; //Initialize the keyboard key flag to lock the key

      

       //USART component initialization

       SPBRG=0x19; //Set the baud rate to 9600bps

       TXSTA=0x04; //Select asynchronous high-speed 8-bit data transmission mode

       RCSTA=0x80; //Enable serial port to work

       TRISC=0x80; //Set to high impedance to prevent interference

       TXEN=1; //Send allowed

       CREN=1; //Receive allowed

       PEIE=1; //Open peripheral interrupt

       RCIE=1; //Open serial port interrupt

       ei(); //Open total interrupt

}

//Display the required delay

void delay()

{

       int i;

       for(i=0;i<1000;i++);     

}

 

//Send subroutine

void send_usart()

{

       uch i;

       for(i=0;i<3;i++)//Send 3 data each time

       {

              if(i==0)TXREG=data/100+0x30;//get the hundredth digit

              if(i==1)TXREG=data%100/10+0x30; //get the tenth digit

              if(i==2)TXREG=data%10+0x30; //get the unit digit

 

              while(1)

              {

                     if(TXIF==1)break;//Wait for sending to complete

              }

       }

}

 

//Interrupt receiving subroutine

void interrupt receive(void)

{

       if(point<3) //Have 3 data been received?

       buffer[point++]=RCREG-48; //No, continue receiving

       else point=0;//Yes, the pointer is reset

       data=buffer[0]*100+buffer[1]*10+buffer[2]; //combine into three digits

}

 

//Digital tube display

void display(int dis_data)

{

       int dis_data_ge,dis_data_shi,dis_data_bai;

       dis_data_ge =dis_data%10; //unit digit

       dis_data_shi=dis_data%100/10; //ten digit

       dis_data_bai=dis_data/100; //hundreds digit

       if(dis_data==0) //If the data is 0, only 1 bit is displayed

       {

              dis_data_ge=0;

              dis_data_shi=10;

              dis_data_bai=10;

       }

       if(dis_data<10) //If the data is less than 10, the tens and hundreds digits are closed

       {

              dis_data_shi=10;

              dis_data_bai=10;

       }

       if(dis_data<100) //If the data is less than 100, only two digits will be displayed

       {

              dis_data_bai=10;

       }

      

       RC0=0;

       RC1=0;

       RC2=1;

       PORTB=shu[dis_data_ge]; //ones digit sent to display

       delay();

      

       RC0=0;

       RC2=0;

       RC1=1;

       PORTB=shu[dis_data_shi]; //ten digits sent to display

       delay();

      

       RC1=0;

       RC2=0;

       RC0=1;

       PORTB=shu[dis_data_bai]; //Hundreds digit is sent to display

       delay();  

}

 

//Keyboard (line reversal method)

void presskey()

{

       uch line,column,key;

       TRISD=0x07;

       PORTD=0xf0;

       column=PORTD & 0x07; //Get column code

 

       TRISD=0xf0;

       PORTD=0x0f;

       line=PORTD&0xf0; //Get line code

       key=line|column; //synthesize key value

      

       if(key==0)keylock=0; //Ensure that only one key is pressed at a time

       if(keylock==0)

       {

              if(key!=0)keylock=1;

              switch(key) //Find key value

              {

                     case 0x11:temp=3;flag++;break;

                     case 0x12:temp=2;flag++;break;

                     case 0x14:temp=1;flag++;break;

                     case 0x21:temp=4;flag++;break;

                     case 0x22:temp=5;flag++;break;

                     case 0x24:temp=6;flag++;break;

                     case 0x41:temp=9;flag++;break;

                     case 0x42:temp=8;flag++;break;

                     case 0x44:temp=7;flag++;break;

                     case 0x82:temp=0;flag++;break;

                     case 0x81:flag=4;send_usart();break;//send

                     case 0x84:flag=0;data=0;break;//clear key

              }

       }

       if(flag==1) //First key press

       {

              if(temp==0)flag=0;//If 0 is pressed, ignore

              temp1=temp;

              data=temp1;

       }

       if(flag==2)//Second key press

       {

              temp2=temp;

              data=temp1*10+temp2; //The original ones place becomes tens place

       }

       if(flag==3)//The third key press

       {

              temp3=temp;

              data=temp1*100+temp2*10+temp3; //The original tens digit becomes hundreds digit

              flag=4;//Block button

       }    

}

 

//Main program

main()

{

       init();

       while(1)

       {

              presskey();

              display(data);

       }

}

 

4. Computer control software

The host computer software is written in VB, and its functions are very simple, mainly including sending and receiving functions. The software interface is shown in Figure 5.

PIC microcontroller example 9: PC and microcontroller serial full-duplex communication - wujieflash - Universal Electronics Network

The software mainly consists of 2 "TextBox", 3 buttons, and some text controls.

Now paste all the programs:

Private Sub Cmdquit_Click()

i = MsgBox("Do you want to leave?", vbQuestion Or vbYesNo, "Warm Tips")

If i = 6 Then

Shell "explorer.exe http://blog.163.com/wujieflash"

End

End If

End Sub

 

Private Sub Cmdsend_Click()

If Len(Text1.Text) = 1 Then MSComm1.Output = "0" + "0" + Text1.Text 'If it is less than 3 digits, add 3 digits and send it again

If Len(Text1.Text) = 2 Then MSComm1.Output = "0" + Text1.Text

If Len(Text1.Text) = 3 Then MSComm1.Output = Text1.Text

End Sub

 

Private Sub cmdzero_Click()

Text2.Text = ""

Text1.Text = ""

End Sub

 

Private Sub Form_Load()

MSComm1.Settings = "9600,n,8,1" 'Set the data transmission rate and the character format for sending

MSComm1.CommPort = 4 'Set the communication serial port to 4

MSComm1.InputLen = 0 'Set or return the number of bytes read from the receive buffer at one time

MSComm1.InBufferSize = 512 'Set the receive buffer to 512BYTE

MSComm1.InBufferCount = 0

MSComm1.OutBufferSize = 512 'Set the send buffer to 512BYTE

MSComm1.OutBufferCount = 0

MSComm1.RThreshold = 3 'Every 3 characters in the receive buffer triggers a receive event

MSComm1.SThreshold = 1

MSComm1.PortOpen = True

End Sub

 

Private Sub Label6_Click()

Shell "explorer.exe http://blog.163.com/wujieflash" 'My blog's hyperlink, please come and support me

End Sub

 

Private Sub MSComm1_OnComm()

Text2.Text = MSComm1.Input

End Sub

 

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then

KeyAscii = 0

MsgBox ("Please enter a numeric character!")

End If

End Sub

The software is a bit simple, but it has complete functions.

5. Conclusion

This time, the simulation of this example was a bit troublesome, but I did learn a lot about serial communication. After implementing it myself, I felt pretty good. I think most of the novices like me or friends who are looking for information in this area will benefit a lot.

Reference address:PIC microcontroller example 9: PC and microcontroller serial full-duplex communication

Previous article:PIC microcontroller example 4: design and simulation of temperature measurement system
Next article:PIC microcontroller example eight: four ways of 24X24 dot matrix display

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号