Control the light on and off through IO.
//============================================================================
//File name: light.h
//Functional overview: small light component header file
//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)
//Version update: 2014-12-10 V1.0
//Chip type: KEA128
//============================================================================
#ifndef _LIGHT_H //Prevent duplicate definition (starting with _LIGHT_H)
#define _LIGHT_H
//Header file includes
#include "common.h" //Includes common element header files
#include "gpio.h" //Use gpio components
//Indicator port and pin definition
#define LIGHT_0 (PORTC|0) //Port/pin used by light 0
#define LIGHT_1 (PORTC|1) //Port/pin used by light 1
#define LIGHT_2 (PORTC|2) //Port/pin used by light 2
#define LIGHT_3 (PORTC|3) //Port/pin used by light 3
//Light status macro definition (the physical level corresponding to light on and light off is determined by the hardware connection)
#define LIGHT_ON 1 //Light on
#define LIGHT_OFF 0 //Light off
//================Interface function declaration===============================================
//============================================================================
//Function name: light_init
//Function parameters: port_pin: (port number)|(pin number) (e.g. (PORTB)|(5) means pin 5 of port B)
// state: Set the state of the small light. Defined by macro.
//Function returns: None
//Function summary: indicator light driver initialization.
//============================================================================
void light_init(uint_16 port_pin, uint_8 state);
//============================================================================
//Function name: light_control
//Function parameters: port_pin: (port number)|(pin number) (e.g. (PORTB)|(5) means pin 5 of port B)
// state: Set the state of the small light. Defined by macro.
//Function returns: None
//Function summary: Control the brightness of the indicator light.
//============================================================================
void light_control(uint_16 port_pin, uint_8 state);
//============================================================================
//Function name: light_change
//Function parameters: port_pin: (port number)|(pin number) (e.g. (PORTB)|(5) means pin 5 of port B)
//Function returns: None
//Function summary: switch the indicator light on or off.
//============================================================================
void light_change(uint_16 port_pin);
#endif //Prevent duplicate definition (ending with _LIGHT_H)
//===========================================================================
//statement:
//(1)The source code we developed has passed the test of the hardware system provided by our center. We sincerely contribute it to the society. If there are any shortcomings, please feel free to point them out.
//(2) For users who use hardware systems other than those of our center, please carefully match the system according to your own hardware when porting the code.
//
//Freescale Embedded Center, Suzhou University
//Technical consultation: 0512-65214835 http://sumcu.suda.edu.cn
//===========================================================================
//File name: light.c
//Functional overview: small light component source file
//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)
//Version update: 2014-12-10 V1.0
//Chip type: KEA128
//===========================================================================
#include "light.h"
//===========================================================================
//Function name: light_init
//Function parameters: port_pin: (port number)|(pin number) (e.g. (PORTB)|(5) means pin 5 of port B)
// state: Set the state of the small light. Defined by the macro in light.h.
//Function returns: None
//Function summary: indicator light driver initialization.
//===========================================================================
void light_init(uint_16 port_pin, uint_8 state)
{
gpio_init(port_pin, 1, state);
}
//===========================================================================
//Function name: light_control
//Function parameters: port_pin: (port number)|(pin number) (e.g. (PORTB)|(5) means pin 5 of port B)
// state: Set the state of the small light. Defined by the macro in light.h.
//Function returns: None
//Function summary: Control the brightness of the indicator light.
//===========================================================================
void light_control(uint_16 port_pin, uint_8 state)
{
gpio_set(port_pin, state);
}
//===========================================================================
//Function name: light_change
//Function parameters: port_pin: (port number)|(pin number) (e.g. (PORTB)|(5) means pin 5 of port B)
//Function returns: None
//Function summary: switch the indicator light on or off.
//===========================================================================
void light_change(uint_16 port_pin)
{
gpio_reverse(port_pin);
}
Main program header file
//============================================================================
//File name: includes.h
//Functional summary: project header file
//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)
//Version update: 2014-12-10 V1.0
//Chip type: KEA128
//============================================================================
#ifndef _INCLUDES_H //Prevent duplicate definitions (at the beginning)
#define _INCLUDES_H
//Contains the component header file used
#include "common.h"
#include "gpio.h"
#include "light.h"
//Preventing duplicate declarations of global variables using prefix processing
#ifdef GLOBLE_VAR //GLOBLE_VAR macro definition in main.c file
#define G_VAR_PREFIX //Use global variables in main.c file without "extern" prefix
#else
#define G_VAR_PREFIX extern //Global variables used in other files are automatically prefixed with "extern"
#endif
//Declare global variables (global variable types are always prefixed with G_VAR_PREFIX)
G_VAR_PREFIX char g_uart_num;
//Define macro constants
#define RUN_COUNTER_MAX 1500000ul //Flashing frequency of the small light
#endif //Prevent duplicate definition (end)
c file
//For instructions, see the Readme.txt file in the Doc folder under the project folder
#define GLOBLE_VAR //Only need to be defined once in main.c to prevent duplicate definition of global variables
#include "includes.h"
int main(void)
{
//1. Declare variables used by the main function
uint_32 mRuncount; //Main loop counter
//2. Turn off the general interrupt
DISABLE_INTERRUPTS; //Disable total interrupt
//3. Initialize peripheral modules
light_init(LIGHT_0, LIGHT_OFF); //Initialize light 0
light_init(LIGHT_1, LIGHT_OFF); //Initialize light 1
light_init(LIGHT_2, LIGHT_OFF); //Initialize light 2
light_init(LIGHT_3, LIGHT_OFF); //Initialize light 3
//4. Initialization of local variables used in main
mRuncount = 0; //Main loop counter
//5. Global variable initialization
//6. Enable module interrupt
//7. Enable general interrupt
ENABLE_INTERRUPTS; //Enable total interrupt
//Enter the main loop
//Main loop starts= ...
for (;;)
{
//Running indicator light flashes----------------------------------------------------------
mRuncount++; //Main loop count counter + 1
if (mRuncount >= RUN_COUNTER_MAX) //The main loop count counter is greater than the set macro constant
{
mRuncount = 0; // Clear the main loop count counter
light_change(LIGHT_0); //Light 0 (LIGHT_0) status change
light_change(LIGHT_1); //Light 1 (LIGHT_1) status change
light_change(LIGHT_2); //Light 2 (LIGHT_2) status change
light_change(LIGHT_3); //Light 3 (LIGHT_3) status change
}
//The following user program is added--------------------------------------------------------
} //Main loop end_for
//End of main loop= ...
return 0;
}
Previous article:NXP Freescale Kinetis KEA128 Study Notes 3--GPIO Module (I)
Next article:NXP Freescale Kinetis KEA128 Study Notes 3 - Watchdog
Recommended ReadingLatest update time:2024-11-16 20:51
- Popular Resources
- Popular amplifiers
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
- How to use the Smart Analog Combo in MSP430 MCUs
- Construction of TI cc2541 protocol stack development environment
- 【ST NUCLEO-G071RB Review】——by lvxinn2006
- [NXP Rapid IoT Review] DIY expansion board success
- 【RT-Thread Reading Notes】2. RT-Thread Study Chapters 1-3 Reading Notes
- What do you think about RISC-V?
- DIY Energy Saving Socket Strip
- Can you recommend some 16-bit or 32-bit dual in-line microcontrollers with a price range of 2-6 yuan?
- Very pleasantly surprised, let me show you the winning nail clipper set.
- SensorTile.box accelerometer IIS3DHHCTR test project