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.
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
- Popular Resources
- Popular amplifiers
- Embedded Security Processor Application and Practice
- uCOS-III Kernel Implementation and Application Development Practical Guide - Based on STM32 (Wildfire)
- Principles and Practices of Embedded Systems (Shen Jianhua, Wang Ci)
- FreeRTOS kernel implementation and application development practical guide: based on STM32
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Huawei P50 series released: the world's first 3D nano-crystal mobile phone
- How to test the continuity of the communication harness?
- 422 to Ethernet
- Thermocouple Calibration Methods
- Please help me with a slightly more detailed analysis of this picture
- FAQ_How to choose TCXO for S2-lp
- [CB5654 Intelligent Voice Development Board Review] First Look at the Intelligent Voice Development Board
- Live broadcast at 3pm today | Keysight Technologies HDMI / DP2.0 online test seminar
- MSP430 G2553 Launchpad implements capacitance measurement
- 【Showing goods】The fourth wave