Communication Mode
Parallel
Suitable for short distance communication, the parallel communication control is simple and the relative transmission speed is fast (8 bits are transmitted together).
Serial
Can only be transmitted by one person.
Sync (Understanding)
Establish the direct control of the receiver clock by the sender clock, so that the two sides achieve full synchronization. At this point, the distance between the bits of the transmitted data is an integer multiple of the "bit interval", while the transmitted characters do not leave a gap between them.
The sender's synchronization with the receiver can be done externally and synchronously
asynchronous (Regular)
is transmitted in the form of a character (frame). Data bits are transferred from low to high.
Format:
The free time here is arbitrary.
Serial Communication Format (transmission direction)
simplex (can only be along One Direction)
Half-duplex (bidirectional, but time-sharing)
Full Duplex
error checking for serial communication
Parity Check
When the data is sent, the 1 bits trailing by the data bits are parity bits (1/0). The sum of the number of 1 in the data and the number of the test bit 1 should be odd at parity, and the number of 1 in the data and the number of checksum digits 1 should be even when parity. When a character is received, the number of 1 is validated, and if the character is inconsistent, an error occurs during the transfer of the data.
Code and checksum
The sender sums the data blocks (or each byte xor), resulting in a byte of the checksum character (checksum) appended to the end of the data block. When the receiver receives the data, the data block (except for the checksum byte) is summed (or each byte is different or), and the resulting result is compared to the "checksum" of the sender, consistent without error.
Cyclic redundancy Check
Through a mathematical budget to achieve effective information and parity between the cycle check, often used to disk information transmission, storage area integrity check.
The basic structure of the
serial port
sbuf:51 Special registers in a single chip computer, Serial data buffers (one to receive a send), two is actually a common address 99H, but the two are separate in physics. When it is sent to use, the sbuf=xxx is used; (XXX for the data to be transmitted) when receiving use, use xxx=sbuf; remember, because it's serial, so the transmission is a one-person. T1 overflow rate: T1 timer Overflow frequency (that is, the countdown of the timer at every low meter full to the high carry time): Used to compute the baud rate (number of bits transmitted in binary code per second)
to implement the transmission of string communication between MCU and computer
tool: Stc-isp
Code:
#include <reg52.h> #include <stdio.h>//printf header file #define UC unsigned char #define UINT unsigned
int UC flag,i,flag_t,s[50]= "", j=0,flag_n=0;
UC code table[]= "I get"; void Init () {tmod=0x20;
Timer working mode, select the timer 1, working mode 28-bit initial automatic reload 8-bit timer. TH1=0XFD;
Timer 1 initial value, set baud rate of 9600 crystal 11.0529MHZ?
TL1=0XFD; Tr1=1;
Open Timer 1 sm0=0; Sm1=1; 10-bit asynchronous reception, (8-bit data) baud rate variable ren=1; Allow the serial port to receive bit ea=1; Allow interruption (total gate) Es=1;
Allow serial port Interrupt} void Main () {init ();
while (1) {if (flag==1) {if (flag_n!=0)//Make the second and subsequent I get XX newline, not with you transfer on one line (simply for the format good-looking) {Ti=1;
printf ("\ n"); while (!
TI);
ti=0;
for (i=0;i<6;i++) {sbuf=table[i]; while (!
TI);
ti=0;
for (i=0;s[i]!= ' # ' &&i<50;i++) {sbuf=s[i]; while (!
TI);
ti=0;
} flag=0;
if (flag_t==1)//send finished, output at computer end. {ti=1;
T1 must be set to 1 before printf.
printf ("\nyou transfer%s", s); while (!
TI);
ti=0;
flag_t=0; }} void Ser () Interrupt 4 {
if (RI)//Receive data, manually RI clear 0 {ri=0; if (flag==0&&j!=0)//1. The loop assignment is ' yes ' (string end marker), j=0, for the second pass the string is again from scratch {//2.flag is 0 and J is not 0 o'clock, guaranteed to be second and later,
Transmission string (Control output format) flag_n++;
for (j=0;s[j]!= ' # ' &&j<50;j++) s[j]= ';
j=0;
} s[j]=sbuf;
flag=1; if (s[j]== ' # ' | |
j==49)//With ' # ' as the end of the transmission string, I define the character array up to 50 so 49 should also end.
Flag_t=1;
else J + +; } if (TI)//Send data {}}
Run Screenshots:
Code Interpretation: Basically is a few modules: timer, interrupt and serial communication
interrupted
Register Introduction
IE (Interrupt enable): (addressable) set the opening and closing of each interrupt source
IP (interrupt prior) interrupt priority registers : (addressable) used to set the level at which each interrupt source belongs to a level two interrupt
Interrupt Source:
Interrupt Response Condition:1. Interrupt source Interrupt Request 2. Interrupt allowed for this interrupt source is 1 3.CPU open interrupt (ea=1)
Code writing:1. Open the total interrupt ea 2 first. And then open a specific interrupt to control 3. Set IP 4 If you have a special priority problem. Interrupt function Write format void function name () interrupt interrupt number (serial number shown above)//Interrupt function return value must be void//function name write// The interrupt number is used to determine which interrupt source
Timer
Register Introduction
tcon Support bit addressing: control registers, control the start and stop of T1, T0, and set overflow flags
Tmod, does not support bit addressing: Timer/Counter working method registers, determine the mode of work and function
Timer Code writing steps: 1.ea=1; 2.etx=1; Turn on Timer x interrupt
3. Configuration mode of work tmod=0x ...; According to their own needs according to the table to match 3. Configure Timer initial value //thx= (65535-n)/256; tlx= (65535-n)%256; N is determined by the length of time you want to clock. A timer is counted by a clock cycle.
4.trx=1; Turn on Timer x
Serial Communication Register Introduction Pcon Power Management Registers: ( non-addressable)
Used to manage the power supply part of SCM, including power-on reset detection, power off mode
, idle mode, etc.
Scon: (addressable) to set the working mode of the serial port, receive/Send control, and set status flag
baud-rate calculation
br> Smod is the first in Pcon, the default is 0 Fosc for the crystal frequency, so they set different baud rate, but also to consider the different problems of crystal oscillator.
Code writing 1. After the writing is finished 2. Es=1 3 is also required. When transmitting data, SBUF=XX receives data, XX=SBUF 4. Interrupt function Writing Be sure to have ri clear 0, but Ti's 0 in the main function//Because TI in the interrupt, (1) No if (TI), then the processing and RI Confusion (2) If there is TI judge//So it is possible never to transmit data, Because the first Ti is 0, can not enter the TI condition, there is no transmission//data (SBUF=XX). And in the transmission of data will be once again into the interrupt, is not processed in//broken into another interrupt, resulting in communication anomalies.
I believe that with the explanations of these modules, the code comments should be understood ~ If there are any problems and do not understand can be raised ~