Compare commits
No commits in common. "275c40498fb026a7dace972b099616b1b30dd927" and "495cbbba44991104599e0698b8b7418f5716fe33" have entirely different histories.
275c40498f
...
495cbbba44
@ -1,107 +0,0 @@
|
|||||||
#include "fan_speeeed.h"
|
|
||||||
#include "uart.h"
|
|
||||||
|
|
||||||
uint16_t timer_period_ms = 1;
|
|
||||||
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_update_period(timer_period_ms);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCA0_update_period(uint16_t timer_period) {
|
|
||||||
TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
edge_counter = 0;
|
|
||||||
return fan_speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void init_fan_gpio() {
|
|
||||||
// CONFIGURE PINS AS ANALOG INPUTS
|
|
||||||
|
|
||||||
// PIN FOR FAN 1
|
|
||||||
PORTD.DIRSET &= ~PIN6_bm;
|
|
||||||
PORTD.PIN6CTRL &= ~ PORT_ISC_gm;
|
|
||||||
PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc;
|
|
||||||
|
|
||||||
// PIN FOR FAN 2
|
|
||||||
PORTD.DIRSET &= ~PIN4_bm;
|
|
||||||
PORTD.PIN4CTRL &= ~ PORT_ISC_gm ;
|
|
||||||
PORTD.PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc;
|
|
||||||
|
|
||||||
// PIN FOR REFRENCE
|
|
||||||
PORTD.DIRSET &= PIN7_bm;
|
|
||||||
PORTD.PIN7CTRL &= ~ PORT_ISC_gm ;
|
|
||||||
PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc;
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_AC0(){
|
|
||||||
//Wincontroll disabled
|
|
||||||
AC0.CTRLB = 0x00;
|
|
||||||
|
|
||||||
//SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON
|
|
||||||
// FAN USE PD6 COMPARE WITH PD3
|
|
||||||
AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN2_gc;
|
|
||||||
|
|
||||||
// ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA
|
|
||||||
AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
|
|
||||||
|
|
||||||
// TURN ON INTERUPT
|
|
||||||
AC0.INTCTRL = 0x01;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void init_AC1(){
|
|
||||||
//Wincontroll disabled
|
|
||||||
AC1.CTRLB = 0x00;
|
|
||||||
|
|
||||||
//SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON
|
|
||||||
// FAN USE PD4, COMPARE WITH PD3
|
|
||||||
AC1.MUXCTRL = AC_MUXPOS_AINP2_gc | AC_MUXNEG_AINN2_gc;
|
|
||||||
|
|
||||||
// ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA
|
|
||||||
AC1.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
|
|
||||||
|
|
||||||
// TURN ON INTERUPT
|
|
||||||
AC1.INTCTRL = 0x01;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ISR(AC0_AC_vect){ // AC0 vec flag
|
|
||||||
cli();
|
|
||||||
fan1_edge_counter++;
|
|
||||||
AC0.STATUS |= 0x10; //CMP flag to 0
|
|
||||||
sei();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ISR(AC1_AC_vect){ // AC1 vec flag
|
|
||||||
cli();
|
|
||||||
fan2_edge_counter++;
|
|
||||||
AC1.STATUS |= 0x10; //CMP flag to 0
|
|
||||||
sei();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TIMER INTERUPT
|
|
||||||
ISR (TCA0_OVF_vect) {
|
|
||||||
cli();
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* File: fanseeeed.h
|
|
||||||
* Author: inami, Helle Augland Grasmo
|
|
||||||
*
|
|
||||||
* Created on 13. mars 2024, 13:38
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef FANSEEEED_H
|
|
||||||
#define FANSEEEED_H
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <float.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <avr/io.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
The code has inspiration from "Getting Started with Analog comparator(AC)" by Microchip for setting up analog comparrator.
|
|
||||||
* web link: https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ApplicationNotes/ApplicationNotes/TB3211-Getting-Started-with-AC-DS90003211.pdf
|
|
||||||
* and inspiration form practice 6 for TCA0 setup
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
// INITALICE TIMER COUNTER
|
|
||||||
void init_TCA0();
|
|
||||||
|
|
||||||
// UPDATE TIMER PERIOD
|
|
||||||
void TCA0_update_period_ms (uint16_t timer_period);
|
|
||||||
|
|
||||||
// TAKES INN A TIME AND A THE COUNTED FAN DIPS
|
|
||||||
// RETURNS THE RPM OF THE FAN
|
|
||||||
uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms);
|
|
||||||
|
|
||||||
// INITIALISING FAN PORTS
|
|
||||||
void init_fan_gpio();
|
|
||||||
|
|
||||||
// INIT AC0 TO COMPARE PD6 AND PD7
|
|
||||||
void init_AC0();
|
|
||||||
|
|
||||||
// INIT AC1 TO COMPARE PD4 AND PD7
|
|
||||||
void init_AC1();
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* FANSEEEED_H */
|
|
||||||
|
|
||||||
@ -4,9 +4,9 @@
|
|||||||
*
|
*
|
||||||
* Created on March 6, 2024, 12:34 PM
|
* Created on March 6, 2024, 12:34 PM
|
||||||
*/
|
*/
|
||||||
|
#include <stdbool.h>
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
#include "voltage.h"
|
#include "voltage.h"
|
||||||
#include <stdbool.h>
|
|
||||||
#define RTC_PERIOD (511)
|
#define RTC_PERIOD (511)
|
||||||
#define DELAY_TIME 1000
|
#define DELAY_TIME 1000
|
||||||
#include "eeprom.h"
|
#include "eeprom.h"
|
||||||
@ -24,11 +24,11 @@
|
|||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
|
|
||||||
// Fan history variables
|
// Fan history variables
|
||||||
volatile uint16_t fan1_history[512] = {0};
|
volatile uint16_t fan1_history[512] = { 0 };
|
||||||
volatile uint16_t fan2_history[512] = {0};
|
volatile uint16_t fan2_history[512] = { 0 };
|
||||||
|
|
||||||
// Default config is 500ms sample rate
|
// Default config is 500ms sample rate
|
||||||
volatile config_t config = {500};
|
volatile config_t config = { 500 };
|
||||||
volatile bool store_config = false;
|
volatile bool store_config = false;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@ -47,10 +47,10 @@ int main() {
|
|||||||
sei();
|
sei();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// If we have made a config change, store it.
|
// If we have made a config change, store it.
|
||||||
if (store_config) {
|
if (store_config) {
|
||||||
write_struct_from_EEPROM(config);
|
write_struct_from_EEPROM(config);
|
||||||
store_config = false;
|
store_config = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,19 +4,17 @@
|
|||||||
<logicalFolder name="HeaderFiles"
|
<logicalFolder name="HeaderFiles"
|
||||||
displayName="Header Files"
|
displayName="Header Files"
|
||||||
projectFiles="true">
|
projectFiles="true">
|
||||||
<itemPath>uart.h</itemPath>
|
|
||||||
<itemPath>voltage.h</itemPath>
|
|
||||||
<itemPath>fan_speeeed.h</itemPath>
|
|
||||||
<itemPath>themistor-temp.h</itemPath>
|
|
||||||
<itemPath>command-handler.h</itemPath>
|
<itemPath>command-handler.h</itemPath>
|
||||||
<itemPath>eeprom.h</itemPath>
|
|
||||||
<itemPath>i2c.h</itemPath>
|
<itemPath>i2c.h</itemPath>
|
||||||
|
<itemPath>themistor-temp.h</itemPath>
|
||||||
|
<itemPath>uart.h</itemPath>
|
||||||
|
<itemPath>eeprom.h</itemPath>
|
||||||
|
<itemPath>voltage.h</itemPath>
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
<logicalFolder name="ExternalFiles"
|
<logicalFolder name="ExternalFiles"
|
||||||
displayName="Important Files"
|
displayName="Important Files"
|
||||||
projectFiles="true">
|
projectFiles="true">
|
||||||
<itemPath>Makefile</itemPath>
|
<itemPath>Makefile</itemPath>
|
||||||
<itemPath>prosjekt.mc3</itemPath>
|
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
<logicalFolder name="LinkerScript"
|
<logicalFolder name="LinkerScript"
|
||||||
displayName="Linker Files"
|
displayName="Linker Files"
|
||||||
@ -27,12 +25,11 @@
|
|||||||
projectFiles="true">
|
projectFiles="true">
|
||||||
<itemPath>main.c</itemPath>
|
<itemPath>main.c</itemPath>
|
||||||
<itemPath>uart.c</itemPath>
|
<itemPath>uart.c</itemPath>
|
||||||
<itemPath>voltage.c</itemPath>
|
|
||||||
<itemPath>fan_speeeed.c</itemPath>
|
|
||||||
<itemPath>thermistor-temp.c</itemPath>
|
|
||||||
<itemPath>command-handler.c</itemPath>
|
|
||||||
<itemPath>i2c.c</itemPath>
|
|
||||||
<itemPath>eeprom.c</itemPath>
|
<itemPath>eeprom.c</itemPath>
|
||||||
|
<itemPath>voltage.c</itemPath>
|
||||||
|
<itemPath>i2c.c</itemPath>
|
||||||
|
<itemPath>command-handler.c</itemPath>
|
||||||
|
<itemPath>thermistor-temp.c</itemPath>
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
<projectmakefile>Makefile</projectmakefile>
|
<projectmakefile>Makefile</projectmakefile>
|
||||||
@ -45,11 +42,11 @@
|
|||||||
<targetPluginBoard></targetPluginBoard>
|
<targetPluginBoard></targetPluginBoard>
|
||||||
<platformTool>nEdbgTool</platformTool>
|
<platformTool>nEdbgTool</platformTool>
|
||||||
<languageToolchain>XC8</languageToolchain>
|
<languageToolchain>XC8</languageToolchain>
|
||||||
<languageToolchainVersion>2.45</languageToolchainVersion>
|
<languageToolchainVersion>2.46</languageToolchainVersion>
|
||||||
<platform>2</platform>
|
<platform>2</platform>
|
||||||
</toolsSet>
|
</toolsSet>
|
||||||
<packs>
|
<packs>
|
||||||
<pack name="AVR-Dx_DFP" vendor="Microchip" version="2.4.286"/>
|
<pack name="AVR-Dx_DFP" vendor="Microchip" version="2.3.272"/>
|
||||||
</packs>
|
</packs>
|
||||||
<ScriptingSettings>
|
<ScriptingSettings>
|
||||||
</ScriptingSettings>
|
</ScriptingSettings>
|
||||||
@ -114,7 +111,6 @@
|
|||||||
</HI-TECH-COMP>
|
</HI-TECH-COMP>
|
||||||
<HI-TECH-LINK>
|
<HI-TECH-LINK>
|
||||||
<property key="additional-options-checksum" value=""/>
|
<property key="additional-options-checksum" value=""/>
|
||||||
<property key="additional-options-checksumAVR" value=""/>
|
|
||||||
<property key="additional-options-code-offset" value=""/>
|
<property key="additional-options-code-offset" value=""/>
|
||||||
<property key="additional-options-command-line" value=""/>
|
<property key="additional-options-command-line" value=""/>
|
||||||
<property key="additional-options-errata" value=""/>
|
<property key="additional-options-errata" value=""/>
|
||||||
@ -160,54 +156,7 @@
|
|||||||
<property key="remove-unused-sections" value="true"/>
|
<property key="remove-unused-sections" value="true"/>
|
||||||
</HI-TECH-LINK>
|
</HI-TECH-LINK>
|
||||||
<Tool>
|
<Tool>
|
||||||
<property key="AutoSelectMemRanges" value="auto"/>
|
|
||||||
<property key="communication.activationmode" value="nohv"/>
|
|
||||||
<property key="communication.interface" value="updi"/>
|
|
||||||
<property key="communication.speed" value="0,500"/>
|
|
||||||
<property key="debugoptions.debug-startup" value="Use system settings"/>
|
|
||||||
<property key="debugoptions.reset-behaviour" value="Use system settings"/>
|
|
||||||
<property key="debugoptions.useswbreakpoints" value="true"/>
|
<property key="debugoptions.useswbreakpoints" value="true"/>
|
||||||
<property key="event.recorder.debugger.behavior" value="Running"/>
|
|
||||||
<property key="event.recorder.enabled" value="false"/>
|
|
||||||
<property key="event.recorder.scvd.files" value=""/>
|
|
||||||
<property key="firmware.path"
|
|
||||||
value="Press to browse for a specific firmware version"/>
|
|
||||||
<property key="firmware.toolpack"
|
|
||||||
value="Press to select which tool pack to use"/>
|
|
||||||
<property key="firmware.update.action" value="firmware.update.use.latest"/>
|
|
||||||
<property key="freeze.timers" value="false"/>
|
|
||||||
<property key="lastid" value=""/>
|
|
||||||
<property key="memories.aux" value="false"/>
|
|
||||||
<property key="memories.bootflash" value="true"/>
|
|
||||||
<property key="memories.configurationmemory" value="true"/>
|
|
||||||
<property key="memories.configurationmemory2" value="true"/>
|
|
||||||
<property key="memories.dataflash" value="true"/>
|
|
||||||
<property key="memories.eeprom" value="true"/>
|
|
||||||
<property key="memories.exclude.configurationmemory" value="true"/>
|
|
||||||
<property key="memories.flashdata" value="true"/>
|
|
||||||
<property key="memories.id" value="true"/>
|
|
||||||
<property key="memories.instruction.ram.ranges"
|
|
||||||
value="${memories.instruction.ram.ranges}"/>
|
|
||||||
<property key="memories.programmemory" value="true"/>
|
|
||||||
<property key="memories.programmemory.ranges" value="0-ffff"/>
|
|
||||||
<property key="poweroptions.powerenable" value="false"/>
|
|
||||||
<property key="programmerToGoFilePath"
|
|
||||||
value="C:/Users/inami/Documents/mikrokontrollersystemer-prosjekt/prosjekt.X/debug/default/prosjekt_ptg"/>
|
|
||||||
<property key="programoptions.eraseb4program" value="true"/>
|
|
||||||
<property key="programoptions.preservedataflash" value="false"/>
|
|
||||||
<property key="programoptions.preservedataflash.ranges"
|
|
||||||
value="${memories.dataflash.default}"/>
|
|
||||||
<property key="programoptions.preserveeeprom" value="false"/>
|
|
||||||
<property key="programoptions.preserveeeprom.ranges" value="1400-15ff"/>
|
|
||||||
<property key="programoptions.preserveprogram.ranges" value=""/>
|
|
||||||
<property key="programoptions.preserveprogramrange" value="false"/>
|
|
||||||
<property key="programoptions.preserveuserid" value="false"/>
|
|
||||||
<property key="programoptions.programuserotp" value="false"/>
|
|
||||||
<property key="toolpack.updateoptions"
|
|
||||||
value="toolpack.updateoptions.uselatestoolpack"/>
|
|
||||||
<property key="toolpack.updateoptions.packversion"
|
|
||||||
value="Press to select which tool pack to use"/>
|
|
||||||
<property key="voltagevalue" value=""/>
|
|
||||||
</Tool>
|
</Tool>
|
||||||
<XC8-CO>
|
<XC8-CO>
|
||||||
<property key="coverage-enable" value=""/>
|
<property key="coverage-enable" value=""/>
|
||||||
@ -232,52 +181,7 @@
|
|||||||
<property key="wpo-lto" value="false"/>
|
<property key="wpo-lto" value="false"/>
|
||||||
</XC8-config-global>
|
</XC8-config-global>
|
||||||
<nEdbgTool>
|
<nEdbgTool>
|
||||||
<property key="AutoSelectMemRanges" value="auto"/>
|
|
||||||
<property key="communication.activationmode" value="nohv"/>
|
|
||||||
<property key="communication.interface" value="updi"/>
|
|
||||||
<property key="communication.speed" value="0,500"/>
|
|
||||||
<property key="debugoptions.debug-startup" value="Use system settings"/>
|
|
||||||
<property key="debugoptions.reset-behaviour" value="Use system settings"/>
|
|
||||||
<property key="debugoptions.useswbreakpoints" value="true"/>
|
<property key="debugoptions.useswbreakpoints" value="true"/>
|
||||||
<property key="event.recorder.debugger.behavior" value="Running"/>
|
|
||||||
<property key="event.recorder.enabled" value="false"/>
|
|
||||||
<property key="event.recorder.scvd.files" value=""/>
|
|
||||||
<property key="firmware.path"
|
|
||||||
value="Press to browse for a specific firmware version"/>
|
|
||||||
<property key="firmware.toolpack"
|
|
||||||
value="Press to select which tool pack to use"/>
|
|
||||||
<property key="firmware.update.action" value="firmware.update.use.latest"/>
|
|
||||||
<property key="freeze.timers" value="false"/>
|
|
||||||
<property key="lastid" value=""/>
|
|
||||||
<property key="memories.aux" value="false"/>
|
|
||||||
<property key="memories.bootflash" value="true"/>
|
|
||||||
<property key="memories.configurationmemory" value="true"/>
|
|
||||||
<property key="memories.configurationmemory2" value="true"/>
|
|
||||||
<property key="memories.dataflash" value="true"/>
|
|
||||||
<property key="memories.eeprom" value="true"/>
|
|
||||||
<property key="memories.exclude.configurationmemory" value="true"/>
|
|
||||||
<property key="memories.flashdata" value="true"/>
|
|
||||||
<property key="memories.id" value="true"/>
|
|
||||||
<property key="memories.instruction.ram.ranges"
|
|
||||||
value="${memories.instruction.ram.ranges}"/>
|
|
||||||
<property key="memories.programmemory" value="true"/>
|
|
||||||
<property key="memories.programmemory.ranges" value="0-ffff"/>
|
|
||||||
<property key="poweroptions.powerenable" value="false"/>
|
|
||||||
<property key="programoptions.eraseb4program" value="true"/>
|
|
||||||
<property key="programoptions.preservedataflash" value="false"/>
|
|
||||||
<property key="programoptions.preservedataflash.ranges"
|
|
||||||
value="${memories.dataflash.default}"/>
|
|
||||||
<property key="programoptions.preserveeeprom" value="false"/>
|
|
||||||
<property key="programoptions.preserveeeprom.ranges" value="1400-15ff"/>
|
|
||||||
<property key="programoptions.preserveprogram.ranges" value=""/>
|
|
||||||
<property key="programoptions.preserveprogramrange" value="false"/>
|
|
||||||
<property key="programoptions.preserveuserid" value="false"/>
|
|
||||||
<property key="programoptions.programuserotp" value="false"/>
|
|
||||||
<property key="toolpack.updateoptions"
|
|
||||||
value="toolpack.updateoptions.uselatestoolpack"/>
|
|
||||||
<property key="toolpack.updateoptions.packversion"
|
|
||||||
value="Press to select which tool pack to use"/>
|
|
||||||
<property key="voltagevalue" value=""/>
|
|
||||||
</nEdbgTool>
|
</nEdbgTool>
|
||||||
</conf>
|
</conf>
|
||||||
</confs>
|
</confs>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -2,57 +2,68 @@
|
|||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
|
|
||||||
void ADC0_init(void) {
|
void ADC0_init(void) {
|
||||||
/* Initializing ADC0 pin*/
|
/* Initializing ADC0 pin*/
|
||||||
/*Voltage reading on pin pd6*/
|
/*Voltage reading on pin pd6*/
|
||||||
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
|
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
|
||||||
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */
|
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */
|
||||||
PORTD.PIN6CTRL &= PORT_PULLUPEN_bm;
|
PORTD.PIN6CTRL &= PORT_PULLUPEN_bm;
|
||||||
|
|
||||||
/* Thermistor */
|
/* Thermistor */
|
||||||
PORTD.PIN3CTRL &= ~PORT_ISC_gm;
|
PORTD.PIN3CTRL &= ~PORT_ISC_gm;
|
||||||
PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */
|
PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */
|
||||||
PORTD.PIN3CTRL &= PORT_PULLUPEN_bm;
|
PORTD.PIN3CTRL &= PORT_PULLUPEN_bm;
|
||||||
|
|
||||||
ADC0.CTRLC = ADC_PRESC_DIV4_gc;
|
ADC0.CTRLC = ADC_PRESC_DIV4_gc;
|
||||||
VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */
|
VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */
|
||||||
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
|
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
|
||||||
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */
|
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */
|
||||||
/* Select ADC channel */
|
/* Select ADC channel */
|
||||||
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
|
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t ADC0_read(void) {
|
uint16_t ADC0_read(void) {
|
||||||
/* Start ADC conversion */
|
/* Start ADC conversion */
|
||||||
ADC0.COMMAND = ADC_STCONV_bm;
|
ADC0.COMMAND = ADC_STCONV_bm;
|
||||||
/* Wait until ADC conversion done */
|
/* Wait until ADC conversion done */
|
||||||
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
|
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
// Clear the interrupt flag by writing 1:
|
// Clear the interrupt flag by writing 1:
|
||||||
ADC0.INTFLAGS = ADC_RESRDY_bm;
|
ADC0.INTFLAGS = ADC_RESRDY_bm;
|
||||||
return ADC0.RES;
|
return ADC0.RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t thermistor_voltage_read() {
|
uint16_t thermistor_voltage_read() {
|
||||||
/* Gets value for the diode */
|
/* Gets value for the diode */
|
||||||
ADC0.MUXPOS = 0x03; // Read PD3
|
ADC0.MUXPOS = 0x03; // Read PD3
|
||||||
uint16_t adc_val = ADC0_read();
|
uint16_t adc_val = ADC0_read();
|
||||||
|
|
||||||
return adc_val;
|
return adc_val;
|
||||||
}
|
}
|
||||||
// Gets the value over thermistor
|
// Gets the value over thermistor
|
||||||
uint16_t external_voltage_read() {
|
uint16_t external_voltage_read(){
|
||||||
ADC0.MUXPOS = 0x06; // Read PD6
|
ADC0.MUXPOS = 0x06; // Read PD6
|
||||||
uint16_t adc_val = ADC0_read();
|
uint16_t adc_val = ADC0_read();
|
||||||
|
|
||||||
return adc_val;
|
return adc_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t internal_voltage_read() {
|
uint16_t internal_voltage_read() {
|
||||||
/* Gets value for the internal voltage reffreance*/
|
/* Gets value for the internal voltage reffreance*/
|
||||||
|
|
||||||
ADC0.MUXPOS = 0x44;
|
ADC0.MUXPOS = 0x44;
|
||||||
uint8_t adc_val = ADC0_read();
|
uint8_t adc_val = ADC0_read();
|
||||||
|
|
||||||
|
return adc_val*10;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Takes inn a messured value and converts it to voltage */
|
||||||
|
uint16_t convert_to_voltage(uint16_t adc_val){
|
||||||
|
uint16_t min_in= 0;
|
||||||
|
uint16_t min_out= 0;
|
||||||
|
uint16_t max_in= 1023;
|
||||||
|
uint16_t max_out= 3.3;
|
||||||
|
uint16_t voltage = (adc_val-min_in)*(max_out-min_out)/(max_in-min_in) + min_out;
|
||||||
|
return voltage;
|
||||||
|
|
||||||
return adc_val * 10;
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user