git-svn-id: http://moon:8086/svn/projects/HendiControl@10 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-02-14 20:27:43 +00:00
parent e899baf919
commit 189e3454d9
3 changed files with 97 additions and 15 deletions
+73 -2
View File
@@ -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)
{