Skip to content
Snippets Groups Projects
Commit 8014594c authored by Marek Maškarinec's avatar Marek Maškarinec
Browse files

examples: blink D0 and D4 in iwdg example

parent e16e34ad
No related branches found
No related tags found
No related merge requests found
...@@ -4,10 +4,6 @@ ...@@ -4,10 +4,6 @@
#include "ch32v003fun.h" #include "ch32v003fun.h"
#include <stdio.h> #include <stdio.h>
#define APB_CLOCK SYSTEM_CORE_CLOCK
uint32_t count;
static void iwdg_setup(uint16_t reload_val, uint8_t prescaler) { static void iwdg_setup(uint16_t reload_val, uint8_t prescaler) {
IWDG->CTLR = 0x5555; IWDG->CTLR = 0x5555;
IWDG->PSCR = prescaler; IWDG->PSCR = prescaler;
...@@ -22,39 +18,56 @@ static void iwdg_feed() { ...@@ -22,39 +18,56 @@ static void iwdg_feed() {
IWDG->CTLR = 0xAAAA; IWDG->CTLR = 0xAAAA;
} }
static void gpios_on() {
GPIOD->BSHR = 1 | (1<<4);
GPIOC->BSHR = 1;
}
static void gpios_off() {
GPIOD->BSHR = (1<<16) | (1<<(16+4));
GPIOC->BSHR = (1<<16);
}
int main() int main()
{ {
SystemInit48HSI(); SystemInit48HSI();
// Enable GPIOs // Enable GPIOs
RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC;
// GPIO D0 Push-Pull
GPIOD->CFGLR &= ~(0xf<<(4*0));
GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0);
// GPIO D4 Push-Pull
GPIOD->CFGLR &= ~(0xf<<(4*4));
GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4);
// GPIO C0 Push-Pull // GPIO C0 Push-Pull
GPIOC->CFGLR &= ~(0xf<<(4*0)); GPIOC->CFGLR &= ~(0xf<<(4*0));
GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0);
GPIOC->BSHR = 1; gpios_on();
Delay_Ms( 1500 ); Delay_Ms( 1500 );
GPIOC->BSHR = (1<<16); gpios_off();
Delay_Ms( 1000 ); Delay_Ms( 1000 );
// set up watchdog to about 4 s // set up watchdog to about 4 s
iwdg_setup(0xfff, IWDG_Prescaler_128); iwdg_setup(0xfff, IWDG_Prescaler_128);
GPIOC->BSHR = 1; gpios_on();
Delay_Ms( 3000 ); Delay_Ms( 3000 );
GPIOC->BSHR = (1<<16); gpios_off();
// feed the watch dog. Now there should be about 8 blinks // feed the watch dog. Now there should be about 8 blinks
iwdg_feed(); iwdg_feed();
while(1) while(1)
{ {
GPIOC->BSHR = 1; gpios_on();
Delay_Ms( 250 ); Delay_Ms( 250 );
GPIOC->BSHR = (1<<16); gpios_off();
Delay_Ms( 250 ); Delay_Ms( 250 );
count++;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment