GPIO_InitTypeDef GPIO_InitStructure;
void Delay(__IO uint32_t nCount);
int main(void) {
SystemInit();
SystemCoreClockUpdate();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
while (1) {
GPIO_WriteBit(GPIOC, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)));
Delay(500000);
}
}
void Delay(__IO uint32_t nCount) {
while (nCount--) {
}
}
Code language: PHP (php)
Delay using Systick Timer
GPIO_InitTypeDef GPIO_InitStructure;
volatile uint32_t TimingDelay;
void SysTick_Handler(void) {
if (TimingDelay != 0) {
TimingDelay--;
}
}
void Delay_ms(__IO uint32_t nTime);
void Delay_us(__IO uint32_t nTime);
int main(void) {
SystemInit();
SystemCoreClockUpdate();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
if (SysTick_Config(SystemCoreClock / 1000)) {
while (1);
}
while (1) {
GPIO_WriteBit(GPIOC, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)));
Delay_ms(500);
}
}
void Delay_ms(__IO uint32_t nTime) {
TimingDelay = nTime;
while (TimingDelay != 0);
}
void Delay_us(__IO uint32_t nTime) {
nTime *= 13;
while (nTime--);
}
Code language: PHP (php)
LED blinking with Sleep
void setupLED(GPIO_InitTypeDef *LED_InitStruct);
void sleep(uint32_t nSec);
int main(int argc, char **argv)
{
int LEDVal = 0;
GPIO_InitTypeDef LED_InitStruct;
setupLED(&LED_InitStruct);
if(SysTick_Config(SystemCoreClock / 1000)) {
while(1);
}
while(1) {
GPIO_WriteBit(GPIOC, GPIO_Pin_13 , LEDVal);
sleep(250);
LEDVal = !LEDVal;
}
return 0;
}
void setupLED(GPIO_InitTypeDef *LED_InitStruct)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_StructInit(LED_InitStruct);
LED_InitStruct->GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 ;
LED_InitStruct->GPIO_Mode = GPIO_Mode_Out_PP;
LED_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, LED_InitStruct);
}
static __IO uint32_t g_timeToWakeUp;
void sleep(uint32_t nSec)
{
g_timeToWakeUp = nSec;
while(g_timeToWakeUp != 0);
}
void SysTick_Handler(void)
{
if (g_timeToWakeUp != 0x00) {
g_timeToWakeUp--;
}
}
void assert_failed(uint8_t* file, uint32_t line)
{
while(1);
}
Code language: PHP (php)
You may like