works but not freq

This commit is contained in:
Elp03 2024-04-21 12:43:51 +02:00
parent 57a35a483d
commit a48a089a3c
3 changed files with 176 additions and 59 deletions

View File

@ -4,41 +4,55 @@
uint16_t timer_period_ms = 1; uint16_t timer_period_ms = 1;
uint16_t fan_speed; uint16_t fan_speed;
volatile uint16_t falling_edge_counter = 0; volatile uint16_t falling_edge_counter = 0;
#define MAX_PER 0x50
void TCA0_init() { void TCA0_init() {
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ;
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ //code
TCA0_update_period_ms(); PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc;
// port d ??? why
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.INTCTRL = TCA_SINGLE_OVF_bm;
//TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc; /* Sysclk /1024 */
//TCA0_update_period_ms();
} }
void 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 */ TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
} }
uint16_t RPM_calculation() { uint16_t RPM_calculation(uint16_t test, uint16_t time) {
fan_speed = (falling_edge_counter/timer_period_ms)*6000*3; fan_speed = (test / time)*6000 * 3;
falling_edge_counter= 0; falling_edge_counter = 0;
printf("%u", fan_speed); printf("%u", fan_speed);
return fan_speed; return fan_speed;
} }
void PORT_init(){ void PORT_init() {
//compare or capture //compare or capture
//TCB3.CCMP = 0x00; TCB3.CCMP = 0x00;
//count //count
TCB3.CNT = 0x00; TCB3.CNT = 0x00;
PORTA.IN = PIN0_bm; PORTA.IN = PIN5_bm;
PORTA.PIN0CTRL = PORT_PULLUPEN_bm; PORTA.PIN5CTRL = PORT_PULLUPEN_bm;
//PORTD.PIN2CTRL = PORT_ISC_FALLING_gc | 0b01000000; //PORTD.PIN2CTRL = PORT_ISC_FALLING_gc | 0b01000000;
// TCB3.CTRLB =0<< TCB_ASYNC_bp /* Asynchronous Enable: disabled */|0<< TCB_CCMPEN_bp /* Pin Output Enable: disabled */|0<< TCB_CCMPINIT_bp /* Pin Initial State: disabled */| TCB_CNTMODE_FRQ_gc;/* Input Capture Frequency measurement */ // TCB3.CTRLB =0<< TCB_ASYNC_bp /* Asynchronous Enable: disabled */|0<< TCB_CCMPEN_bp /* Pin Output Enable: disabled */|0<< TCB_CCMPINIT_bp /* Pin Initial State: disabled */| TCB_CNTMODE_FRQ_gc;/* Input Capture Frequency measurement */
// TCB3.EVCTRL =1<< TCB_CAPTEI_bp /* Event Input Enable: enabled */|0<< TCB_EDGE_bp /* Event Edge: disabled */|0<< TCB_FILTER_bp;/* Input Capture Noise Cancellation Filter: disabled */ // TCB3.EVCTRL =1<< TCB_CAPTEI_bp /* Event Input Enable: enabled */|0<< TCB_EDGE_bp /* Event Edge: disabled */|0<< TCB_FILTER_bp;/* Input Capture Noise Cancellation Filter: disabled */
// TCB3.INTCTRL =1<< TCB_CAPT_bp /* Capture or Timeout: enabled */|0<< TCB_OVF_bp;/* OverFlow Interrupt: disabled */ // TCB3.INTCTRL =1<< TCB_CAPT_bp /* Capture or Timeout: enabled */|0<< TCB_OVF_bp;/* OverFlow Interrupt: disabled */
// TCB3.CTRLA = TCB_CLKSEL_DIV1_gc /* CLK_PER */|1<< TCB_ENABLE_bp /* Enable: enabled */|0<< TCB_RUNSTDBY_bp /* Run Standby: disabled */|0<< TCB_SYNCUPD_bp /* Synchronize Update: disabled */|0<< TCB_CASCADE_bp;/* Cascade Two Timer/Counters: disabled */ // TCB3.CTRLA = TCB_CLKSEL_DIV1_gc /* CLK_PER */|1<< TCB_ENABLE_bp /* Enable: enabled */|0<< TCB_RUNSTDBY_bp /* Run Standby: disabled */|0<< TCB_SYNCUPD_bp /* Synchronize Update: disabled */|0<< TCB_CASCADE_bp;/* Cascade Two Timer/Counters: disabled */
//ASYNC disabled; CCMPINIT disabled; CCMPEN disabled; CNTMODE FRQPW; //ASYNC disabled; CCMPINIT disabled; CCMPEN disabled; CNTMODE FRQPW;
TCB3.CTRLB = 0x04; TCB3.CTRLB = 0x03;
//DBGRUN disabled; //DBGRUN disabled;
//TCB3.DBGCTRL = 0x00; //TCB3.DBGCTRL = 0x00;
@ -56,36 +70,53 @@ void PORT_init(){
//Temporary Value //Temporary Value
//TCB3.TEMP = 0x00; //TCB3.TEMP = 0x00;
EVSYS.CHANNEL0 = 0x40; //EVSYS.CHANNEL0 = 0x40;
//EVSYS.SWEVENTA = 0x01; //EVSYS.SWEVENTA = 0x01;
EVSYS.USERTCB3CAPT = 0x01; //EVSYS.USERTCB3CAPT = 0x01;
//RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled; //RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled;
TCB3.CTRLA = 0x01; TCB3.CTRLA = 0x01;
} }
void testsettup() {
PORTA.IN = PIN3_bm;
PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
EVSYS.CHANNEL0 = 0x43;
TCB0.EVCTRL = TCB_CAPTEI_bm;
TCB0.INTCTRL = TCB_CAPT_bm;
TCB0.CTRLB = TCB_CNTMODE_FRQ_gc;
EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc;
TCB0.CTRLA = TCB_CLKSEL_TCA0_gc | TCB_ENABLE_bm;
}
/*
void TCB0_Init(void){ void TCB0_Init(void){
TCB3.CTRLB = TCB_CNTMODE_FRQPW_gc; /* Input Capture Frequency */ TCB3.CTRLB = TCB_CNTMODE_FRQPW_gc; /* Input Capture Frequency */
TCB3.EVCTRL = TCB_CAPTEI_bm; /* Event Input Enable: enabled */ // TCB3.EVCTRL = TCB_CAPTEI_bm; /* Event Input Enable: enabled */
TCB3.EVCTRL |= (1 << TCB_CAPTEI_bp | 1 << TCB_EDGE_bp | 1 << TCB_FILTER_bp); /* Event Input Enable: enabled */ // TCB3.EVCTRL |= (1 << TCB_CAPTEI_bp | 1 << TCB_EDGE_bp | 1 << TCB_FILTER_bp); /* Event Input Enable: enabled */
TCB3.INTCTRL = TCB_CAPT_bm; /* Capture or Timeout: enabled */ // TCB3.INTCTRL = TCB_CAPT_bm; /* Capture or Timeout: enabled */
TCB3.CTRLA = TCB_CLKSEL_DIV1_gc /* CLK_PER/1 (From Prescaler) - This is needed to be able to count to 40 Hz with a 4 Mhz system clock */ //TCB3.CTRLA = TCB_CLKSEL_DIV1_gc /* CLK_PER/1 (From Prescaler) - This is needed to be able to count to 40 Hz with a 4 Mhz system clock */
| TCB_ENABLE_bm /* Enable: enabled */ // | TCB_ENABLE_bm /* Enable: enabled */
| TCB_RUNSTDBY_bm; /* Run Standby: enabled */ // | TCB_RUNSTDBY_bm; /* Run Standby: enabled */
} //}
void EVSYS_Init(void)
{ void EVSYS_Init(void) {
/* Zero-cross detector 0 out linked to event channel 0 */ /* Zero-cross detector 0 out linked to event channel 0 */
EVSYS.CHANNEL0 = EVSYS_CHANNEL0_PORTA_PIN5_gc; EVSYS.CHANNEL0 = EVSYS_CHANNEL0_PORTA_PIN5_gc;
/* TCB uses event channel 0 */ /* TCB uses event channel 0 */
EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc; EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc;
//test
//EVSYS.CHANNEL0 = 0x40;
//EVSYS.SWEVENTA = 0x01;
//EVSYS.USERTCB3CAPT = 0x01;
} }
void PORT_Init(void) void PORTB_init() {
{ PORTA.OUTSET |= PIN5_bm;
PORTB.OUTSET |= PIN5_bm; PORTA.DIRSET |= PIN5_bm;
PORTB.DIRSET |= PIN5_bm;
} }
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
//-------------------------------------- S E T U P----------------------------------------------------- //-------------------------------------- S E T U P-----------------------------------------------------
@ -100,22 +131,22 @@ ISR ( TCA0_OVF_vect ) {
sei(); sei();
}*/ }*/
void printnie(uint8_t en, uint8_t to){ void printnie(uint8_t en, uint8_t to) {
printf("CNT %u", en); printf("CNT %u", en);
printf("ccmp %u", to); printf("ccmp %u", to);
} }
ISR (TCB3_INT_vect){ /*ISR (TCB0_INT_vect){
uint16_t yo = TCB3.CNT; uint16_t yo = TCB0.CNT;
uint16_t stewui = TCB3.CCMP; uint16_t stewui = TCB0.CCMP;
printnie(yo, stewui); printnie(yo, stewui);
TCB3.INTFLAGS = 0x03; TCB3.INTFLAGS = 0x03;
//TCB3.INTFLAGS = PIN0_bm; //TCB3.INTFLAGS = PIN0_bm;
} }
*/
/* /*
ISR(PORTD_PORT_vect){ ISR(PORTD_PORT_vect){

View File

@ -26,9 +26,13 @@ extern "C" {
void TCA0_init (); void TCA0_init ();
void PORT_init(); void PORT_init();
void PORTB_init();
void testsettup();
void TCA0_update_period_ms (); void TCA0_update_period_ms ();
uint16_t RPM_calculation(); uint16_t RPM_calculation(uint16_t test, uint16_t time);
void EVSYS_Init(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -13,10 +13,7 @@
#include <util/delay.h> #include <util/delay.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
//#define F_CPU 4E6 //#define F_CPU 4E6
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "uart.h" #include "uart.h"
@ -25,22 +22,26 @@
#include "themistor-temp.h" #include "themistor-temp.h"
#include "fan_speeeed.h" #include "fan_speeeed.h"
/*
int main() { void main_1() {
init_uart((uint16_t)9600); init_uart((uint16_t)9600);
stdout = &USART_stream; stdout = &USART_stream;
TCA0_init (); TCA0_init ();*/
//PORT_init(); //PORT_init();
//test //test
//TCB0_Init();
TCB0_Init(); //PORT_init();
EVSYS_Init(); //EVSYS_Init();
PORT_Init(); //PORTB_init();
TCB3.INTCTRL = 0b00000001; // Bit 0 ? CAPT Capture Interrupt Enable // testsettup();
sei(); //TCB3.INTCTRL = 0b00000001; // Bit 0 ? CAPT Capture Interrupt Enable
//sei();
while (1) { //while (1) {
/* /*
printf("loop"); printf("loop");
uint16_t yo = TCB3.CNT; uint16_t yo = TCB3.CNT;
@ -48,9 +49,16 @@ int main() {
uint16_t stewui = TCB3.CCMP; uint16_t stewui = TCB3.CCMP;
printf("CCMP %u", stewui); printf("CCMP %u", stewui);
*/ */
; //EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc;
} // EVSYS.CHANNEL0 = EVSYS_CHANNEL0_PORTA_PIN5_gc;
} /* TCB uses event channel 0 */
/// EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc;
//uint8_t test = TCB3.CCMP;
//printf("ccmp %u", test);
// _delay_ms(500);
// ;
// }
//}
/*ISR (TCB3_INT_vect){ /*ISR (TCB3_INT_vect){
uint16_t yo = TCB3.CNT; uint16_t yo = TCB3.CNT;
@ -62,4 +70,78 @@ int main() {
TCB3.INTFLAGS = 0x03; TCB3.INTFLAGS = 0x03;
//TCB3.INTFLAGS = PIN0_bm; //TCB3.INTFLAGS = PIN0_bm;
}*/ }*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define MAX_PER 0x50
volatile uint16_t data = 0;
volatile uint16_t cnt = 0;
int main(void) {
init_uart((uint16_t)9600);
stdout = &USART_stream;
//TCA Init
/*
PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc;
PORTD.DIR = PIN0_bm;
TCA0.SINGLE.PERBUF = MAX_PER;
TCA0.SINGLE.CMP0BUF = MAX_PER / 2;
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x3;
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
*/
TCA0_init();
//TCB Init
/*
PORTA.IN = PIN3_bm;
PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
EVSYS.CHANNEL0 = 0x43;
TCB0.EVCTRL = TCB_CAPTEI_bm;
TCB0.INTCTRL = TCB_CAPT_bm;
TCB0.CTRLB = TCB_CNTMODE_FRQ_gc;
EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc;
TCB0.CTRLA = TCB_CLKSEL_TCA0_gc | TCB_ENABLE_bm;
*/
//testsettup();
PORT_init();
sei();
/* Replace with your application code */
while (1) {
if (data) {
printf("TEST1_____ %u ", data);
data = RPM_calculation(data, cnt);
printf("TEST1 %u ", data);
//printf("ccnt %d", cnt);
data = 0;
}
}
}
ISR(TCB0_INT_vect) {
data = TCB0.CCMP;
cnt = TCB0.CNT;
TCB0.INTFLAGS = TCB_CAPT_bm;
}