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

COMPUTER COMMUNICATION PROGRAMMING USING c VIA SERIAL PORT WITH MICROCONTROLLER 8051

30 8 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

Định dạng
Số trang 30
Dung lượng 792,91 KB

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

Cấu trúc

  • I. INTRODUCTION (4)
  • II. INTRODUCTION TO 8051 (4)
    • 1. Introduction (0)
    • 2. Application (4)
    • 3. Family Member Specifications (4)
    • 5. CPU (6)
    • 6. RAM (Random-access memory) (6)
    • 7. ROM (Read Only Memory) (6)
    • 8. SBUF resister (7)
    • 9. Timers and Counters (7)
    • 10. Interrupts (7)
    • 11. Oscillator (8)
    • 12. I/O Ports (8)
    • 13. Serial Communication port (8)
  • III. PROGRAMMING COMPUTER COMMUNICATION VIA SERIAL PORT (9)
    • 1. Serial communication (9)
    • 2. Tranmission modes: simplex, half-duplex and full-duplex (9)
    • 3. Bit rate and Baud rate and UART (11)
    • 4. Parity bit, stop bit and data bits (13)
    • 5. RS232 and DB-9 Serial Port (13)
    • 6. Use C# with .NET Framework and create Windows Application (13)
  • IV. CIRCUIT DESIGN (15)
    • 1. Block diagram (15)
    • 2. C# with .NET Framework to create Window Application (15)
    • 3. KeilC to create Hex file to add to 8051 (18)
    • 4. Communicate with EEPROM to stored data (26)
      • 4.1. SPI interface (26)
    • 5. Simulation by using Proteus ISIS (26)
  • V. CONCLUSION (29)
  • VI. REFERENCES (30)

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

INTRODUCTION

This project aims to enable the 8051 microcontroller to interpret input data received via the COM serial port By utilizing C# programming language, we facilitate seamless communication between the microcontroller and external devices The 8051’s integrated UART (Universal Asynchronous Receiver/Transmitter) simplifies data exchange by allowing easy reading and writing of serial data without complex I/O operations This powerful feature eliminates the need to manually toggle individual I/O lines, ensuring accurate data transmission through start bits, stop bits, and parity bits, thus enhancing communication efficiency and reliability.

INTRODUCTION TO 8051

Application

The 8051 microcontroller has numerous important applications across various industries It plays a crucial role in power management due to its efficient measurement system that significantly helps conserve energy Additionally, the 8051 is integrated into modern touch screen devices, making it suitable for applications in mobile phones, music players, and gaming systems In the automotive sector, the 8051 microcontroller is widely used for hybrid vehicle management, cruise control systems, and anti-lock braking systems, showcasing its versatility and importance in advanced vehicle technologies.

Family Member Specifications

Find below, the specifications for various popular 8051 family members developed by mentioned semiconductor companies.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

All 8051 microcontrollers have unique architecture as shown in the figure, which consists of functional blocks to build 8051 powerful controlling machines.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

CPU

Microcontroller 8051 has a central processing unit which is also called ALU (Arithmetic Logic Unit) which performs all arithmetic and logical operation.

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).

ROM (Read Only Memory)

• In 8051, 4KB ROM is available for program storage.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

• 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 large applications.

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

SBUF resister

SBUF register is an 8-bit register used for serial communication in 8051 microcontrollers.

The Serial Buffer or SBUF register is used to hold the serial data while transmisson or reception.

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.

The 8051 microcontroller receives data serially through the SBUF register, where bytes are received via the receiver The microcontroller then de-frames the data by removing the start and stop bits, converting the serial data into a complete byte Finally, the processed byte is stored back in the SBUF register for further use, enabling effective serial communication.

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

• TMOD and TCON registers are used to select mode and control the timer operation

Interrupts

• Interrupts are requested by internal or external peripherals which are masked while unused.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

• 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.

Oscillator

• It is used to provide a clock to the 8051 which decides the speed and baud rate.

• We use crystals of frequency varying from 4MHz to 30 MHz.

Normally we use 11.0592 MHz frequency which is required for 9600 baud rate in serial communication.

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 (A8-A15)

• P3 I/O pins also have dual functions as,

• 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.

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

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

PROGRAMMING COMPUTER COMMUNICATION VIA SERIAL PORT

Serial communication

- Serial communication is a communication method that uses one or two transmission lines to send and receive data, and that data is continuously sent and received one bit at a time.

- 8051 has two serial communication pins TXD and RXD used for transmitting and receive data serially.

Tranmission modes: simplex, half-duplex and full-duplex

Simplex mode enables unidirectional communication, similar to a one-way street, where only one device transmits while the other receives This mode allows the entire channel capacity to be utilized for sending data in a single direction, making it ideal for applications that require unidirectional data flow.

- Full-duplex communication: A method where send and receive both have their own transmission line so data can be simultaneously sent and received.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

- 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 Full- duplex communication mode to receive, transmit data at the same time.

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.

• 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 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.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

502 Bad GatewayUnable to reach the origin service The service may be down or it may not be responding to traffic from cloudflared

Parity bit, stop bit and data bits

502 Bad GatewayUnable to reach the origin service The service may be down or it may not be responding to traffic from cloudflared

Parity bit: a parity bit of "1" or "0" is added to the data so as to make the number of

502 Bad GatewayUnable to reach the origin service The service may be down or it may not be responding to traffic from cloudflared

502 Bad GatewayUnable to reach the origin service The service may be down or it may not be responding to traffic from cloudflared

RS232 and DB-9 Serial Port

RS232 is a standard serial communication protocol used to connect computers with peripheral devices, enabling reliable serial data exchange It operates by utilizing voltage levels to facilitate data transfer between connected devices Suitable for communication over distances up to 50 feet, RS232 supports data transfer rates of approximately 1.492 kbps According to EIA standards, RS232 is specifically designed for connecting Data Transmission Equipment (DTE) and Data Communication Equipment (DCE), making it a widely adopted interface in serial communication systems.

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.

Use C# with NET Framework and create Windows Application

To enable data transmission and reception between a general-purpose computer (such as a PC or laptop) and an on-chip microcontroller (like the 8051 MCU), it is essential to have the NET Framework (version 2.0 or higher) installed on your computer This ensures proper communication and compatibility between the devices, facilitating seamless data exchange for embedded system development and testing.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

The NET Framework provides essential SerialPort properties through the System.IO.Ports namespace, which includes multiple layers with SerialPort being the most significant This class offers comprehensive tools to control key communication parameters such as baud rate, stop bits, and other vital settings, facilitating effective serial port management in various applications.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

CIRCUIT DESIGN

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.

Using C# with the NET Framework is the most efficient method for Windows OS to develop UART communication applications The NET Framework, a proprietary software platform by Microsoft, runs primarily on Windows and provides a fast and reliable way to establish serial connections, configure communication settings, and send or receive data via the UART protocol.

This is the result of Windows form application and code:

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

This article provides a comprehensive guide on creating a Windows Forms application using C# It covers essential steps, including importing necessary namespaces such as System, System.Windows.Forms, and System.Threading, which are vital for developing robust WinForms applications The code example demonstrates how to initialize and set up a basic form within the namespace WindowsFormsApp2, emphasizing the significance of defining a partial class that inherits from Form By following these instructions, developers can efficiently build responsive and user-friendly Windows desktop applications using C#.

} private void Form1_Load(object sender, EventArgs e) { try { serialPort1.Open();

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

} } private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{ if (serialPort1.IsOpen) { serialPort1.Close();

To send data through a serial port in C#, you can read the input from a textbox, append a delimiter, and then transmit each character individually First, retrieve the user input from the textbox and determine its length Then, append a "#" character to mark the end of the message Next, iterate through each character of the modified string and write it to the serial port using a loop, ensuring reliable data transmission This approach enables efficient and organized communication over a serial connection.

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

} } private void button2_Click(object sender, EventArgs e) { serialPort1.Write("@");

} private void button3_Click(object sender, EventArgs e) { serialPort1.Write("A");

} private void button4_Click(object sender, EventArgs e) { serialPort1.Write("B");

} private void label1_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):

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

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.

The provided code snippet sets up the SPI communication pins using sbit declarations for SPI_SCLK, SPI_MOSI, SPI_MISO, and SPI_CS on port P2, enabling proper data transmission between devices It also defines control pins for an LCD display, including rs, rw, and en on port P3, which are essential for managing data and command signals Function prototypes for lcdcmd and lcddat are declared to facilitate sending commands and data to the LCD Additionally, the code declares variables for data handling and loop control, such as unsigned char c and unsigned int i, j, supporting efficient execution of LCD operations and SPI communication in embedded system applications.

This article introduces key functions for implementing software-based SPI communication and EEPROM management in embedded systems The `Soft_SPI_Init()` function initializes the software SPI interface, ensuring proper communication setup The `Soft_SPI_Write()` and `Soft_SPI_Read()` functions facilitate data transmission and reception over SPI, enabling efficient data exchange with peripherals EEPROM operations are handled through functions like `EEPROM_25LCxxx_Write()`, which writes data to specific memory addresses, and `EEPROM_25LCxxx_Read()`, which reads data from the EEPROM Additionally, `EEPROM_25LCxxx_WIP()` checks the write-in-progress status, ensuring data integrity during EEPROM operations The `main()` function orchestrates these processes, providing the core logic for embedded system applications involving SPI communication and EEPROM management.

{ i = 1; lcdcmd(0x38); //USE 2 LINE, EACH CELL IS A MAXTRIX 5X7

Delay_ms(5); lcdcmd(0x10); //DISPLAY, CURSOR OFF Delay_ms(5); lcdcmd(0x0c); //DISPLAY, CURSOR OFF lcdcmd(0x01); //CLEAR SCREEN

Delay_ms(5); lcdcmd(0x81); //DISPLAY, CURSOR OFF Delay_ms(5);

/ Khoi tao giao tiep SPI

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

P1=0x00; c=""; for (i=0;i>8); // Gui byte cao cua dia chi

Soft_SPI_Write(add & 0x00FF); // Gui byte thap cua dia chi

SPI_CS = 1; while(EEPROM_25LCxxx_WIP());

} unsigned char EEPROM_25LCxxx_Read(unsigned int add) { unsigned char b;

Soft_SPI_Write(0x03); // Gui lenh doc

Soft_SPI_Write(add>>8); // Gui byte cao cua dia chi

Soft_SPI_Write(add & 0x00FF); // Gui byte thap cua dia chi b = Soft_SPI_Read();

} unsigned char EEPROM_25LCxxx_WIP()

TIEU LUAN MOI download : skknchat123@gmail.com moi nhat

Soft_SPI_Write(0x05); // Gui lenh doc thanh ghi trang thai result = Soft_SPI_Read();

} unsigned char Soft_SPI_Read(void) {

/ software SPI read, MSB read first unsigned char i, b; for(i=0;i

Ngày đăng: 08/11/2022, 16:02

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN

w