4.通过寄存器点亮led
typedef unsigned int u32;
#define GPIOC_BASE 0x40011000
#define PERIPH_BASE 0x40000000
#define APB2_BASE (PERIPH_BASE+0x10000)
#define AHB_BASE (PERIPH_BASE+0x20000)
#define RCC_BASE (AHB_BASE+0x1000)
#define RCC_APB2ENR (RCC_BASE+0x18)
#define GPIOx_CRH_OFF 0x0004
#define GPIOx_ODR_OFF 0x000C
#define GPIOx_CRH (GPIOC_BASE+GPIOx_CRH_OFF)//0x4001 1004
#define GPIOx_ODR (GPIOC_BASE+GPIOx_ODR_OFF)//0x4001 100C
void delay(u32 x)
{
u32 i = 0;
while(x--)
{
i = 10000000;
while(i--);
}
}
int main(void)
{
*((unsigned int *)RCC_APB2ENR) |= 0x00000010;
//*((unsigned int *)GPIOx_CRH) = 0x00300000;//error
//先清除寄存器相应位的值
*((unsigned int *)GPIOx_CRH) &= 0xff0fffff;
//然后再进行对相应位赋值
*((unsigned int *)GPIOx_CRH) |= 0x00300000;
//*((unsigned int *)GPIOx_CRH) = *((unsigned int *)GPIOx_CRH) + 0x00300000;
//*(GPIOx_CRH) = 0x00300000;
*((unsigned int *)GPIOx_ODR) &= ~(1<<13);
*((unsigned int *)GPIOx_ODR) |= 0x00000000;
//闪烁
while(1)
{
//亮
*((unsigned int *)GPIOx_ODR) &= ~(1<<13);
*((unsigned int *)GPIOx_ODR) |= 0x00000000;
//延时一些时间
//sleep(2);
delay(1);
//循环
//灭
*((unsigned int *)GPIOx_ODR) &= ~(1<<13);
*((unsigned int *)GPIOx_ODR) |= 0x00002000;//0000 0000 0000 0000 0010 0000 0000 0000
//延时一些时间
delay(1);
}
return 0;
}
|