Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8013

SDK • Re: Accurate 60Hz timer?

$
0
0
I went with systick, since it seemed the easiest to implement. (And reminiscent of working with Microchip PIC.) I simplified the implementation used in the helpful link gmx posted.

It turns out that I need a 59.541984733Hz refresh rate. It's a video game, that had a 4.992MHz crystal. I calculated all of the multiples up to around 150Mhz and got a great match at 124.8Mhz. And the vocalc.py script showed that it works without error. Below are the necessary parts.

Code:

#include "hardware/structs/systick.h"uint8_t u8_systick_count;extern void isr_systick() // override systick IRQ in crt0.S file{    u8_systick_count++;        // 1 systick is 2 scanlines, so counts are half    // for simplicity, the 38 scanlines of VBLANK are placed at the beginning        if (u8_systick_count == 67) // (96 + 38) / 2        ; // scanline 96    else if (u8_systick_count == 131) { // (224 + 38) / 2        u8_systick_count = 0;        ; // scanline 224    }}int main() {    set_sys_clock_khz(124800, true);    stdio_init_all();        // tick rate is 2 scanlines    u8_systick_count = 0;    systick_hw->csr  = 0;       // disable     systick_hw->rvr  = 15999UL; // interrupt every two scanlines    systick_hw->cvr  = 0;       // clear the count to force initial reload    systick_hw->csr  = 0x7;     // use processor clock, enable interrupt, enable systick counter        // ...}

Statistics: Posted by Mike**K — Sat May 10, 2025 10:38 pm



Viewing all articles
Browse latest Browse all 8013

Trending Articles