chipKIT® Development Platform

Inspired by Arduino™

Max32 I2C clash

Created Sat, 21 Dec 2013 06:47:09 +0000 by Ian Billing


Ian Billing

Sat, 21 Dec 2013 06:47:09 +0000

Please can someone help? The following code works perfectly fine on my Max32 (with WiFi shield and SD card) when it is the only code that I load but, when I incorporate it into my project it seizes up. I is probably due to a clash with another IRQ or a pin that I am using for I/O.

BTW, the code sets the time obtained from an NTP server, via I2C (pins 20 & 21), of the DS1307 that I have hooked up:

/*****************************************************************************************************************************
 *						setDateDs1307 
 *                             strSetTime to come in the format: "2013/12/21 11:21:04"
 *****************************************************************************************************************************/
void setDateDs1307(String strSetTime){    
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  strSetTime = strSetTime.substring(17,19) + strSetTime.substring(14,16) + strSetTime.substring(11,13) + '3'+ strSetTime.substring(8,10)+ strSetTime.substring(5,7) + strSetTime.substring(2,4);
  
  second = (byte) ((strSetTime.charAt(0) - 48) * 10 + strSetTime.charAt(1) - 48);
  minute = (byte) ((strSetTime.charAt(2) - 48) * 10 + strSetTime.charAt(3) - 48);
  hour = (byte) ((strSetTime.charAt(4) - 48) * 10 + strSetTime.charAt(5) - 48);
  dayOfWeek = (byte) ((strSetTime.charAt(6) - 48));
  dayOfMonth = (byte) ((strSetTime.charAt(7) - 48) * 10 + strSetTime.charAt(8) - 48);
  month = (byte) ((strSetTime.charAt(9) - 48) * 10 + strSetTime.charAt(10) - 48);
  year = (byte) ((strSetTime.charAt(11) - 48) * 10 + strSetTime.charAt(12) - 48);
  
  String strError;
  if (strSetTime.length() != 13) strError = "Time string != 13 - error in time format, unable to set DS1307 date/ time";
  if (second >59) strError = "Seconds > 59 : error in time format, unable to set DS1307 date/ time";
  if (minute >59) strError = "Minutes > 59 : error in time format, unable to set DS1307 date/ time";
  if (hour >24) strError = "Hours > 24 : error in time format, unable to set DS1307 date/ time";
  if (dayOfWeek >7) strError = "dayOfWeek > 7 : error in time format, unable to set DS1307 date/ time";
  if (dayOfWeek == 0) strError = "dayOfWeek = 0 : error in time format, unable to set DS1307 date/ time";
  if (month >12) strError = "Month > 12 : error in time format, unable to set DS1307 date/ time";
  if (month == 0) strError = "Month = 0 : error in time format, unable to set DS1307 date/ time";
  if (year >20) strError = "Year > 2020 : error in time format, unable to set DS1307 date/ time";
  if (year <13) strError = "Year < 13 : error in time format, unable to set DS1307 date/ time";

  if (strError.length()>0){
      Serial.println(strError);
      return;
     }
 
  Serial.println("Setting DS1307 RTC: ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.println(year, DEC);

  Wire.beginTransmission(0x68); //The address of the DS1307 is 0x68
  Wire.send(byte(0x00));
  Wire.send(decToBcd(second));  // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){return ( (val/10*16) + (val%10) );}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){return ( (val/16*10) + (val%16) );}

and, for reference, here are all the i/o pins that I am using:

/***********************************************************************************************
 *						    VOID setupIOpins
 ************************************************************************************************/
void setupIOpins(){
  //Pins 71, 70, 9, 6, 5, 3 are used for the 16x2 LCD
  //Pins 4, 11,12 & 13 are used by the SD card
  //Pin 10 is used by the WiFi shield
  //Pins 3, 5, 6, 9, 70, 71 are used by the LCD
  //Pins 72, 73, 74, 75 are used by front panel push buttons (75 is also the Max32 RTC input - future TODO)
  //Pin 76 is used by watchdog
  //Pin 77 is digital input D9
  //Pin 82  is digital input D8
  
  //Unusable pins
  //Pins 24, 26, 27, 29, 43, 40, 41, 42, 44, 46, 50, 51, 52, 53, are Unusable
  //Pin 44 is the 3.3V ref voltage input for analog pins
  //Pins 40, 41, 42 are connected to analog inputs A11, A12, A13
  
  //LED pins
  //pin(3);   //WiFi shield LED1 (used for LCD) 
  //pin(5);   //WiFi shield LED2 (used for LCD)
  //pin(6);   //WiFi shield LED3 (used for LCD)
  //pin(9);   //WiFi shield LED4 (used for LCD
  
  //pin(7);  //used for software reset in conjunction with watchdog on pin 76 
  
  //Watchdog
  pinMode(76, OUTPUT);
  digitalWrite(76, LOW);
  
  //PANEL LEDs - which double as flip flop outputs 1 -4
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);
  pinMode(LED4,OUTPUT);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
   
  //Digital Outputs 
  pinMode(D0, OUTPUT); 
  pinMode(D1, OUTPUT); 
  pinMode(D2, OUTPUT); 
  pinMode(D3, OUTPUT); 
  pinMode(D4, OUTPUT); 
  pinMode(D5, OUTPUT); 
  pinMode(D6, OUTPUT); 
  pinMode(D7, OUTPUT);
 digitalWrite(D0, LOW);
 digitalWrite(D1, LOW);
 digitalWrite(D2, LOW);
 digitalWrite(D3, LOW);
 digitalWrite(D4, LOW);
 digitalWrite(D5, LOW);
 digitalWrite(D6, LOW);
 digitalWrite(D7, LOW);
   
 //Push button switches - actve low
  pinMode(BTN1, INPUT); 
  pinMode(BTN2, INPUT);
  pinMode(BTN3, INPUT); 
  pinMode(BTN4, INPUT);

  //Digital inputs -active low
  pinMode(D8, INPUT); 
  pinMode(D9, INPUT); 
  pinMode(D10, INPUT); 
  pinMode(D11, INPUT); 
  pinMode(D12, INPUT); 
  pinMode(D13, INPUT); 
  pinMode(D14, INPUT); 
  pinMode(D15, INPUT);

  digitalWrite(BTN1, HIGH);       
  digitalWrite(BTN2, HIGH);
  digitalWrite(BTN3, HIGH);       
  digitalWrite(BTN4, HIGH);
  digitalWrite(D8, HIGH);
  digitalWrite(D9, HIGH);
  digitalWrite(D10, HIGH);
  digitalWrite(D11, HIGH);
  digitalWrite(D12, HIGH);
  digitalWrite(D13, HIGH);
  digitalWrite(D14, HIGH);
  digitalWrite(D15, HIGH);

}

Ian Billing

Sat, 21 Dec 2013 17:36:06 +0000

To answer my own question:

As stand alone code I had Wire.begin() in Setup() but had omitted to add it when I brought the code into my project. :roll: :roll:


majenko

Sat, 21 Dec 2013 19:18:37 +0000

D'oh. I was thinking along the same lines, but from the other direction. I was meaning to ask if there were two bits of code that do a Wire.begin(), as that can cause lockups...