X-Git-Url: https://defiant.homedns.org/gitweb/?p=ros_wild_thumper.git;a=blobdiff_plain;f=avr%2Fnano%2Fmain.c;h=cf3070cb92c34ff23ccdca1437b24bfb0b891ddc;hp=16a316cd655688767528f4d2763634f24ed1fcfe;hb=e46de78ee34c663f5ac1ceb0656acd0cf9370288;hpb=7d4c9ee68cdd0e924df9d5517c5849047a76e875 diff --git a/avr/nano/main.c b/avr/nano/main.c index 16a316c..cf3070c 100644 --- a/avr/nano/main.c +++ b/avr/nano/main.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "uart.h" /* @@ -25,6 +26,8 @@ * 0x0A Voltage LSB * 0x0B Distance forward2 MSB * 0x0C Distance forward2 LSB + * 0x0D Current MSB + * 0x0E Current LSB * * 0x15 Distance forward1 MSB (read only) * 0x16 Distance forward1 LSB (read only) @@ -37,6 +40,33 @@ */ +/** + * Nano Pinout + * A0 PC0/ADC0 + * A1 PC1/ADC1 + * A2 PC2/ADC2 + * A3 PC3/ADC3 + * A4 PC4/ADC4 + * A5 PC5/ADC5 + * A6 ADC6 + * A7 ADC7 + * D0 PD0/RX + * D1 PD1/TX + * D2 PD2/IND0 + * D3 PD3/IND1 + * D4 PD4/T0 + * D5 PD5/T1 + * D6 PD6/AIN0 + * D7 PD7/AIN1 + * D8 PB0/ICP + * D9 PB1/OC1 + * D10 PB2/SS + * D11 PB3/MOSI + * D12 PB4/MISO + * D13 PB5/SCK + */ + + #define TWI_ACK TWCR = (1<>8; + TWI_ACK; + break; + case 0x0E: // Current LSB + TWDR = tmp16; + TWI_ACK; + break; case 0x15: // Distance forward1 MSB tmp16 = dist_forward1; TWDR = tmp16>>8; @@ -176,6 +216,8 @@ ISR(TWI_vect) case TW_SR_STOP: TWI_ACK; break; + case TW_NO_INFO: + break; default: TWI_RESET; } @@ -231,6 +273,16 @@ static unsigned short get_voltage(void) { } +/* + * zero offset: 0.5V + * sensitivity: 133mV/A + */ +static unsigned short get_current(void) { + double volt = ReadChannel(3)*5.0171; // mV + return (volt-517.78)/0.12656; // mA +} + + ISR(INT0_vect) { static uint16_t t_start=0; uint16_t t_now = TCNT1; @@ -305,7 +357,7 @@ int main(void) { EICRA = (1 << ISC10) | (1 << ISC00); PCICR = (1 << PCIE2); - printf("\r\nStart\r\n"); + printf_P(PSTR("\r\nStart\r\n")); set_sleep_mode(SLEEP_MODE_IDLE); sei(); @@ -320,6 +372,9 @@ int main(void) { case 0x09: // voltage voltage = get_voltage(); break; + case 0x0d: // current + current = get_current(); + break; case 0xff: // Magic reg that starts the bootloader if (bootloader == 0xa5) { cli();