HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY AND EDUCATION FACULTY OF HIGH QUALITY TRANING COMPUTER ORGANIZATION AND ARCHITECTURE COMPUTER COMMUNICATION PROGRAMMING USING C# VIA SE
Trang 1HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY AND EDUCATION
FACULTY OF HIGH QUALITY TRANING
COMPUTER ORGANIZATION AND ARCHITECTURE
COMPUTER COMMUNICATION PROGRAMMING USING C# VIA SERIAL PORT WITH MICROCONTROLLER 8051
Lecturer: Ph.D Pham Van Khoa
Class: COOA335364E_21_2_02CLC
Ho Chi Minh City, April 2022
Trang 2LIST OF STUDENT
REPORT OF LECTURER
Trang 3
TABLE OF CONTENTS
I INTRODUCTION 1
II INTRODUCTION TO 8051 1
1 Introduction 1
2 Application 1
3 Family Member Specifications 1
4 8051 Architecture & Pin diagram 2
5 CPU 3
6 RAM (Random-access memory) 3
7 ROM (Read Only Memory) 3
8 SBUF resister 4
9 Timers and Counters 4
10 Interrupts 4
11 Oscillator 5
12 I/O Ports 5
13 Serial Communication port 5
III PROGRAMMING COMPUTER COMMUNICATION VIA SERIAL PORT 6
1 Serial communication 6
2 Tranmission modes: simplex, half-duplex and full-duplex 6
3 Bit rate and Baud rate and UART 7
4 Parity bit, stop bit and data bits 8
5 RS232 and DB-9 Serial Port 8
6 Use C# with NET Framework and create Windows Application 8
IV CIRCUIT DESIGN 10
1 Block diagram 10
2 C# with NET Framework to create Window Application 10
3 KeilC to create Hex file to add to 8051 13
4 Communicate with EEPROM to stored data 20
4.1 SPI interface 20
5 Simulation by using Proteus ISIS 20
V CONCLUSION 23
VI REFERENCES 24
Trang 41
I INTRODUCTION
The objective of this report is to make the 8051 microcontroller understand whatever we input via the COM serial port The idea of this project is to
program the 8051 microcontroller by using C# programming language to make
it understand the input, which is one of the 8051’s many powerful features integrated UART, known as a serial port to easily read and write values to the serial port instead of turning on and off one of the I/O lines in rapid succession
-to properly "clock out" each individual bit, including start bits, s-top bits and parity bits
II INTRODUCTION TO 8051
1 Introduction
The 8051 is the first microcontroller of the MCS-51 family developed by Intel Corporation in 1980 It was developed using N-type Metal-Oxide-Semiconductor (NMOS) technology and later it came to be identified by a letter C in their names e.g 80C51 which was developed with Complementary Metal-Oxide-Semiconductor (CMOS) technology which consumes less power than NMOS and made it better compatible for battery-powered applications
2 Application
Here we will discuss the various important applications of the 8051 microcontroller Power management: The 8051 microcontroller is equipped with
an efficient measurement system and it helps the microcontroller save energy to
a large extent Touch screen: modern microcontrollers featuring touch screens and
8051 microcontrollers also come with touch screen features Therefore it has a wide application in mobile phones, music players and games Automotive field: The 8051 microcontroller has a wide application in the automotive sector and especially in hybrid vehicle management In addition, cruise control and anti-braking systems are other areas where it has great use
3 Family Member Specifications
Find below, the specifications for various popular 8051 family members developed by mentioned semiconductor companies
Trang 52
4 8051 Architecture & Pin diagram
All 8051 microcontrollers have unique architecture as shown in the figure, which consists of functional blocks to build 8051 powerful controlling machines
Trang 63
5 CPU
Microcontroller 8051 has a central processing unit which is also called ALU (Arithmetic Logic Unit) which performs all arithmetic and logical operation
6 RAM (Random-access memory)
• Microcontroller 8051 has 128-byte RAM for data storage
• It is a Volatile type of memory That means the data is lost when power to the device is turned off
• It is used during execution time to store data temporarily
• RAM consists of a register bank, stack, and temporary data storage with some special function registers (SFR’s)
7 ROM (Read Only Memory)
• In 8051, 4KB ROM is available for program storage
Trang 74
• It is a Non-Volatile type of memory It means that data is not lost even in
the event of power failure
• 8051 has a 16-bit address It means it can access 2^16 memory locations
and we can interface up to 64 KB of program memory externally in case of
The internal RAM address of SBUF in 8051 in 99H:
For a byte of data to be transferred via the transmitted line, it must be placed in
the SBUF register
The moment a byte is written into SBUF, it is framed with the start and stop bits
and transferred serially via the transmitted line
SBUF holds the byte are received serially via the Receiver, the 8051 de-frames
it by eliminating the stop and the start bits, making a byte out of the data
received, and then placing it in SBUF
9 Timers and Counters
• Microcontroller 8051 has two timer pins T0 and T1
• By these timers, we can generate a delay of a particular time in timer mode
• We can count external pulses or events in counter mode
• Two 16-bit timer registers are available as T0 (TH0 & TL0) and T1 (TH1
& TL1), e.g If we want to load T0 then we can load Higher 8-bit in TH0
& Lower 8-bit in TL0
• TMOD and TCON registers are used to select mode and control the timer
operation
10 Interrupts
• Interrupts are requested by internal or external peripherals which are
masked while unused
Trang 85
• Interrupt handler routines are called after each interrupts event occurs
• These routines are called an Interrupt Service Routine (ISR) and are located
in special memory loc
• INT0 and INT1 pins used to accept external interrupts
12 I/O Ports
• 8051 has four Input/output port P0, P1, P2, P3
• Each port is 8 bit wide and their SFR (P0, P1, P2, P3) are bit accessible i.e
we can set or reset individual bit
• Some ports have dual functionality on their pins as,
• P0 I/O pins are multiplexed with an 8-bit data bus and lower order address bus (AD0-AD7) which de-multiplexed by ALE signal and latch used in external memory access operation
• P2 I/O pins are multiplexed with remaining higher order address bus A15)
(A8-• P3 I/O pins also have dual functions as,
• P3.0 – RXD – Serial data receive
• P3.1 – TXD – Serial data transmit
• P3.2 – INT0 – External Interrupt 0
• P3.3 – INT1 – External Interrupt 1
• P3.4 – T0 – Clock input for counter 0
• P3.5 – T1 – Clock input for counter 1
• P3.6 – WR – Signal for writing to external memory
• P3.7 – RD – Signal for reading from external memory
• P0 and P2 can’t be used as I/O pins in the external memory access operation
13 Serial Communication port
• 8051 has two serial communication pins TXD and RXD used for transmitting and receive data serially via the SBUF register
• SCON SFR used to control serial operation
Trang 9- 8051 has two serial communication pins TXD and RXD used for transmitting and receive data serially
2 Tranmission modes: simplex, half-duplex and full-duplex
- In Simplex mode, the communication is unidirectional, as on a one-way street Only one of the two devices on a link can transmit, the other can only receive The simplex mode can use the entire capacity of the channel to send data in one direction
- Full-duplex communication: A method where send and receive both have their own transmission line so data can be simultaneously sent and received
Trang 107
- Half-duplex communication: A method where communication is performed using one transmission line while switching between send and receive.For this reason, simultaneous communication cannot be performed
In the case of communicate between 8051 and computer, we need to use duplex communication mode to receive, transmit data at the same time
Full-3 Bit rate and Baud rate and UART
Bit rate – the number of binary ‘bits’, 1s or 0s to be transmitted per second
Baud rate – the number of line ‘symbols’ transmitted per second
Standard baud rates 8051 supported are 1200, 2400, 4800, 19200, 38400, 57600, and 115200 Normally most of the time 9600 bps is used when speed is not a big issue
Baud Rate calculation:
• To meet the standard baud rates generally crystal with 11.0592 MHz is used
• As we know, 8051 divides crystal frequency by 12 to get a machine cycle frequency of 921.6 kHz
• The internal UART block of 8051 divides this machine cycle frequency by
32, which gives the frequency of 28800 Hz which is used by UART
• To achieve a baud rate of 9600, again 28800 Hz frequency should be divided by 3
• This is achieved by using Timer1 in mode-2 (auto-reload mode) by putting
Trang 118
UART stands for Universal Asynchronous Receiver-Transmitter A UART generates its internal data clock to the microcontroller It synchronizes that clock with the data stream by using the start bit transition The receiver needs the baud rate to know ahead of time to properly receive the data stream
4 Parity bit, stop bit and data bits
Stop bit: This sets the length of the bit that indicates the end of the data.This is normally selected as 1 bit, 1.5 bits, or 2 bits.The start bit length is fixed as 1 bit so this setting is not necessary In this case, the stop bit is set at 1
Parity bit: a parity bit of "1" or "0" is added to the data so as to make the number
of "1" data bits even for EVEN and odd for ODD On the receiving side, the number of "1" data bits is counted and the data is judged as being correct if the number is even when EVEN and odd when ODD But in this problem, to connect
to 8051 by serialPort, we don’t need the parity check, so parity bit is none
Data bits: This specifies how many bits each item of data is composed from This depends on the device being used, but normally specify 7 bits for alphanumeric characters and symbols, and specify 8 bits for 1 byte binary data In this case, we choose 8 bits since we want to sent character, which from ASCII table
5 RS232 and DB-9 Serial Port
RS232 is a standard protocol used for serial communication, it
is used for connecting computer and its peripheral devices to
allow serial data exchange between them As it obtains the
voltage for the path used for the data exchange between the
devices It is used in serial communication up to 50 feet with
the rate of 1.492kbps As EIA defines, the RS232 is used for
connecting Data Transmission Equipment (DTE) and Data
Communication Equipment (DCE)
A COM port is simply an I/O interface that enables the connection of a serial device to a computer You may also hear COM ports referred to as serial ports
6 Use C# with NET Framework and create Windows Application
In order to perform transmission and reception between a general purpose computer (like PC, laptop, ) and an on-chip computer (8051 MCU), the NET Framework have to install on your computer (at least version 2.0)
Trang 129
The NET Framework is the key with provide the SerialPort properties on library: namespace System.IO.Ports included many layer And the most significant layer is SerialPort This also supplies tool to control other properties like baud rate, stop bits,
Trang 1310
IV CIRCUIT DESIGN
1 Block diagram
2 C# with NET Framework to create Window Application
Since user can not use code to communicate with 8051, we must create some application that user can use it easily
In Windows OS, the most efficient way is using C# with NET Framework The NET Framework is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows .NET Framework provide a fastest way to connect, configurate, send or receive data serially, or in this case is
communicate with UART protocol
This is the result of Windows form application and code:
Trang 14string sender_str = textBox1.Text;
int dodai = sender_str.Length;
string dodai_str = dodai.ToString();
sender_str = sender_str + '#';
for (int i = 0; i < sender_str.Length; i++) { serialPort1.Write(sender_str.Substring(i,1));
Trang 15We also can use another programming language to create Windows Application
In this case, we use Java, with Apache NetBeans IDE and jSerialComm library Here is the result of Windows Application
But unlike NETFramework, we have to set the parameters of the UART
protocol by following code (Java and jSerialComm):
Trang 1613
3 KeilC to create Hex file to add to 8051
To 8051 MCU can process the data (received from C# Windows form), we must add code to 8051
Here is the code we added to 8051 and complier is KeilC KeilC will compile and create HEX file to we can add it to 8051
void lcdcmd(unsigned char);
void lcddat(unsigned char);
unsigned char c;
unsigned int i;
unsigned int j;
Trang 1714
void Soft_SPI_Init();
void Soft_SPI_Write(unsigned char b);
unsigned char Soft_SPI_Read(void);
void EEPROM_25LCxxx_Write(unsigned int add, unsigned char b);
unsigned char EEPROM_25LCxxx_Read(unsigned int add);
unsigned char EEPROM_25LCxxx_WIP();
Trang 18} else if ((i % 32 != 0) && (i % 16 == 0)) {
lcdcmd(0xC0); //NEW LINE lcddat(c);
Trang 1916
lcdcmd(0x01); //CLEAR SCREEN c="";
continue;
} if(c=='#'){continue;}
if (i % 32 == 0) {
lcdcmd(0x01); //CLEAR SCREEN }
else if ((i % 32 != 0) && (i % 16 == 0)) {
lcdcmd(0xC0); //NEW LINE }
else {
EEPROM_25LCxxx_Write(i-1, c);
lcddat(c);
} i++;
}
}
// Ghi mot byte b sang EEPROM, tai dia chi add
void EEPROM_25LCxxx_Write(unsigned int add, unsigned char b)
Trang 2017
WRITE
cao cua dia chi
Soft_SPI_Write(add & 0x00FF); // Gui byte thap cua dia chi
cao cua dia chi
Soft_SPI_Write(add & 0x00FF); // Gui byte thap cua dia chi
Trang 2118
{
unsigned char result;
SPI_CS = 0;
thanh ghi trang thai
Trang 23To communicate with 8051 and EEPROM (25LC256), we can use this
simulation (with code of Keil C in above)
5 Simulation by using Proteus ISIS
Proteus Schematic
Trang 2421
To sketch the circuit here, we have to:
• Use an IC MAX232 between COM port DB9 and 8051 Since the
difference of the voltage input/output of RS232 protocol and 8051 (the figure shown below)
• Since the logic 1 of 8051 is 5V, when the logic 1 of RS232 DB9 is 3 to 25V, so we need 2 NOT gates to turn the logic 0 → logic 1 You can read
-it from datasheet of MAX232
Trang 2522
(Simplified schematic of MAX232)
• Connect to LCD 16x2:
o D0→D7: receive data at 8-bit to show character from ASCII table
o RS/RW/E: set the status of 16x2 (write/change cursor/new line/ )
o VDD: connect to ground / VCC: connect to VCC (5V) /
VEE: change the saturation of LCD
• To communicate serially to 8051, we connect RXD on DP9 to RXD on
8051 and TXD on DP9 to TXD on 8051 (through MAX232)