RTClib not working with avr's native coding format.

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is my code,  i want to read the data from DS3231 RTC using the RTClib,h library and print it in 1602 LCD. It is compiled sucessfully. but the arduino got hanged after the rtc.begin() line. but it is working when i write the code in arduino's way. but not working in the standard coding format ( i mean, without using void setup and loop ,).  link to the adafrut's RTClib : https://github.com/adafruit/RTClib

 


#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <Wire.h>

RTC_DS3231 rtc;
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

DateTime now;
int main()
{
    DDRB |= 0b00100000; //pb5/D13 as output
    lcd.begin(16, 2);
    lcd.print("hello world");
    _delay_ms(1000);
    lcd.clear();

    rtc.begin(); // after this line this code get hangged in arduino,
    //it is not responding the below lines.

    //Wire.endTransmission();
    lcd.print("after rtc");

    while (true)
    {
        PORTB ^= (1 << PB5);
        lcd.clear();
        DateTime now = rtc.now();
        lcd.print(now.hour());
        lcd.print(':');
        lcd.print(now.minute());
        lcd.print(':');
        lcd.print(now.second());
        lcd.setCursor(0, 1);
        lcd.print(rtc.getTemperature());
        _delay_ms(1000);
    }
}

And the below code is working with fine , i used same library and almost same code. the only differance is i used arduino's way of coding structure.


#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <Wire.h>

RTC_DS3231 rtc;
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

DateTime now;
void setup()
{
    DDRB |= 0b00100000; //pb5/D13 as output
    lcd.begin(16, 2);
    lcd.print("hello world");
    _delay_ms(1000);
    lcd.clear();

    rtc.begin();
    //Wire.endTransmission();
    lcd.print("after rtc");
}
void loop()
{
    // blink the led one sec delay and print time and temprature.
    PORTB ^= (1 << PB5);
    lcd.clear();
    DateTime now = rtc.now();
    lcd.print(now.hour());
    lcd.print(':');
    lcd.print(now.minute());
    lcd.print(':');
    lcd.print(now.second());
    lcd.setCursor(0, 1);
    lcd.print(rtc.getTemperature());
    _delay_ms(1000);
}

i attached the working code's output below .

any help would be appreciated. smiley

 

thank you,

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try changing while(true) to while(1) like this

    while (1)
    {
        PORTB ^= (1 << PB5);
        lcd.clear();
        DateTime now = rtc.now();
        lcd.print(now.hour());
        lcd.print(':');
        lcd.print(now.minute());
        lcd.print(':');
        lcd.print(now.second());
        lcd.setCursor(0, 1);
        lcd.print(rtc.getTemperature());
        _delay_ms(1000);
    }

true is not defined in following includes

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>

So your code is compiling so it must be defined in the other includes where "true" might be defined as 0

Just guessing.

 

Last Edited: Sun. May 2, 2021 - 07:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Heisen wrote:
Try changing while(true) to while(1) like this

If that were the problem, the code wouldn't even compile.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SOORYADEV.K wrote:
it is working when i write the code in arduino's way

So, presumably, the Arduino framework is setting up some stuff that you have omitted from your "non-Arduino" version ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you Heisen for replaying.  smileyheart

i tried your suggestion. but the same problem is appearing. the code is hanging after rtc.begin().  ie , the "hello world" line is printitng with 1 sec delay and after clearing the display it shows nothing in display. thats why i tried to print "after rtc". the code is not working from that line.

Last Edited: Sun. May 2, 2021 - 08:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SOORYADEV.K wrote:
replaying. 

FYI, note that it's "replying"

 

i tried your suggestion. but the same problem is appearing.

It would - see #3.

 

in #4, I wrote:
the Arduino framework is setting up some stuff that you have omitted from your "non-Arduino" version 

See: https://forum.arduino.cc/t/can-you-use-main-in-a-regular-arduino-program/43239/4

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil i searched a lot about it in internet. but nothing got helpful sad. i'm a beginner at this so, i don't know much about arduino's frameworks. i thinks the same, but i don't know, which thing that i missed in this code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

https://forum.arduino.cc/t/main-function/295213/5

 

I Googled[1] "how to write main() in Arduino"

 

i'm a beginner at this

At what, exactly?

 

If you want to do without the Arduino framework, that will require that you have at least a reasonable grasp of "bare metal" programming.

 

So what is your actual goal here? If the Arduino approach works, why not just use it?

 

Some microcontroller "getting started" tips: https://www.avrfreaks.net/commen...

 

Some C learning & reference materials: http://blog.antronics.co.uk/2011...

 

And don't forget the 'Tutorials' section on this site ...

 

 

[1] Other internet search engines are available.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sun. May 2, 2021 - 08:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you awneil for sharing resources, i will try these.smileyyes

If you want to do without the Arduino framework, that will require that you have at least a reasonable grasp of "bare metal" programming.

yes, you are correct. i know to program pic  base level and mid range ics using MPLAB X IDE ,and 8051 assembler languages etc..( not a pro, but i know to code in register level and can understand artitecture of ic by reading datasheet). actually i meant that , i don't know much about arduino's way of working.that is more confusing for me. i usually use registers level coding,  thank you for your replay. i think i should use atmel studio than the arduino IDE.

 

So what is your actual goal here? If the Arduino approach works, why not just use it?

actually my entire code was done in the avr's native way.And i found something strange while i using this library. so for debugging purpose i use this example.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There's nothing to say that RTClib will work outside of the Arudino ecosystem. At the top of the .cpp file are a load of conditional '#defines' which will almost certainly need sorting out.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
There's nothing to say that RTClib will (sic?) work outside of the Arudino ecosystem

Did you mean "will" - or "will not" ?

 

 At the top of the .cpp file are a load of conditional '#defines' which will almost certainly need sorting out.

And, presumably, a number of other things that the Arduino framework will have set up for it?

 

I guess it may also depend upon other Arduino things - which would also have their own dependencies ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SOORYADEV.K wrote:
the avr's native way

None of it is "native" to the AVR - a microcontroller neither knows nor cares what what IDE or framework you use to develop your source code.

 

If you want to use Arduino stuff in an Atmel Microchip Studio project, the easiest way is probably to start with a working Arduino project featuring all the facilities you want - and import that into Studio:

 

https://www.avrfreaks.net/forum/microchip-trying-wean-people-arduino-atmel-studio

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


awneil wrote:

Brian Fairchild wrote:
There's nothing to say that RTClib will (sic?) work outside of the Arudino ecosystem

Did you mean "will" - or "will not" ?

 

Will. As in, RTClib is written to work in an Arduino ecosystem and might makes assumptions about the environment in which it is executing.

 

 

awneil wrote:

 At the top of the .cpp file are a load of conditional '#defines' which will almost certainly need sorting out.

And, presumably, a number of other things that the Arduino framework will have set up for it?

 

I guess it may also depend upon other Arduino things - which would also have their own dependencies ...

 

The GitHub page for RTClib says this...

 

 

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
RTClib is written to work in an Arduino ecosystem and might makes assumptions about the environment in which it is executing.

yes

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Arduino uses C++, although some of the lib's may work in C as well, but will depend on how it is written.  In AS7 you will need to start with a C++ project in order to use most Arduino lib's.

However the OP has stated the project compiles ok, so this may be a moot point.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"