Simple CH558 timer not working - Page 1
Forum section: electronics / projects

author: dirkx
author_rank: Newbie
timestamp: May 18, 2024, 10:19:27 pm
content: I've created a ch558 variant forhttps://github.com/DeqingSun/ch55xduino(https://github.com/dirkx/ch55xduino/tree/FEAT_CH558_Support) for a small board (https://github.com/dirkx/cheapbot).This generally works well. But I am having trouble to get Timer3 (the only really 'free' timer) to work. As a pure `fire an IRQ' sort of time.Most minimal example below. It just flashes an LED.Does this ring a bell with someone / something obviously wrong ? The example follows the vendor's CH559 example (there is no CH558 example yet).Dw#include <Serial.h>uint32_t tick = 0;void setup() {// Enable the LEDdigitalWrite(40, LOW);pinMode(40, OUTPUT);// give the Arduino IDE some time to switch.delay(3000);USBSerial_println("Running " __FILE__ " " __DATE__ " " __TIME__);#define DIV (1) // Divisor of F_CPU#define ENDCOUNT (100) // Count// set divisorT3_SETUP |= bT3_EN_CK_SE; // Enable to accessing divisor setting register.T3_CK_SE_L = DIV & 0xff;T3_CK_SE_H = (DIV >>& 0xff;T3_SETUP &= ~bT3_EN_CK_SE; // clear access again.T3_CTRL |= bT3_CLR_ALL; // Force clear FIFO and count of timer3.T3_CTRL &= ~bT3_CLR_ALL; // And go back to reset by softwareT3_SETUP |= bT3_IE_END; // Enable interrutsT3_CTRL &= ~bT3_MOD_CAP; // Timer or PWM mode// T3_STAT |= bT3_IF_DMA_END | bT3_IF_FIFO_OV | bT3_IF_FIFO_REQ | bT3_IF_ACT | bT3_IF_END;T3_STAT |= bT3_IF_END; // Interrupt when we pass ENDCOUNTT3_END_L = ENDCOUNT & 0xff;T3_END_H = (ENDCOUNT >>& 0xff;IE_TMR3 = 1; // Timer3 interrupt is enabled.EA = 1; // Global interrupt enable control bit}void Timer3Interrupt(void) __interrupt {// clear count-passed endpoint interupt.if (T3_STAT & bT3_IF_END)T3_STAT |= bT3_IF_END;tick ++;digitalWrite(40, tick & 1);}void loop() {{static uint32_t last = 0;if (last != tick) {last = tick;USBSerial_print("IRQ seen:");USBSerial_println(tick);delay(500);}}{static uint32_t last;if (millis() - last > 2500) {last = millis();USBSerial_println("Still alive..");}}}

author: aliarifat794
author_rank: Regular Contributor
timestamp: May 23, 2024, 10:38:28 am
content: Try to edit your code like this.#include <Serial.h>uint32_t tick = 0;// Define the Timer3 Interrupt Vectorvoid Timer3Interrupt(void) __interrupt (INT_NO_TMR3) {if (T3_STAT & bT3_IF_END) {T3_STAT |= bT3_IF_END; // Clear the interrupt flagtick++;digitalWrite(40, tick & 1);}}void setup() {// Enable the LEDpinMode(40, OUTPUT);digitalWrite(40, LOW);// give the Arduino IDE some time to switch.delay(3000);USBSerial_println("Running " __FILE__ " " __DATE__ " " __TIME__);#define DIV (1) // Divisor of F_CPU#define ENDCOUNT (100) // Count// Set divisorT3_SETUP |= bT3_EN_CK_SE; // Enable accessing divisor setting register.T3_CK_SE_L = DIV & 0xff;T3_CK_SE_H = (DIV >>& 0xff;T3_SETUP &= ~bT3_EN_CK_SE; // Clear access again.T3_CTRL |= bT3_CLR_ALL; // Force clear FIFO and count of timer3.T3_CTRL &= ~bT3_CLR_ALL; // And go back to reset by softwareT3_SETUP |= bT3_IE_END; // Enable interruptsT3_CTRL &= ~bT3_MOD_CAP; // Timer or PWM modeT3_STAT |= bT3_IF_END; // Interrupt when we pass ENDCOUNTT3_END_L = ENDCOUNT & 0xff;T3_END_H = (ENDCOUNT >>& 0xff;IE_TMR3 = 1; // Timer3 interrupt is enabled.EA = 1; // Global interrupt enable control bit}void loop() {static uint32_t last = 0;if (last != tick) {last = tick;USBSerial_print("IRQ seen:");USBSerial_println(tick);delay(500);}static uint32_t lastMillis;if (millis() - lastMillis > 2500) {lastMillis = millis();USBSerial_println("Still alive..");}}

author: ledtester
author_rank: Super Contributor
timestamp: May 23, 2024, 10:54:49 am
content: Quote from: aliarifat794 on May 23, 2024, 10:38:28 amFor code you should enclose it in a...block.