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.
Previous article:LPC824 low power pin configuration debugger
Next article:S3C2440 serial port code_function_initialization_send Byte_send string_Printf
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- LOTO Lesson 5: Triode Audio Amplifier Circuit Practice
- [ADI Live] Academic lecturer discusses fault monitoring of MEMS sensors and industrial equipment with you
- Computer power supply plus current regulation
- Linear optocoupler isolation amplifier
- PCB design software Protel 99 SE and AD copper-free hole and copper-free slot design
- Why is a bootstrap circuit used in BUCK power chip?
- The circuit observation room has changed
- The states of the terminal device in the TI ZigBee protocol stack involved in the protocol stack
- Shanghai Hangxin ACM32F070 Development Board + Touch Function Evaluation Board Evaluation - Chapter 6 CAN Error Handling
- Has anyone encountered Ubuntu 20.04's suspended animation?