ventisca
Newbie level 4
[HELP] RTC DS1307 with SQW/Out enable Error
Dear all
(I'm sorry for my bad english )
I'm using ATmega32 and RTC DS1307.
I want to get the millisecond value, so i enabled the SQW/out signal and count it using Timer/Counter 1 (connect SQW pin to PINB.1 ).
I'm currently using port C as a I2C port. (SCL=0, SDA=1)
i check the result with the LCD, it run perfectly for several second, and then the output get weird.
i'm using display format :
(time from RTC)-(time from counter)
or to be briefly
hh:mm:ss(from RTC)-ss(from counter):counter1
The second from RTC and counter1 should be the same ,
but suddenly the second from RTC shows random value.
What's wrong?
Thank's in advance
(based on video, the error not alaways occur after 10 second. It can be occur after 16 seconds, 22 seconds, 31 seconds, etc. It's random i think)
(i have tried other IC's DS1307 and/or the other clock 32768 Hz's crystal, but the error still there)
(but if i disable the SQW/out OR remove the SQW pull-up resistor, THE ERROR IS GONE !!!! .)
(but without the pull up resistor, the ATMEGA can't read the SQW signal. I need the SQW signal so i can't disable it or remove the SQW's pull up resistor )
This is the SrcCode, (using CVAVR)
Dear all
(I'm sorry for my bad english )
I'm using ATmega32 and RTC DS1307.
I want to get the millisecond value, so i enabled the SQW/out signal and count it using Timer/Counter 1 (connect SQW pin to PINB.1 ).
I'm currently using port C as a I2C port. (SCL=0, SDA=1)
i check the result with the LCD, it run perfectly for several second, and then the output get weird.
i'm using display format :
(time from RTC)-(time from counter)
or to be briefly
hh:mm:ss(from RTC)-ss(from counter):counter1
The second from RTC and counter1 should be the same ,
but suddenly the second from RTC shows random value.
What's wrong?
Thank's in advance
(based on video, the error not alaways occur after 10 second. It can be occur after 16 seconds, 22 seconds, 31 seconds, etc. It's random i think)
(i have tried other IC's DS1307 and/or the other clock 32768 Hz's crystal, but the error still there)
(but if i disable the SQW/out OR remove the SQW pull-up resistor, THE ERROR IS GONE !!!! .)
(but without the pull up resistor, the ATMEGA can't read the SQW signal. I need the SQW signal so i can't disable it or remove the SQW's pull up resistor )
This is the SrcCode, (using CVAVR)
Code:
/************************************************** ***
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 20/08/2012
Author : ventisca
Company : Ini bajakan cuy :P
Comments:
Chip type : ATmega32
Program type : Application
AVR Core Clock frequency: 16.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 512
*****************************************************/
#include <mega32.h>
#include <stdio.h>
// I2C Bus functions
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>
// Alphanumeric LCD functions
#include <alcd.h>
unsigned char count=0;
// Timer1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
count++;
if (count==60)
{
count=0;
}
}
// Declare your global variables here
unsigned char hour, minute, second;
unsigned char buf[32];
unsigned int timer1;
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: T1 pin Falling Edge
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0E;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x0F;
OCR1AL=0xFF;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// I2C Bus initialization
// I2C Port: PORTC
// I2C SDA bit: 1
// I2C SCL bit: 0
// Bit Rate: 100 kHz
// Note: I2C settings are specified in the
// Project|Configure|C Compiler|Libraries|I2C menu.
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 4096Hz
rtc_init(1,1,0);
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTA Bit 7
// RD - PORTA Bit 0
// EN - PORTA Bit 6
// D4 - PORTA Bit 5
// D5 - PORTA Bit 4
// D6 - PORTA Bit 3
// D7 - PORTA Bit 2
// Characters/line: 16
lcd_init(16);
lcd_clear();
// Global enable interrupts
#asm("sei")
rtc_set_date(1,1,1,1);
rtc_set_time(12,10,0);
#asm("cli")
TCNT1H = 0;
TCNT1L = 0;
#asm("sei")
while (1)
{
// Place your code here
rtc_get_time(&hour,&minute,&second);
timer1 = TCNT1;
sprintf(buf,"%d:%d:%d-%d:%d",hour,minute,second,count,timer1);
lcd_clear();
lcd_gotoxy(0,0);
lcd_puts(buf);
}
}
Last edited: