Merge remote-tracking branch 'origin/main' into voltage-reading

This commit is contained in:
Inamr 2024-03-12 12:57:20 +01:00
commit d8bd6917f6
5 changed files with 324 additions and 243 deletions

4
.gitignore vendored
View File

@ -26,4 +26,6 @@
# Large Files # Large Files
*.exe *.exe
*.zip *.zip
*.pdf *.pdf
/prosjekt.X/build/
/prosjekt.X/dist/

View File

@ -1,72 +1,84 @@
/* /*
* File: main.c * File: main.c
* Author: Sebastian H. Gabrielli * Author: Sebastian H. Gabrielli
* *
* Created on March 6, 2024, 12:34 PM * Created on March 6, 2024, 12:34 PM
*/ */
#include "header.h" #include "header.h"
#define F_CPU 4000000UL #define RTC_PERIOD (511)
#define RTC_PERIOD (511) #define DELAY_TIME 1000
#define DELAY_TIME 1000 #include <avr/io.h>
#include <avr/io.h> #include <avr/interrupt.h>
#include <avr/interrupt.h> #include <util/delay.h>
#include <util/delay.h> #include <string.h>
#include <string.h> #include <stdbool.h>
#include <stdbool.h>
#include <stdio.h> #define F_CPU 4E6
#include <stdio.h>
void sensor_init(void) { #include <stdlib.h>
/* Disable digital input buffer */ #include "uart.h"
#include <util/delay.h>
}
int main() {
void ADC0_init(void) { init_uart((uint16_t)9600);
PORTD.PIN6CTRL &= ~PORT_ISC_gm; stdout = &USART_stream;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= PORT_PULLUPEN_bm; void sensor_init(void) {
/* Disable digital input buffer */
ADC0.CTRLC = ADC_PRESC_DIV4_gc;
VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */ while (1) {
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ printf("Hello, world!\n");
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */ _delay_ms(500);
/* Select ADC channel */ }
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; }
}
void ADC0_init(void) {
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
uint16_t ADC0_read(void) { PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Start ADC conversion */ PORTD.PIN6CTRL &= PORT_PULLUPEN_bm;
ADC0.COMMAND = ADC_STCONV_bm;
/* Wait until ADC conversion done */ ADC0.CTRLC = ADC_PRESC_DIV4_gc;
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */
; ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
} | ADC_RESSEL_10BIT_gc; /* 10-bit mode */
/* Clear the interrupt flag by writing 1: */ /* Select ADC channel */
ADC0.INTFLAGS = ADC_RESRDY_bm; ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
return ADC0.RES; }
}
bool ADC0_conversationDone(void) { uint16_t ADC0_read(void) {
if (ADC0.INTFLAGS && ADC_RESRDY_bm) { /* Start ADC conversion */
return true; ADC0.COMMAND = ADC_STCONV_bm;
} else { /* Wait until ADC conversion done */
return false; while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
} ;
}
} /* Clear the interrupt flag by writing 1: */
ADC0.INTFLAGS = ADC_RESRDY_bm;
int main(void) { return ADC0.RES;
sensor_init(); }
ADC0_init();
bool ADC0_conversationDone(void) {
while (1) { if (ADC0.INTFLAGS && ADC_RESRDY_bm) {
//printf("loop\n"); return true;
if (ADC0_conversationDone()) { } else {
adcVal = ADC0_read(); return false;
VREF.ADC0REF = VREF_REFSEL_VDD_gc; }
printf("The values: \n");
printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); }
}
} int main(void) {
sensor_init();
ADC0_init();
while (1) {
//printf("loop\n");
if (ADC0_conversationDone()) {
adcVal = ADC0_read();
VREF.ADC0REF = VREF_REFSEL_VDD_gc;
printf("The values: \n");
printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal);
}
}
} }

View File

@ -1,171 +1,173 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configurationDescriptor version="65"> <configurationDescriptor version="65">
<logicalFolder name="root" displayName="root" projectFiles="true"> <logicalFolder name="root" displayName="root" projectFiles="true">
<logicalFolder name="HeaderFiles" <logicalFolder name="HeaderFiles"
displayName="Header Files" displayName="Header Files"
projectFiles="true"> projectFiles="true">
<itemPath>header.h</itemPath> <itemPath>uart.h</itemPath>
</logicalFolder> <itemPath>header.h</itemPath>
<logicalFolder name="ExternalFiles" </logicalFolder>
displayName="Important Files" <logicalFolder name="ExternalFiles"
projectFiles="true"> displayName="Important Files"
<itemPath>Makefile</itemPath> projectFiles="true">
</logicalFolder> <itemPath>Makefile</itemPath>
<logicalFolder name="LinkerScript" </logicalFolder>
displayName="Linker Files" <logicalFolder name="LinkerScript"
projectFiles="true"> displayName="Linker Files"
</logicalFolder> projectFiles="true">
<logicalFolder name="SourceFiles" </logicalFolder>
displayName="Source Files" <logicalFolder name="SourceFiles"
projectFiles="true"> displayName="Source Files"
<itemPath>main.c</itemPath> projectFiles="true">
</logicalFolder> <itemPath>main.c</itemPath>
</logicalFolder> <itemPath>uart.c</itemPath>
<projectmakefile>Makefile</projectmakefile> </logicalFolder>
<confs> </logicalFolder>
<conf name="default" type="2"> <projectmakefile>Makefile</projectmakefile>
<toolsSet> <confs>
<developmentServer>localhost</developmentServer> <conf name="default" type="2">
<targetDevice>AVR128DB48</targetDevice> <toolsSet>
<targetHeader></targetHeader> <developmentServer>localhost</developmentServer>
<targetPluginBoard></targetPluginBoard> <targetDevice>AVR128DB48</targetDevice>
<platformTool>nEdbgTool</platformTool> <targetHeader></targetHeader>
<languageToolchain>XC8</languageToolchain> <targetPluginBoard></targetPluginBoard>
<languageToolchainVersion>2.46</languageToolchainVersion> <platformTool>nEdbgTool</platformTool>
<platform>3</platform> <languageToolchain>XC8</languageToolchain>
</toolsSet> <languageToolchainVersion>2.46</languageToolchainVersion>
<packs> <platform>3</platform>
<pack name="AVR-Dx_DFP" vendor="Microchip" version="2.3.272"/> </toolsSet>
</packs> <packs>
<ScriptingSettings> <pack name="AVR-Dx_DFP" vendor="Microchip" version="2.3.272"/>
</ScriptingSettings> </packs>
<compileType> <ScriptingSettings>
<linkerTool> </ScriptingSettings>
<linkerLibItems> <compileType>
</linkerLibItems> <linkerTool>
</linkerTool> <linkerLibItems>
<archiverTool> </linkerLibItems>
</archiverTool> </linkerTool>
<loading> <archiverTool>
<useAlternateLoadableFile>false</useAlternateLoadableFile> </archiverTool>
<parseOnProdLoad>false</parseOnProdLoad> <loading>
<alternateLoadableFile></alternateLoadableFile> <useAlternateLoadableFile>false</useAlternateLoadableFile>
</loading> <parseOnProdLoad>false</parseOnProdLoad>
<subordinates> <alternateLoadableFile></alternateLoadableFile>
</subordinates> </loading>
</compileType> <subordinates>
<makeCustomizationType> </subordinates>
<makeCustomizationPreStepEnabled>false</makeCustomizationPreStepEnabled> </compileType>
<makeUseCleanTarget>false</makeUseCleanTarget> <makeCustomizationType>
<makeCustomizationPreStep></makeCustomizationPreStep> <makeCustomizationPreStepEnabled>false</makeCustomizationPreStepEnabled>
<makeCustomizationPostStepEnabled>false</makeCustomizationPostStepEnabled> <makeUseCleanTarget>false</makeUseCleanTarget>
<makeCustomizationPostStep></makeCustomizationPostStep> <makeCustomizationPreStep></makeCustomizationPreStep>
<makeCustomizationPutChecksumInUserID>false</makeCustomizationPutChecksumInUserID> <makeCustomizationPostStepEnabled>false</makeCustomizationPostStepEnabled>
<makeCustomizationEnableLongLines>false</makeCustomizationEnableLongLines> <makeCustomizationPostStep></makeCustomizationPostStep>
<makeCustomizationNormalizeHexFile>false</makeCustomizationNormalizeHexFile> <makeCustomizationPutChecksumInUserID>false</makeCustomizationPutChecksumInUserID>
</makeCustomizationType> <makeCustomizationEnableLongLines>false</makeCustomizationEnableLongLines>
<HI-TECH-COMP> <makeCustomizationNormalizeHexFile>false</makeCustomizationNormalizeHexFile>
<property key="additional-warnings" value="true"/> </makeCustomizationType>
<property key="asmlist" value="true"/> <HI-TECH-COMP>
<property key="call-prologues" value="false"/> <property key="additional-warnings" value="true"/>
<property key="default-bitfield-type" value="true"/> <property key="asmlist" value="true"/>
<property key="default-char-type" value="true"/> <property key="call-prologues" value="false"/>
<property key="define-macros" value=""/> <property key="default-bitfield-type" value="true"/>
<property key="disable-optimizations" value="false"/> <property key="default-char-type" value="true"/>
<property key="extra-include-directories" value=""/> <property key="define-macros" value=""/>
<property key="favor-optimization-for" value="-speed,+space"/> <property key="disable-optimizations" value="false"/>
<property key="garbage-collect-data" value="true"/> <property key="extra-include-directories" value=""/>
<property key="garbage-collect-functions" value="true"/> <property key="favor-optimization-for" value="-speed,+space"/>
<property key="identifier-length" value="255"/> <property key="garbage-collect-data" value="true"/>
<property key="local-generation" value="false"/> <property key="garbage-collect-functions" value="true"/>
<property key="operation-mode" value="free"/> <property key="identifier-length" value="255"/>
<property key="opt-xc8-compiler-strict_ansi" value="false"/> <property key="local-generation" value="false"/>
<property key="optimization-assembler" value="true"/> <property key="operation-mode" value="free"/>
<property key="optimization-assembler-files" value="true"/> <property key="opt-xc8-compiler-strict_ansi" value="false"/>
<property key="optimization-debug" value="false"/> <property key="optimization-assembler" value="true"/>
<property key="optimization-invariant-enable" value="false"/> <property key="optimization-assembler-files" value="true"/>
<property key="optimization-invariant-value" value="16"/> <property key="optimization-debug" value="false"/>
<property key="optimization-level" value="-O1"/> <property key="optimization-invariant-enable" value="false"/>
<property key="optimization-speed" value="false"/> <property key="optimization-invariant-value" value="16"/>
<property key="optimization-stable-enable" value="false"/> <property key="optimization-level" value="-O1"/>
<property key="preprocess-assembler" value="true"/> <property key="optimization-speed" value="false"/>
<property key="short-enums" value="true"/> <property key="optimization-stable-enable" value="false"/>
<property key="tentative-definitions" value="-fno-common"/> <property key="preprocess-assembler" value="true"/>
<property key="undefine-macros" value=""/> <property key="short-enums" value="true"/>
<property key="use-cci" value="false"/> <property key="tentative-definitions" value="-fno-common"/>
<property key="use-iar" value="false"/> <property key="undefine-macros" value=""/>
<property key="verbose" value="false"/> <property key="use-cci" value="false"/>
<property key="warning-level" value="-3"/> <property key="use-iar" value="false"/>
<property key="what-to-do" value="ignore"/> <property key="verbose" value="false"/>
</HI-TECH-COMP> <property key="warning-level" value="-3"/>
<HI-TECH-LINK> <property key="what-to-do" value="ignore"/>
<property key="additional-options-checksum" value=""/> </HI-TECH-COMP>
<property key="additional-options-code-offset" value=""/> <HI-TECH-LINK>
<property key="additional-options-command-line" value=""/> <property key="additional-options-checksum" value=""/>
<property key="additional-options-errata" value=""/> <property key="additional-options-code-offset" value=""/>
<property key="additional-options-extend-address" value="false"/> <property key="additional-options-command-line" value=""/>
<property key="additional-options-trace-type" value=""/> <property key="additional-options-errata" value=""/>
<property key="additional-options-use-response-files" value="false"/> <property key="additional-options-extend-address" value="false"/>
<property key="backup-reset-condition-flags" value="false"/> <property key="additional-options-trace-type" value=""/>
<property key="calibrate-oscillator" value="false"/> <property key="additional-options-use-response-files" value="false"/>
<property key="calibrate-oscillator-value" value="0x3400"/> <property key="backup-reset-condition-flags" value="false"/>
<property key="clear-bss" value="true"/> <property key="calibrate-oscillator" value="false"/>
<property key="code-model-external" value="wordwrite"/> <property key="calibrate-oscillator-value" value="0x3400"/>
<property key="code-model-rom" value=""/> <property key="clear-bss" value="true"/>
<property key="create-html-files" value="false"/> <property key="code-model-external" value="wordwrite"/>
<property key="data-model-ram" value=""/> <property key="code-model-rom" value=""/>
<property key="data-model-size-of-double" value="24"/> <property key="create-html-files" value="false"/>
<property key="data-model-size-of-double-gcc" value="no-short-double"/> <property key="data-model-ram" value=""/>
<property key="data-model-size-of-float" value="24"/> <property key="data-model-size-of-double" value="24"/>
<property key="data-model-size-of-float-gcc" value="no-short-float"/> <property key="data-model-size-of-double-gcc" value="no-short-double"/>
<property key="display-class-usage" value="false"/> <property key="data-model-size-of-float" value="24"/>
<property key="display-hex-usage" value="false"/> <property key="data-model-size-of-float-gcc" value="no-short-float"/>
<property key="display-overall-usage" value="true"/> <property key="display-class-usage" value="false"/>
<property key="display-psect-usage" value="false"/> <property key="display-hex-usage" value="false"/>
<property key="extra-lib-directories" value=""/> <property key="display-overall-usage" value="true"/>
<property key="fill-flash-options-addr" value=""/> <property key="display-psect-usage" value="false"/>
<property key="fill-flash-options-const" value=""/> <property key="extra-lib-directories" value=""/>
<property key="fill-flash-options-how" value="0"/> <property key="fill-flash-options-addr" value=""/>
<property key="fill-flash-options-inc-const" value="1"/> <property key="fill-flash-options-const" value=""/>
<property key="fill-flash-options-increment" value=""/> <property key="fill-flash-options-how" value="0"/>
<property key="fill-flash-options-seq" value=""/> <property key="fill-flash-options-inc-const" value="1"/>
<property key="fill-flash-options-what" value="0"/> <property key="fill-flash-options-increment" value=""/>
<property key="format-hex-file-for-download" value="false"/> <property key="fill-flash-options-seq" value=""/>
<property key="initialize-data" value="true"/> <property key="fill-flash-options-what" value="0"/>
<property key="input-libraries" value="libm"/> <property key="format-hex-file-for-download" value="false"/>
<property key="keep-generated-startup.as" value="false"/> <property key="initialize-data" value="true"/>
<property key="link-in-c-library" value="true"/> <property key="input-libraries" value="libm"/>
<property key="link-in-c-library-gcc" value=""/> <property key="keep-generated-startup.as" value="false"/>
<property key="link-in-peripheral-library" value="false"/> <property key="link-in-c-library" value="true"/>
<property key="managed-stack" value="false"/> <property key="link-in-c-library-gcc" value=""/>
<property key="opt-xc8-linker-file" value="false"/> <property key="link-in-peripheral-library" value="false"/>
<property key="opt-xc8-linker-link_startup" value="false"/> <property key="managed-stack" value="false"/>
<property key="opt-xc8-linker-serial" value=""/> <property key="opt-xc8-linker-file" value="false"/>
<property key="program-the-device-with-default-config-words" value="false"/> <property key="opt-xc8-linker-link_startup" value="false"/>
<property key="remove-unused-sections" value="true"/> <property key="opt-xc8-linker-serial" value=""/>
</HI-TECH-LINK> <property key="program-the-device-with-default-config-words" value="false"/>
<XC8-CO> <property key="remove-unused-sections" value="true"/>
<property key="coverage-enable" value=""/> </HI-TECH-LINK>
<property key="stack-guidance" value="false"/> <XC8-CO>
</XC8-CO> <property key="coverage-enable" value=""/>
<XC8-config-global> <property key="stack-guidance" value="false"/>
<property key="advanced-elf" value="true"/> </XC8-CO>
<property key="constdata-progmem" value="true"/> <XC8-config-global>
<property key="gcc-opt-driver-new" value="true"/> <property key="advanced-elf" value="true"/>
<property key="gcc-opt-std" value="-std=c99"/> <property key="constdata-progmem" value="true"/>
<property key="gcc-output-file-format" value="dwarf-3"/> <property key="gcc-opt-driver-new" value="true"/>
<property key="mapped-progmem" value="false"/> <property key="gcc-opt-std" value="-std=c99"/>
<property key="omit-pack-options" value="false"/> <property key="gcc-output-file-format" value="dwarf-3"/>
<property key="omit-pack-options-new" value="1"/> <property key="mapped-progmem" value="false"/>
<property key="output-file-format" value="-mcof,+elf"/> <property key="omit-pack-options" value="false"/>
<property key="smart-io-format" value=""/> <property key="omit-pack-options-new" value="1"/>
<property key="stack-size-high" value="auto"/> <property key="output-file-format" value="-mcof,+elf"/>
<property key="stack-size-low" value="auto"/> <property key="smart-io-format" value=""/>
<property key="stack-size-main" value="auto"/> <property key="stack-size-high" value="auto"/>
<property key="stack-type" value="compiled"/> <property key="stack-size-low" value="auto"/>
<property key="user-pack-device-support" value=""/> <property key="stack-size-main" value="auto"/>
<property key="wpo-lto" value="false"/> <property key="stack-type" value="compiled"/>
</XC8-config-global> <property key="user-pack-device-support" value=""/>
</conf> <property key="wpo-lto" value="false"/>
</confs> </XC8-config-global>
</configurationDescriptor> </conf>
</confs>
</configurationDescriptor>

25
prosjekt.X/uart.c Normal file
View File

@ -0,0 +1,25 @@
#include "uart.h"
void init_uart(uint16_t baud) {
// Configure UART pin directions
PORTB.DIR &= ~PIN1_bm;
PORTB.DIR |= PIN0_bm;
// Set the baudrate
USART3.BAUD = (uint16_t)USART3_BAUD_RATE(baud);
// Enable UART TX & RX
USART3.CTRLB |= USART_TXEN_bm;
}
void USART3_sendChar(char c) {
// Hold the code while the UART is not ready to send
while (!(USART3.STATUS & USART_DREIF_bm)) { ; }
// UART is ready, send the character.
USART3.TXDATAL = c;
}
int USART3_printChar(char c, FILE *stream) {
USART3_sendChar(c);
return 0;
}

40
prosjekt.X/uart.h Normal file
View File

@ -0,0 +1,40 @@
/*
* File: uart.h
* Author: Sebastian H. Gabrielli
*
* Created on March 6, 2024, 3:19 PM
*/
#ifndef UART_H
#define UART_H
#ifdef __cplusplus
extern "C" {
#endif
#include <avr/io.h>
#include <stdio.h>
#ifndef F_CPU
#define F_CPU 4E6
#endif
#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5)
// Initialize the USART3 controller
void init_uart(uint16_t baud);
// Send a single character over UART
void USART3_sendChar(char c);
// Send a string of characters over UART
int USART3_printChar(char c, FILE *stream);
static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE);
#ifdef __cplusplus
}
#endif
#endif /* UART_H */