eeucalyptus / ch32v003-temperature

Using the ch32v003 as a temperature sensor
MIT License
15 stars 1 forks source link

Almost Cycle-accurate measurements #2

Open cnlohr opened 10 months ago

cnlohr commented 10 months ago

This lets you capture the data very quickly, and with much more precision than currently available.

#include "ch32v003fun.h"
#include <stdio.h>

#define ITERATIONS 1
#define SERIALPLOT

volatile uint32_t ldelta;
uint32_t llast;

void AWU_IRQHandler( void ) __attribute__((section(".text.vector_handler")))  __attribute__((interrupt));

void AWU_IRQHandler( void )
{
    uint32_t l = SysTick->CNT;
    ldelta = (l - llast);
    llast = l;
    EXTI->INTFR = EXTI_Line9;
}

int main()
{
    SystemInit();

    // enable power interface module clock
    RCC->APB1PCENR |= RCC_APB1Periph_PWR | RCC_APB1Periph_WWDG;

    // enable low speed oscillator (LSI)
    RCC->RSTSCKR |= RCC_LSION;
    while ((RCC->RSTSCKR & RCC_LSIRDY) == 0) {}

    // enable AutoWakeUp event
    EXTI->FTENR |= EXTI_Line9;
    EXTI->INTENR |= EXTI_Line9;

    // configure AWU prescaler and configure AWU window comparison value
    // /16 /(63+1) ~= 120Hz
    PWR->AWUPSC = PWR_AWU_Prescaler_8;
    PWR->AWUWR = 63;
    PWR->AWUCSR = (1 << 1); // AWUEN

    // enable interrupt
    NVIC_EnableIRQ( AWU_IRQn );

    uint32_t t;
    uint32_t lastt = TIM1->CNT;

    while(1)
    {
        // Wait for data to become available.   
        uint32_t printtime = 0;
        while( !( printtime = ldelta ) );
        ldelta = 0;
        printf("%d\n", printtime ); //raw_to_temperature(count));
    }
}

Just tested it at my desk here.

Also, I am seeing significant variation per part.

Part Test Count at 25C Count at -19 C Counts Per Deg C
1 200300 203100 63
2 211000 215000 90
3 189500 192100 59

Additionally flex your PCB

Quescent at 25 C: 189900 Diagonal Torsion: 1888537 Inverse Torsion: 191050

Just using my little PCBs I had at my desk applying about 1 N*cm of force to the PCB.

eeucalyptus commented 10 months ago

I'll look into it tonight, thank you for the input!