Cortex-M3 Learning LPC1768 - Button Experiment

Publisher:玄幻剑客Latest update time:2019-11-26 Source: 51heiKeywords: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-1 Joystick button connection diagram


             

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 12Mhz                                                                                                                                        

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

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

* Input 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

* Input parameters: None

* Export parameters: None

* Note: None

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

void PortInit(void)

{

   GPIO1->FIODIR = 0xB0000000; /* LEDs 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

* Input parameters: None

* Export parameters: None

* Note: None

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

int main(void)

{

    unsigned char LedFlag = 1; // Record LED status

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

               SysTick_Config(SystemFrequency/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     

                             }

               }

}



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, and an external crystal oscillator is used. 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 specifically 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 Learning LPC1768 - Button Experiment

Previous article:LPC824 low power pin configuration debugger
Next article:S3C2440 serial port code_function_initialization_send Byte_send string_Printf

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号