Document directory
- (1) experiment Overview
- (2) experiment principle and Flowchart
- (3) experiment source code
I. Connecting
In the previous lecture, we learned the basic process of developing the cc2430 program through the simplest small experiment of LED flashing. The knife has been worn out (although I have never been a stone), I have started to slaughter insects now :). Next, let's take a few basic experiments on cc2430. Each small experiment is divided into three parts: "experiment Introduction", "program flowchart", and "experiment source code and analysis.
This topic describes external interruptions.
Ii. External interruption (1) experiment Introduction
Interrupt is an internal mechanism for the microcontroller to process internal or external events in real time. When an internal or external event occurs, the Interrupt System of the single chip microcomputer will force the CPU to suspend the program being executed, and then process the interrupt event, return to the interrupted program and continue execution.
Interrupt is divided into external interrupt and internal interrupt. cc2430 contains 18 interrupt sources (for details about the interrupt description and definition of the interrupt vector, refer to cc2430 Chinese manual).
Now let's take a look at the circuit diagram of this development board:
The S1 key is connected to p0.1 on the Development Board. In this experiment, the p0.1 interruption is triggered by pressing S1 to control the led1 light/off in the interrupt service subroutine.
(2) experiment principle and Flowchart
The flowchart of the experiment is as follows:
(3) experiment source code // header file
# Include <iocc2430.h>
// Latency subfunction
# Define led1 p1_0
# Define led2 p1_1
# Define led3 p1_2
# Define led4 p1_3
Void delay (unsigned N)
{
Unsigned tt;
For (TT = 0; TT <n; TT ++ );
For (TT = 0; TT <n; TT ++ );
For (TT = 0; TT <n; TT ++ );
For (TT = 0; TT <n; TT ++ );
For (TT = 0; TT <n; TT ++ );
}
// 32 m crystal oscillator Initialization
Void xtal_init (void)
{
Sleep & = ~ 0x04; // all powered on
While (! (Sleep & 0x40); // The crystal oscillator is enabled and stable.
Clkcon & = ~ 0x47 ;//32 MHz Crystal Oscillator
Sleep | = 0x04;
}
// LED light Initialization
Void led_init (void)
{
P1sel = 0x00; // P1 is a common I/O port
P1dir | = 0x0f; // p1.0 p1.1 p1.2 p1.3 output
Led1 = 0;
Led2 = 0;
Led3 = 0;
Led4 = 0;
}
// Io and External Interrupt Initialization
Void io_init (void)
{
P0indium & = ~ 0x02; // p0.1 is pulled up and down
Ea = 1; // enable the overall interruption
P0ie = 1; // P0 interrupt enabling
Pictl | = 0x09; // The p0.1 port is interrupted, and the descent edge is triggered.
P0ifg & = ~ 0x02; // clear the p0.1 interrupt flag
};
// Main Function
Void main (void)
{
Xtal_init ();
Led_init ();
Io_init ();
While (1); // wait for interruption
}
// Interrupt service subroutine
# Pragma vector = p0int_vector
_ Interrupt void p0_isr (void)
{
Ea = 0; // Guanzhong disconnection
Delay (10000 );
Delay (10000 );
Delay (10000 );
Delay (10000 );
Delay (10000 );
If (p0ifg & 0x02)> 0) // key interruption
{
P0ifg & = ~ 0x02; // clear the p0.1 interrupt flag
Led1 =! Led1;
}
P0if = 0; // The P0 interrupt flag is cleared to 0.
Ea = 1; // interrupt
}
First initialize the system clock: select a 32 MHz crystal oscillator.
Then initialize the LED: Set P1 to the general I/O port, and set p1.0 ~ In the p1.3 direction, turn off four LED lights.
Configure the SFR registers related to external interruptions to enable the Enable of various levels, involving three SFr:EA,Ien1,Pictl(For more information about SFR, see the cc2430 Chinese manual):
EA-- Enable the overall interruption;
Ien1.5-- P0 interrupt enabling;
Pictl.3-- Enable p0.1 port interruption;
Pictl.0-- Set the drop edge of the p0.1 port to trigger interruption.
Then use while (1) in the main function to wait for the interruption.
Cc2430 tips
(1) Summary of bit assignment syntax
In many cases, we need to assign a value (0 or 1) to a single-byte SFr to precisely control hardware devices.
Some SFrSupports bit addressingFor example, tcon and P0. In this case, the assignment of BITs is very simple. You only need to query the bit definitions of SFR bit access in the iocc2430.h header file:
P0_0 = 0; // value 0 to the first place of P0
P0_0 = 1; // assign 1 to the first place of P0
But some SFR andBit addressing not supportedIn this examplePictlTo assign a value to one of the bits, the syntax is as follows:
Pictl & = ~ 0x01; // value 0 to 1st bits
Pictl | = 0x01; // value 1 to 1st bits
You can remember& = ~,| =These two commonly used bit assignment syntax.
(2) Summary of Interrupt enabling
When an interruption is involved in a program, the interruption must be enabled before the interruption is triggered.
The layer structure of the interrupt enabling system of C51 is very obvious:
Interrupt BOSS: EaIs the boss, responsibleTotalInterrupt enabling:
Ea = 1;
Division leader: For eachFeature(Such as P0, Timer 1, etc.) enable control, such SFR is generally addressable, the name generally contains IE (Interrupt enable ):
P0ie = 1;
Interrupted team members: Team, but because each function component also contains multiple interruptions, the last level is for thisEvery interruptionSuch SFR is generally not addressable, and the name generally contains IE (Interrupt enable) or IM (Interrupt Mask ):
Pictl | = 0x01;
You do not need to memorize to interrupt SFr. You only need to understand its hierarchy and then query the manual or header file.
(3) Writing interrupt programs
The use of interrupt in a program generally includes two parts: Preparation of Interrupt Service subprograms and enable interrupt enabling. Interrupt enabling has been introduced above. The following describes the preparation of Interrupt Service subprograms:
SpecifyInterrupt vectorYou can query the interrupt vectors section in the iocc2430.h header file. The syntax is as follows:
# Pragma vector = interrupt vector
Then, write the interrupt handler. The structure is as follows:
_ Interrupt void function name (void)
{
// Interrupt
// Interrupt handling
// Clear the interrupt mark 0
// Guanzhong disconnection
}
Iii. Conclusion
This article introduces the implementation of a simple external interrupt based on cc2430. With the basis of the interrupt, next we will introduce another very important module-timer. Cc2430 has four timers, which can be divided into three categories: timer 1, Timer 2, and timer 3/4 (3 and 4 are basically the same in usage ).
Next article: ZigBee journey (iv): several important cc2430 Basic Experiments-timer interruption