Store › Forums › Video Experimenter › General Discussion › RTC with Video Experimenter
- This topic has 17 replies, 2 voices, and was last updated 11 years, 3 months ago by Michael.
-
AuthorPosts
-
May 17, 2013 at 2:25 pm #619Jack1Member
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);
}
May 18, 2013 at 1:08 pm #1659MichaelKeymasterYou 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.
May 18, 2013 at 4:45 pm #1660Jack1MemberI think it uses I2C communications. Is there any other way i could get time displayed on the video?
May 19, 2013 at 1:23 pm #1662MichaelKeymasterYou 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.
May 19, 2013 at 4:37 pm #1664Jack1MemberCould you provide some example code on ISR and those timers to get me started?
May 19, 2013 at 8:02 pm #1666MichaelKeymasterYou 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.htmlSorry, 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.
May 20, 2013 at 1:23 pm #1668Jack1MemberThank you! I think i can do it now. Thanks for your help
May 21, 2013 at 12:35 pm #1669Jack1MemberSo 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);
}May 21, 2013 at 1:20 pm #1670MichaelKeymasterWhat do you mean by “doesn’t work”. You gotta provide information.
May 21, 2013 at 1:40 pm #1671Jack1MemberIt 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.
May 21, 2013 at 1:57 pm #1672MichaelKeymasterdo 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.
May 21, 2013 at 2:03 pm #1673Jack1MemberI’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.May 21, 2013 at 2:58 pm #1674Jack1MemberThe 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:
June 11, 2013 at 9:48 pm #1708Jack1MemberStill no answer. :/
June 12, 2013 at 1:31 am #1709MichaelKeymasterThe 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.
-
AuthorPosts
- You must be logged in to reply to this topic.