I atmega32: II Chip DS1307 Hình Cấu tạo chip DS1307 Các chân DS1307 mô tả sau: - X1 X2: ngõ kết nối với thạch anh 32.768KHz làm nguồn tạo dao động cho chip - VBAT: cực dương nguồn pin 3V nuôi chip - GND: chân mass chung cho pin 3V Vcc - Vcc: nguồn cho giao diện I2C, thường 5V dùng chung với vi điều khiển Chú ý Vcc không cấp nguồn VBAT cấp DS1307 hoạt động (nhưng không ghi đọc được) - SQW/OUT: ngõ phụ tạo xung vuông (Square Wave / Output Driver), tần số xung tạo lập trình Như chân không liên quan đến chức DS1307 đồng hồ thời gian thực, bỏ trống chân nối mạch - SCL SDA đường giao xung nhịp liệu giao diện I2C mà tìm hiểu TWI AVR /***************************************************** 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 Program type : ATmega16 : Application AVR Core Clock frequency: 8.000000 MHz Memory model : Small External RAM size Data Stack size :0 : 256 *****************************************************/ #include #include // I2C Bus functions #asm equ i2c_port=0x15 ;//PORTC equ sda_bit=1 equ scl_bit=0 #endasm #include // DS1307 Real Time Clock functions #include //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 #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 ; } 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 = if(!Up_key) h++; if(!Dw_key) h ; } if(F_set==2) //Tang hoac giam gio neu F_set = { if(!Up_key) m++; if(!Dw_key) m ; } if(F_set==3) { //Tang hoac giam gio neu F_set = 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]; } 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]; } 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 // 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 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 initialization // Clock source: System Clock // Clock value: Timer1 Stopped // Mode: Normal top=0xFFFF // OC1A output: Discon // OC1B output: Discon // Noise Canceler: Of // Input Capture on Falling Edge // Timer1 Overflow Interrupt: Of // Input Capture Interrupt: Of // Compare A Match Interrupt: Of // Compare B Match Interrupt: Of TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter initialization // Clock source: System Clock // Clock value: 125.000 kHz // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Of // INT1: Of // INT2: Of MCUCR=0x00; MCUCSR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x01; // USART initialization // USART disabled UCSRB=0x00; // Analog Comparator initialization // Analog Comparator: Of // Analog Comparator Input Capture by Timer/Counter 1: Of ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC disabled ADCSRA=0x00; // 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) { rtc_get_time(&h,&m,&s); if((h==14)&&(m>2)&&(m