работа Arduino pro mini 5v от 3.3v

Vasia.Z
Vasia.Z аватар
Offline
Зарегистрирован: 30.11.2013

К распберри через spi и i2c подключена про мини
Чтобы не городить конвертер уровней поключил напрямую и запитал ардуину от отдельного dc-dc на 3.3вольта, т.к. с 3.3в распи можно взять 30 или 50мА.
Написал тестовый скетч, который должен послать результат analogRead`а по i2c про запросу.
Код:

#include <Wire.h>

#define TWI_FREQ 100000L

#define SLAVE_ADDRESS 0x77
#define BUZZER_PIN 3
#define BATTERY_VLTG_DIVIDER_PIN A7

int number = 0;
int result = 0;
int state = 0;
 
double temp;
int vltg;

int len=1;
int maxlen = 2;

void setup() {
 pinMode(13, OUTPUT);
 pinMode(BATTERY_VLTG_DIVIDER_PIN, INPUT);
 
 // initialize i2c as slave
 Wire.begin(SLAVE_ADDRESS);
 
 // define callbacks for i2c communication
 Wire.onReceive(receiveData);
 Wire.onRequest(sendData);
}
 
void loop() {
 delay(10);
 temp = GetTemp();
 // vltg = random(200,900);
 vltg = analogRead(BATTERY_VLTG_DIVIDER_PIN);
 // vltg = GetVltg();
}
 
// callback for received data
void receiveData(int byteCount){
  result = 0;
  len = 1;
  while(Wire.available()) {
    number = Wire.read();
 
    if (number == 13){
      if (state == 0){
        digitalWrite(13, HIGH); // set the LED on
        state = 1;
        result = 0x12;
      } else{
        digitalWrite(13, LOW); // set the LED off
        state = 0;
        result = 0x14;
      }
      len = 1;
    }
   
    if(number==88) {
      result = (int)temp;
      len = 1;
    }

    if(number==0x01){
      // result = vltg;
      result = (int)vltg;
      // result = 0x8814;
      len=2;
      // ;  
    }
  }
}
 
// callback for sending data
void sendData(){
  uint8_t buff[len];
  if (len == 1){
    buff[0] = result;
  }
  if (len == 2){
    buff[0] = lowByte(result);
    buff[1] = highByte(result);
  }
  Wire.write(buff, len);
}
 
// Get the internal temperature of the arduino
double GetTemp(void)
{
 unsigned int wADC;
 double t;
 ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
 ADCSRA |= _BV(ADEN); // enable the ADC
 delay(20); // wait for voltages to become stable.
 ADCSRA |= _BV(ADSC); // Start the ADC
 while (bit_is_set(ADCSRA,ADSC));
 wADC = ADCW;
 t = (wADC - 324.31 ) / 1.22;
 return (t);
 // return random(0, 10);
}
// int GetVltg(void)
// {
//   //double readed;// = analogRead(BATTERY_VLTG_DIVIDER_PIN);
//   int readed = analogRead(BATTERY_VLTG_DIVIDER_PIN);
//   //magic
//   // int readed = 987;
//   // double tmp = 3.3*readed/1023;
//   // int vltg_calculated = tmp*100;
//   int vltg_calculated = readed;
//   //magic
//   return vltg_calculated;
// }

Если в loop'е vltg сделать случайным, а не считывать с ноги ардуины, то все работает нормально, а если считывать - то скрипт на питоне, читающий из i2c вылетает с ошибкой IOError: [Errno 5] Input/output error

Фьюзы на ардуине не перешивал

Вопрос - чтобы "взлетело" что сделать? все мозги уже сломал

Vasia.Z
Vasia.Z аватар
Offline
Зарегистрирован: 30.11.2013

нашел в чем дело - GetTemp, дездумно скопипасченная из туториала про работе с i2c, что-то меняла в настройках АЦП ардуины