Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 27 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
27
Dung lượng
0,97 MB
Nội dung
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 LIST OF STUDENT Phan Minh Nhật Trần Tuấn Kiệt Phạm Trung Hiếu Ngô Ngọc Duy 20119147 20119009 20119001 20119120 REPORT OF LECTURER TABLE OF CONTENTS I INTRODUCTION II INTRODUCTION TO 8051 1 Introduction Application Family Member Specifications 8051 Architecture & Pin diagram CPU RAM (Random-access memory) ROM (Read Only Memory) SBUF resister Timers and Counters 10 Interrupts 11 Oscillator 12 I/O Ports 13 Serial Communication port III PROGRAMMING COMPUTER COMMUNICATION VIA SERIAL PORT Serial communication Tranmission modes: simplex, half-duplex and full-duplex Bit rate and Baud rate and UART Parity bit, stop bit and data bits RS232 and DB-9 Serial Port Use C# with NET Framework and create Windows Application IV CIRCUIT DESIGN 10 Block diagram 10 C# with NET Framework to create Window Application 10 KeilC to create Hex file to add to 8051 13 Communicate with EEPROM to stored data 20 4.1 SPI interface 20 Simulation by using Proteus ISIS 20 V CONCLUSION 23 VI REFERENCES 24 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 II INTRODUCTION TO 8051 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-OxideSemiconductor (CMOS) technology which consumes less power than NMOS and made it better compatible for battery-powered applications 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 antibraking systems are other areas where it has great use Family Member Specifications Find below, the specifications for various popular 8051 family members developed by mentioned semiconductor companies 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 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 • 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 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 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 • 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 11 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 12 I/O Ports • 8051 has four Input/output port P0, P1, P2, P3 • Each port is 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 (A8A15) • 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 • P3.3 – INT1 – External Interrupt • P3.4 – T0 – Clock input for counter • P3.5 – T1 – Clock input for counter • 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 III 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 - 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 - 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 Fullduplex 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 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 • 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 as the timer will overflow after every cycles • we can achieve different baud rates by putting the division factor in the TH1 register IV CIRCUIT DESIGN Block diagram 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: 10 C# code to make Winform using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsFormsApp2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { serialPort1.Open(); } catch { MessageBox.Show("Can't open port"); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); }; } private void button1_Click(object sender, EventArgs e) { 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 Thread.Sleep(300); } } 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): 12 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 #include "main.h" #include " \lib\Delay.h" #include sbit SPI_SCLK = P2^0; sbit SPI_MOSI = P2^1; sbit SPI_MISO = P2^2; sbit SPI_CS = P2^3; sbit rs=P3^5; sbit rw=P3^4; sbit en=P3^3; void lcdcmd(unsigned char); void lcddat(unsigned char); unsigned char c; unsigned int i; unsigned int j; 13 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(); void main() { i = 1; lcdcmd(0x38); //USE 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 Soft_SPI_Init(); SCON = 0x50; TMOD = 0x20; TH1 = 0xFD; TR1 = 1; 14 TI = 1; P1=0x00; c=""; for (i=0;i>8); cao cua dia chi // Gui byte Soft_SPI_Write(add & 0x00FF); dia chi // Gui byte thap cua Soft_SPI_Write(b); SPI_CS = 1; while(EEPROM_25LCxxx_WIP()); } unsigned char EEPROM_25LCxxx_Read(unsigned int add) { unsigned char b; SPI_CS = 0; Soft_SPI_Write(0x03); // Gui lenh doc Soft_SPI_Write(add>>8); cao cua dia chi Soft_SPI_Write(add & 0x00FF); dia chi b = Soft_SPI_Read(); SPI_CS = 1; return b; } unsigned char EEPROM_25LCxxx_WIP() 17 // Gui byte // Gui byte thap cua { unsigned char result; SPI_CS = 0; Soft_SPI_Write(0x05); ghi trang thai // Gui lenh doc result = Soft_SPI_Read(); SPI_CS = 1; return result&0x01; } void Soft_SPI_Init() { SPI_CS = 1; SPI_SCLK = 0; SPI_MISO = 1; } unsigned char Soft_SPI_Read(void) { // software SPI read, MSB read first unsigned char i, b; for(i=0;i