- VBAT: cực dương của một nguồn pin 3V nuôi chip.. - Vcc: nguồn cho giao diện I2C, thường là 5V và dùng chung với vi điều khiển.. Chú ý là nếu Vcc không được cấp nguồn nhưng VBAT được c
Trang 1I atmega32:
II Chip DS1307.
Hình 1 Cấu tạo chip DS1307.
Các chân của DS1307 được mô tả như sau:
- X1 và X2: là 2 ngõ kết nối với 1 thạch anh 32.768KHz làm nguồn tạo dao
động cho chip.
- VBAT: cực dương của một nguồn pin 3V nuôi chip.
- GND: chân mass chung cho cả pin 3V và Vcc.
- Vcc: nguồn cho giao diện I2C, thường là 5V và dùng chung với vi điều
khiển Chú ý là nếu Vcc không được cấp nguồn nhưng VBAT được cấp thì DS1307 vẫn đang hoạt động (nhưng không ghi và đọc được).
- SQW/OUT: một ngõ phụ tạo xung vuông (Square Wave / Output Driver),
tần số của xung được tạo có thể được lập trình Như vậy chân này hầu như không liên quan đến chức năng của DS1307 là đồng hồ thời gian thực, chúng ta
sẽ bỏ trống chân này khi nối mạch.
- SCL và SDA là 2 đường giao xung nhịp và dữ liệu của giao diện I2C mà
chúng ta đã tìm hiểu trong bài TWI của AVR.
Trang 2/***************************************************** This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l
http://www.hpinfotech.com
Project :
Version :
Date : 10/9/2013
Author : NeVaDa
Company :
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
Trang 3External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
// I2C Bus functions
#asm
.equ i2c_port=0x15 ;//PORTC
.equ sda_bit=1
.equ scl_bit=0
#endasm
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h> //thu vien ds 1307
#define led1 PORTC.6
#define led2 PORTC.3
#define led3 PORTC.4
#define led4 PORTC.5
#define led5 PORTC.2
#define led6 PORTC.7
#define Set_key PIND.4
#define Dw_key PIND.5
Trang 4#define Up_key PIND.7
#define ok PIND.6
//unsigned char i;
unsigned char font[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; unsigned char h,m,s;
unsigned char t_view,index,F_set;
void Fix_time(void) // Kiem tra va hieu chinh gia tri cua gio,phut,giay
{
//Tang
if(s==60)
{
s=0;m++;
}
if(m==60)
{
m=0;h++;
}
if(h==24) h=0;
//Giam
if(s== -1)
{
s=59;m ;
Trang 5}
if(m== -1)
{
m=59;h ;
}
if(h== -1)h= 23;
}
///////////cài dat thoi gian dong ho
void Keypad(void) // Kiem tra phim nhan
{
if(!Set_key)
{
while(!Set_key); //phim Set duoc nhan ?
F_set++; // Bien F_set co gia tri tu 0->2
if((F_set==4)||(ok==0))
{
F_set=0;
rtc_set_time(h,m,s);
}
}
if(F_set==1)
{ //Tang hoac giam phut neu F_set = 1
if(!Up_key) h++;
if(!Dw_key) h ;
}
if(F_set==2) //Tang hoac giam gio neu F_set = 2
Trang 6{
if(!Up_key) m++;
if(!Dw_key) m ;
}
if(F_set==3)
{ //Tang hoac giam gio neu F_set = 2
if(!Up_key) s++;
if(!Dw_key) s ;
}
Fix_time(); //kiem tra tran so
delay_ms(200);
}
// dung timer1 de quet led
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
TCNT0=230;
index++;
if(index==1 )
{
t_view=h;
led1=0;led2=0;led3=0;led4=0;led5=0;led6=0; led1=1;led2=0;led3=0;led4=0;led5=0;led6=0; PORTA=font[t_view/10];
}
Trang 7
if(index==2 )
{// t_view=h;
led1=0;led2=0;led3=0;led4=0;led5=0;led6=0; led1=0;led2=1;led3=0;led4=0;led5=0;led6=0; PORTA=font[t_view%10];
}
if(index==3)
{
t_view=m;
led1=0;led2=0;led3=0;led4=0;led5=0;led6=0; led1=0;led2=0;led3=1;led4=0;led5=0;led6=0; PORTA=font[t_view/10];
}
if(index==4 )
{
led1=0;led2=0;led3=0;led4=0;led5=0;led6=0; led1=0;led2=0;led3=0;led4=1;led5=0;led6=0; PORTA=font[t_view%10];
}
if(index==5)
{
t_view=s;
led1=0;led2=0;led3=0;led4=0;led5=0;led6=0; led1=0;led2=0;led3=0;led4=0;led5=1;led6=0; PORTA=font[t_view/10];
}
Trang 8
if(index==6 ) //t_view=h;
{
led1=0;led2=0;led3=0;led4=0;led5=0;led6=0;
led1=0;led2=0;led3=0;led4=0;led5=0;led6=1;
PORTA=font[t_view%10];
index=0;
}
// Place your code here
}
void main(void)
{
h=0;
m=0;
s=0;
index=0;
// 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=0xFF;
// Port B initialization
Trang 9// 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=0x0F;
// 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=0xFF;
// 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=0xF0;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x04;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
Trang 10// Mode: Normal top=0xFFFF // 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: Off // Compare B Match Interrupt: Off TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization // Clock source: System Clock // Clock value: 125.000 kHz // Mode: Normal top=0xFF
// OC2 output: Disconnected ASSR=0x00;
TCCR2=0x00;
Trang 11TCNT2=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=0x01;
// 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;
Trang 12// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Global enable interrupts
#asm("sei")
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock initialization // Square wave output on pin SQW/OUT: On // Square wave frequency: 1Hz
rtc_init(0,1,0);
while (1)
{
Keypad();
if(F_set==0)
{
Trang 13
rtc_get_time(&h,&m,&s); if((h==14)&&(m>2)&&(m<12)) {
PORTB.0=1;
}
else
{
PORTB.0=0;
}
}
// quetled(h,m,s);
// Place your code here
}
}