diff --git a/circuit/atkbbb/atkbbb.brd b/circuit/atkbbb/atkbbb.brd index 376de3878b02a26bccdbe84325b0a5c7e4eaae18..09f60c8d3f6ae541b5290c099a3a3a010a9e82b2 100644 --- a/circuit/atkbbb/atkbbb.brd +++ b/circuit/atkbbb/atkbbb.brd @@ -1469,7 +1469,6 @@ design rules under a new name.</description> <via x="22.5" y="9" extent="1-16" drill="0.35"/> <via x="18.5" y="0.9" extent="1-16" drill="0.35"/> <wire x1="27.6" y1="33.6" x2="28" y2="33.4" width="0" layer="19" extent="1-1"/> -<wire x1="25.65" y1="7.675" x2="26" y2="5.95" width="0" layer="19" extent="1-1"/> </signal> <signal name="+3V3"> <contactref element="D1" pad="1"/> @@ -1611,9 +1610,9 @@ design rules under a new name.</description> <wire x1="26.8450125" y1="10.275" x2="26.98500625" y2="10.41499375" width="0.1524" layer="1"/> <via x="26.98500625" y="10.41499375" extent="1-16" drill="0.35"/> <wire x1="26.98500625" y1="10.41499375" x2="28.48500625" y2="10.41499375" width="0.1524" layer="16"/> -<wire x1="28.48500625" y1="10.41499375" x2="28.9" y2="10" width="0.1524" layer="16"/> -<via x="28.9" y="10" extent="1-16" drill="0.35"/> -<wire x1="28.9" y1="10" x2="29.275" y2="9.625" width="0.1524" layer="1"/> +<wire x1="28.48500625" y1="10.41499375" x2="29.1" y2="9.8" width="0.1524" layer="16"/> +<via x="29.1" y="9.8" extent="1-16" drill="0.35"/> +<wire x1="29.1" y1="9.8" x2="29.275" y2="9.625" width="0.1524" layer="1"/> <wire x1="29.275" y1="9.625" x2="31.55" y2="9.625" width="0.1524" layer="1"/> <wire x1="31.55" y1="9.625" x2="33.0924" y2="9.625" width="0.1524" layer="1"/> <wire x1="33.0924" y1="9.625" x2="33.1" y2="9.6174" width="0.1524" layer="1"/> @@ -2110,11 +2109,11 @@ design rules under a new name.</description> <contactref element="U2" pad="50"/> <contactref element="U4" pad="9"/> <wire x1="31.55" y1="7.675" x2="32.2950125" y2="7.675" width="0.1524" layer="1"/> -<wire x1="31.55" y1="7.675" x2="29.475" y2="7.675" width="0.1524" layer="1"/> -<wire x1="29.475" y1="7.675" x2="29.4" y2="7.6" width="0.1524" layer="1"/> -<via x="29.4" y="7.6" extent="1-16" drill="0.35"/> -<wire x1="29.4" y1="7.6" x2="29.2" y2="7.4" width="0.1524" layer="16"/> -<wire x1="29.2" y1="7.4" x2="26.7" y2="7.4" width="0.1524" layer="16"/> +<wire x1="31.55" y1="7.675" x2="28.9" y2="7.675" width="0.1524" layer="1"/> +<wire x1="28.9" y1="7.9" x2="28.9" y2="7.675" width="0.1524" layer="1"/> +<via x="28.9" y="7.9" extent="1-16" drill="0.35"/> +<wire x1="28.9" y1="7.9" x2="28.4" y2="7.4" width="0.1524" layer="16"/> +<wire x1="28.4" y1="7.4" x2="26.7" y2="7.4" width="0.1524" layer="16"/> <wire x1="26.7" y1="7.4" x2="25.9" y2="8.2" width="0.1524" layer="16"/> <wire x1="29.4576" y1="15.1576" x2="29.4576" y2="16.8" width="0.1524" layer="16"/> <via x="29.4576" y="16.8" extent="1-16" drill="0.35"/> diff --git a/circuit/atkbbb/atkbbb_2018-11-03.zip b/circuit/atkbbb/atkbbb_2018-11-03.zip new file mode 100644 index 0000000000000000000000000000000000000000..0f2574c70125696898bf7f866097e7d6d2cfbf5c Binary files /dev/null and b/circuit/atkbbb/atkbbb_2018-11-03.zip differ diff --git a/circuit/atkbbb/eagle.epf b/circuit/atkbbb/eagle.epf index 9b0ee90ca42a2ced1b8f153427e6c76a76b7cbfa..a6366b1fda0a3ec0860c4b337a50a43e53179207 100644 --- a/circuit/atkbbb/eagle.epf +++ b/circuit/atkbbb/eagle.epf @@ -23,10 +23,10 @@ UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries_jake/eagle/marekr/borkedlab UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/comm.lbr" [Win_1] -Type="Library Editor" -Number=2 -File="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/connector.lbr" -View="-9.77392 -4.63336 9.77392 7.36301" +Type="Schematic Editor" +Number=1 +File="atkbbb.sch" +View="-7.79426 208.223 62.0717 270.814" WireWidths=" 0.0762 0.1016 0.127 0.15 0.2 0.2032 0.254 0.3048 0.4064 0.508 0.6096 0.8128 1.016 1.27 2.54 0.1524" PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0" PadDrills=" 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6" @@ -42,10 +42,10 @@ DimensionExtWidths=" 0.127 0.254 0.1 0.13 0.26 0" DimensionExtLengths=" 1.27 2.54 1 2 3 0" DimensionExtOffsets=" 1.27 2.54 1 2 3 0" SmdSizes=" 0.3048 0.1524 0.4064 0.2032 0.6096 0.3048 0.8128 0.4064 1.016 0.508 1.27 0.6604 1.4224 0.7112 1.6764 0.8128 1.778 0.9144 1.9304 0.9652 2.1844 1.0668 2.54 1.27 3.81 1.9304 5.08 2.54 6.4516 3.2512 1.27 0.635" -WireBend=0 -WireBendSet=0 +WireBend=4 +WireBendSet=31 WireCap=1 -MiterStyle=1 +MiterStyle=0 PadShape=0 ViaShape=1 PolygonPour=0 @@ -64,13 +64,15 @@ SwapLevel=0 ArcDirection=0 AddLevel=2 PadsSameType=0 -Layer=94 +Layer=91 +Views=" 1: -7.79426 208.223 62.0717 270.814" +Sheet="1" [Win_2] -Type="Schematic Editor" -Number=1 -File="atkbbb.sch" -View="-7.79426 208.223 62.0717 270.814" +Type="Library Editor" +Number=2 +File="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/fab.lbr" +View="-0.508 -0.508 25.908 25.908" WireWidths=" 0.0762 0.1016 0.127 0.15 0.2 0.2032 0.254 0.3048 0.4064 0.508 0.6096 0.8128 1.016 1.27 2.54 0.1524" PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0" PadDrills=" 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6" @@ -86,10 +88,10 @@ DimensionExtWidths=" 0.127 0.254 0.1 0.13 0.26 0" DimensionExtLengths=" 1.27 2.54 1 2 3 0" DimensionExtOffsets=" 1.27 2.54 1 2 3 0" SmdSizes=" 0.3048 0.1524 0.4064 0.2032 0.6096 0.3048 0.8128 0.4064 1.016 0.508 1.27 0.6604 1.4224 0.7112 1.6764 0.8128 1.778 0.9144 1.9304 0.9652 2.1844 1.0668 2.54 1.27 3.81 1.9304 5.08 2.54 6.4516 3.2512 1.27 0.635" -WireBend=4 -WireBendSet=31 +WireBend=0 +WireBendSet=0 WireCap=1 -MiterStyle=0 +MiterStyle=1 PadShape=0 ViaShape=1 PolygonPour=0 @@ -108,15 +110,13 @@ SwapLevel=0 ArcDirection=0 AddLevel=2 PadsSameType=0 -Layer=91 -Views=" 1: -7.79426 208.223 62.0717 270.814" -Sheet="1" +Layer=16 [Win_3] Type="Board Editor" Number=4 File="atkbbb.brd" -View="24.4705 -13.6332 51.0938 56.1487" +View="26.3354 -8.6102 48.5215 49.5416" WireWidths=" 0.0762 0.1016 0.127 0.15 0.2 0.508 0.6096 2.54 1.27 0.8128 1.016 0.3048 0.4064 0.2032 0.1524 0.254" PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0" PadDrills=" 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6" @@ -161,7 +161,7 @@ Type="Control Panel" Number=0 [Desktop] -Screen="3840 1080" +Screen="1920 1080" Window="Win_1" Window="Win_2" Window="Win_3" diff --git a/circuit/daughter/eagle.epf b/circuit/daughter/eagle.epf index 482d02e029cc43e7f52a58f4d6dac60fa204b2e2..41efed7702d72505c97f7906b3dff166644169d1 100644 --- a/circuit/daughter/eagle.epf +++ b/circuit/daughter/eagle.epf @@ -72,7 +72,7 @@ Sheet="1" Type="Board Editor" Number=4 File="atkbbdaughter.brd" -View="-14.7675 -0.991697 80.6286 36.1256" +View="-20.4118 -4.64549 94.0635 39.8953" WireWidths=" 0.0762 0.1016 0.127 0.15 0.2 0.508 0.6096 2.54 1.27 0.8128 1.016 0.3048 0.4064 0.2032 0.254 0.1524" PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0" PadDrills=" 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6" @@ -117,7 +117,7 @@ Type="Control Panel" Number=0 [Desktop] -Screen="3840 1080" +Screen="1920 1080" Window="Win_1" Window="Win_2" Window="Win_3" diff --git a/embedded/atkbbb/atkbbb/adc.h b/embedded/atkbbb/atkbbb/adc.h new file mode 100644 index 0000000000000000000000000000000000000000..295b2e6712df7beeadfc75fcd3d4c3242e5c1da5 --- /dev/null +++ b/embedded/atkbbb/atkbbb/adc.h @@ -0,0 +1,113 @@ +#include <avr/pgmspace.h> + +#define ADCACAL0_offset 0x20 +#define ADCACAL1_offset 0x21 +#define ADCBCAL0_offset 0x24 +#define ADCBCAL1_offset 0x25 + +//#define ADC_CH_CHIF_bm 0x01 /* Channel Interrupt Flag bit mask. */ + + +#define ADC_ConvMode_and_Resolution_Config(_adc, _signedMode, _resolution) \ + ((_adc)->CTRLB = ((_adc)->CTRLB & (~(ADC_RESOLUTION_gm|ADC_CONMODE_bm)))| \ + (_resolution| ( _signedMode? ADC_CONMODE_bm : 0))) +#define ADC_Reference_Config(_adc, _convRef) \ + ((_adc)->REFCTRL = ((_adc)->REFCTRL & ~(ADC_REFSEL_gm)) | _convRef) +#define ADC_Prescaler_Config(_adc, _div) \ + ((_adc)->PRESCALER = ((_adc)->PRESCALER & (~ADC_PRESCALER_gm)) | _div) + +#define ADC_Ch_InputMode_and_Gain_Config(_adc_ch, _inputMode, _gain) \ + (_adc_ch)->CTRL = ((_adc_ch)->CTRL & \ + (~(ADC_CH_INPUTMODE_gm|ADC_CH_GAIN_gm))) | \ + ((uint8_t) _inputMode|_gain) + +#define ADC_Ch_InputMux_Config(_adc_ch, _posInput, _negInput) \ + ((_adc_ch)->MUXCTRL = (uint8_t) _posInput | _negInput) +#define ADC_Enable(_adc) ((_adc)->CTRLA |= ADC_ENABLE_bm) +#define COMMON_MODE_CYCLES 16 + +#define ADC_Conversions_Start(_adc, _channelMask) \ + (_adc)->CTRLA |= _channelMask & \ + (ADC_CH0START_bm | ADC_CH1START_bm | \ + ADC_CH2START_bm | ADC_CH3START_bm) +#define ADC_Ch_Conversion_Start(_adc_ch) ((_adc_ch)->CTRL |= ADC_CH_START_bm) +#define ADC_Ch_Conversion_Complete(_adc_ch) \ + (((_adc_ch)->INTFLAGS & ADC_CH_CHIF_bm) != 0x00) + + +#define ADC_BandgapReference_Enable(_adc) ((_adc)->REFCTRL |= ADC_BANDGAP_bm) +#define ADC_BandgapReference_Disable(_adc) ((_adc)->REFCTRL &= ~ADC_BANDGAP_bm) + + +uint8_t SP_ReadCalibrationByte( uint8_t index ) +{ + uint8_t result; + /* Load the NVM Command register to read the calibration row. */ + NVM_CMD = NVM_CMD_READ_CALIB_ROW_gc; + result = pgm_read_byte(index); + /* Clean up NVM Command register. */ + NVM_CMD = NVM_CMD_NO_OPERATION_gc; + return result; +} +void ADC_Wait_8MHz(ADC_t * adc) +{ + /* Store old prescaler value. */ + uint8_t prescaler_val = adc->PRESCALER; + + /* Set prescaler value to minimum value. */ + adc->PRESCALER = ADC_PRESCALER_DIV4_gc; + + /* Wait 4*COMMON_MODE_CYCLES for common mode to settle. */ + _delay_us(4*COMMON_MODE_CYCLES); + + /* Set prescaler to old value*/ + adc->PRESCALER = prescaler_val; +} + +int16_t ADC_ResultCh_GetWord_Signed(ADC_CH_t * adc_ch, int8_t signedOffset) +{ + int16_t answer; + + /* Clear interrupt flag.*/ + adc_ch->INTFLAGS = ADC_CH_CHIF_bm; + + /* Return result register contents*/ + answer = adc_ch->RES - signedOffset; + + return answer; +} +uint16_t ADC_ResultCh_GetWord(ADC_CH_t * adc_ch) +{ + /* Clear interrupt flag.*/ + adc_ch->INTFLAGS = ADC_CH_CHIF_bm; + + /* Return result register contents*/ + return adc_ch->RES;; +} + +int8_t ADC_Offset_Get_Signed(ADC_t * adc, ADC_CH_t *ch, int oversampling_n) +{ + if (oversampling_n) { + int16_t offset=0; + for (int i=0; i<oversampling_n; i++) { + /* Do one conversion to find offset. */ + ADC_Ch_Conversion_Start(ch); + + do{ + } while (!ADC_Ch_Conversion_Complete(ch)); + offset += ADC_ResultCh_GetWord_Signed(ch, 0x00); + } + return ((int8_t)(offset/oversampling_n)); + } else { + int8_t offset=0; + + /* Do one conversion to find offset. */ + ADC_Ch_Conversion_Start(ch); + + do{ + } while (!ADC_Ch_Conversion_Complete(ch)); + offset = (uint8_t)ADC_ResultCh_GetWord_Signed(ch, 0x00); + + return offset; + } +} \ No newline at end of file diff --git a/embedded/atkbbb/atkbbb/atkbbb.cproj b/embedded/atkbbb/atkbbb/atkbbb.cproj index 1db73fbd67a89c42b74bb062badcd929fadd2d11..83125a77e6fe760f2066052ceaf6826105a059b7 100644 --- a/embedded/atkbbb/atkbbb/atkbbb.cproj +++ b/embedded/atkbbb/atkbbb/atkbbb.cproj @@ -98,44 +98,47 @@ <PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <ToolchainSettings> <AvrGcc> - <avrgcc.common.Device>-mmcu=atxmega256a3u -B "%24(PackRepoDir)\atmel\XMEGAA_DFP\1.1.68\gcc\dev\atxmega256a3u"</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\XMEGAA_DFP\1.1.68\include</Value> - </ListValues> - </avrgcc.compiler.directories.IncludePaths> - <avrgcc.compiler.optimization.level>Optimize (-O1)</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\XMEGAA_DFP\1.1.68\include</Value> - </ListValues> - </avrgcc.assembler.general.IncludePaths> - <avrgcc.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcc.assembler.debugging.DebugLevel> - </AvrGcc> + <avrgcc.common.Device>-mmcu=atxmega256a3u -B "%24(PackRepoDir)\atmel\XMEGAA_DFP\1.1.68\gcc\dev\atxmega256a3u"</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\XMEGAA_DFP\1.1.68\include</Value> + </ListValues> + </avrgcc.compiler.directories.IncludePaths> + <avrgcc.compiler.optimization.level>Optimize (-O1)</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\XMEGAA_DFP\1.1.68\include</Value> + </ListValues> + </avrgcc.assembler.general.IncludePaths> + <avrgcc.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcc.assembler.debugging.DebugLevel> +</AvrGcc> </ToolchainSettings> </PropertyGroup> <ItemGroup> + <Compile Include="adc.h"> + <SubType>compile</SubType> + </Compile> <Compile Include="atkhandler.c"> <SubType>compile</SubType> </Compile> diff --git a/embedded/atkbbb/atkbbb/atkhandler.c b/embedded/atkbbb/atkbbb/atkhandler.c index 7834ebdc2d24ef8fc31090e7896658a35fab0be1..58304ed95546369159a3cd70436da8a5dc5d5168 100644 --- a/embedded/atkbbb/atkbbb/atkhandler.c +++ b/embedded/atkbbb/atkbbb/atkhandler.c @@ -31,7 +31,17 @@ void atk_handle_packet(uint8_t *packet, uint8_t length){ case ATK_HANDLER_INSIDE: switch (packet[i]){ case DELIM_KEY_TEST: - // see the packet, make sure you're not doing something else with this light + pin_clear(&stlclk); + uint32_t counter = 0; + uint16_t loop = 0; + while(loop < 12){ + counter ++; + if(counter > 1200000){ + pin_toggle(&stlclk); + loop ++; + counter = 0; + } + } atk_reply_packet(packet, testReply, 4); i ++; break; @@ -45,6 +55,14 @@ void atk_handle_packet(uint8_t *packet, uint8_t length){ //pin_toggle(&stlerr); break; + case DELIM_KEY_ADCGET: + ; + uint16_t adc = get_adc(); + i += 1; + uint8_t pckt[3] = {142, (uint8_t)((adc) << 8), (uint8_t)(adc)}; + atk_reply_packet(packet, pckt, 3); + break; + case DELIM_KEY_SERVOVAL: ; // empty statement for C error uint32_t servoval = (uint32_t)(packet[i + 1] << 24) | (uint32_t)(packet[i + 2] << 16) | (uint32_t)(packet[i + 3] << 8) | (uint32_t)(packet[i + 4]); diff --git a/embedded/atkbbb/atkbbb/atkhandler.h b/embedded/atkbbb/atkbbb/atkhandler.h index 269f6842e9a9b0371abe3f915fc273b6bfcd429a..d1d6c23d11946b585a8ea6aea1f7a05905c46bf8 100644 --- a/embedded/atkbbb/atkbbb/atkhandler.h +++ b/embedded/atkbbb/atkbbb/atkhandler.h @@ -21,6 +21,7 @@ #define DELIM_KEY_WAIT 132 #define DELIM_KEY_SERVOVAL 141 +#define DELIM_KEY_ADCGET 142 void atk_handle_packet(uint8_t *packet, uint8_t length); diff --git a/embedded/atkbbb/atkbbb/hardware.h b/embedded/atkbbb/atkbbb/hardware.h index 10d097ee28dbc8aa14a0fa7c4b8881c2fb2c3c62..642dfdaa1ae9d7a8d1d20b3ab733d775850a56a7 100644 --- a/embedded/atkbbb/atkbbb/hardware.h +++ b/embedded/atkbbb/atkbbb/hardware.h @@ -42,4 +42,6 @@ uartport_t *ups[SYSTEM_NUM_UPS]; void set_servo(uint16_t val); +uint16_t get_adc(void); + #endif /* HARDWARE_H_ */ \ No newline at end of file diff --git a/embedded/atkbbb/atkbbb/main.c b/embedded/atkbbb/atkbbb/main.c index 7b47ccbf488aacfea250b454b16d62024dfdcab9..a2ba9232169f1eafb47dd2ca4ddba091fe5f1ca3 100644 --- a/embedded/atkbbb/atkbbb/main.c +++ b/embedded/atkbbb/atkbbb/main.c @@ -10,6 +10,7 @@ #include "hardware.h" #include "fastmath.h" #include "pin.h" +#include "adc.h" void clock_init(void){ OSC.XOSCCTRL = OSC_XOSCSEL_XTAL_256CLK_gc | OSC_FRQRANGE_12TO16_gc; // select external source @@ -39,6 +40,8 @@ void atkps_init(void){ atkport_init(&atkp0, 0, &up0); } +// -------------------------------- SERVO PWM GEN CODE + void set_pwm_cca(uint16_t val){ uint8_t vall = (uint8_t) val; uint8_t valh = (uint8_t) (val >> 8); @@ -73,6 +76,50 @@ void set_servo(uint16_t val){ set_pwm_cca(750 + val); } +// -------------------------------- ADC CODE + +void init_adc(void){ + /* + PORTB.DIRCLR = PIN0_bm; + PORTB.DIRCLR = PIN1_bm; + */ + //ADCB.CALL = SP_ReadCalibrationByte(PROD_SIGNATURES_START + ADCBCAL0_offset); + //ADCB.CALH = SP_ReadCalibrationByte(PROD_SIGNATURES_START + ADCBCAL1_offset); + + ADCB.CTRLB = ADC_RESOLUTION_12BIT_gc | ADC_CONMODE_bm; + + ADCB.REFCTRL = ADC_REFSEL_INTVCC_gc; + + ADCB.PRESCALER = ADC_PRESCALER_DIV64_gc; + + ADCB.CH0.CTRL = ADC_CH_INPUTMODE_DIFFWGAIN_gc | ADC_CH_GAIN_64X_gc; + ADCB.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN1_gc | ADC_CH_MUXNEG_PIN0_gc; + + ADCB.CH0.INTCTRL = ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_HI_gc; + + // start it up + ADCB.CTRLA |= ADC_ENABLE_bm; +} + +uint16_t get_adc(void){ + ADCB.CH0.CTRL |= ADC_CH_START_bm; + while(!(ADCB.CH0.INTFLAGS & ADC_CH_CHIF_bm)){}; + uint8_t resl = ADCB.CH0.RESL; + uint8_t resh = ADCB.CH0.RESH; + uint16_t result = resh << 8 | resl; + return result;//ADCB.CH0.RES; +} + +uint16_t lookup[128] = { + 2047, 2147, 2247, 2347, 2446, 2544, 2641, 2736, 2830, 2922, 3011, 3099, 3184, 3266, 3345, 3421, 3494, 3563, 3629, 3691, 3749, 3802, 3852, 3897, 3938, 3974, 4005, 4032, 4054, 4071, 4084, 4091, 4094, 4091, 4084, 4071, 4054, 4032, 4005, 3974, 3938, 3897, 3852, 3802, 3749, 3691, 3629, 3563, 3494, 3421, 3345, 3266, 3184, 3099, 3011, 2922, 2830, 2736, 2641, 2544, 2446, 2347, 2247, 2147, 2047, 1947, 1847, 1747, 1648, 1550, 1453, 1358, 1264, 1172, 1083, 995, 910, 828, 749, 673, 600, 531, 465, 403, 345, 292, 242, 197, 156, 120, 89, 62, 40, 23, 10, 3, 0, 3, 10, 23, 40, 62, 89, 120, 156, 197, 242, 292, 345, 403, 465, 531, 600, 673, 749, 828, 910, 995, 1083, 1172, 1264, 1358, 1453, 1550, 1648, 1747, 1847, 1947 +}; + +void init_dac(void){ + // DAC0 on PB2 + DACB.CTRLB = (DACB.CTRLB & ~DAC_CHSEL_gm) | DAC_CHSEL_DUAL_gc; + DACB.CTRLC = (DACB.CTRLC & ~(DAC_REFSEL_gm | DAC_LEFTADJ_bm)) | DAC_REFSEL_AVCC_gc; + DACB.CTRLA = (DACB.CTRLA & ~DAC_CH0EN_bm) | DAC_CH1EN_bm | DAC_ENABLE_bm; +} int main(void) { @@ -95,9 +142,20 @@ int main(void) // servo begin servo_pwm_begin(); + // adc begin + init_adc(); + + // dac begin + init_dac(); + uint16_t tck = 0; + uint16_t angle = 0; while (1) { + //uint16_t res = get_adc(); + //DACB.CH1DATA = res; + //DACB.CH1DATA = lookup[angle]; + //angle = (angle + 1) % 128; atkport_scan(&atkp0, 2); tck ++; if(!fastModulo(tck, 4096)){ diff --git a/images/20181105-DSC03268.jpg b/images/20181105-DSC03268.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e6d03af504fac52197e3334a198217218cf40c4b Binary files /dev/null and b/images/20181105-DSC03268.jpg differ