Metro M4 w/ DS3234

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Jl315
 
Posts: 2
Joined: Fri Mar 25, 2022 3:59 pm

Metro M4 w/ DS3234

Post by Jl315 »

Hello thanks in advance for any help that you can provide. I am new to all this so my coding skills are limited. I just replaced an arduino mega with a Metro M4 express. I installed the arduino IDE onto the metro and swapped over a DS3234 RTC. as far as I can tell I have the pins correctly installed and have updated the sketch to reflect new pins. When the time is printed to the serial monitor the time is all wacky and does not print anything meaningful. Thank you for any help and suggestions

Code: Select all

///////////////////////////////// Nextion Library ///////////////////////////////////////
#include <SPI.h>
///////////////////////////////// DS18B20 Sensor Library ///////////////////////////////
#include <DallasTemperature.h>
#include <OneWire.h>

/////////////////////////////// RTC Inputs ///////////////////////////////////////////
//int chipSelect = 53 ;
int chipSelect = 3 ;
int intFreq = 4 ;

////////////////////////////// RTC ///////////////////////////////////////////////////
#define WRITE_CONTROL_REG 0x8E
#define READ_CONTROL_REG 0x0E
#define WRITE_TIME_REG 0x80
#define READ_TIME_REG 0x00

///////////////////////////////Data Wire Is Plugged into Pin 46 ///////////////////////////////////////////////////
//#define ONE_WIRE_BUS 46
#define ONE_WIRE_BUS 2

/////////////////////////////// Setup A oneWire Instance To Communicate With Any oneWire Device /////////////////
OneWire oneWire(ONE_WIRE_BUS);

//////////////////////////////Pass oneWire Reference to Dallas Temperature /////////////////////////////////////
DallasTemperature sensors(&oneWire);


////////////////////////////Assign The Unique Adresses Of oneWire Temperature Sensor ///////////////////////////
DeviceAddress ISOtank = {0x28, 0xAD, 0x61, 0x74, 0x06, 0x00, 0x00, 0xA3};
 
/////////////////////////////// RTC Code ////////////////////////////////////////////////// 
 uint8_t mybuffer[4];
// float f;
 //memcpy(&f, mybuffer, 4);
 
 struct timeParameters{
  uint8_t ss ;
  uint8_t mm ;
  uint8_t hh ; 
  uint8_t dy ; 
  uint8_t d ;
  uint8_t m ;
  uint8_t y ;
} ;




void RTC_init(int chipSelect, int intFreq){
  /*
   * The DS3234 offers four output frequencies:
   * Options:
   * 1:     1     kHz
   * 2:     1.024 kHz
   * 3:     4.096 kHz
   * 4:     8.192 kHz
   * 5:     OFF
   */

   pinMode(chipSelect, OUTPUT) ;
   digitalWrite(chipSelect, LOW) ;
   SPI.transfer(READ_CONTROL_REG) ;
   byte origianlConfig = SPI.transfer(0x00) ;   
   digitalWrite(chipSelect, HIGH) ;
   delay(10) ;

   byte configModifier ;
   byte newConfig ;

   if (intFreq == 5){
    configModifier = 0b10111111 ;
    newConfig = configModifier & origianlConfig ;
   }
   else if (intFreq < 5){
    uint8_t freqOption = intFreq -1 ;
    configModifier = (freqOption << 3) | 0b1000000 ;
    newConfig = configModifier | (origianlConfig & 0b11100011) ;
   }

   digitalWrite(chipSelect, LOW) ;
   SPI.transfer(WRITE_CONTROL_REG) ;
   SPI.transfer(newConfig) ;
   digitalWrite(chipSelect, HIGH) ;
   delay(10) ;
}

static uint8_t convertValueIN(uint8_t value){
  uint8_t convertedVal = value - 6 * (value >> 4) ;
  return convertedVal ;
}

static uint8_t convertValOUT(uint8_t value){
  uint8_t convertedVal = value +6 * (value /10) ;
  return convertedVal ;
}
   
void setTime(int chipSelect, timeParameters *timeVals){
  pinMode(chipSelect, OUTPUT) ;

  digitalWrite(chipSelect, LOW) ;
  SPI.transfer(WRITE_TIME_REG) ;
  SPI.transfer(convertValOUT(timeVals->ss)) ;
  SPI.transfer(convertValOUT(timeVals->mm)) ;
  SPI.transfer(convertValOUT(timeVals->hh)) ;
  SPI.transfer(convertValOUT(timeVals->dy)) ;
  SPI.transfer(convertValOUT(timeVals->d)) ;
  SPI.transfer(convertValOUT(timeVals->m)) ;
  SPI.transfer(convertValOUT(timeVals->y)) ;
  digitalWrite(chipSelect, HIGH) ;
  delay(10) ;
}

void readTime(int chipSelect, timeParameters *timeVals){
  pinMode(chipSelect, OUTPUT) ;

  digitalWrite(chipSelect, LOW) ;
  SPI.transfer(READ_TIME_REG) ;
  timeVals->ss = convertValueIN(SPI.transfer(0x00)) ;
  timeVals->mm = convertValueIN(SPI.transfer(0x00)) ;
  timeVals->hh = convertValueIN(SPI.transfer(0x00)) ;
  timeVals->dy = convertValueIN(SPI.transfer(0x00)) ;
  timeVals->d = convertValueIN(SPI.transfer(0x00)) ;
  timeVals->m = convertValueIN(SPI.transfer(0x00)) ;
  timeVals->y = convertValueIN(SPI.transfer(0x00)) ;
  digitalWrite(chipSelect, HIGH);
  delay(10);
}

///////////////////////////////////////////// RTC Variable To Set Time ///////////////////////////////////////////////////
uint8_t s = 00;
uint8_t mi = 33;
uint8_t h = 20;
uint8_t da = 3;
uint8_t d = 22;
uint8_t mo = 3;
uint8_t y = 22;
  timeParameters TimeNow = {  s, mi, h, da, d, mo, y } ;



uint32_t timeTrigs = 0 ; 

void timeFunc(){
  timeTrigs += 1 ;
}

void setup() {
//////////////////////////////////////// Begin RTC ///////////////////////////////////////////////////////////////////
   SPI.begin() ;
   SPI.setBitOrder(MSBFIRST) ;
   SPI.setDataMode(SPI_MODE1) ;
   
   
   RTC_init(chipSelect, intFreq) ; 
   setTime(chipSelect, &TimeNow) ;
   attachInterrupt(0, timeFunc, RISING) ;
/////////////////////////////////////// Begin Serial Communication /////////////////////////////////////////////////   
   Serial.begin(9600) ;
   Serial1.begin(9600);
   delay(10) ;

//////////////////////////////////////// Begin DS18B20 ////////////////////////////////////////////////////////////   
   sensors.begin();
  //////////// Set The Resolution ///////
  sensors.setResolution(ISOtank, 9);
}



void loop() {
//////////////////////////////////////////// RTC Run ////////////////////////////////////////////////////////////////////  
  int NewHour;
  delay(1000) ;
  readTime(chipSelect, &TimeNow) ;


////////////////////////////////////////////// Print Time to Serial Monitor /////////////////////////////////////////////////////
  Serial.print("Time:  ") ; 
 
  if(TimeNow.hh>12){
   Serial.print(TimeNow.hh-12);
   Serial.print(":") ;
     if(TimeNow.mm < 10)
   Serial.print("0");
   Serial.print(TimeNow.mm) ;
   Serial.print(":") ;
  if(TimeNow.ss < 10)
   Serial.print("0");
   Serial.print(TimeNow.ss) ;
   Serial.println(" P.M.");
 }
   
 else if(TimeNow.hh<12){
  Serial.print(TimeNow.hh);
  Serial.print(":");
    if(TimeNow.mm < 10)
   Serial.print("0");
   Serial.print(TimeNow.mm) ;
   Serial.print(":") ;
  if(TimeNow.ss < 10)
   Serial.print("0");
   Serial.print(TimeNow.ss) ;
   Serial.println(" A.M.");
   
  }
   
  else if(Serial.print(TimeNow.hh)){
   Serial.print(":");
   if(TimeNow.mm < 10)
   Serial.print("0");
   Serial.print(TimeNow.mm) ;
   Serial.print(":") ;
   if(TimeNow.ss < 10)
   Serial.print("0");
   Serial.print(TimeNow.ss) ;
   Serial.println(" P.M.");
}
  Serial.print("Date:  ") ;
  Serial.print(TimeNow.m) ;
  Serial.print("/") ;
  Serial.print(TimeNow.d) ;
  Serial.print("/") ;
  Serial.println(TimeNow.y) ;

  //Serial.print("Time Triggers:  ") ; 
  //Serial.print(timeTrigs) ;
 // timeTrigs = 0 ;

/////////////////////////////// Print Temperature to Serial Monitor //////////////////////////////////////////////////////////////////
 delay(30);
 Serial.print("Requesting temperature...");
 Serial.print("Temperature is...");
 Serial.print(sensors.getTempF(ISOtank));
 Serial.println(" F");
 



 /////////////////////////////////////////////////////////// Print Time to Nextion .///////////////////////////////////////////////////////
 if(TimeNow.hh>12){
  Serial1.print(F("TIME.txt=\""));
  Serial1.print((TimeNow.hh)-12);
  Serial1.print(F(":"));
  if(TimeNow.mm < 10)
    Serial1.print("0");  
    Serial1.print(TimeNow.mm);
    Serial1.print(F(":"));
  if(TimeNow.ss < 10)
    Serial1.print("0");  
    Serial1.print(TimeNow.ss);
    Serial1.print(F(" P.M."));
    Serial1.print(F("\""));
    Serial1.print(F("\xFF\xFF\xFF"));
 }

 else if(TimeNow.hh<12){
    Serial1.print(F("TIME.txt=\""));
    Serial1.print(TimeNow.hh);
    Serial1.print(F(":"));
    if(TimeNow.mm < 10)
      Serial1.print("0");  
      Serial1.print(TimeNow.mm);
      Serial1.print(F(":"));
    if(TimeNow.ss < 10)
       Serial1.print("0");  
       Serial1.print(TimeNow.ss);
       Serial1.print(F(" A.M."));
       Serial1.print(F("\""));
       Serial1.print(F("\xFF\xFF\xFF"));
 }

 else if(Serial1.print(F("TIME.txt=\""))){
    Serial1.print(TimeNow.hh);
    Serial1.print(F(":"));
    if(TimeNow.mm < 10)
      Serial1.print("0");  
      Serial1.print(TimeNow.mm);
      Serial1.print(F(":"));
    if(TimeNow.ss < 10)
      Serial1.print("0");  
      Serial1.print(TimeNow.ss);
      Serial1.print(F(" P.M."));
      Serial1.print(F("\""));
      Serial1.print(F("\xFF\xFF\xFF"));
 }

  Serial1.print(F("DATE.txt=\""));
  Serial1.print(TimeNow.m);
  Serial1.print(F("/"));
  Serial1.print(TimeNow.d);
  Serial1.print(F("/"));
  Serial1.print(TimeNow.y);
  Serial1.print(F("\""));
  Serial1.print(F("\xFF\xFF\xFF"));


/////////////////////////////// Print Temp to Nextion ///////////////////////////////
 
  Serial1.print(F("ISOtemp.txt=\""));
sensors.requestTemperatures();

  Serial1.print(sensors.getTempF(ISOtank));
  Serial1.print(F("/"));
  
  Serial1.print(F("\""));
  Serial1.print(F("\xFF\xFF\xFF"));

}

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Metro M4 w/ DS3234

Post by adafruit_support_mike »

The first thing I notice is your use of the F() macro. That only works with ATmega microcontrollers.

The 32-bit M0 and M4 microcontrollers have enough RAM that holding strings in memory isn't as much of a problem, and aren't restricted from reading program memory the way ATmega microcontrollers are. All constants, including literal strings and variables declared with 'const' are kept in program memory and not stored in RAM.

User avatar
Jl315
 
Posts: 2
Joined: Fri Mar 25, 2022 3:59 pm

Re: Metro M4 w/ DS3234

Post by Jl315 »

Thank you for the reply. I will replace the F() macros. In the mean time I commented those lines out, all they do is print to my Nextion display, however something is still buggy because it is not reading the time properly.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Metro M4 w/ DS3234

Post by adafruit_support_mike »

Beyond the F() macro, it looks like you've rolled your own version of RTClib:

https://github.com/adafruit/RTClib

Calendar/date based code is notoriously tricky because it's full of special cases and intermediate operations. When working in that realm, it's best to start from a known-good reference implementation and move ideas over in small steps.

Locked
Please be positive and constructive with your questions and comments.

Return to “Metro, Metro Express, and Grand Central Boards”