Introduction to MCU C language

Tearful blue shirt 2021-02-23 17:30:11
introduction mcu language



Learn a programming language , The most important thing is to create a practice environment , Only by learning and practicing can we learn well .Keil Software is currently the most popular development 80C51 Series of MCU software ,Keil Provided include C compiler 、 Macro assembly 、 The connector 、 Library management and a powerful simulation debugger, etc , Through an integrated development environment (?Vision) Put these parts together .

Please install before learning KEILC51 Software , After learning to use assembly language , Study C Language programming is a relatively easy thing , We will introduce... Through a series of examples C Language programming methods . chart 1-1 The circuit diagram shown uses 89c51 Single chip microcomputer as the main chip , This property belongs to 80C51 series , It has 8K Of FLASH ROM, It can be repeated , It's perfect for experiments .89c51 Of P1 Pin on 8 A light-emitting diode ,P3.2~P3.4 Pin on 4 A button switch , Our task is to get the receiver to P1 The LED on the pin lights up as required .

 Single chip microcomputer C Language introduction tutorial


1 ordinary C Program is introduced

example 1-1: Let's connect P1.0 On the pin LED Luminescence .

/************************************************

Single light flashing program

*************************************************/

#include "reg51.h"// This sentence will 51 Common ports for , The definition files of internal registers and so on are included in this program

sbit P1_0=P1^0;

void main()

{ P1_1=0;

}

The purpose of this program is to connect P1.0 On the pin LED Lighten up . Let's analyze this C What information does a language program contain .

1)" File contains " Handle .

The first line of the program is a " File contains " Handle .

So-called " File contains " It means that one file includes all the contents of another file , So the program here is just 4 That's ok , but C The compiler processes dozens or hundreds of lines . Here the program contains REG51.h The purpose of the document is to use P1 This symbol , Notice C compiler , What's written in the program P1 Refer to 80C51 Single chip microcomputer P1 Ports, not other variables . How can this be done ?

open reg51.h You can see something like this :

/*--------------------------------------------------------------------REG51.H

Header file for generic 80C51 and 80C31 microcontroller.

Copyright (c) 1988-2001 Keil Elektronik GmbH and Keil Software, Inc.

All rights reserved.

--------------------------------------------------------------------------*/

/* BYTE Register */

sfr P0 = 0x80;

sfr P1 = 0x90;

sfr P2 = 0xA0;

sfr P3 = 0xB0;

sfr PSW = 0xD0;

sfr ACC = 0xE0;

sfr B = 0xF0;

sfr SP = 0x81;

sfr DPL = 0x82;

sfr DPH = 0x83;

sfr PCON = 0x87;

sfr TCON = 0x88;

sfr TMOD = 0x89;

sfr TL0 = 0x8A;

sfr TL1 = 0x8B;

sfr TH0 = 0x8C;

sfr TH1 = 0x8D;

sfr IE = 0xA8;

sfr IP = 0xB8;

sfr SCON = 0x98;

sfr SBUF = 0x99;

/* BIT Register */

/* PSW */

sbit CY = 0xD7;

sbit AC = 0xD6;

sbit F0 = 0xD5;

sbit RS1 = 0xD4;

sbit RS0 = 0xD3;

sbit OV = 0xD2;

sbit P = 0xD0;

/* TCON */

sbit TF1 = 0x8F;

sbit TR1 = 0x8E;

sbit TF0 = 0x8D;

sbit TR0 = 0x8C;

sbit IE1 = 0x8B;

sbit IT1 = 0x8A;

sbit IE0 = 0x89;

sbit IT0 = 0x88;

/* IE */

sbit EA = 0xAF;

sbit ES = 0xAC;

sbit ET1 = 0xAB;

sbit EX1 = 0xAA;

sbit ET0 = 0xA9;

sbit EX0 = 0xA8;

/* IP */

sbit PS = 0xBC;

sbit PT1 = 0xBB;

sbit PX1 = 0xBA;

sbit PT0 = 0xB9;

sbit PX0 = 0xB8;

/* P3 */

sbit RD = 0xB7;

sbit WR = 0xB6;

sbit T1 = 0xB5;

sbit T0 = 0xB4;

sbit INT1 = 0xB3;

sbit INT0 = 0xB2;

sbit TXD = 0xB1;

sbit RXD = 0xB0;

/* SCON */

sbit SM0 = 0x9F;

sbit SM1 = 0x9E;

sbit SM2 = 0x9D;

sbit REN = 0x9C;

sbit TB8 = 0x9B;

sbit RB8 = 0x9A;

sbit TI = 0x99;

sbit RI = 0x98;

be familiar with 80C51 Readers of the internal structure can see that , Here are some definitions of symbols , That is to say, the corresponding relationship between symbol name and address is stipulated . Notice that there are

sfr P1 = 0x90;

Such a line ( It is shown in bold above ), By defining P1 And address 0x90 Corresponding ,P1 My address is 0x90(0x90 yes C How to write hexadecimal numbers in a language , It is equivalent to writing in assembly language 90H).

You can also see a word that appears frequently here :sfr

sfr And standards C Keywords of language , It is Keil In order to have direct access to 80C51 Medium SFR And a new keyword , Its usage is :

sfrt Variable name = Address values .

 Single chip microcomputer C Language introduction tutorial


2) Symbol P1_0 To express P1.0 Pin .

stay C In language , If you write directly P1.0,C The compiler doesn't recognize , and P1.0 It's not a legal C Language variable name , So we have to give it a different name , It's called P1_0, But P1_0 Is it P1.0 Well ? You think so ,C The compiler doesn't think so , So they have to be connected , It's used here Keil C Key words of sbit To define ,sbit There are three ways to use :

The first method :sbit Bit variable name = Address values

The second method :sbit Bit variable name =SFR name ^ Variable bit address value

The third method :sbit Bit variable name =SFR Address values ^ Variable bit address value

As defined PSW Medium OV There are three ways :

sbit OV=0xd2 (1) explain :0xd2 yes OV Bit address value of

sbit OV=PSW^2 (2) explain : among PSW Must use first sfr Well defined

sbit OV=0xD0^2 (3) explain :0xD0 Namely PSW Address value of

So here we use sfr P1_0=P1^0; It's defined by symbols P1_0 To express P1.0 Pin , You can get up if you want P10 A kind of name , As long as the following program is also changed .

3)main be called " The main function ".

every last C Language programs have and only have one main function , Tangent must have a main function , It is placed in a position that does not require , You can put it at the end of the program ( recommend ), There must be a pair of braces after the function "{}", Write other programs in braces .

From the above analysis, we understand part of C Characteristics of language , Let's take a more complicated example .

example 1-2 Let's connect P1.0 On the pin LED Twinkle

/*************************************************

Single light flashing program

*************************************************/

#include "reg51.h"

#define uchar unsigned char

#define uint unsigned int

sbit P10=P1^0;

/* Delay program

from Delay Parameters determine the delay time

*/

void mDelay(unsigned int Delay)

{ unsigned int i;

for(;Delay>0;Delay--)

{ for(i=0;i<124;i++)

{;}

}

}

void main()

{ for(;;)

{ P10=!P10; // Take the opposite P1.0 Pin

mDelay(1000);

}

}

Program analysis : The main program main Let's not look at the first line in , The second line is "P1_0=!P1_0;", stay P1_0 There's a sign in front of it "!", Symbol "!" yes C A language operator , It's like in mathematics "+"、"-" equally , It's a kind of operation , Meaning is " Take the opposite ", That is to say, reverse the value of the variable after the symbol .

 Single chip microcomputer C Language introduction tutorial


Be careful : Negation is only for the value of a variable , It doesn't automatically change the variable itself . It can be said that C The compiler is dealing with "!P1_0" when , take P1_0 The value of gives a temporary variable , And then reverse the temporary variable , Not directly to P1_0 Take the opposite , Therefore, the assignment symbol should be used after negation ("=") Assign the negated value to P1_0, such , If the original P1.0 Is a low level (LED bright ), So after the reversal ,P1.0 It's high level (LED destroy ), conversely , If P1.0 Is a high level , After reverse ,P1.0 It's low level , This command is executed over and over again , Connect to P1.0 When you put on the light, it keeps going " bright "、" destroy ".

The key to this instruction being repeated is main The first line of the program in :for(;;), This is not detailed here , For the time being, readers just need to know , This line of program is followed by a pair of braces "{}" Constitutes an infinite loop statement , The statement in the braces is executed repeatedly .

The third line is :"mDelay(1000);", The purpose of this line of programs is to delay 1s Time , Because the speed of single chip microcomputer executing instructions is very fast , If there is no delay , The light goes out as soon as it comes on , It's on right after it's off , Too fast , The human eye can't tell .

here mDelay(1000) Not by Keil C Library functions provided , That is, you can't write such a program to achieve delay under any circumstances . If you write this line when you write other programs , You'll find that compilation doesn't work . So why is it right here ? observe , You can see that there are void mDelay(…) This line of business , so ,mDelay The word is our own name , And wrote some program lines for this , If you don't have such a program line in your program , Then you can't use mDelay(1000) 了 . Some people have quick brains , It may come to mind right away , Can I copy this program to my other programs as well , And then you can use it mDelay(1000) What about it ? The answer is , Of course, that's OK . One more point needs to be made ,mDelay The name is given by the programmer himself , You can change it yourself , But once you change the name ,main() The name of the function needs to be changed accordingly .

mDelay There's a parenthesis behind it , There's data in parentheses (1000), This 1000 It's called " Parameters ", It can be used to adjust the delay time in a certain range , Here we use 1000 To ask for a delay time of 1000 millisecond , Do that , It has to be written by ourselves mDelay It was decided by that procedure .

版权声明
本文为[Tearful blue shirt]所创,转载请带上原文链接,感谢
https://fheadline.com/2021/02/20210223172414575Y.html

  1. Mi's projector 72 hours experience, you should be most concerned about these six issues
  2. Kwai computer hosts are not selling cheap.
  3. What are people searching for this year? Google search hot list, "new coronavirus" tops the list
  4. Fantasy fiction ranking: Douluo mainland fourth, Futian third, who is the most popular fantasy in September?
  5. 活动运营:满足这4点,没有什么活动做不好
  6. 一文看懂活动运营
  7. Netizen's "trader" reveals secrets: it's normal for a netizen to make 10000 yuan a month in three months' packaging
  8. What is constipation? How does child constipation do? It's not just fruits and vegetables
  9. Teach you to shake the voice of the 3 ways to increase the powder, 3 days to achieve the tiktok thousand!
  10. 新手怎么做电商运营?初入电商行业需要注意什么?
  11. 如何快速搞清用户运营,不用苦逼找流量?
  12. Tiktok international version of the little sister! How to create your own U.S. app store account
  13. Four essential methods for content operators, so that you don't worry about KPI any more
  14. 2021年春节假期延长至2月27日?回应来了
  15. Activity operation: to meet these four points, there is no activity that cannot be done well
  16. Understand the operation of activities
  17. How do novices do e-commerce operations? What do you need to pay attention to when you first enter the e-commerce industry?
  18. How to quickly understand the user operation, without having to force to find traffic?
  19. Will the Spring Festival holiday be extended to February 27 in 2021? Here comes the response
  20. 百家号蓝v认证会额外给企业号增加哪些功能权益?
  21. What additional functional rights and interests will Baijia blue V certification add to enterprise number?
  22. 百家号蓝v认证所需资料及认证流程?
  23. What information and certification process do you need for Baijia blue V certification?
  24. 百家号认证费用是多少,有哪些需要的资料尼?
  25. How much is the fee of hundred family number certification? What information do you need?
  26. 孩子上网校=玩手机?哪家网校环境好?我亲自体验了学而思等4家
  27. Children online school = play mobile phone? Which online school environment is good? I personally experienced four companies including XRS
  28. 支付宝和微信怎么办?华为、银联联手反击了
  29. 2020年网络十大热词 不懂你就OUT了
  30. 这么简单的PPT目录页,随便一做就是好几页
  31. 忠言逆耳,2020年苹果手机别瞎买,这3款iPhone最值得考虑
  32. What about Alipay and WeChat? Huawei and UnionPay joined hands to fight back
  33. In 2020, the top ten hot words on the Internet will be out if you don't understand them
  34. Such a simple ppt directory page, just do a few pages
  35. Don't buy Apple's mobile phones in 2020. These three iPhones are worth considering
  36. 百家号企业蓝v认证教程?
  37. Office 2021预览版发布,无需订阅即可使用
  38. 一大波新手机在路上,旧手机怎么办?用这22种方式改造它
  39. Hundred enterprises blue V certification course?
  40. Preview version of office 2021 is published and can be used without subscription
  41. A wave of new mobile phones are on the way. What about old ones? Transform it in 22 ways
  42. 名创优品上市12名员工身家超亿元 将预留总股数5%激励更多年轻人
  43. 12 employees with assets of over 100 million yuan will reserve 5% of the total shares to encourage more young people
  44. 2021年百度流量扶持企业百家号,如何抓住流量红
  45. 全球最具价值品牌50强
  46. 如果你用的是华为手机,花1分钟关掉这5个设置,再用2年都不卡
  47. 品牌如何使用工具监控舆情
  48. 2020教育品牌影响力舆情报告发布,多维呈现教育行业变革
  49. In 2021, Baidu's traffic will support enterprise Baijia. How to catch the traffic red
  50. Top 50 most valuable brands in the world
  51. If you are using a Huawei mobile phone, take one minute to turn off these five settings, and then use them for two years
  52. How brands use tools to monitor public opinion
  53. 2020 education brand influence public opinion report released, multi dimensional presentation of education industry reform
  54. 四川华西集团有限公司原党委委员、董事、副总经理杨硕被开除党籍和公职
  55. 什么是问答营销?问答营销的成功案例有那些?
  56. 买苹果还是选华为?手机店老板的回答一针见血,说到我心坎了
  57. Yang Shuo, former member of the Party committee, director and deputy general manager of Sichuan Huaxi Group Co., Ltd., was expelled from the party and public office
  58. What is question and answer marketing? What are the successful cases of Q & a marketing?
  59. Apple or Huawei? The answer from the owner of the mobile phone shop is to the point, and it's in my heart
  60. 一个赞多少钱?那些“刷量”买来的繁荣