void main()
{
WDTCTL = WDTPW + WDTHOLD; //关看门狗
P1DIR |= BIT0;
P1DIR |= BIT5; ////P1.0 P1.5 设置为输出
P3DIR = ~( BIT2); //P3.2 设置为输入
P1REN |= BIT0 + BIT5; //上拉下拉电阻使能
P1IE |= BIT0 + BIT5; //P1.0 P1.5中断使能
P2IFG =0; //中断标志清除,防止配置过程中出现中断标志为1
_EINT(); //开总中断
}
/*******************端口中断程序********************/
#pragma vector = PORT2_VECTOR
__interrupt void port2(void)
{
switch(P2IV)
{
case 0x0e: P1OUT ^= BIT0;break; //LED1 亮灭
case 0x10: P1OUT ^= BIT5;break; //LED2 亮灭
default :break;
}
}
全的
如有帮助,望采纳
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
//GPIOB_BASE=0x40000000+0x10000+0x0C00=0x40010C00,address of GPIOB
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
//GPIOC_BASE=0x40000000+0x10000+0x1000=0x40011000,address of GPIOC
#define GPIOD_BASE (APB2PERIPH_BASE + 0x1400)
//GPIOD_BASE=0x40000000+0x10000+0x1400=0x40011400,address of GPIOD
#define GPIOE_BASE (APB2PERIPH_BASE + 0x1800)
//GPIOE_BASE=0x40000000+0x10000+0x0800=0x40011800,address of GPIOE
#define GPIOF_BASE (APB2PERIPH_BASE + 0x1C00)
//GPIOF_BASE=0x40000000+0x10000+0x0800=0x40011C00,address of GPIOF
#define GPIOG_BASE (APB2PERIPH_BASE + 0x2000)
//GPIOG_BASE=0x40000000+0x10000+0x0800=0x40012000,address of GPIOG
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define LED0 MEM_ADDR(BITBAND(GPIOA_ODR_Addr,8))
//#define LED0 *((volatile unsigned long *)(0x422101a0)) //PA8
//define typedef type alias
typedef struct
{
volatile unsigned int CR;
volatile unsigned int CFGR;
volatile unsigned int CIR;
volatile unsigned int APB2RSTR;
volatile unsigned int APB1RSTR;
volatile unsigned int AHBENR;
volatile unsigned int APB2ENR;
volatile unsigned int APB1ENR;
volatile unsigned int BDCR;
volatile unsigned int CSR;
} RCC_TypeDef;
#define RCC ((RCC_TypeDef *)0x40021000)
//define typedef type alias
typedef struct
{
volatile unsigned int CRL;
volatile unsigned int CRH;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
volatile unsigned int BRR;
volatile unsigned int LCKR;
} GPIO_TypeDef;
//Gpioa points to the address gpioa _ base, and the data type stored in gpioa _ base is gpio_typedef
#define GPIOA ((GPIO_TypeDef *)GPIOA_BASE)
void LEDInit( void )
{
RCC->APB2ENR|=1<<2; //GPIOA Clock on
GPIOA->CRH&=0XFFFFFFF0;
GPIOA->CRH|=0X00000003;
}
//A rough time delay
void Delay_ms( volatile unsigned int t)
{
unsigned int i,n;
for (n=0;n<t;n++)
for (i=0;i<800;i++);
}
int main(void)
{
LEDInit();
while (1)
{
LED0=0;//Led goes out
Delay_ms(500);//Delay time
LED0=1;//Led bright
Delay_ms(500);//Delay time
}
}
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED1= P1^0;
sbit LED2= P1^5;
sbit KEY= P3^2;
void delay(uint i)
{
uchar t;
while(i--)
{
for(t=0;t<120;t++);
}
}
void main()
{
LED1=0;
LED2=0;
temp=0;
while(1)
{
if(KEY)
{
delay(200);
if(KEY)
{
if(temp==0)
{
LED1=0;
LED2=0;
temp++;
}else if(temp==1){
LED1=1;
LED2=0;
temp++;
}else if(temp==2){
LED1=1;
LED2=1;
temp++;
}else if(temp==3){
LED1=0;
LED2=1;
temp=0;
}
}
}
}
}
这多简单的逻辑,给一个变量k,按键按一次,k<4,k+1,如果k>=4直接赋值k=0,然后k在0-3区间,4个数值对应4种状态就好