Cortex-M3 processor key experiment program self-study

Publisher:闪耀的星空Latest update time:2018-03-09 Source: eefocusKeywords:Cortex-M3 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    There is always input when there is output. Today, let's test the function of the button. The registers related to the GPIO port have been discussed in the first section, so I won't repeat them here. If you want to read data from the port, first set the FIODIR register as input, and then read data from the FIOPIN register. This register has read and write functions. Here is the circuit diagram of this experiment, as shown below:

Figure 1 JoyST IC k-button connection diagram

    There is another key circuit, but it is connected to external interrupt 0. Its circuit diagram is shown below:

    This experiment does not involve external interrupts, but is used for ordinary IO input, so here we will summarize the study of external interrupts. The main program of this experiment is given below:

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

      File name: mian.c

      Function: Main scheduling function and application function

      Compilation environment: MDKV4.12

      Clock: External 12M Hz

      Date: 11/08/16

      Author: Lazy Cat Loves to Fly

      Note: NULL

      -------------------------------------------------- ----------------------------------

      Modification content: NULL

      Modification date: XXXX year xx month xx day xx hour xx minute

      Modified by: xxx xxx xxx

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

      #include "main.h"

      volatile unsigned lONg SysTickCnt; /* Used for system clock counting*/

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

      * Function name: void SysTick_Handler (void)

      * Function: System beat timer interrupt function, counts once every 1ms

      * Entry parameters: None

      * Export parameters: None

      * Note: None

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

      void SysTick_Handler (void)

      {

      SysTickCnt++;

      }

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

      * Function name: void Delay (unsigned long tick)

      * Function: millisecond delay function

      * Input parameter: unsigned long tick -- delay time

      * Export parameters: None

      * Note: None

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

      void DelayMs (unsigned long tick)

      {

      unsigned long systickcnt;

      systickcnt = SysTickCnt;

      while ((SysTickCnt - systickcnt) < tick);

      }

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

      * Function name: void PortInit(void)

      * Function: Port initialization

      * Entry parameters: None

      * Export parameters: None

      * Note: None

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

      void PortInit(void)

      {

      GPIO1->FIODIR = 0xB0000000; /*  LED s on PORT1 defined as Output */

      GPIO2->FIODIR = 0x0000007C; /* LEDs on PORT2 defined as Output */

      LEDAllOff (); /* Turn off all lights during initialization * /

      }

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

      * Function name: int main(void)

      * Function: Main function

      * Entry parameters: None

      * Export parameters: None

      * Note: None

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

      int main(void)

      {

      unsigned char  LED Flag = 1; // Record LED status

      SystEMI nit (); /* System initialization, the function is defined in the system_LPC17xx.c folder*/

      SysT IC k_Config (System Frequency / 1000 - 1); /* Configure clock interrupt, interrupt once every 1ms */

      /* Defined in core_cm3.h */

      PortInit(); /* Port initialization*/

      while(1)

      {

      if (!LedFlag)

      {

      Led1On(); // Turn on the LED

      }

      else

      {

      Led1Off(); // Turn off the LED

      }

      if (!KEY_VAL)

      {

      DelayMs(10);

      while (!KEY_VAL);

      LedFlag ^=1; //Led status changes once

      }

      if (!KEY_EN) // This is to test whether the joystick button functions normally

      {

      DelayMs(10);

      while (!KEY_EN);

      Led8Neg(); // Turn on the LED // The LED state changes once

      }

      }

      }

    The previous section did not explain the program in detail. Here is a detailed analysis. The source files included in the project are shown in the figure below:

    In the project, startup_LPC17XX.s is the startup file of M3. The startup file is written in assembly language. Its functions are generally as follows:

      1) Initialization of heap and stack

      2) Vector table definition

      3) Address remapping and transfer of interrupt vector table

      4) Set the system clock frequency

      5) Initialization of interrupt registers

      6) Enter the C application

    In the project, main.c is the application I wrote, which is the program of this experiment. core_cm3.c and core_cm3.h are mainly M3 peripheral driver source code and header files. Generally, they do not need to be modified when used, and can be called directly. system_LPC17xx.c and system_LPC17xx.h are files about the system, which mainly provide the system initialization function SystemInit(). The size of the crystal oscillator defined by default in the file is 12M. An external crystal oscillator is used, and PLL0 multiplication is also used. Regarding the multiplication problem, I will summarize it slowly later. The initialization of the chip LPC1768 mainly includes clock configuration, power management, power consumption management, etc. In comparison, the clock configuration is relatively complex because it includes two PLL multiplication circuits, one is the main PLL0 which mainly provides clocks for the system and USB, and the other is PLL1 which provides 48M clocks for USB, but they can also be used. Since the clock configuration is relatively flexible, it is also relatively complex to set these parameters, but these have been clearly defined in the system file, so if you want to change it, you only need to modify the corresponding macros or functions in the system file.

    The following is a brief summary of the main() function. The first is the system initialization function SystemInit(). As mentioned above, it is in the source file system_LPC17xx.c. This function mainly completes the configuration of the clock, system power consumption PCONP, clock output, flash acceleration and other system resource configurations. If you want to modify it, you can refer to the modification method of the source file. Although it is an English comment, it is very simple. If you are interested, you can open it and take a look. However, in general, we can use it directly without modification.

    The function SysTick_Config (SystemFrequency/1000 - 1) is used to configure the system clock beat. Its prototype is in the source file core_m3.c. The delay functions used in the experimental program are all hardware delays, which are actually generated by the system beat timer. The reasons for using hardware delays are 1. It does not occupy software system resources, and 2. It is more accurate. The system timer is very simple to configure and easy to use. It is designed to provide interval interrupts for system software or system management software. The clock source of the system beat timer can be the core clock or the external clock. The external clock is introduced from the P3.26 pin. Of course, if you want to input the clock from this pin, you need to configure this pin to the STCLK function first. The system beat timer is a 24-bit timer that generates an interrupt when the count value reaches 0. The function of the system beat timer is to provide a fixed time interval before the next interrupt. Since the beat timer is 24 bits, it cannot be confused with other timers when used. Be sure to pay attention to the limit of the timing duration and do not exceed the limit.

    Finally, let's talk about the data type. In 8-bit machines, the data bit width is usually 8 bits, so when defining variables, it is generally faster to use single bytes. However, in 32-bit machines, the data bit width is generally 32 bits, so it is generally better to use 4 bytes when defining variables. There are definitions of data types in core_cm3.c. If you are interested, you can open it and take a look.


Keywords:Cortex-M3 Reference address:Cortex-M3 processor key experiment program self-study

Previous article:Cortex-M3 processor GPIO experiment self-study
Next article:Self-study of external interrupt program of Cortex-M3 processor

Recommended ReadingLatest update time:2024-11-16 19:39

The first built-in assembly function of Cortex-M3 (MDK environment)
#include "memmap.h" #include "gpio.h" #include "Defination.c"   u8 i='a';   __asm ​​u8 plus(u8 a)           //Note the two "_" { MOV R1,R0       //Since R0 passes the first parameter, the value of a is assigned to the R1 register ADD R1,#6       //Add 6 to the value of R1 MOV R0,R1       //Return
[Microcontroller]
ARM Cortex-M3 Study Notes (4-6)
I'm studying ARM Cortex-M3 recently, and I found a book called "An Definitive Guide to The ARM Cortex-M3" which is considered a classic. This series of study notes is actually the reading notes I made while studying this book. Mutually exclusive access to memory Cortex-M3 provides three pairs of instructions for mut
[Microcontroller]
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号