Timer/counter is one of the important resources of single-chip microcomputer, and the timer/counter of CC2530 has more working modes compared with the ordinary 51 series single-chip microcomputer. According to the study manual, CC2530 timer/counter has three working modes, namely free mode, modular mode and up/down counting mode. No matter which mode, the timer can be used in two ways: query and interrupt. This topic describes the programming method of CC2530 timer/counter in modular mode.
First, we use the query method to use the Timer1 timer. When using the Timer timer, we must first initialize the timer. The code is as follows:
void INIT_Timer1()
{
T1CTL = 0x00; //1 division, stop running
T1CTL = 0x0e; //128 frequency division mode
T1CCTL0 |= 0x04; //Set timer1 channel 0 output comparison mode
T1CC0L = 0x24;
T1CC0H = 0xF4;
IRCON &= ~0x02;
}
It is easy to see that when using Timer1, we need to use five registers: T1CTL, T1CCTL0, T1CC0L, T1CC0H and IRCON. T1CTL is a control register, which is used to ① set the divider division value such as f/8 or f/128, where f is the mark frequency. ② Select the three modes of the timer. The two registers T1CC0L and T1CC0H store the final value of the count (when the count reaches this value, the interrupt flag is set, or an interrupt is generated). IRCON is the interrupt flag. The reason why the code is written
IRCON &= ~0x02;
This is to clear the interrupt flag corresponding to Timer1 first.
T1CCTL0 |= 0x04; //Set timer1 channel 0 output comparison mode
So what is the significance of the assignment operation to T1CCTL0 in this line of code?
The modulo mode needs to enable the output compare mode of channel 0, otherwise the counter will only generate an overflow interrupt (corresponding overflow flag) when it reaches 0XFF. That is, if the output compare mode of channel 0 is not set, the counter value will not generate an overflow interrupt (corresponding overflow flag will not be set to 1) after reaching T1CC0. This needs special attention. The code of the entire program is posted below:
#include #define LED1 P1_0 //Define LED1 as P1.0 int count = 0; void INIT_LED(void) { P1SEL &= ~0x01; //Set P1.0 to normal I/O function P1DIR |= 0x01; //Set P1.0 as output direction LED1 = 0; //Turn on LED1 } void INIT_Timer1() { T1CTL = 0x00; //1 division, stop running T1CTL = 0x0e; //128 frequency division mode T1CCTL0 |= 0x04; //Set timer1 channel 0 output comparison mode T1CC0L = 0x24; T1CC0H = 0xF4; IRCON &= ~0x02; } void main( void ) { INIT_LED(); INIT_Timer1(); LED1 = 1 ; //int count = 0; while(1) { if(IRCON &= 0x02) { count++; if(count==1) { IRCON &= ~0x02 ; count = 0; LED1 = !LED1; } } } } The reason why the program is written in the query mode is that the following code is if(IRCON &= 0x02) { count++; if(count==1) { IRCON &= ~0x02 ; count = 0; LED1 = !LED1; } } In the main program, the interrupt flag is constantly queried. If the interrupt flag is set, it needs to be cleared to 0 by software.
Previous article:CC2530 bare metal programming series notes 4--use of GPIO port
Next article:51 MCU Series Knowledge 6--Interrupt System (1)
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- Ranking of installed capacity of smart driving suppliers from January to September 2024: Rise of independent manufacturers and strong growth of LiDAR market
- Industry first! Xiaopeng announces P7 car chip crowdfunding is completed: upgraded to Snapdragon 8295, fluency doubled
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Excuse me, does anyone have the dimension drawing of TQFN20 package?
- TIOBE Index May 2022
- Where can I download examples and peripheral libraries for NXP Kinetis MCU?
- EEWORLD University - Breaking through K60 in one day
- The Beacon made of CC2640R2F chip has a larger current after running for a period of time. The broadcast is normal, but the current becomes smaller after restart.
- Introduction to the operating environment and interface mode of RFID application system
- Many choices in life are your destiny.
- Experience using the bootloader of TMS320VC33
- Is the CS+ for CACX development environment free to use now?
- Please help me. When the circuit board motor is working, the LED light controlled by the IO port of the main control IC will flash.