cc2530 third experiment: the key interrupt control light water
Effect by pressing again, a water lights
Experimental relevant circuit diagram:
Experiments related registers:
Initialization function
//初始化LED灯
//设置P1SEL,通用为0,外设为1 11111100
P1SEL &=~0x03;
//设置P0SEL, 11101111
P0SEL &= ~0x10;
//设置P1DIR,输出为1,输入为0 00000011
P1DIR |= 0x03;
//设置P0DIR 00010000
P0DIR |= 0x10;
//初始化按钮和中断
//初始化按钮
//初始化P0SEL,通用为0 11111101
P0SEL&=~0x02;
//初始化P0DIR,输入为0 11111101
P0DIR&=~0x02;
//初始化中断
//设置引脚中断 1为中断使能 00000010
P0IEN |= 0x02;
//设置端口中断
IEN1=1;
//设置端口中断触发方式 1为中断使能
IEN1|=0x20;
//设置总中断
EA=1;
//清空标志位
P0IFG=0;
Interrupt service routine
//中断服务函数
#pragma vector = P0INT_VECTOR //这一行的P0INT_VECTOR是中断源的名称吗?
__interrupt void P0ISR(void)
{
//清除端口中断标志位
P0IFG=0;
//LED灯流水亮一遍
BLink();
//清除端口中断标志位
P0IF=0;
}
The code above must pay attention to P0IFG must be removed prior to P0IF, saying P0IF and P0IFG in the end is doing with? What is the relationship between them, the flag register port? (Remember to check)
P0IFG is the port 0 interrupt status flag, P0IF interrupt flag register within cpu, what the hell, did not understand, ah, ask the teacher it!
All codes
#include <ioCC2530.h>
typedef unsigned int uint;
typedef unsigned char uchar;
#define LED1 P1_0
#define LED2 P1_1
#define LED3 P0_4
#define BUTTON1 P0_1
uint time=300;
//延迟函数
void Delay(uint time)
{
for(uint i=0;i<time;i++)
for(uint j=0;j<1070;j++);
}
//初始化函数
void Init(void)
{
//初始化LED灯
//设置P1SEL,通用为0,外设为1 11111100
P1SEL &=~0x03;
//设置P0SEL, 11101111
P0SEL &= ~0x10;
//设置P1DIR,输出为1,输入为0 00000011
P1DIR |= 0x03;
//设置P0DIR 00010000
P0DIR |= 0x10;
//初始化按钮和中断
//初始化按钮
//初始化P0SEL,通用为0 11111101
P0SEL&=~0x02;
//初始化P0DIR,输入为0 11111101
P0DIR&=~0x02;
//初始化中断
//设置引脚中断 1为中断使能 00000010
P0IEN |= 0x02;
//设置端口中断
IEN1|=0x20;
//设置端口中断触发方式 1为下降沿触发
PICTL|=0x01;
//设置总中断
EA=1;
//清空标志位
P0IFG=0;
}
void BLink()
{
Delay(time);
LED1=0;
LED2=1;
LED3=1;
Delay(time);
LED1=0;
LED2=0;
LED3=1;
Delay(time);
LED1=0;
LED2=0;
LED3=0;
Delay(time);
LED1=1;
LED2=1;
LED3=1;
}
//中断服务函数
#pragma vector = P0INT_VECTOR //这一行的P0INT_VECTOR是中断源的名称吗?
__interrupt void P0ISR(void)
{
//清除端口中断标志位
P0IFG=0;
//LED灯流水亮一遍
BLink();
//清除端口中断标志位
P0IF=0;
}
//主函数
void main()
{
Init();
LED3=0;
while(1)
{
//LED3=0;
}
//return ;
}
This experiment difficulty and focus primarily on interrupting the initialization process,
initialization pin register P0IEN | = 0x02;
Set port interrupt register IEN1 | = 0x20;
Set port interrupt trigger PICTL | = 0x01;
Set the global interrupt EA = 1
empty flag: P0IFG = 0;