Microcontroller C language programming and & or | header files are common

Publisher:吾道明亮Latest update time:2015-02-05 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I. Common Questions
       1. The header files reg51.h and reg52.h are actually the same, and you can use both.
       2. The void before main() can be added or not, anyway, they are all functions without return value.
       3. Not every program needs to use the infinite loop while(1). For example, it is not necessary to light up a small LED light. As long as it is executed once, it will always be at a high level without looping.
       4. Both bit operations and byte operations can be used to control the output level of the I/O port. Bit operations require the declaration of sbit, while byte operations do not. 
       5. There are two types of delay programs: a: loop delay; b: timer precise delay
       6. If the defined variable is not initialized, the default initial value is zero.
       7. When defining an array (such as: number[]={1, 2, 3, 4, 5, 6, };), a semicolon must be added at the end, and each element is separated by a comma. The number of elements can be added or not in [].
       8. When the program is more complex, the program can be modularized, defined before the main function, and referenced in the main function, but pay attention to the difference between global variables and local variables.
       9. Some arrays are preceded by code, which means that the array is written into FLASH, and the array content remains unchanged. If no code is added
      , it is written into RAM. 10. Can P0^0 be assigned directly without bit operation? No, because there is an XOR symbol in P0^0, so bit operation must be performed before normal assignment.
      11. The serial chip PL2303 is a domestic chip. The maximum baud rate during online simulation is 14400. Too high will cause program download failure.
      12. The STC89C52 program storage space is 8K FLASH . When downloading Hex files, some students may notice that the file exceeds 8K and think that the storage space is not enough. This statement is wrong. The size of the space occupied by the program should be in the keil software. The number of bytes after code . 2. Header file        1. If the header file is not in the keil software library, you need to write it yourself and add it to the project, or directly copy a copy to the keil software C51——>INC folder, so that you can directly reference the header file.  3. Variables Static variables are often used when modularizing functions.
                                      



 

4. AND &, OR |
Sometimes you will see such programming language in the program. A&=b; is equivalent to: a=a&b
Similarly, a|=b; is equivalent to: a=a|b
5. Interrupt program example: stopwatch
#include
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32; 
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0, //code is written to flash
                                     0x99,0x92,0x82,0xf8,
                                    0x80,0x90,0x88,0x83,
                                    0xa7,0xa1,0x86,0x8e};
uint8 a[6]; //No code before, it is written to ram
uint16 counter = 0;
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;

void timer1_init() //Timer function
{
 TMOD |= 0x10; //Select timer mode
 TMOD &= 0xdf;      
 TH1 = 0xFC; //Initial value of fixed time
 TL1 = 0x67;
 TR1 = 1; //Start timing
}
void int_init() //Interrupt function
{
 ET1 = 1; //Select interrupt 1
 EA = 1; //Enable interrupt function
}
void refresh_led() //Refresh function
{
   static uint8 j = 0; //Static variable, the first definition is valid and the entire function retains
  switch(j)
    {
     case 0: ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[0]];break;
  case 1: ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[1]];break;
  case 2: ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[2]];break;
  case 3: ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[3]];break;
  case 4: ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;j++;P0 = number[a[4]];break;
  case 5: ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;j = 0;P0 = number[a[5]];break;
  default: break;
  }
 
}
main() //Main function
{
 ENLED = 0; ADDR3 = 1; //Set the state of the 38 decoder
 timer1_init(); //Timer function
 int_init(); //Interrupt on
 while(1); //Infinite loop to ensure that the program is in the running state
 
}
void interrupt_timer1() interrupt 3 //Interrupt function, each time the timer overflows, it will trigger an interrupt
{
   static uint32 sec = 0;                     
   TH1 = 0xFC;
   TL1 = 0x67;
   counter++;
   if(counter == 1000)
   {
    sec++;
  counter = 0;
  a[0] = sec%10;
  a[1] = sec/10%10;
  a[2] = sec/100%10;
  a[3] = sec/1000%10;
  a[4] = sec/10000%10;
  a[5] = sec/100000%10;
   }
   refresh_led();
      
}
VI. Writing header files
The format is: #ifndef _KEY_H_
               #define _KEY_H_
                 …
               #endif
The written header file needs to be placed under the project where the Keil software is located.
Keywords:MCU Reference address:Microcontroller C language programming and & or | header files are common

Previous article:Detailed steps for using keil software
Next article:Chip design: modelsim do script

Latest Microcontroller Articles
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号