Compare commits

..

No commits in common. "10c82d001eea8851c70eb022880660b33f26ae8b" and "6528a82b0402a1d057c64d7e150b19cb39e9f5cb" have entirely different histories.

3 changed files with 26 additions and 39 deletions

View File

@ -8,62 +8,57 @@ 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 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);
uint16_t RPM_calculation(uint16_t test, uint16_t time) {
fan_speed = (test / time)*6000 * 3;
falling_edge_counter = 0;
printf("falling edge: ", falling_edge_counter);
printf("fanspeed %u \n", fan_speed);
printf("%u", fan_speed);
return fan_speed;
}
void PORTB_init() {
PORTA.OUTSET |= PIN5_bm;
PORTA.DIRSET |= PIN5_bm;
}
// Voltage reference
#define VDACREF
#define VREF
#define DACREF_VALUE (VDACREF * 256 / VREF)
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;
@ -78,30 +73,23 @@ 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;
// TURN ON INTERUPT
// SET CMP TO 1
AC0.INTCTRL = 0x01;
}
void test(){
printf("yo");
}
ISR(AC0_AC_vect){ // AC0 vec flag
cli();
falling_edge_counter++;
AC0.STATUS |= 0x10; //CMP flag to 0.
//printf("yo");
sei();
test();
}
ISR (TCA0_OVF_vect) {
cli();
RPM_calculation(1);
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
sei();
}
//-----------------------------------------------------------------------------------------------------
//-------------------------------------- S E T U P-----------------------------------------------------

View File

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

View File

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