- update
git-svn-id: http://moon:8086/svn/projects/HendiControl@10 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
@@ -8,9 +8,14 @@
|
||||
#include <stdio.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#define ARDUINO_NANO
|
||||
|
||||
#ifdef ARDUINO_NANO
|
||||
#define F_CPU (16000000UL) // MHz
|
||||
#else
|
||||
#define F_CPU (18432000UL) // MHz
|
||||
#define USART_BAUDRATE 115200UL
|
||||
#endif
|
||||
#define USART_BAUDRATE 9600UL
|
||||
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
|
||||
#define TIMER_RELOAD (0xFFFF-(F_CPU/1024/4-1))
|
||||
static uint8_t rx_data;
|
||||
@@ -18,16 +23,41 @@ static uint8_t count = 0;
|
||||
volatile uint8_t msgCode = 0;
|
||||
static uint8_t msgData = 0;
|
||||
static uint8_t ledState = 0;
|
||||
static int16_t adc_value[16];
|
||||
static size_t adc_ch = 0;
|
||||
|
||||
enum MessageCode
|
||||
{
|
||||
NOP=0,
|
||||
Command,
|
||||
Timer,
|
||||
AdcComplete,
|
||||
Error = 0xFF
|
||||
};
|
||||
|
||||
ISR(USART0_RX_vect)
|
||||
ISR(ADC_vect)
|
||||
{
|
||||
int16_t value_low = (int16_t)ADCL;
|
||||
int16_t value_high = (int16_t)ADCH;
|
||||
ADCSRA |= (1<<ADIF);
|
||||
int16_t value = value_high << 8 | value_low;
|
||||
|
||||
if (((adc_value[adc_ch] - value) < -1) || ((adc_value[adc_ch] - value) > 1))
|
||||
{
|
||||
msgCode = AdcComplete;
|
||||
msgData = adc_ch;
|
||||
}
|
||||
|
||||
adc_value[adc_ch] = value;
|
||||
adc_ch++;
|
||||
if (adc_ch == 16)
|
||||
{
|
||||
adc_ch = 0;
|
||||
}
|
||||
adc_start_conversion(adc_ch);
|
||||
}
|
||||
|
||||
ISR(USART_RX_vect)
|
||||
{
|
||||
rx_data = UDR0;
|
||||
|
||||
@@ -80,6 +110,34 @@ void timer_init()
|
||||
|
||||
}
|
||||
|
||||
void adc_init()
|
||||
{
|
||||
PRR &= ~(1 << PRADC);
|
||||
ADCSRA |= (1 << ADEN) | 7;
|
||||
}
|
||||
|
||||
void adc_start_conversion(int ch)
|
||||
{
|
||||
ADMUX = (ADMUX & 0xF0) | (ch & 0x0F) | (1 << REFS0);
|
||||
ADCSRA |= (1 << ADSC) | (1 << ADIE);
|
||||
}
|
||||
|
||||
int adc_is_fininshed()
|
||||
{
|
||||
return (int)(0 == (ADCSRA & (1 << ADSC)));
|
||||
|
||||
}
|
||||
int16_t adc_read()
|
||||
{
|
||||
return adc_value;
|
||||
|
||||
while(!adc_is_fininshed());
|
||||
uint16_t value_low = (uint16_t)ADCL;
|
||||
uint16_t value_high = (uint16_t)ADCH;
|
||||
ADCSRA |= (1<<ADIF);
|
||||
return value_high << 8 | value_low;
|
||||
}
|
||||
|
||||
void uart_putc(char c)
|
||||
{
|
||||
while((UCSR0A & (1<<UDRE0)) == 0)
|
||||
@@ -121,14 +179,17 @@ FILE uart_file = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
/* Replace with your application code */
|
||||
uart_init();
|
||||
timer_init();
|
||||
adc_init();
|
||||
|
||||
stdout = &uart_file;
|
||||
printf("UART test version 1.0\n\n");
|
||||
|
||||
sei();
|
||||
adc_start_conversion(adc_ch);
|
||||
while (1)
|
||||
{
|
||||
|
||||
@@ -141,6 +202,16 @@ int main(void)
|
||||
printf("Command : %c\n", msgData);
|
||||
break;
|
||||
|
||||
case AdcComplete:
|
||||
{
|
||||
size_t ch = msgData;
|
||||
if (ch == 6)
|
||||
{
|
||||
printf("ADC=%d\n", adc_value[ch]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Timer:
|
||||
switch(ledState)
|
||||
{
|
||||
|
||||
@@ -26,13 +26,13 @@
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
<d4p1:anyType i:type="FileInfo">
|
||||
<AbsolutePath>D:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.150\include\avr\iom644pa.h</AbsolutePath>
|
||||
<AbsolutePath>D:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.150\include\avr\iom328p.h</AbsolutePath>
|
||||
<Attribute></Attribute>
|
||||
<Category>header</Category>
|
||||
<Condition>C</Condition>
|
||||
<FileContentHash>dmamizNMG7EXBqtW9kb8VA==</FileContentHash>
|
||||
<FileContentHash>UMk4QUzkkuShabuoYtNl/Q==</FileContentHash>
|
||||
<FileVersion></FileVersion>
|
||||
<Name>include/avr/iom644pa.h</Name>
|
||||
<Name>include/avr/iom328p.h</Name>
|
||||
<SelectString></SelectString>
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
@@ -59,13 +59,13 @@
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
<d4p1:anyType i:type="FileInfo">
|
||||
<AbsolutePath>D:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.150\gcc\dev\atmega644pa</AbsolutePath>
|
||||
<AbsolutePath>D:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.150\gcc\dev\atmega328p</AbsolutePath>
|
||||
<Attribute></Attribute>
|
||||
<Category>libraryPrefix</Category>
|
||||
<Condition>GCC</Condition>
|
||||
<FileContentHash i:nil="true" />
|
||||
<FileVersion></FileVersion>
|
||||
<Name>gcc/dev/atmega644pa</Name>
|
||||
<Name>gcc/dev/atmega328p</Name>
|
||||
<SelectString></SelectString>
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
@@ -74,7 +74,7 @@
|
||||
<PackPath>D:/Program Files (x86)/Atmel/Studio/7.0/Packs/atmel/ATmega_DFP/1.2.150/Atmel.ATmega_DFP.pdsc</PackPath>
|
||||
<PackVersion>1.2.150</PackVersion>
|
||||
<PresentInProject>true</PresentInProject>
|
||||
<ReferenceConditionId>ATmega644PA</ReferenceConditionId>
|
||||
<ReferenceConditionId>ATmega328P</ReferenceConditionId>
|
||||
<RteComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
||||
<d4p1:string></d4p1:string>
|
||||
</RteComponents>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<ProjectVersion>7.0</ProjectVersion>
|
||||
<ToolchainName>com.Atmel.AVRGCC8.C</ToolchainName>
|
||||
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
|
||||
<avrdevice>ATmega644PA</avrdevice>
|
||||
<avrdevice>ATmega328P</avrdevice>
|
||||
<avrdeviceseries>none</avrdeviceseries>
|
||||
<OutputType>Executable</OutputType>
|
||||
<Language>C</Language>
|
||||
@@ -26,7 +26,7 @@
|
||||
<OverrideVtorValue>exception_table</OverrideVtorValue>
|
||||
<BootSegment>2</BootSegment>
|
||||
<ResetRule>0</ResetRule>
|
||||
<eraseonlaunchrule>0</eraseonlaunchrule>
|
||||
<eraseonlaunchrule>1</eraseonlaunchrule>
|
||||
<EraseKey />
|
||||
<AsfFrameworkConfig>
|
||||
<framework-data xmlns="">
|
||||
@@ -42,7 +42,7 @@
|
||||
</AsfFrameworkConfig>
|
||||
<avrtool>com.atmel.avrdbg.tool.atmelice</avrtool>
|
||||
<avrtoolserialnumber>J41800089261</avrtoolserialnumber>
|
||||
<avrdeviceexpectedsignature>0x1E960A</avrdeviceexpectedsignature>
|
||||
<avrdeviceexpectedsignature>0x1E950F</avrdeviceexpectedsignature>
|
||||
<com_atmel_avrdbg_tool_simulator>
|
||||
<ToolOptions>
|
||||
<InterfaceProperties>
|
||||
@@ -55,25 +55,36 @@
|
||||
</ToolNumber>
|
||||
<ToolName>Simulator</ToolName>
|
||||
</com_atmel_avrdbg_tool_simulator>
|
||||
<avrtoolinterface>JTAG</avrtoolinterface>
|
||||
<avrtoolinterface>debugWIRE</avrtoolinterface>
|
||||
<com_atmel_avrdbg_tool_atmelice>
|
||||
<ToolOptions>
|
||||
<InterfaceProperties>
|
||||
<IspClock>125000</IspClock>
|
||||
<JtagDbgClock>6000000</JtagDbgClock>
|
||||
</InterfaceProperties>
|
||||
<InterfaceName>JTAG</InterfaceName>
|
||||
<InterfaceName>debugWIRE</InterfaceName>
|
||||
</ToolOptions>
|
||||
<ToolType>com.atmel.avrdbg.tool.atmelice</ToolType>
|
||||
<ToolNumber>J41800089261</ToolNumber>
|
||||
<ToolName>Atmel-ICE</ToolName>
|
||||
</com_atmel_avrdbg_tool_atmelice>
|
||||
<avrtoolinterfaceclock>6000000</avrtoolinterfaceclock>
|
||||
<custom>
|
||||
<ToolOptions xmlns="">
|
||||
<InterfaceProperties>
|
||||
</InterfaceProperties>
|
||||
<InterfaceName>debugWIRE</InterfaceName>
|
||||
</ToolOptions>
|
||||
<ToolType xmlns="">custom</ToolType>
|
||||
<ToolNumber xmlns="">
|
||||
</ToolNumber>
|
||||
<ToolName xmlns="">Custom Programming Tool</ToolName>
|
||||
</custom>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<ToolchainSettings>
|
||||
<AvrGcc>
|
||||
<avrgcc.common.Device>-mmcu=atmega644pa -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.2.150\gcc\dev\atmega644pa"</avrgcc.common.Device>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.2.150\gcc\dev\atmega328p"</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>
|
||||
@@ -111,7 +122,7 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<ToolchainSettings>
|
||||
<AvrGcc>
|
||||
<avrgcc.common.Device>-mmcu=atmega644pa -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.2.150\gcc\dev\atmega644pa"</avrgcc.common.Device>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.2.150\gcc\dev\atmega328p"</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>
|
||||
|
||||
Reference in New Issue
Block a user