Microcontroller C language in application design

Publisher:神光骑士Latest update time:2018-03-11 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    1 Introduction

    In many practical applications such as tunnel kiln temperature control systems and self-service lockers, we use the high-level C language of single-chip microcomputers to compile application programs to meet users' different requirements for various performance parameters. The following example introduces the advantages of C language in application design.

    2 System Structure

    The system consists of two parts: electrical and mechanical. The mechanical part consists of locker boxes and electric locks; the electrical part consists of a single-chip microcomputer, control relays, operation panel, and working status indicators. The system structure is shown in Figure 1.

 

   9.jpg

    3. System Function Programming

    The system program consists of the following parts: main program, access program, monitoring management program, control output program, cabinet box sorting program, keyboard interrupt, clock timing interrupt and alarm program. The following mainly introduces the box sorting program, access program and password duplication prevention program. The software design adopts a modular structure design method, and all programs are written in the single-chip high-level C language.

    The main program completes the initialization of each peripheral interface, then enters the keyboard sampling and display program, and the clock interrupt program completes the display scan update and sampling processing. The following is the box position number initialization subroutine.

    10.jpg

The jICunnum [] variable     in the above program is used to save the box position number of the box position. Initially, jicunnum[i]=i (i=1, 2, ..., 32), that is, all 32 boxes are set to 1, 2, ..., 32 respectively, so as to facilitate the box position status indication and the output control of the receiving relay to complete the locking control of the cabinet box position, because the opening of the box position electric control lock is operated according to the box position number corresponding to the box position. For example, the initial number corresponding to box position 5 is jicunnum[5]=5, and the others are similar.

    When storing items, if there is an empty space in the box, the system will automatically generate a 12-digit password at random and save it. The password will also be printed out and the customer can carry it with him/her. The password random generation procedure is as follows:
  void Take(){
  1.qq:Mimac=ReadKey();
  2.if(Mimac!=0x11){
  3.if(Mimacount<numset){
  4.Mimac--;Mimac=c&0x00f;
  5.display[Mimacount]=Mimac;
  6.Mimatemp*=10;
  7.Mimatemp+=Mimac;
  8.Mimacount++;goto qq;}
  9.goto endt;}
  10.for(j=0;j<count;j++){
  11.if(jicun[j]=Mimatemp){
  12.orinum=j;}}
  13.endt:return();}

    The third line rand() in the above program is a random number generation subroutine. Lines 2 to 4 in the program automatically generate a 12-digit password at random and save it in decimal form in the variable Mimanew.

    In order to prevent the password generated this time from being repeated with the password generated on the same day, a discrimination procedure is set in the program. Lines 5 to 7 of the program compare the randomly generated 12-digit password with other passwords that have been generated before, where the jiold[] variable is used to store the password generated on the same day, and the totalcount variable is the number of passwords that have been generated.

 

    If the comparison result is the same, it means that the newly generated password is repeated, the program returns, and a new 12-digit password is generated. If it is different, it means that the password is not repeated, and the new password is saved in the password variable j IC un[], and a new box is automatically opened, and the corresponding box position indicator is on. Lines 8 to 11 adjust the password count pointer (count) and the password count pointer (totalcount) generated on the day.

    When the locker is full, that is, the count is greater than 32, the working status indicates that the locker is full, reminding subsequent customers not to continue operating in the locker.

    When taking out items, enter the 12-digit password through the operation panel. The 12-digit password data is stored in the variable display[i] (i=1, 2, ..., 12) unit, and the display program displays the value in display[] on the digital tube . The control system compares the password entered this time with the 12-digit password originally stored in the variable jicun[]. If they are the same, open the door lock corresponding to the original storage position of the item and turn off the corresponding position occupancy indicator light; otherwise, do not perform any operation. The procedure for taking out items is as follows:
  void Take(){
  1.qq:Mimac=ReadKey();
  2.if(Mimac!=0x11){
  3.if(Mimacount<numset){
  4.Mimac--;Mimac=c&0x00f;
  5.display[Mimacount]=Mimac;
  6.Mimatemp*=10;
  7.Mimatemp+=Mimac;
  8.Mimacount++;goto qq;}
  9.goto endt;}
  10.for(j=0;j<count;j++){
  11.if(jicun[j]=Mimatemp){
  12.orinum=j;}}
  13.endt:return();}

    The first line of the program ReadKey() is a keyboard key reading subroutine. Lines 2 to 9 wait for the 12-digit password to be entered on the operation panel and temporarily save the entered password in decimal form in the variable Mimatemp. The second line of the program is used to determine whether to exit the key. Lines 10 to 12 determine whether the entered 12-digit password is the same as the original password of a memory variable jicun[]. If they are the same, open the door lock of the box where the items were originally stored; otherwise, do not perform any operation. Line 3 Mimacount is used to calculate the position of the number of password inputs, line 4 Mimac is used to save a single digit of the password, and line 12 orinum is used to save the box number corresponding to the password. numset is the number of digits of the password initially set, here, numset=12.

    To prevent malicious operations, when the password does not match, you can enter it repeatedly three times, and then set the prohibited operation flag, automatically lock and delay for a period of time (for example, 3 minutes), and then allow operations; reset the set prohibited operation flag in the clock timing interrupt program to resume normal operation.

    As mentioned above, the jicunnum[] variable is used to store the bin number of the bin, and the initial state is jicunnum[i]=i (i=1, 2, ..., 32). However, after a period of operation, the original state of jicunnum[] has changed. If the 7th customer takes the item now, the memory of jicunnum[7] is no longer 7. In order to realize the bin status indication and receive the relay control output, it is necessary to ensure that the operation password is consistent with its bin number. After each item is taken out, the bin number variable must be reordered. The following is the bin sorting program.
  void sort(){
  1.count--;
  2.tempnum=jicunnum[orinum];
  3.for(i=orinum; i<count; i++){
  4.jicun[i]=jicun[i+1];
  5.jicunnum[i]=jicunnum[i+1]}
  6.jicunnum[count]=tempnum;
  7.return();}

    The first line in the program is an operation to decrement the counter by 1, indicating that the item has been taken away and a slot has been vacated. The second line saves the slot number corresponding to the password. Lines 3 to 5 move the slot number (jicunnum[]) and the corresponding password (jicun[]) behind the empty slot forward one by one until all the slots i=count that have been stored have been moved forward one by one.

    Line 6 of the program puts the bin number of the items that have been taken out into the last memory variable jicunnum[count].

    After the slot sorting program re-sorts the items, the slot number of the 7th customer, jicunnum[7], now stores the slot number after the empty slot at that time. The variables count, orinum, and jicun[] in the program are defined the same as before.

    During the process of storing or retrieving items, in order to let users know the working status of the system, such as: storing or retrieving items, whether the cabinet locations are full or empty, which locations are empty, etc., the working status indicator light reminds customers.

    In order to facilitate operation and management, a monitoring management program is set up in the program. In case of special circumstances, when any box needs to be opened at any time, the system operator has the right to complete the operation. Different management authority passwords are set, and only system administrators can enter the operation; the operation process is the same as when taking out items.

    References
[1] Xu Aijun, Peng Xiuhua. Single-chip high-level language C51 application design [M]. Beijing: Electronic Industry Press, 1999.


Keywords:MCU Reference address:Microcontroller C language in application design

Previous article:A C language program for a tracking car
Next article:Taxi meter VHDL program

Recommended ReadingLatest update time:2024-11-16 13:48

51 MCU IIC & EEPROM driver
#include reg52.h #include "./delay/delay.h" sbit SCL = P2^0; sbit SDA = P2^1; bit ack = 0;   unsigned char flag = 1; #define LCDPORT P0 #define LCD_WRITE_DATA 1 #define LCD_WRITE_COM 0 sbit RS = P2^4; sbit RW = P2^5; sbit E = P2^6;   #define SUCC 0 #define ERR 1   void iic_start() { SDA = 1; //Operate SDA first, th
[Microcontroller]
Research on Photoelectric Detection Based on Single Chip Microcomputer
1 Introduction In the late 1950s, with the rapid development of material technology, semiconductor technology, laser technology, microelectronics technology and optical technology, the development of optoelectronic technology was greatly promoted, making this technology widely concerned. In particular, it has b
[Microcontroller]
Research on Photoelectric Detection Based on Single Chip Microcomputer
Instruction sets that must be understood when developing PIC microcontrollers
  PIC microcontrollers have computing functions and memory like CPUs and are controlled by software. However, the processing power is average and the memory capacity is also limited, depending on the type of PIC. But their highest operating frequency is about 20MHz, and the memory capacity is about 1K-4K bytes for wri
[Microcontroller]
PIC microcontroller lights up a lamp experiment
If you only know C51, you are OUT. Now companies also require you to know AVR, PIC and other microcontrollers, so I am starting to learn PIC now. It's so simple, so easy! This is the first PIC microcontroller program I wrote. The purpose is to light up a light.   #include pic.h //#define uchar __CONFIG(0x3B31);
[Microcontroller]
The difference between ISP, IAP and ICP programming methods of MCU
The difference between ISP, IAP and ICP programming methods of MCU Anyone who plays with microcontrollers should have heard of these words. But I have never been clear about the difference between them. After checking the information today, I summarized them as follows. ISP: In System Programing IAP: In applicat
[Microcontroller]
A simple method to call assembly subroutine in C language of 51 single chip microcomputer
1. In the assembly file, just add the following three sentences before the program: PUBLIC _delay, _binrlc ; define the public subroutine name, here two subroutines are defined (underlined) LUOYUAN SEGMENT CODE ; The program segment name is LUOYUAN, you can call it anything RSEG LUOYUAN Put the subroutine below (the
[Microcontroller]
Simulation and production of music player based on single chip microcomputer
Abstract: In order to solve the shortcomings of dedicated music player chips that can only be burned once and the content of the playback cannot be changed, this paper gives a method of using a single-chip microcomputer to design a music player, and at the same time, using Proteus software to simulate the music play
[Microcontroller]
Simulation and production of music player based on 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号