Compare commits

..

2 Commits

Author SHA1 Message Date
8649d59d8d add positive edge 2024-04-25 12:29:10 +02:00
c9b220616d clean comments 2024-04-24 14:24:40 +02:00
3 changed files with 39 additions and 223 deletions

View File

@ -1,3 +1,5 @@
#include <avr/ioavr128db48.h>
#include "fan_speeeed.h" #include "fan_speeeed.h"
#include "uart.h" #include "uart.h"
@ -6,7 +8,7 @@ uint16_t fan_speed;
volatile uint16_t falling_edge_counter = 0; volatile uint16_t falling_edge_counter = 0;
#define MAX_PER 0x50 #define MAX_PER 0x50
void TCA0_init() { void init_TCA0() {
//code //code
PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc; PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc;
@ -19,7 +21,6 @@ void TCA0_init() {
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x03; TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x03;
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm; TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
//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_update_period_ms(); //TCA0_update_period_ms();
@ -36,124 +37,44 @@ uint16_t RPM_calculation(uint16_t test, uint16_t time) {
return fan_speed; return fan_speed;
} }
void PORT_init() {
//compare or capture
TCB3.CCMP = 0x00;
//count
TCB3.CNT = 0x00;
PORTA.IN = PIN5_bm;
PORTA.PIN5CTRL = PORT_PULLUPEN_bm;
//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.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.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;
TCB3.CTRLB = 0x03;
//DBGRUN disabled;
//TCB3.DBGCTRL = 0x00;
//FILTER disabled; EDGE disabled; CAPTEI enabled;
TCB3.EVCTRL = 0x01;
//OVF disabled; CAPT enabled;
TCB3.INTCTRL = 0x01;
//OVF disabledAPT disabled;
//TCB3.INTFLAGS = 0; CAPT disabled;
//TCB3.INTFLAGS = 0x00;
//Temporary Value
//TCB3.TEMP = 0x00;
//EVSYS.CHANNEL0 = 0x40;
//EVSYS.SWEVENTA = 0x01;
//EVSYS.USERTCB3CAPT = 0x01;
//RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled;
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){
TCB3.CTRLB = TCB_CNTMODE_FRQPW_gc; /* Input Capture Frequency */
// 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.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 */
// | TCB_ENABLE_bm /* Enable: enabled */
// | TCB_RUNSTDBY_bm; /* Run Standby: enabled */
//}
void EVSYS_Init(void) {
/* Zero-cross detector 0 out linked to event channel 0 */
EVSYS.CHANNEL0 = EVSYS_CHANNEL0_PORTA_PIN5_gc;
/* TCB uses event channel 0 */
EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc;
//test
//EVSYS.CHANNEL0 = 0x40;
//EVSYS.SWEVENTA = 0x01;
//EVSYS.USERTCB3CAPT = 0x01;
}
void PORTB_init() { void PORTB_init() {
PORTA.OUTSET |= PIN5_bm; PORTA.OUTSET |= PIN5_bm;
PORTA.DIRSET |= PIN5_bm; PORTA.DIRSET |= PIN5_bm;
} }
// Voltage reference
#define VDACREF
#define VREF
#define DACREF_VALUE (VDACREF * 256 / VREF)
void init_ac(){
// CONFIGURE PINS AS ANALOG INPUTS
PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc;
// SET BENCH MARK
//SELECT POSITIVER INPUTS muxs
AC0.MUXCTRLA = AC_MUXPOS_AINP0_gc | AC_MUXNEG_DACREF_gc;
// 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;
// SET CMP TO 1
AC0.INTCTRL = 0x01;
}
ISR(AC0_AC_vect){ // AC0 vec flag
AC0.STATUS |= 0x10; //CMP flag to 0.
}
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
//-------------------------------------- S E T U P----------------------------------------------------- //-------------------------------------- S E T U P-----------------------------------------------------
/*
ISR ( TCA0_OVF_vect ) {
cli();
RPM_calculation();
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
sei();
}*/
void printnie(uint8_t en, uint8_t to) {
printf("CNT %u", en);
printf("ccmp %u", to);
}
/*ISR (TCB0_INT_vect){
uint16_t yo = TCB0.CNT;
uint16_t stewui = TCB0.CCMP;
printnie(yo, stewui);
TCB3.INTFLAGS = 0x03;
//TCB3.INTFLAGS = PIN0_bm;
}
*/
/*
ISR(PORTD_PORT_vect){
// cli();
falling_edge_counter ++;
PORTD.INTFLAGS = PIN2_bm;
//sei();
}
* */

View File

@ -24,15 +24,11 @@ extern "C" {
#include <avr/io.h> #include <avr/io.h>
#include <avr/cpufunc.h> #include <avr/cpufunc.h>
void TCA0_init (); void init_TCA0();
void PORT_init();
void PORTB_init();
void testsettup();
void TCA0_update_period_ms (); void TCA0_update_period_ms ();
uint16_t RPM_calculation(uint16_t test, uint16_t time); uint16_t RPM_calculation(uint16_t test, uint16_t time);
void EVSYS_Init(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -22,56 +22,6 @@
#include "themistor-temp.h" #include "themistor-temp.h"
#include "fan_speeeed.h" #include "fan_speeeed.h"
/*
void main_1() {
init_uart((uint16_t)9600);
stdout = &USART_stream;
TCA0_init ();*/
//PORT_init();
//test
//TCB0_Init();
//PORT_init();
//EVSYS_Init();
//PORTB_init();
// testsettup();
//TCB3.INTCTRL = 0b00000001; // Bit 0 ? CAPT Capture Interrupt Enable
//sei();
//while (1) {
/*
printf("loop");
uint16_t yo = TCB3.CNT;
printf("CNT %u", yo);
uint16_t stewui = TCB3.CCMP;
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){
uint16_t yo = TCB3.CNT;
printf("CNT %u", yo);
uint16_t stewui = TCB3.CCMP;
printf("CCMP %u", stewui);
TCB3.INTFLAGS = 0x03;
//TCB3.INTFLAGS = PIN0_bm;
}*/
#define F_CPU 4000000UL #define F_CPU 4000000UL
#include <avr/io.h> #include <avr/io.h>
@ -79,69 +29,18 @@ void main_1() {
#include <util/delay.h> #include <util/delay.h>
#define MAX_PER 0x50
volatile uint16_t data = 0;
volatile uint16_t cnt = 0;
int main(void) { int main(void) {
init_uart((uint16_t)9600); init_uart((uint16_t)9600);
stdout = &USART_stream; 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(); sei();
PIN2_bm
/* Replace with your application code */ /* Replace with your application code */
while (1) { 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;
} }