em đang làm đồng hồ dùng 4led.
em có dùng timer để quét led, nhưng vấn đề là khi đọc DS1307 thì lại sai. (phần cừng hoàn toàn đúng, em có test ko dùng timer thì đã ok)
Mong các bác góp ý.
em có dùng timer để quét led, nhưng vấn đề là khi đọc DS1307 thì lại sai. (phần cừng hoàn toàn đúng, em có test ko dùng timer thì đã ok)
Mong các bác góp ý.
Code:
#include <mega8.h> #include <delay.h> // I2C Bus functions #asm .equ __i2c_port=0x15 ;PORTC .equ __sda_bit=4 .equ __scl_bit=5 #endasm #include <i2c.h> // DS1307 Real Time Clock functions #include <ds1307.h> #define S1 PORTB.0 #define S2 PORTB.1 #define S3 PORTB.2 unsigned char Font[11]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; unsigned char a[8]; unsigned char i=0,n=0; unsigned char hour,min,sec; //unsigned char day,month,year; void Display_time() { rtc_get_time(&hour,&min,&sec); a[0] = hour/10; a[1] = hour%10; a[2] = min/10; a[3] = min%10; } //void Display_date() //{ // rtc_get_time(&day,&month,&year); // a[4] = day/10; // a[5] = day%10; // a[6] = month/10; // a[7] = month%10; //} // Timer 1 output compare A interrupt service routine interrupt [TIM1_COMPA] void timer1_compa_isr(void) { // Place your code here PORTC = 0xFF - (1<<i); PORTD = Font[a[i]]; i++; if (i>3) i=0; } // Declare your global variables here void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In // State7=0 State6=0 State5=0 State4=0 State3=0 State2=P State1=P State0=P PORTB=0x07; DDRB=0xF8; // Port C initialization // Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTC=0x00; DDRC=0x7F; // Port D initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTD=0x00; DDRD=0xFF; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped TCCR0=0x00; TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 1000.000 kHz // Mode: CTC top=OCR1A // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: On // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x0A; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x07; OCR1AL=0xD0; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off MCUCR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x10; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // I2C Bus initialization i2c_init(); // DS1307 Real Time Clock initialization // Square wave output on pin SQW/OUT: Off // SQW/OUT pin state: 0 rtc_init(0,0,0); // Global enable interrupts #asm("sei") rtc_set_time(13,18,00); //rtc_set_date(20,10,9); while (1) { // Place your code here Display_time(); delay_ms(50); //Display_date(); //delay_ms(50); }; }
Comment