Fixed fanspeed

This commit is contained in:
Elp03 2024-04-26 17:00:04 +02:00
parent f2019dd57a
commit 10c82d001e
3 changed files with 29 additions and 22 deletions

View File

@ -8,32 +8,42 @@ uint16_t fan_speed;
volatile uint16_t falling_edge_counter = 0;
#define MAX_PER 0x50
void init_TCA0() {
//code
PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc;
// PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc;
// port d ??? why
PORTD.DIR = PIN0_bm;
//PORTD.DIR = PIN0_bm;
//code
//TCA0.SINGLE.PERBUF = MAX_PER;
TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
TCA0.SINGLE.CMP0BUF = MAX_PER/2;
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x03;
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
//TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
//TCA0.SINGLE.CMP0BUF = MAX_PER/2;
//TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x03;
//TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
//TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm;
//TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc; /* Sysclk /1024 */
//TCA0_update_period_ms();
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ;
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */
TCA0.SINGLE.PERBUF = 3906;
//TCA0_update_period_ms();
}
void TCA0_update_period_ms() {
TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
}
uint16_t RPM_calculation(uint16_t test, uint16_t time) {
fan_speed = (test / time)*6000 * 3;
uint16_t RPM_calculation(uint16_t time) {
printf("time: %u", time);
fan_speed = (falling_edge_counter / time);
fan_speed = fan_speed/2;
fan_speed = fan_speed * 60/5;
printf("falling edge: %u", falling_edge_counter);
falling_edge_counter = 0;
printf("%u", fan_speed);
printf("falling edge: ", falling_edge_counter);
printf("fanspeed %u \n", fan_speed);
return fan_speed;
}
@ -45,13 +55,15 @@ void PORTB_init() {
void init_ac0(){
// CONFIGURE PINS AS ANALOG INPUTS
PORTD.DIRSET &= PIN6_bm;
PORTD.DIRSET &= ~PIN6_bm;
PORTD.PIN6CTRL &= ~ PORT_ISC_gm;
PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc;
PORTD.DIRSET &= ~PIN7_bm;
PORTD.PIN7CTRL &= ~ PORT_ISC_gm ;
PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc;
PORTD.DIRSET &= PIN3_bm;
PORTD.PIN3CTRL &= ~ PORT_ISC_gm ;
PORTD.PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc;
@ -66,29 +78,25 @@ void init_ac0(){
// OPTIONAL: ENABLE INNPUT PIN BY WRITING "1" TO OUTEN
//AC0.CTRLA = 0x40; // 0b01000000
// ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA
AC0.CTRLA |= PIN0_bm; // 0b00000001
//AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
//AC0.CTRLA |= PIN0_bm; // 0b00000001
AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
// SET CMP TO 1
// TURN ON INTERUPT
AC0.INTCTRL = 0x01;
}
void test(){
printf("yo %u", falling_edge_counter);
}
ISR(AC0_AC_vect){ // AC0 vec flag
cli();
AC0.STATUS |= 0x10; //CMP flag to 0.
falling_edge_counter++;
AC0.STATUS |= 0x10; //CMP flag to 0.
//printf("yo");
test();
sei();
}
ISR (TCA0_OVF_vect) {
cli();
RPM_calculation(falling_edge_counter,1E-3);
RPM_calculation(1);
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
sei();
}

View File

@ -26,7 +26,7 @@ extern "C" {
void init_TCA0();
void TCA0_update_period_ms ();
uint16_t RPM_calculation(uint16_t test, uint16_t time);
uint16_t RPM_calculation(uint16_t time);
void init_ac0();
#ifdef __cplusplus

View File

@ -36,7 +36,6 @@ int main(void) {
init_uart((uint16_t)9600);
stdout = &USART_stream;
init_TCA0();
TCA0_update_period_ms();
init_ac0();
sei();