MCU C language programming: MCU and PC communication

Publisher:静默思考Latest update time:2013-10-29 Source: 21icKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/* Name: MCU and PC communication

Description: The microcontroller can receive the PC

Send digital characters, press the microcontroller

After pressing K1, the microcontroller can send

String. Completed in Proteus environment

For this experiment, you need to install Virtual

Serial Port Driver and Serial Port Debug Assistant

This example buffers 100 numeric characters.

When the buffer is full, new numbers are stored from the front (ring buffer).

*/

#include

#define uchar unsigned char

#define uint unsigned int

uchar Receive_Buffer[101]; //Receive buffer

 


uchar Buf_Index=0;

//Digital tube encoding

// Buffer space index

 


uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};

//Delay

void DelayMS(uint ms)

{

uchar i;

while(ms--) for(i=0;i<120;i++);

}

//Main program

void main()

{

uchar i;

P0=0x00;

Receive_Buffer[0]=-1;

SCON=0x50; Serial port mode 1, allowing reception

TMOD=0x20; //T1 working mode 2

TH1=0xfd; //Baud rate 9600

TL1=0xfd;

PCON=0x00; baud rate is not doubled

EA=1;EX0=1;IT0=1;

ES=1;IP=0x01;

TR1=1;

while(1)

{

for(i=0;i<100;i++)

{ //Receive -1 to end the display

if(Receive_Buffer[i]==-1) break;

P0=DSY_CODE[Receive_Buffer[i]];

DelayMS(200);

}

DelayMS(200);

}

  }[page]

//Serial port receive interrupt function

void Serial_INT() interrupt 4

{

uchar c;

if(RI==0) return; //Disable serial port interrupt

ES=0; // Clear the receive interrupt flag

RI=0;

c=SBUF;

if(c>=\'0\'&&c<=\'9\')

 

{ // Cache each newly received character and put -1 after it as the end mark

Receive_Buffer[Buf_Index]=c-\'0\';

Receive_Buffer[Buf_Index+1]=-1;

Buf_Index=(Buf_Index+1)%100;

}

ES=1;

}

void EX_INT0() interrupt 0 //External interrupt 0

{

uchar *s="This is the string sent by 8051!rn";

uchar i=0;

while(s[i]!=\'\')

{

SBUF=s[i];

while(TI==0);

TI=0;

i++;

}

}

MCU C language programming: MCU and PC communication

Keywords:MCU Reference address:MCU C language programming: MCU and PC communication

Previous article:Research on intelligent car tracking module based on MSP430 single chip microcomputer
Next article:MCU C language programming: MCU sends a string to the host

Recommended ReadingLatest update time:2024-11-16 21:54

Using low-cost MCU's UART to drive smart cards
The growing need for security and enhanced functionality in the banking, identification and telecommunications markets has increased the use of smart cards worldwide, while at the same time reducing the use of less secure magnetic stripe cards. However, the hardware and firmware development of appropriate communicat
[Microcontroller]
Using low-cost MCU's UART to drive smart cards
Application of C language bit field structure in PIC microcontroller
    When I need to use flag bits in my program, I thought I could define the bits the same way as Keil51, but I kept getting errors. Later, I learned that the MC18 compiler does not support bit definition (the PICC18 compiler does). Here is a solution: bit field structure.     When storing some information, it doe
[Microcontroller]
MCU multi-channel AD acquisition source program (8 channels) STC15w series
12864 is a serial connection. AD0~AD4 is connected to 5V power supply. AD5~AD7 is connected to GND. STC15W408AS comes with 8 channels of 10-bit AD   . The program only uses the upper eight bits. The range is 255. The program contains 10-bit AD acquisition. 1024. The code has been commented. I am a novice. Please corr
[Microcontroller]
MCU multi-channel AD acquisition source program (8 channels) STC15w series
51 MCU IO port expansion - 74HC165 chip parallel to serial experiment
We all know that there are two major types of communication: serial and parallel. The biggest advantage of serial is that it occupies less bus, but the transmission rate is low; parallel is just the opposite, occupying more bus and high transmission rate. There are many such chips on the market, some are serial input
[Microcontroller]
51 MCU IO port expansion - 74HC165 chip parallel to serial experiment
Smart meter design based on LPC1114 microcontroller and ATT7053 chip
ATT7053AU is a single-phase multi-function metering chip (EMU) with SPI. The working range of the chip is 3.0 ~ 3.6V. The crystal oscillator used is 5.5296MHz. 1.att7053 circuit diagram 2.att7053  PCB diagram 3. Finished finished board 4.LPC1114 microcontroller and ATT7053 use SPI communication method. The sof
[Microcontroller]
Smart meter design based on LPC1114 microcontroller and ATT7053 chip
51 MCU register group settings
Everyone knows that the 51 MCU has four groups of registers R0-R7. In the past year, when I was communicating with novice friends, I found that many friends often had problems using the register group. Although this is not a difficult problem, if an error occurs, it will also cause serious consequences.     First, let
[Microcontroller]
Interface Design between LCD Display and PIC Microcontroller
1 Introduction In the design of intelligent instruments, it is usually necessary to display the measurement and control information. Due to the power consumption, volume and other conditions of the instrument, general CRT displays are often not suitable. Although semiconductor digital tubes can display numbers a
[Microcontroller]
Interface Design between LCD Display and PIC Microcontroller
Design of PWM Special Signal Generator Based on AT89S51 Single Chip Microcomputer
1 Introduction As one of the most widely used methods of nondestructive testing, eddy current nondestructive testing has the advantages of simple sensor structure, high sensitivity, large measurement range, no influence from oil and other media, strong anti-interference ability, etc. It has been widely used
[Microcontroller]
Design of PWM Special Signal Generator Based on AT89S51 Single Chip Microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号