no worries
This commit is contained in:
parent
f0a435d578
commit
ac5a054f8b
@ -1,36 +1,37 @@
|
||||
//#include <avr/ioavr128db48.h>
|
||||
|
||||
#include "fan_speeeed.h"
|
||||
#include "uart.h"
|
||||
|
||||
uint16_t timer_period_ms = 1;
|
||||
uint16_t fan_speed;
|
||||
volatile uint16_t falling_edge_counter_1 = 0;
|
||||
volatile uint16_t falling_edge_counter_2 = 0;
|
||||
#define MAX_PER 0x50
|
||||
uint16_t timer_period = 0.01;
|
||||
uint16_t fan_speed = 0;
|
||||
volatile uint16_t fan1_edge_counter = 0;
|
||||
volatile uint16_t fan2_edge_counter = 0;
|
||||
|
||||
|
||||
void init_TCA0() {
|
||||
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();
|
||||
//TCA0.SINGLE.PERBUF = 3906;
|
||||
TCA0_update_period();
|
||||
}
|
||||
|
||||
void TCA0_update_period_ms() {
|
||||
TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
|
||||
void TCA0_update_period() {
|
||||
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) {
|
||||
fan_speed = (fancounter / time);
|
||||
// COUNTINGS / TIME = FREQUENCY
|
||||
// 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 * 60/5;
|
||||
fancounter = 0;
|
||||
edge_counter = 0;
|
||||
return fan_speed;
|
||||
}
|
||||
|
||||
|
||||
void init_fanports() {
|
||||
void init_fan_gpio() {
|
||||
// CONFIGURE PINS AS ANALOG INPUTS
|
||||
|
||||
// PIN FOR FAN 1
|
||||
@ -49,7 +50,7 @@ void init_fanports() {
|
||||
PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc;
|
||||
}
|
||||
|
||||
void init_ac0(){
|
||||
void init_AC0(){
|
||||
//Wincontroll disabled
|
||||
AC0.CTRLB = 0x00;
|
||||
|
||||
@ -65,7 +66,7 @@ void init_ac0(){
|
||||
}
|
||||
|
||||
|
||||
void init_ac1(){
|
||||
void init_AC1(){
|
||||
//Wincontroll disabled
|
||||
AC1.CTRLB = 0x00;
|
||||
|
||||
@ -83,7 +84,7 @@ void init_ac1(){
|
||||
|
||||
ISR(AC0_AC_vect){ // AC0 vec flag
|
||||
cli();
|
||||
falling_edge_counter_1++;
|
||||
fan1_edge_counter++;
|
||||
AC0.STATUS |= 0x10; //CMP flag to 0
|
||||
sei();
|
||||
}
|
||||
@ -91,7 +92,7 @@ ISR(AC0_AC_vect){ // AC0 vec flag
|
||||
|
||||
ISR(AC1_AC_vect){ // AC1 vec flag
|
||||
cli();
|
||||
falling_edge_counter_2++;
|
||||
fan2_edge_counter++;
|
||||
AC1.STATUS |= 0x10; //CMP flag to 0
|
||||
sei();
|
||||
}
|
||||
@ -99,15 +100,10 @@ ISR(AC1_AC_vect){ // AC1 vec flag
|
||||
// TIMER INTERUPT
|
||||
ISR (TCA0_OVF_vect) {
|
||||
cli();
|
||||
RPM_calculation(falling_edge_counter_1,1);
|
||||
RPM_calculation(falling_edge_counter_2,1);
|
||||
falling_edge_counter_1 = 0;
|
||||
falling_edge_counter_2 = 0;
|
||||
RPM_calculation(fan1_edge_counter,timer_period_ms);
|
||||
RPM_calculation(fan2_edge_counter,timer_period_ms);
|
||||
fan1_edge_counter = 0;
|
||||
fan2_edge_counter = 0;
|
||||
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
|
||||
sei();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
//-------------------------------------- S E T U P-----------------------------------------------------
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* File: fanseeeed.h
|
||||
* Author: inami
|
||||
* Author: inami, Helle Augland Grasmo
|
||||
*
|
||||
* Created on 13. mars 2024, 13:38
|
||||
*/
|
||||
@ -22,26 +22,27 @@ extern "C" {
|
||||
#include <math.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/cpufunc.h>
|
||||
|
||||
|
||||
|
||||
// INITALICE TIMER COUNTER
|
||||
void init_TCA0();
|
||||
|
||||
// UPDATE TIMER PERIODE
|
||||
// UPDATE TIMER PERIOD
|
||||
void TCA0_update_period_ms ();
|
||||
|
||||
// TAKES INN A TIME AND A THE COUNTED FAN DIPS
|
||||
// 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
|
||||
void init_fanports();
|
||||
void init_fan_gpio();
|
||||
|
||||
// INIT AC0 TO COMPARE PD6 AND PD7
|
||||
void init_ac0();
|
||||
void init_AC0();
|
||||
|
||||
// INIT AC1 TO COMPARE PD4 AND PD7
|
||||
void init_ac1();
|
||||
void init_AC1();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -24,10 +24,6 @@
|
||||
|
||||
#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);
|
||||
stdout = &USART_stream;
|
||||
init_TCA0();
|
||||
init_fanports();
|
||||
init_ac0();
|
||||
init_ac1();
|
||||
|
||||
init_fan_gpio();
|
||||
init_AC0();
|
||||
init_AC1();
|
||||
sei();
|
||||
|
||||
/* Replace with your application code */
|
||||
while (1) {
|
||||
//printf("loop")
|
||||
;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user