Arduino Timer Interrupts for the CH32V003 WCH MCU

by blopa1961 in Circuits > Arduino

131 Views, 1 Favorites, 0 Comments

Arduino Timer Interrupts for the CH32V003 WCH MCU

Ch32V003.jpg

This is a simple hardware timer Blink example for the CH32V003 chip using Arduino IDE 2.x.

I know this example looks trivial, but given the lack of documentation on how to use timer interrupts on this chip it actually took me DAYS to find out; and I had to force install the latest (unpublished) CH32V00x Board Manager.

Board Manager version 1.0.4 will not work with this sketch because it requires the HardwareTimer function. You will need the latest updates available HERE.

CH32V003 Timer Interrupts

CODE:

// (c) 2024 Pablo Montoreano
/*******************************************************
  simple Blink sketch using a timer
  for WCH CH32V003
  compile selecting CH32V00x board
  to compile this sketch you will need a Board Manager version that includes
  HardwareTimer (as of today 30/Sep/24 unpublished, I had to force install it from github)
  no 3rd party libraries used
*********************************************************/
#include <HardwareTimer.h>
HardwareTimer myTimer(TIM2);
static const unsigned int myPort= 16;  // output port (this is the onboard LED port, marked D6 in the board I have)
static volatile int myLED;
void timerHandler(void) {
  myTimer.pause();
  myLED= (myLED == HIGH) ? LOW : HIGH;
  digitalWrite(myPort, myLED);
  myTimer.setOverflow(1000000, MICROSEC_FORMAT);
  myTimer.resume();
}
void setup() {
  pinMode(myPort, OUTPUT);
  myLED= LOW;
  digitalWrite(myPort, myLED); // start pulse for channel sep
  myTimer.setOverflow(1000000, MICROSEC_FORMAT);
  myTimer.attachInterrupt(timerHandler);
  myTimer.resume();
}
void loop() {
}

I also published a real world example of whow to use Arduino timer interrups with the CH32V003 in Step 3 of THIS instructable.