RTC with Video Experimenter

Store Forums Video Experimenter General Discussion RTC with Video Experimenter

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #619
    Jack1
    Member

    How can i use an rtc clock at the same time as video experimenter? The rtc is ds1307.
    Here is my code which doesnt work at the moment:

    #include 
    #include
    #include
    #include

    #define W 136
    #define H 96

    RTC_DS1307 RTC;
    TVout tv;
    char s[32];

    void setup() {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
    Serial.print("setup()");
    tv.begin(PAL, W, H);
    initOverlay();
    tv.select_font(font6x8);
    tv.fill(0);
    }

    // Initialize ATMega registers for video overlay capability.
    // Must be called after tv.begin().
    void initOverlay() {
    TCCR1A = 0;
    // Enable timer1. ICES0 is set to 0 for falling edge detection on input capture pin.
    TCCR1B = _BV(CS10);

    // Enable input capture interrupt
    TIMSK1 |= _BV(ICIE1);

    // Enable external interrupt INT0 on pin 2 with falling edge.
    EIMSK = _BV(INT0);
    EICRA = _BV(ISC11);
    }

    // Required to reset the scan line when the vertical sync occurs
    ISR(INT0_vect) {
    display.scanLine = 0;
    }

    void loop() {
    DateTime now = RTC.now();
    Serial.print(now.second(), DEC);
    Serial.println();
    sprintf(s, "%i:%i:%i %i.%i.%u", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    tv.print(0, 0, s);

    delay(1000);
    }
    #1659
    Michael
    Keymaster

    You can’t use anything requiring interrupts with the video experimenter. So, you can’t use Serial (use the pollserial implementation that is part of TVout). How does the RTC library work? If it uses interrupts, then the precise timing needed by TVout is going to interfere.

    Basically it takes all of the Arduino’s capability to generate a video signal, so interfacing with other complex hardware is always going to be very challenging or impossible.

    #1660
    Jack1
    Member

    I think it uses I2C communications. Is there any other way i could get time displayed on the video?

    #1662
    Michael
    Keymaster

    You could read the time from the RTC before starting your video stuff. Then use the Arduino to keep track of time after that. The Arduino itself can keep fairly accurate time. Use Timer2 because Timer1 is used by the video. Using timer2, you can set an appropriate prescalar to run an ISR every 100ms or something like that, and keep track of how often the ISR fires, therefore incrementing the time.

    That’s not a beginner approach, but can be done if you know how the microcontrollers timers work and how to use them.

    #1664
    Jack1
    Member

    Could you provide some example code on ISR and those timers to get me started?

    #1666
    Michael
    Keymaster

    You can look at the DefusableClock program. This device is a clock implemented in Arduino code. It uses timer1 to tick every 1 second, but you’ll need to use timer2. Timer2 is an 8-bit timer, so you need to “tick” more often than one second. Use a prescalar to adjust how often the timer2 counter increments. See the ATmega328 datasheet for that.

    Defusable Clock code is available here:
    https://nootropicdesign.com/defusableclock/hack.html

    Sorry, I don’t have the time to write it all for you. Every week I get a dozen requests to write code for people, and I just can’t possibly do that. Full-time career, running an electronics company on the side, family, etc. I hope everyone understands. nootropic design is a one-man company.

    #1668
    Jack1
    Member

    Thank you! I think i can do it now. Thanks for your help

    #1669
    Jack1
    Member

    So i made the code now but it still doesnt work for some reason.
    Could you see if it has something wrong? Here it is:

    #include 
    #include
    #include
    #include
    #include

    #define W 136
    #define H 96

    RTC_DS1307 RTC;
    TVout tv;
    char s[32];
    DateTime dateOnStart;

    unsigned int DATE_HOUR;
    unsigned int DATE_MIN;
    unsigned int DATE_SEC;
    unsigned int DATE_DAY;
    unsigned int DATE_MONTH;
    unsigned int DATE_YEAR;

    unsigned long int unixTime2;

    void increment() {
    unixTime2 = unixTime2 + 1;

    DateTime unixTime;
    DATE_HOUR = unixTime.hour();
    DATE_MIN = unixTime.minute();
    DATE_SEC = unixTime.second();
    DATE_DAY = unixTime.day();
    DATE_MONTH = unixTime.month();
    DATE_YEAR = unixTime.year();
    }

    void setup() {
    Wire.begin();
    RTC.begin();
    dateOnStart = RTC.now();
    unixTime2 = dateOnStart.unixtime();

    MsTimer2::set(1000, increment);
    MsTimer2::start();

    tv.begin(PAL, W, H);
    initOverlay();
    tv.select_font(font6x8);
    tv.fill(0);
    }

    // Initialize ATMega registers for video overlay capability.
    // Must be called after tv.begin().
    void initOverlay() {
    TCCR1A = 0;
    // Enable timer1. ICES0 is set to 0 for falling edge detection on input capture pin.
    TCCR1B = _BV(CS10);

    // Enable input capture interrupt
    TIMSK1 |= _BV(ICIE1);

    // Enable external interrupt INT0 on pin 2 with falling edge.
    EIMSK = _BV(INT0);
    EICRA = _BV(ISC11);
    }

    // Required to reset the scan line when the vertical sync occurs
    ISR(INT0_vect) {
    display.scanLine = 0;
    }

    void loop() {
    sprintf(s, "%u:%u:%u %u.%u.%u", DATE_HOUR, DATE_MIN, DATE_SEC, DATE_DAY, DATE_MONTH, DATE_YEAR);
    tv.print(0, 0, s);
    }
    #1670
    Michael
    Keymaster

    What do you mean by “doesn’t work”. You gotta provide information.

    #1671
    Jack1
    Member

    It dont show any text on screen. But when i remove all code related to rtc and change sprintf to something that outputs only text then it shows that text.

    #1672
    Michael
    Keymaster

    do you even know if your RTC works? Have you written a program without any video that reads the RTC?

    To debug a program, gradually introduce new functions and see when things go wrong. Take out the timer2 incrementing. Does the time display?

    Do you know for sure if your timer2 increment() function is being called?

    Have you tried introducing a delay in loop()? Your program doesn’t need to constantly update the screen. Once per second is enough.

    In short, figure out which of your code is even running at all, and gradually introduce more code. That’s all I can tell you at this point.

    #1673
    Jack1
    Member

    I’m sure that the RTC is working properly. I actually took timer code and RTC code away and then the date and time showed as zeros. I will try messing aorund with the code some more to see if i get it working.
    I think that the wire library interferes with the video experimenter.

    #1674
    Jack1
    Member

    The date and time is displayed correctly now! New problem is that when there is no video input to the video experimenter the arduino executes too fast and the clock goes fast forward. Here is code:

    #include 
    #include
    #include
    #include

    #define W 136
    #define H 96

    RTC_DS1307 RTC;
    TVout tv;
    char s[32];
    DateTime dateOnStart;

    unsigned int DATE_HOUR;
    unsigned int DATE_MIN;
    unsigned int DATE_SEC;
    unsigned int DATE_DAY;
    unsigned int DATE_MONTH;
    unsigned int DATE_YEAR;

    unsigned long int unixTime2;

    void increment() {
    unixTime2 = unixTime2 + 1;

    DateTime unixTime(unixTime2);
    DATE_HOUR = unixTime.hour();
    DATE_MIN = unixTime.minute();
    DATE_SEC = unixTime.second();
    DATE_DAY = unixTime.day();
    DATE_MONTH = unixTime.month();
    DATE_YEAR = unixTime.year();
    }

    void setup() {
    Wire.begin();
    RTC.begin();
    dateOnStart = RTC.now();
    unixTime2 = dateOnStart.unixtime();

    tv.begin(PAL, W, H);
    initOverlay();
    tv.select_font(font6x8);
    tv.fill(0);
    }

    // Initialize ATMega registers for video overlay capability.
    // Must be called after tv.begin().
    void initOverlay() {
    TCCR1A = 0;
    // Enable timer1. ICES0 is set to 0 for falling edge detection on input capture pin.
    TCCR1B = _BV(CS10);

    // Enable input capture interrupt
    TIMSK1 |= _BV(ICIE1);

    // Enable external interrupt INT0 on pin 2 with falling edge.
    EIMSK = _BV(INT0);
    EICRA = _BV(ISC11);
    }

    // Required to reset the scan line when the vertical sync occurs
    ISR(INT0_vect) {
    display.scanLine = 0;
    }

    void loop() {
    sprintf(s, "%u:%u:%u %u.%u.%u", DATE_HOUR, DATE_MIN, DATE_SEC, DATE_DAY, DATE_MONTH, DATE_YEAR);
    tv.print(0, 0, s);
    increment();
    delay(2500);
    }

    Another problem: there is some strange pixels on the screen.
    Here is a picture:

    #1708
    Jack1
    Member

    Still no answer. :/

    #1709
    Michael
    Keymaster

    The pixels are probably because you are using too much memory. There is only 2K in the Arduino, and the frame buffer uses 1.5K. So when you overflow the stack into the video RAM, you see the random pixels. The RTC library is probably using some buffers.

    Can you shrink your 32 buffer down to 20 bytes? Also, why use unsigned int for all those time fields, when a byte will do. You can cut your memory usage in half by changing them to byte (except for YEAR, which you need an int).

    It’s all about memory. Arduino has very little.

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.