commit 89ed277617f38496c114e6a05afc6ce1dfe34a60 parent c65f07dc65edee99eff8cae651ff15e554f50d44 Author: William Lindholm <william_lindholm@outlook.com> Date: Wed, 6 Nov 2024 12:36:26 +0000 Project 2 initialization Diffstat:
| M | Project1/.vs/Project1/v14/.atsuo | | | 0 | |
| A | Project2/.vs/Project2/v14/.atsuo | | | 0 | |
| A | Project2/Project2.atsln | | | 22 | ++++++++++++++++++++++ |
| A | Project2/Project2/Project2.componentinfo.xml | | | 87 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | Project2/Project2/Project2.cproj | | | 129 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | Project2/Project2/main.c | | | 158 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 files changed, 396 insertions(+), 0 deletions(-)
diff --git a/Project1/.vs/Project1/v14/.atsuo b/Project1/.vs/Project1/v14/.atsuo Binary files differ. diff --git a/Project2/.vs/Project2/v14/.atsuo b/Project2/.vs/Project2/v14/.atsuo Binary files differ. diff --git a/Project2/Project2.atsln b/Project2/Project2.atsln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Atmel Studio Solution File, Format Version 11.00 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{54F91283-7BC4-4236-8FF9-10F437C3AD48}") = "Project2", "Project2\Project2.cproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|AVR = Debug|AVR + Release|AVR = Release|AVR + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Project2/Project2/Project2.componentinfo.xml b/Project2/Project2/Project2.componentinfo.xml @@ -0,0 +1,86 @@ +<?xml version="1.0" encoding="utf-8"?> +<Store xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="AtmelPackComponentManagement"> + <ProjectComponents> + <ProjectComponent z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"> + <CApiVersion></CApiVersion> + <CBundle></CBundle> + <CClass>Device</CClass> + <CGroup>Startup</CGroup> + <CSub></CSub> + <CVariant></CVariant> + <CVendor>Atmel</CVendor> + <CVersion>1.7.0</CVersion> + <DefaultRepoPath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs</DefaultRepoPath> + <DependentComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /> + <Description></Description> + <Files xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> + <d4p1:anyType i:type="FileInfo"> + <AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\</AbsolutePath> + <Attribute></Attribute> + <Category>include</Category> + <Condition>C</Condition> + <FileContentHash i:nil="true" /> + <FileVersion></FileVersion> + <Name>include/</Name> + <SelectString></SelectString> + <SourcePath></SourcePath> + </d4p1:anyType> + <d4p1:anyType i:type="FileInfo"> + <AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\avr\iom4809.h</AbsolutePath> + <Attribute></Attribute> + <Category>header</Category> + <Condition>C</Condition> + <FileContentHash>hF4UEDMR8+PR+4DxBMrlUg==</FileContentHash> + <FileVersion></FileVersion> + <Name>include/avr/iom4809.h</Name> + <SelectString></SelectString> + <SourcePath></SourcePath> + </d4p1:anyType> + <d4p1:anyType i:type="FileInfo"> + <AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.c</AbsolutePath> + <Attribute>template</Attribute> + <Category>source</Category> + <Condition>C Exe</Condition> + <FileContentHash>5vJ+fvkNlm5mxf+kdseKMA==</FileContentHash> + <FileVersion></FileVersion> + <Name>templates/main.c</Name> + <SelectString>Main file (.c)</SelectString> + <SourcePath></SourcePath> + </d4p1:anyType> + <d4p1:anyType i:type="FileInfo"> + <AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.cpp</AbsolutePath> + <Attribute>template</Attribute> + <Category>source</Category> + <Condition>C Exe</Condition> + <FileContentHash>mkKaE95TOoATsuBGv6jmxg==</FileContentHash> + <FileVersion></FileVersion> + <Name>templates/main.cpp</Name> + <SelectString>Main file (.cpp)</SelectString> + <SourcePath></SourcePath> + </d4p1:anyType> + <d4p1:anyType i:type="FileInfo"> + <AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega4809</AbsolutePath> + <Attribute></Attribute> + <Category>libraryPrefix</Category> + <Condition>GCC</Condition> + <FileContentHash i:nil="true" /> + <FileVersion></FileVersion> + <Name>gcc/dev/atmega4809</Name> + <SelectString></SelectString> + <SourcePath></SourcePath> + </d4p1:anyType> + </Files> + <PackName>ATmega_DFP</PackName> + <PackPath>C:/Program Files (x86)/Atmel/Studio/7.0/Packs/atmel/ATmega_DFP/1.7.374/Atmel.ATmega_DFP.pdsc</PackPath> + <PackVersion>1.7.374</PackVersion> + <PresentInProject>true</PresentInProject> + <ReferenceConditionId>ATmega4809</ReferenceConditionId> + <RteComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> + <d4p1:string></d4p1:string> + </RteComponents> + <Status>Resolved</Status> + <VersionMode>Fixed</VersionMode> + <IsComponentInAtProject>true</IsComponentInAtProject> + </ProjectComponent> + </ProjectComponents> +</Store> +\ No newline at end of file diff --git a/Project2/Project2/Project2.cproj b/Project2/Project2/Project2.cproj @@ -0,0 +1,128 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0"> + <PropertyGroup> + <SchemaVersion>2.0</SchemaVersion> + <ProjectVersion>7.0</ProjectVersion> + <ToolchainName>com.Atmel.AVRGCC8.C</ToolchainName> + <ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid> + <avrdevice>ATmega4809</avrdevice> + <avrdeviceseries>none</avrdeviceseries> + <OutputType>Executable</OutputType> + <Language>C</Language> + <OutputFileName>$(MSBuildProjectName)</OutputFileName> + <OutputFileExtension>.elf</OutputFileExtension> + <OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory> + <AssemblyName>Project2</AssemblyName> + <Name>Project2</Name> + <RootNamespace>Project2</RootNamespace> + <ToolchainFlavour>Native</ToolchainFlavour> + <KeepTimersRunning>true</KeepTimersRunning> + <OverrideVtor>false</OverrideVtor> + <CacheFlash>true</CacheFlash> + <ProgFlashFromRam>true</ProgFlashFromRam> + <RamSnippetAddress /> + <UncachedRange /> + <preserveEEPROM>true</preserveEEPROM> + <OverrideVtorValue /> + <BootSegment>2</BootSegment> + <ResetRule>0</ResetRule> + <eraseonlaunchrule>0</eraseonlaunchrule> + <EraseKey /> + <AsfFrameworkConfig> + <framework-data xmlns=""> + <options /> + <configurations /> + <files /> + <documentation help="" /> + <offline-documentation help="" /> + <dependencies> + <content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.52.0" /> + </dependencies> +</framework-data> + </AsfFrameworkConfig> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)' == 'Release' "> + <ToolchainSettings> + <AvrGcc> + <avrgcc.common.Device>-mmcu=atmega4809 -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega4809"</avrgcc.common.Device> + <avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex> + <avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss> + <avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep> + <avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec> + <avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures> + <avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned> + <avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned> + <avrgcc.compiler.symbols.DefSymbols> + <ListValues> + <Value>NDEBUG</Value> + </ListValues> + </avrgcc.compiler.symbols.DefSymbols> + <avrgcc.compiler.directories.IncludePaths> + <ListValues> + <Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value> + </ListValues> + </avrgcc.compiler.directories.IncludePaths> + <avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level> + <avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers> + <avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum> + <avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings> + <avrgcc.linker.libraries.Libraries> + <ListValues> + <Value>libm</Value> + </ListValues> + </avrgcc.linker.libraries.Libraries> + <avrgcc.assembler.general.IncludePaths> + <ListValues> + <Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value> + </ListValues> + </avrgcc.assembler.general.IncludePaths> +</AvrGcc> + </ToolchainSettings> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> + <ToolchainSettings> + <AvrGcc> + <avrgcc.common.Device>-mmcu=atmega4809 -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega4809"</avrgcc.common.Device> + <avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex> + <avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss> + <avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep> + <avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec> + <avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures> + <avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned> + <avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned> + <avrgcc.compiler.symbols.DefSymbols> + <ListValues> + <Value>DEBUG</Value> + </ListValues> + </avrgcc.compiler.symbols.DefSymbols> + <avrgcc.compiler.directories.IncludePaths> + <ListValues> + <Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value> + </ListValues> + </avrgcc.compiler.directories.IncludePaths> + <avrgcc.compiler.optimization.level>Optimize debugging experience (-Og)</avrgcc.compiler.optimization.level> + <avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers> + <avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum> + <avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel> + <avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings> + <avrgcc.linker.libraries.Libraries> + <ListValues> + <Value>libm</Value> + </ListValues> + </avrgcc.linker.libraries.Libraries> + <avrgcc.assembler.general.IncludePaths> + <ListValues> + <Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.7.374\include\</Value> + </ListValues> + </avrgcc.assembler.general.IncludePaths> + <avrgcc.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcc.assembler.debugging.DebugLevel> +</AvrGcc> + </ToolchainSettings> + </PropertyGroup> + <ItemGroup> + <Compile Include="main.c"> + <SubType>compile</SubType> + </Compile> + </ItemGroup> + <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" /> +</Project> +\ No newline at end of file diff --git a/Project2/Project2/main.c b/Project2/Project2/main.c @@ -0,0 +1,157 @@ +/* + * Project2.c + * + * Created: 2024-11-06 11:04:34 + * Author : Willi + */ + +#define F_CPU 20000000 +#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5) + +#include <avr/io.h> +#include <avr/interrupt.h> +#include <avr/cpufunc.h> +#include <util/delay.h> +#include <stdio.h> +#include <stdbool.h> + + +/************************************************************************/ +/* Globals */ +/************************************************************************/ + +/* Led related globals */ +struct LED_BITS +{ + PORT_t *LED_PORT; + uint8_t bit_mapping; +}; + +struct LED_BITS LED_Array[10] = { + {&PORTC, PIN5_bm}, {&PORTC, PIN4_bm}, {&PORTA, PIN0_bm}, {&PORTF, PIN5_bm}, {&PORTC, PIN6_bm}, {&PORTB, PIN2_bm}, {&PORTF, PIN4_bm}, {&PORTA, PIN1_bm}, {&PORTA, PIN2_bm}, {&PORTA, PIN3_bm} +}; + + +/* Serial com related globals */ +unsigned char qcntr = 0,sndcntr = 0; /*indexes into the queue*/ +unsigned char queue[50]; /*character queue*/ + +/* Led related globals */ +bool ADC_READING_READY; +uint16_t ADC_VALUE; + + + +/************************************************************************/ +/* Function declarations */ +/************************************************************************/ +void CLOCK_init (void); +static void USART3_init(void); +void sendmsg (char *s); +void configure_ADC0(void); + + +int main(void) +{ + char ch; + char str_buffer[60]; + uint32_t adc_reading_mv; + + + CLOCK_init(); + USART3_init(); + configure_ADC0(); + + sei(); /* Enable Global Interrupts */ + + while (1) + { + if (USART3.STATUS & USART_RXCIF_bm) + { /* If a character has been received, read it - this structure allows other code to run */ + ch = USART3.RXDATAL; + switch (ch) + { + case 'a': + adc_reading_mv = ((uint32_t)ADC_VALUE * 5000) / 1023; + sprintf(str_buffer, "ADC0 = %d, %lumV\n", ADC_VALUE, adc_reading_mv); + sendmsg(str_buffer); + break; + case 'b': + case 'B': + sprintf(str_buffer, "That was a B or a b\n"); + sendmsg(str_buffer); + break; + default: + sprintf(str_buffer, "That was neither 'a' or 'b'\n"); + sendmsg(str_buffer); + break; + } + } + /* Even if a character has not been received, code inserted here can still run */ + } +} + +void CLOCK_init (void) +{ + /* Do not use low frequency clock, disable CLK_PER Prescaler */ + ccp_write_io( (void *) &CLKCTRL.MCLKCTRLB , (0 << CLKCTRL_PEN_bp)); + /* If set from the fuses during progamming, the CPU will now run at 20MHz (default is /6) */ +} + +static void USART3_init(void) +{ + PORTB.DIR &= ~PIN5_bm; /* this is the RX input */ + PORTB.DIR |= PIN4_bm; /* this is the TX output */ + USART3.BAUD = (uint16_t)USART3_BAUD_RATE(9600); + USART3.CTRLB |= (USART_TXEN_bm | USART_RXEN_bm); + PORTMUX.USARTROUTEA |= PORTMUX_USART3_ALT1_gc; + + USART3.CTRLA = USART_TXCIE_bm; +} + +/************************************************************************/ +/* Enable and configure ADC0 */ +/************************************************************************/ +void configure_ADC0() +{ + ADC0.CTRLA = ADC_RESSEL_10BIT_gc | ADC_FREERUN_bm; // Set 10 bit resolution and free run mode + ADC0.CTRLC = ADC_SAMPCAP_bm | ADC_REFSEL_VDDREF_gc | ADC_PRESC_DIV64_gc; // Enable SAMPCAP, Set reference voltage to VDD, Set ADC prescaler to div 128 + ADC0.MUXPOS = ADC_MUXPOS_AIN3_gc; // Set input to Analog in 3 + ADC0.INTCTRL = ADC_RESRDY_bm; // Enable interrupt on result ready + ADC0.CTRLD = ADC_INITDLY_DLY16_gc; // Initial delay of 16 cycles + ADC0.CTRLA |= ADC_ENABLE_bm; // Enable ADC + ADC0.COMMAND = ADC_STCONV_bm; // Start first measurement +} + +/*this function loads the queue and */ +/*starts the sending process*/ +void sendmsg (char *s) +{ + qcntr = 0; /*preset indices*/ + sndcntr = 1; /*set to one because first character already sent*/ + + while (*s) + queue[qcntr++] = *s++; /*put characters into queue*/ + + USART3.TXDATAL = queue[0]; /*send first character to start process*/ +} + +ISR(USART3_TXC_vect) +{ + /*send next character and increment index*/ + USART3.STATUS |= USART_TXCIF_bm; + if (qcntr != sndcntr) + USART3.TXDATAL = queue[sndcntr++]; + /* Stop sending when the queue is empty. TXC interrupts only happen when a character + has been transmitted. Stoppping sending stops the interrupts */ +} + +/************************************************************************/ +/* ADC0 result ready ISR (handle voltage reading measurements) */ +/************************************************************************/ +ISR(ADC0_RESRDY_vect) +{ + ADC_READING_READY = true; // Set flag informing that new voltage value is ready + ADC_VALUE = ADC0.RES; // Store the reading of the ADC result in a global variable + ADC0.INTFLAGS = ADC_RESRDY_bm; // Clear interrupt flag +} +\ No newline at end of file