no worries

This commit is contained in:
Elp03 2024-04-27 18:45:04 +02:00
parent f0a435d578
commit ac5a054f8b
3 changed files with 35 additions and 45 deletions

View File

@ -1,36 +1,37 @@
//#include <avr/ioavr128db48.h>
#include "fan_speeeed.h" #include "fan_speeeed.h"
#include "uart.h" #include "uart.h"
uint16_t timer_period_ms = 1; uint16_t timer_period_ms = 1;
uint16_t fan_speed; uint16_t timer_period = 0.01;
volatile uint16_t falling_edge_counter_1 = 0; uint16_t fan_speed = 0;
volatile uint16_t falling_edge_counter_2 = 0; volatile uint16_t fan1_edge_counter = 0;
#define MAX_PER 0x50 volatile uint16_t fan2_edge_counter = 0;
void init_TCA0() { void init_TCA0() {
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ;
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */
TCA0.SINGLE.PERBUF = 3906; //TCA0.SINGLE.PERBUF = 3906;
//TCA0_update_period_ms(); TCA0_update_period();
} }
void TCA0_update_period_ms() { void TCA0_update_period() {
TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
} }
uint16_t RPM_calculation(uint16_t fancounter, uint16_t time) { // COUNTINGS / TIME = FREQUENCY
fan_speed = (fancounter / time); // FREQ / 2 = GIVES ACTUAL FREQ
// FREQ * SEC-IN-A-MINUTE(60) / FAN-BLADES
uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms) {
fan_speed = (edge_counter / time_ms);
fan_speed = fan_speed/2; fan_speed = fan_speed/2;
fan_speed = fan_speed * 60/5; fan_speed = fan_speed * 60/5;
fancounter = 0; edge_counter = 0;
return fan_speed; return fan_speed;
} }
void init_fanports() { void init_fan_gpio() {
// CONFIGURE PINS AS ANALOG INPUTS // CONFIGURE PINS AS ANALOG INPUTS
// PIN FOR FAN 1 // PIN FOR FAN 1
@ -49,7 +50,7 @@ void init_fanports() {
PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc;
} }
void init_ac0(){ void init_AC0(){
//Wincontroll disabled //Wincontroll disabled
AC0.CTRLB = 0x00; AC0.CTRLB = 0x00;
@ -65,7 +66,7 @@ void init_ac0(){
} }
void init_ac1(){ void init_AC1(){
//Wincontroll disabled //Wincontroll disabled
AC1.CTRLB = 0x00; AC1.CTRLB = 0x00;
@ -83,7 +84,7 @@ void init_ac1(){
ISR(AC0_AC_vect){ // AC0 vec flag ISR(AC0_AC_vect){ // AC0 vec flag
cli(); cli();
falling_edge_counter_1++; fan1_edge_counter++;
AC0.STATUS |= 0x10; //CMP flag to 0 AC0.STATUS |= 0x10; //CMP flag to 0
sei(); sei();
} }
@ -91,7 +92,7 @@ ISR(AC0_AC_vect){ // AC0 vec flag
ISR(AC1_AC_vect){ // AC1 vec flag ISR(AC1_AC_vect){ // AC1 vec flag
cli(); cli();
falling_edge_counter_2++; fan2_edge_counter++;
AC1.STATUS |= 0x10; //CMP flag to 0 AC1.STATUS |= 0x10; //CMP flag to 0
sei(); sei();
} }
@ -99,15 +100,10 @@ ISR(AC1_AC_vect){ // AC1 vec flag
// TIMER INTERUPT // TIMER INTERUPT
ISR (TCA0_OVF_vect) { ISR (TCA0_OVF_vect) {
cli(); cli();
RPM_calculation(falling_edge_counter_1,1); RPM_calculation(fan1_edge_counter,timer_period_ms);
RPM_calculation(falling_edge_counter_2,1); RPM_calculation(fan2_edge_counter,timer_period_ms);
falling_edge_counter_1 = 0; fan1_edge_counter = 0;
falling_edge_counter_2 = 0; fan2_edge_counter = 0;
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
sei(); sei();
} }
//-----------------------------------------------------------------------------------------------------
//-------------------------------------- S E T U P-----------------------------------------------------

View File

@ -1,6 +1,6 @@
/* /*
* File: fanseeeed.h * File: fanseeeed.h
* Author: inami * Author: inami, Helle Augland Grasmo
* *
* Created on 13. mars 2024, 13:38 * Created on 13. mars 2024, 13:38
*/ */
@ -22,26 +22,27 @@ extern "C" {
#include <math.h> #include <math.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/cpufunc.h>
// INITALICE TIMER COUNTER // INITALICE TIMER COUNTER
void init_TCA0(); void init_TCA0();
// UPDATE TIMER PERIODE // UPDATE TIMER PERIOD
void TCA0_update_period_ms (); void TCA0_update_period_ms ();
// TAKES INN A TIME AND A THE COUNTED FAN DIPS // TAKES INN A TIME AND A THE COUNTED FAN DIPS
// RETURNS THE RPM OF THE FAN // RETURNS THE RPM OF THE FAN
uint16_t RPM_calculation(uint16_t fancounter, uint16_t time); uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms);
// INITIALISING FAN PORTS // INITIALISING FAN PORTS
void init_fanports(); void init_fan_gpio();
// INIT AC0 TO COMPARE PD6 AND PD7 // INIT AC0 TO COMPARE PD6 AND PD7
void init_ac0(); void init_AC0();
// INIT AC1 TO COMPARE PD4 AND PD7 // INIT AC1 TO COMPARE PD4 AND PD7
void init_ac1(); void init_AC1();
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -24,10 +24,6 @@
#define F_CPU 4000000UL #define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
@ -36,15 +32,12 @@ int main(void) {
init_uart((uint16_t)9600); init_uart((uint16_t)9600);
stdout = &USART_stream; stdout = &USART_stream;
init_TCA0(); init_TCA0();
init_fanports(); init_fan_gpio();
init_ac0(); init_AC0();
init_ac1(); init_AC1();
sei(); sei();
/* Replace with your application code */
while (1) { while (1) {
//printf("loop")
; ;
} }
} }