按键控制灯(寄存器)

核心:基地址加偏移地址,然后通过位操作

main.c

void delay(int time)
{
    while(time --);
	
}
int main()
{
    //使能GPIOC
    
    unsigned int *pRcc = (unsigned int *)(0x40021000 +  0x18);
    *pRcc |= (1<<4); 
    
    //使能GPIOA
    pRcc = (unsigned int *)(0x40021000 + 0x18);
    *pRcc |= (1<<2);
    
    //设置GPIOC 13为输出模式
    unsigned int *pMode = (unsigned int *)(0x40011000 + 0x04);
    *pMode |= (0x01 << 20);
    
    //设置GPIOA 10为输入模式
    pMode = (unsigned int *)(0x40010800 + 0x04);
    *pMode &= ~(0x01 << 8);
    *pMode |= (0x01 << 10);//浮空输入
    
    
    


    
    
    while(1)
    {
    //GPIOC13输出1
    unsigned int *pOutput = (unsigned int *)(0x40011000 + 0x0C);
//    *pOutput |= (1 << 13);
    unsigned int *pInput = (unsigned int *)(0x40010800 + 0x08);
       
       
//     //延时
//     delay(1000000); 
//    //GPIOC13输出0      
//    *pOutput &= (0 << 13);        
//    delay(1000000);
        
    //如果GPIOA 10读取的是高电平,灯就灭
        
    if((*pInput &= (1<<10)) > 0)
    {
        
        *pOutput |= (1 << 13);//灭
    }
    else
    {
       *pOutput &= (0 << 13); //亮
    }
        
        
    }
    
    
//    return 0;
}

你可能感兴趣的:(stm32,嵌入式硬件,汇编,单片机,arm开发)