]> defiant.homedns.org Git - ros_wild_thumper.git/blobdiff - avr/nano/main.c
Implement current reading
[ros_wild_thumper.git] / avr / nano / main.c
index 16a316cd655688767528f4d2763634f24ed1fcfe..78e85316980ef5d067b43a55e2d7c6b89c0f8529 100644 (file)
@@ -8,6 +8,7 @@
 #include <util/twi.h>
 #include <avr/eeprom.h>
 #include <avr/wdt.h>
 #include <util/twi.h>
 #include <avr/eeprom.h>
 #include <avr/wdt.h>
+#include <avr/pgmspace.h>
 #include "uart.h"
 
 /*
 #include "uart.h"
 
 /*
@@ -25,6 +26,8 @@
  * 0x0A Voltage LSB
  * 0x0B Distance forward2 MSB
  * 0x0C Distance forward2 LSB
  * 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)
  *
  * 0x15 Distance forward1 MSB (read only)
  * 0x16 Distance forward1 LSB (read only)
@@ -52,6 +55,7 @@ static volatile uint8_t start_dist_fwd1=0;
 static volatile uint8_t start_dist_fwd2=0;
 static volatile uint8_t start_dist_bwd=0;
 static volatile uint16_t voltage=0;
 static volatile uint8_t start_dist_fwd2=0;
 static volatile uint8_t start_dist_bwd=0;
 static volatile uint16_t voltage=0;
+static volatile uint16_t current=0;
 static volatile uint8_t pind_pre=0;
 
 ISR(TWI_vect)
 static volatile uint8_t pind_pre=0;
 
 ISR(TWI_vect)
@@ -81,7 +85,7 @@ ISR(TWI_vect)
                                default:
                                        TWI_NAK;
                        }
                                default:
                                        TWI_NAK;
                        }
-                       ireg++;
+                       if (ireg < 0xff) ireg++;
                        break;
                case TW_ST_SLA_ACK: // start read
                case TW_ST_DATA_ACK: // read
                        break;
                case TW_ST_SLA_ACK: // start read
                case TW_ST_DATA_ACK: // read
@@ -140,6 +144,15 @@ ISR(TWI_vect)
                                        TWDR = tmp16;
                                        TWI_ACK;
                                        break;
                                        TWDR = tmp16;
                                        TWI_ACK;
                                        break;
+                               case 0x0D: // Current MSB
+                                       tmp16 = current;
+                                       TWDR = tmp16>>8;
+                                       TWI_ACK;
+                                       break;
+                               case 0x0E: // Current LSB
+                                       TWDR = tmp16;
+                                       TWI_ACK;
+                                       break;
                                case 0x15: // Distance forward1 MSB
                                        tmp16 = dist_forward1;
                                        TWDR = tmp16>>8;
                                case 0x15: // Distance forward1 MSB
                                        tmp16 = dist_forward1;
                                        TWDR = tmp16>>8;
@@ -231,6 +244,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;
 ISR(INT0_vect) {
        static uint16_t t_start=0;
        uint16_t t_now = TCNT1;
@@ -305,7 +328,7 @@ int main(void) {
        EICRA = (1 << ISC10) | (1 << ISC00);
        PCICR = (1 << PCIE2);
 
        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();
 
        set_sleep_mode(SLEEP_MODE_IDLE);
        sei();
@@ -320,6 +343,9 @@ int main(void) {
                        case 0x09: // voltage
                                voltage = get_voltage();
                                break;
                        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();
                        case 0xff: // Magic reg that starts the bootloader
                                if (bootloader == 0xa5) {
                                        cli();