1. Trang chủ
  2. » Giáo Dục - Đào Tạo

(TIỂU LUẬN) COMPUTER COMMUNICATION PROGRAMMING USING c VIA SERIAL PORT WITH MICROCONTROLLER 8051

28 6 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Computer Communication Programming Using C# Via Serial Port With Microcontroller 8051
Người hướng dẫn Ph.D Pham Van Khoa
Trường học Ho Chi Minh City University of Technology and Education
Chuyên ngành Computer Organization and Architecture
Thể loại Report of Lecturer
Năm xuất bản 2022
Thành phố Ho Chi Minh City
Định dạng
Số trang 28
Dung lượng 792,71 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY AND EDUCATIONFACULTY OF HIGH QUALITY TRANING  COMPUTER ORGANIZATION AND ARCHITECTURE COMPUTER COMMUNICATION PROGRAMMING USING C# VIA SERIAL

Trang 1

HO 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 2

LIST 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 4

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, stop bits and parity bits

TO 8051 1 Introduction

The 8051 is the first microcontroller of the MCS-51 family developed by IntelCorporation 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 Oxide-Semiconductor (CMOS) technology which consumes less power thanNMOS and made it better compatible for battery-powered applications

Metal-2 Application

Here we will discuss the various important applications of the 8051microcontroller 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 screensand 8051 microcontrollers also come with touch screen features Therefore it has

a wide application in mobile phones, music players and games Automotivefield: The 8051 microcontroller has a wide application in the automotive sectorand especially in hybrid vehicle management In addition, cruise control andanti-braking systems are other areas where it has great use

3 Family Member Specifications

Find below, the specifications for various popular 8051 family membersdeveloped by mentioned semiconductor companies

Trang 5

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

2

Trang 6

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 7

• 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 memorylocations and we can interface up to 64 KB of program memory externally

in case of large applications

• Sizes specified of RAM and ROM are different by their manufacturer

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 inthe SBUF register

The moment a byte is written into SBUF, it is framed with the start and stop bitsand 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 9

• 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 orderaddress bus (AD0-AD7) which de-multiplexed by ALE signal and latchused in external memory access operation

• P2 I/O pins are multiplexed with remaining higher order address bus (A8-A15)

• 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

5

Trang 10

III PROGRAMMING COMPUTER

COMMUNICATION VIA SERIAL PORT

1 Serial communication

- Serial communication is a communication method that uses one ortwo transmission lines to send and receive data, and that data iscontinuously sent and received one bit at a time

- 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 way street Only one of the two devices on a link can transmit, the othercan only receive The simplex mode can use the entire capacity of thechannel to send data in one direction

one Full-duplex communication: A method where send and receive bothhave their own transmission line so data can be simultaneously sent andreceived

Trang 11

- Half-duplex communication: A method where communication isperformed using one transmission line while switching between send andreceive.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 bigissue

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 byUART

• 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 253 in TH1 (8-bit reg.)

• So 28800 Hz will get divided by 3 as the timer will overflow after every 3 cycles

• we can achieve different baud rates by putting the division factor in the TH1 register

7

Trang 12

UART stands for Universal Asynchronous Receiver-Transmitter A UARTgenerates its internal data clock to the microcontroller It synchronizes that clockwith the data stream by using the start bit transition The receiver needs the baudrate 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 isnormally 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 iseven when EVEN and odd when ODD But in this problem, to connect to 8051 byserialPort, 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 Thisdepends on the device being used, but normally specify 7 bits for alphanumericcharacters and symbols, and specify 8 bits for 1 byte binary data In this case, wechoose 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 serialdevice 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 purposecomputer (like PC, laptop, ) and an on-chip computer (8051 MCU), the NETFramework have to install on your computer (at least version 2.0)

Trang 13

The .NET Framework is the key withprovide the SerialPort properties on library:namespace System.IO.Ports included manylayer And the most significant layer isSerialPort This also supplies tool to controlother properties like baud rate, stop bits,

9

Trang 14

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 someapplication 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 15

C# code to make Winform

} catch {

MessageBox.Show("Can't open port"); }

string 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));

11

Trang 16

} }

private void button2_Click(object sender, EventArgs e)

We 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 17

3 KeilC to create Hex file to add to 8051

To 8051 MCU can process the data (received from C# Windows form), we mustadd 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;

13

Trang 18

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 19

else if ((i % 32 != 0) && (i % 16 == 0))

{ lcdcmd(0xC0); //NEW LINE lcddat(c);

Trang 20

lcdcmd(0x01); //CLEAR SCREENc="";

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 21

cao cua dia chi

// Gui lenh doc // Gui

byte // Gui byte thap cua

return b;

}

unsigned char EEPROM_25LCxxx_WIP()

17

Trang 24

To 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 25

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 canread it from datasheet of MAX232

21

Trang 26

(Simplified schematic of MAX232)

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)

Ngày đăng: 07/12/2022, 09:51

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w