Learning Goals:Master interrupt enable register IEN0, IEN1, IEN2, Interrupt flag Register PXIFG, IRCON, IRCON2, port interrupt trigger along control register PICTL
The CC2541 has 18 interrupt sources, each of which can generate an interrupt request, and the 18 interrupt sources are as follows
Interrupt request via SFR Register IEN0, IEN1, IEN2 enable or disable, defined as follows:
where IEN0 and IEN1 can be bit addressable, the register IEN2 can not be bit addressable ea:1 open total interrupt IEN1.P0IE:P0 port interrupt enable IEN2.P1IE:P1 port interrupt enable Ien2.p2ie: The P2 port interrupt enables the 5th bit of the IEN1 register to control the interrupt of the P0 port, When setting the Ien1.p0ie, the interrupt on all pins of the P0 port is set to enable the 4th and 1th bits of the IEN2 register to control the P1 and P2 ports respectively, and to control all pin interrupts of the P1 and P2 ports so that if a specific pin setting is enabled for P0, P1, P2 ports, You need to set the register P0ien, P1ien, P2ien, which are defined as follows: 0xAB, 0x8d, 0xAC non-addressable
Port |
Bit bits |
Name |
Initialization |
Write |
Describe |
P0ien |
7:0 |
P0_[7:0]ien |
0x00 |
r/w |
Port 0, bit 7 to bit 0 interrupt enabled. 0: Interrupt disable; 1: Interrupt Enable |
P1ien |
7:0 |
P1_[7:0]ien |
0x00 |
r/w |
Port 1, bit 7 to bit 0 interrupt enabled. 0: Interrupt disable; 1: Interrupt Enable |
P2ien |
7:6 |
--- |
00 |
R0 |
Not used |
P2ien |
5 |
Dpien |
0 |
r/w |
USB d+ interrupt enabled. |
P2ien |
4:0 |
P2_[4:0]ien |
00000 |
r/w |
Port 2, bit 4 to bit 0 interrupt enabled. 0: Interrupt disable; 1: Interrupt Enable |
so the general purpose I/O port interrupt enables three layers: Total interrupt Enable, port interrupt enable, PIN interrupt enable
I/O interrupts, in addition to the configuration interrupt enable, also need to configure the interrupt trigger mode, interrupt trigger mode can be set by the port interrupt control register Pictl, I/O interrupt triggering method is divided into the input rising edge and falling edge trigger, PICTL Register is defined as follows: 0x8c Non-addressable
Port |
bit bit |
name |
Initialize |
read/write |
description |
PI CTL |
7 |
PADSC |
0 |
r/w |
Force pin in output mode. Select output drive capability, provided by the DVDD pin. 0: Minimum drive capacity, 1: Maximum drive capacity |
pictl |
6:4 |
--- |
|
R0 |
Unused /td> |
pictl |
3 |
p2icon |
0 |
r/w |
Port 2, pin 4 to 0 interrupt configuration in input mode, 0: The rising edge of the input causes an interruption; 1: The Falling edge of the input causes an interruption |
pictl |
2 |
p1iconh |
0 |
r/w | td> Port 1, pin 7 to 4 interrupt configuration in input mode, 0: Interrupt caused by rising edge of input, 1: Falling edge of input causes interrupt
pictl |
1 |
p1iconl |
0 |
r/w |
port 1, pin 3 to 0 interrupt configuration in input mode, 0: Interrupt caused by rising edge of input, 1: Falling edge of input causes interrupt |
pictl |
0 |
P0icon |
0 |
r/w |
Port 0, pin 7 to 0 interrupt configuration in input mode, 0: Interrupt caused by rising edge of input, 1: Falling edge of input causes interrupt |
Similar to interrupt enable three layer, interrupt flag also has two layers, respectively, port interrupt flag and PIN interrupt flag port interrupt flag bit in register Ircon and IRCON1, respectively p0if, p1if, p2if, can be bit addressed
Since CC2541 's external interrupts share an interrupt vector, it is necessary to determine which pin is interrupted, and the interrupt Status flag register (P0IFG,P1IFG,P2IFG) can be used to determine which pin has been interrupted, and the register is defined as follows: 0x89, 0x8A, 0x8b Non-addressable
Port |
Bit bits |
Name |
Initialization |
Read/write |
Describe |
P0ifg |
7:0 |
P0IF[7:0] |
0x00 |
R/w0 |
Port 0, bit 7 to bit 0 input interrupt status flag. 0: No interruption, 1: interrupt occurred |
P1ifg |
7:0 |
P1IF[7:0] |
0x00 |
R/w0 |
Port 1, bit 7 to bit 0 input interrupt status flag. 0: No interruption, 1: interrupt occurred |
P2ifg |
7:5 |
--- |
000 |
R0 |
Not used |
P2ifg |
4:0 |
P2IF[4:0] |
0x00 |
R/w0 |
Port 2, bit 4 to bit 0 input interrupt status flag. 0: No interruption, 1: interrupt occurred |
General Purpose I/O port interrupt configuration process:
- Set the I/O port that requires an interrupt to be a general-purpose I/O port, config register Pxsel
- Set I/O port as input, config register Pxdir
- Clear interrupt flag, corresponding register PXIFG status flag position 0
- Setting interrupt triggering for I/O ports
- Start port Interrupt, set the interrupt enable bit to the port of the corresponding PIN to 1, set register IEN0 or IEN1
- Open total Interrupt, set the EA in IEN0 to 1
such as P0.1 as the key interrupt initialization sub-function as follows
1 /******************************************************************************2 * Function Name: Initkey3 * Function: Key interrupt initialization4 * Entry parameters: None5 * Export parameters: None6 ******************************************************************************/7 voidInitkey (void)8 {9P0sel &= ~0x02;//P0.1 set to Universal I/O portTenP0dir &= ~0x02;//P0.1 set to input OneP0IFG &= ~0x02;//P0.1 Interrupt Status flag-clear 0 APictl |=0x00;//P0 port drop Edge trigger -P0ien |=0x02;//P0.1 Interrupt Enable -IEN1 |=0x20;//Port P0 Interrupt enable theEA =1;//Open Total Interrupt -}
what to do in an interrupt service program:
- Determine if the pin interrupt occurred (if the port has only one pin interrupt, it can not be judged)
- Complete specific interruption work
- Clear PIN interrupt flag, PXIFG corresponding pin position clear 0 (must be software clear 0, not clear 0 words Interrupt Service program repeatedly executed)
- Clear port Interrupt Flag, P0IF/P1IF/P2IF 0 (Experimental verification port interrupt flag is not clear 0 can also, but this bit will not automatically clear 0, need software clear 0, for insurance, unified for the Qing 0)
Interrupt Service Program Writing format
1 /******************************************************************************2 * Function Name: P0_ISR3 * Function: Interrupt Service sub-program4 * Entry parameters: None5 * Export parameters: None6 ******************************************************************************/7 #pragmaVector = P0int_vector8__interruptvoidP0_ISR (void)9 {Ten if(0x02& P0IFG)//To determine the key interrupt One { A //Specific function Code - } -P0IFG =0;//Clear Interrupt Flag theP0if =0;//clear Interrupt flag, ircon[5],p0 port interrupt -}
Example program: Key control LED light
1 /******************************************************************************2 * File name: key.c3 * Author: Contacts4 * Time: 2015-04-235 * Version: 1.06 * Description: Key interrupt mode control Flow lamp7 ******************************************************************************/8#include <iocc2541.h>9#include"Delay.h"Ten Onetypedef unsignedCharUchar; Atypedef unsignedint UINT; - - #defineLED1 P1_0 the #defineLED2 p1_1 - #defineLED3 p1_4 - #defineKEY P0_1 - + /****************************************************************************** - * Function Name: initled + * Function: LED lamp initialization A * Entry parameter: Mode,mode of 1 lights all Led,mode to 0 turns off all LEDs at * Export parameters: None - ******************************************************************************/ - voidinitled (uchar mode) - { -P1sel &= ~0x13;//p1.0,p1.1,p1.4 set to Universal I/O port -P1dir |=0x13;//p1.0,p1.1,p1.4 set to output inLED1 = mode;//LED light off control -LED2 =mode; toLED3 =mode; + } - the /****************************************************************************** * * Function Name: Initkey $ * Function: Key interrupt initializationPanax Notoginseng * Entry parameters: None - * Export parameters: None the ******************************************************************************/ + voidInitkey (void) A { theP0sel &= ~0x02;//P0.1 set to Universal I/O port +P0dir &= ~0x02;//P0.1 set to input -P0IFG &= ~0x02;//P0.1 Interrupt Status flag-clear 0 $Pictl |=0x00;//P0 port drop Edge trigger $P0ien |=0x02;//P0.1 Interrupt Enable -IEN1 |=0x20;//Port P0 Interrupt enable -EA =1;//Open Total Interrupt the } - Wuyi /****************************************************************************** the * Function Name: P0_ISR - * Function: Interrupt Service sub-program Wu * Entry parameters: None - * Export parameters: None About ******************************************************************************/ $ #pragmaVector = P0int_vector -__interruptvoidP0_ISR (void) - { - if(0x02& P0IFG)//To determine the key interrupt A { +LED1 =! LED1;//Running Lights theDelay1ms ( +);//delay 1s -LED2 =!LED2; $Delay1ms ( +); theLED3 =!LED3; theDelay1ms ( +); the } theP0IFG =0;//Clear Interrupt Flag -P0if =0;//clear Interrupt flag, ircon[5],p0 port interrupt in } the the /****************************************************************************** About * Program Entry function the ******************************************************************************/ the intMainvoid) the { +Initled (0);//led initialization, off Led1~3 -Initkey ();//Key Interrupt Initialization the Bayi while(1) the { the } -}
CC2541 Bluetooth Learning-General purpose I/O port interrupts