Serial Communication Experiment Between arm and 51 Single-Chip Microcomputer

Source: Internet
Author: User

In the past few days, the serial port communication of arm was achieved. Because of the previous single-chip microcomputer basics, we chose 51 single-chip microcomputer and Its experiment. The experiment is to compile a serial communication program under Linux, compile the program, and download it to the arm board for running, to verify its feasibility. In Linux, the serial port program is to write a data in 51 and collapse the return value. The program in section 51 accepts data sent from arm and displays the data on the digital tube. It sends the data back to arm.

The serial communication program in Linux is as follows:

# Include <stdio. h>
# Include <stdlib. h>
# Include <fcntl. h>
# Include <errno. h>
# Include <termios. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/IOCTL. h>
# Include <Linux/If. h>
# Include <netinet/in. h>
# Include <unistd. h>
# Include <sys/types. h>
// # Include <moxadevice. h>
# Include "mxser. H"
# Define blen 1024
# Define max_count 9999
Unsigned short btable [] = {b9600 };
Int main ()
{
Pid_t PID;
Int iflag1, iflag2;
Struct termios opint1;
Struct termios opint2;
Int fd1, fd2;
Char name [5];
Int ilen1, ilen2;
Char num;
Char rxbuf1 [10];
Int I, J, K;

Printf ("this is two serias ports program \ n ");
I = 0;
Sprintf (name, "/dev/ttyusb0 ");
Fd1 = open (name, o_rdwr | o_ndelay );
If (fd1 <0)
{
Printf ("Open serias Port 1 fail % d! \ N ", errno );
Return-1;
}
Else
{
Printf ("Open serias Port 1 success! \ N ");
}
Ilen1 = rs232_mode;
IOCTL (fd1, moxa_set_op_mode, & ilen1 );
Opint1.c _ cflag = b9600 | cs8 | cread | clocal;
Opint1.c _ iflag = 0;
Opint1.c _ Oflag = 0;
Opint1.c _ lflag = 0;
Opint1.c _ CC [Vmin] = 0;
Opint1.c _ CC [vtime] = 0;
Tcflush (fd1, tcioflush );
Tcsetattr (fd1, tcsanow, & opint1 );
I = 1;
PID = fork ();
If (pid> = 0)
{
Iflag1 = 1;
While (iflag1)
{
// Write (fd1, txbuf1, 20 );
Ilen1 = read (fd1, & num, sizeof (char ));
Printf ("read data is % d \ n", num );
Scanf ("% s", & num );
Write (fd1, & num, sizeof (char ));
}
}
Else if (PID <0)
{
Printf ("fork error ");
Close (fd1 );
}
Close (fd1 );
Printf ("port1 closed! \ N ");

Return 0;

}

51 procedures are as follows:

# Include <reg52.h> // contains the header file, which generally does not need to be changed. The header file contains the definition of special function registers.
# Include <intrins. h>
# Define uchar unsigned char
# Define unit unsigned int
Uchar code table [10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
Uchar led_buf [8] = {0 };
Void delay (unit I)
{
Uchar K, J;
For (k = 0; k <I; k ++)
For (j = 0; j <148; j ++ );
}
/*************************************** ***************************/
/* Main function */
/*************************************** ***************************/
Void main (void)
{

Uchar I = 0;
// Delay( 100 );
Scon = 0x50;/* scon: Mode 1, 8-bit UART, enabling receiving */
Tmod | = 0x20;/* tmod: timer 1, Mode 2, 8-bit reload */
Th1 = 0xfd;/* Th1: Reload value for 9600 baud @ 11.0592 MHz */
Tr1 = 1;/* tr1: timer 1 run */
Ea = 1;/* Total open interruptions */
Es = 1;/* enable serial port interruption */

While (1)/* The main loop does not take any action */
{
P0 = table [led_buf [I];
P2 = I ++;
Delay (1 );
If (I = 8)
I = 0;
}
}
/*************************************** ***************************/
/* Serial interrupt program */
/*************************************** ***************************/
Void uart_ser (void) interrupt 4 // serially interrupt the Service Program
{
Unsigned char temp; // defines temporary variables
Uchar I = 0;

If (RI) // determines that the message is interrupted.
{
Led_buf [I] = sbuf-48;
Ri = 0; // flags are cleared
If (I = 8)
{
I = 0;
}
I ++;
Temp = sbuf; // read the buffer Value
// P1 = temp; // output the value to the P1 port for observation
Sbuf = temp; // send the received value back to the computer.
}
If (Ti) // if it is a sending flag, it is cleared.
Ti = 0;
}

The two programs verify the feasibility. The disadvantage is that the data returned from the microcontroller is not the original data after being read by the arm. I personally feel that the data type has been changed or something, so I am depressed, I am grateful to you for telling me what you know.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.