Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <io.h>
- #include <interrupt.h>
- #include <delay.h>
- #include <stdio.h>
- #include <mega32.h>
- // Alphanumeric LCD functions
- #include <alcd.h>
- // Declare your global variables here
- // TWI functions
- #include <twi.h>
- #ifndef cbi
- #define cbi(port,bit) (port)&=~(1<<(bit))
- #endif
- #ifndef sbi
- #define sbi(port,bit) (port)|=(1<<(bit))
- #endif
- #define TWI_PORT PORTTC
- #define TWI_DDR DDRC
- #define TWI_PIN PINC
- #define SDA_PIN 6
- #define SCL_PIN 5
- #define DS1307_SLA 0x68
- #define _222K10
- //#define 100K32
- #define TWI_W 0
- #define TWI_R 1
- #define TWI_START (1<<TWINT)|(1<<TWSTA)|(1<<TWEN) //0xA4 : START CONDITION
- #define TWI_STOP (1<<TWINT)|(1<<TWSTO)|(1<<TWEN) //0x94 : STOP CONDITION
- #define TWI_Clear_TWINT (1<<TWINT)|(1<<TWEN) //0x84 :XOA TWIN, NOT ACK
- #define TWI_Read_ACK (1<<TWINT)|(1<<TWEN)|(1<<TWEA) //0xC4 : XOA TWIN
- void TWI_Init(void)
- {
- TWSR=0x00; //PRESCALER=1
- // TWBR=_100K;
- TWCR=(1<<TWINT)|(1<<TWEN);
- }
- unsigned char TWI_DS1307_wadr(unsigned char Addr)
- {
- TWCR=TWI_START; //goi START CONDITION
- while((TWCR&0x80)==0x00); //cho TWINT bit =1
- if((TWSR&0xF8)!=0x08) return TWSR; // NEU START CO LOI THI THOAT
- TWDR=(DS1307_SLA<<1)+TWI_W; // dia chi DS va bit W
- TWCR=TWI_Clear_TWINT; // xoa TWINT, bat dau goi SLA
- while((TWCR&0x80)==0x00); // cho TWINT bit=1
- if((TWSR&0xF8)!=0x18) return TWSR; //co loi khi goi dia chi, thoat
- TWDR=Addr; // GUI DIA CHI THANH GHI CAN GHI VAO
- TWCR=TWI_Clear_TWINT; //START SENT ADDRESS BY CLEANING TWINT
- while((TWCR&0x80)==0x00); // CHECK VA CHO TWINT BIT =1
- if((TWSR&0xF8)!=0x28) return TWSR; //NEU DU LIEU GUI KO THANH CONG, THOAT
- TWCR=TWI_STOP; // STOP CONDITION
- return 0;
- }
- unsigned char TWI_DS1307_wblock(unsigned char Addr,unsigned char Data[],unsigned char len) // ghi 1 mang vao DS
- {
- unsigned char i;
- TWCR=TWI_START; // goi Start Condition
- while((TWCR&0x80)==0x00); //cho TWINT bit =1
- if((TWSR&0xF8)!=0x08) return TWSR; // Start co loi,thoat
- TWDR=(DS1307_SLA<<1)+TWI_W; // dia chi DS va bit W
- TWCR=TWI_Clear_TWINT; // xoa TWINT de bat dau goi
- while((TWCR&0x80)==0x00); //cho TWINT bit=1
- if((TWSR&0xF8)!=0x18) return TWSR; //loi truyen SLA, thoat
- TWDR=Addr; //goi thanh dia chi can ghi vao
- TWCR=TWI_Clear_TWINT; //xoa TWINT de bat dau goi
- while((TWCR&0x80)==0x00); //cho TWINT bit =1
- if((TWSR&0xF8)!=0x28) return TWSR; //loi gui dia chi thanh ghi,thoat
- for (i=0;i<len;i=i++)
- {
- TWDR=Data[i]; //chuan bi xuat du lieu
- TWCR=TWI_Clear_TWINT; //xoa TWINT, bat dau send
- while((TWCR&0x80)==0x00); //cho TWINT bit=1
- if((TWSR&0xF8)!=0x28) return TWSR; // neu status ko bang 0x28 thi return
- }
- TWCR=TWI_STOP; // Stop condition
- return 0;
- }
- unsigned char TWI_DS1307_rblock(unsigned char Data[],unsigned char len) // doc 1 mang tu DS
- {
- unsigned char i;
- TWCR=TWI_START; // GUI DIEU KIEN BAT DAU
- while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x08)); //TWINT=1 HOAC 0x08
- TWDR=(DS1307_SLA<<1)+TWI_R; // GOI DIA CHI SLA + R
- TWCR=TWI_Clear_TWINT; //BAT DAU, XOA TWINT
- while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x40)); // TWINT =1 HOAC 0x40
- //NHAN LEN-1 BYTE DAU TIEN
- for(i=0;i<len-1;i++)
- {
- TWCR=TWI_Read_ACK; //XOA TWINT, SE GOI ACK SAU KHI NHAN 1 BYTE MOI
- while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x50)); //TWINT =1 HOAC 0x50
- Data[i]=TWDR; //doc du lieu vao mang data
- }
- // nhan byte cuoi
- TWCR=TWI_Clear_TWINT; //XOA TWINT DE NHAN BYTE CUOI, SAU DO SET NOT ACK
- while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x58)); // cho TWINT=1 hoac 0x58
- Data[len-1]=TWDR;
- TWCR=TWI_STOP;
- return 0;
- }
- volatile unsigned char Second=55,Minute=59,Hour=11,Day=1,Date=31,Month=12,Year=17,Mode=1,AP=1;
- //chon che do 12h hay 24h, mode o bit 6 cua thanh ghi hours, Mode =1 :12h, Mode=0:24h
- //AP : nam o bit 5 cua thanh ghi Hours,AP=1:PM, AP=0:AM
- volatile unsigned char tData[7],Time_count=0;
- char dis[5]; // bien dis dung de luu tru string hien thi len LCD
- //doi BCD sang thap phan va nguoc lai
- unsigned char BCD2Dec(unsigned char BCD)
- {
- unsigned char L,H;
- L=BCD&0x0F;
- H=(BCD>>4)*10;
- return(H+L);
- }
- unsigned char Dec2BCD(unsigned char Dec)
- {
- unsigned char L,H;
- L=Dec%10;
- H=(Dec/10)<<4;
- return(H+L);
- }
- // chuong trinh con hien thi thoi gian doc dc tu DS1307
- void Display(void)
- {
- Second =BCD2Dec(tData[0]&0x7F);
- Minute =BCD2Dec(tData[1]);
- if(Mode!=0) Hour=BCD2Dec(tData[2]&0x1F); //mode 12h
- else Hour=BCD2Dec(tData[2]&0x3F); //mode 24h
- Date =BCD2Dec(tData[4]);
- Month =BCD2Dec(tData[5]);
- Year =BCD2Dec(tData[6]);
- lcd_clear();
- //in gio,phut,giay
- lcd_gotoxy(0,0);
- // lcd_puts("Time:");
- sprintf(dis,"Time:%d:%d:%d",Hour,Minute,Second);
- lcd_puts(dis);
- /* lcd_gotoxy(7,0); lcd_puts(dis); lcd_gotoxy(9,0); lcd_putchar('-');
- sprintf(dis,"%i",Minute);
- lcd_gotoxy(10,0); lcd_puts(dis); lcd_gotoxy(12,0); lcd_putchar('-');
- sprintf(dis,"%i",Second);
- lcd_gotoxy(13,0); lcd_puts(dis); */
- if(Mode!=0) //Mode 12h
- {
- lcd_gotoxy(16,0);
- if(tData[2]=(AP<<5)) lcd_putchar('P');
- else lcd_putchar('A');
- }
- //in ngay,thang,nam
- lcd_gotoxy(0,1);
- //lcd_puts("Date:");
- sprintf(dis,"Date:2000+%d/%d/%d",Year,Month,Date);
- lcd_puts(dis);
- /* lcd_gotoxy(7,1);
- if(Year<10) lcd_putchar('0'); //neu nam <10, them so 0 ben trai
- lcd_puts(dis); lcd_gotoxy(9,1); lcd_putchar('-'); // in nam
- sprintf(dis,"%i",Month);
- lcd_gotoxy(10,1); lcd_puts(dis); lcd_gotoxy(12,1); lcd_putchar('-'); // in thang
- sprintf(dis,"Date:");
- lcd_gotoxy(13,1); lcd_puts(dis); //in ngay */
- }
- void main(void)
- {
- // Declare your local variables here
- // Input/Output Ports initialization
- // Port A initialization
- // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
- DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
- // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
- PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
- // Port B initialization
- // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
- DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
- // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
- PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
- // Port C initialization
- // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
- DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
- // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
- PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
- // Port D initialization
- // Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out
- DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
- // State: Bit7=0 Bit6=0 Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=0
- PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
- // Timer/Counter 0 initialization
- // Clock source: System Clock
- // Clock value: Timer 0 Stopped
- // Mode: Normal top=0xFF
- // OC0 output: Disconnected
- TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (1<<CS02) | (0<<CS01) | (1<<CS00);
- TIMSK=(1<<TOIE0);
- TCNT0=0x00;
- OCR0=0x00;
- // Timer(s)/Counter(s) Interrupt(s) initialization
- TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
- // External Interrupt(s) initialization
- // INT0: Off
- // INT1: Off
- // INT2: Off
- MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);
- MCUCSR=(0<<ISC2);
- // TWI initialization
- // Mode: TWI Master
- // Bit Rate: 100 kHz
- twi_master_init(100);
- // Alphanumeric LCD initialization
- // Connections are specified in the
- // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
- // RS - PORTA Bit 0
- // RD - PORTA Bit 1
- // EN - PORTA Bit 2
- // D4 - PORTA Bit 4
- // D5 - PORTA Bit 5
- // D6 - PORTA Bit 6
- // D7 - PORTA Bit 7
- // Characters/line: 8
- lcd_init(16);
- // Global enable interrupts
- #asm("sei")
- lcd_clear();
- lcd_gotoxy(0,0);
- lcd_puts("hello Ds1307");
- lcd_gotoxy(0,1);
- lcd_puts("anh khoa");
- delay_ms(100);
- tData[0]=Dec2BCD(Second);
- tData[1]=Dec2BCD(Minute);
- if(Mode!=0)tData[2]=Dec2BCD(Hour)|(Mode<<6)|(AP<<5); //mode12h
- else tData[2]=Dec2BCD(Hour);
- tData[3]=Dec2BCD(Day);
- tData[4]=Dec2BCD(Date);
- tData[5]=Dec2BCD(Month);
- tData[6]=Dec2BCD(Year);
- TWI_Init(); //khoi dong TWI
- TWI_DS1307_wblock(0x00,tData,7); // ghi lien tiep cac bien thoi gian vao DS1307
- delay_ms(1); // choDs1307 xu ly
- //doc va hien thi thoi gian lan dau tien
- TWI_DS1307_wadr(0x00); //set dia chi ve 0
- delay_ms(1); // cho DS1307 xy ly
- TWI_DS1307_rblock(tData,7); //doc ca khoi thoi gian 7 byte
- Display(); //hien thi ket qua len LCD
- //-----------
- while (1)
- {
- // Place your code here
- }
- }
- ISR(TIM0_OVF)
- {
- Time_count++;
- if(Time_count>=10)
- {
- //doc DS1307
- TWI_DS1307_wadr(0x00); // set dia chi ve 0
- delay_ms(1);
- TWI_DS1307_rblock(tData,7); //doc ca khoi thoi gian 7 byte
- // hien thi ket qua len LCD
- if(BCD2Dec(tData[0])!=Second)
- {
- Second=BCD2Dec(tData[0]&0x7F);
- sprintf(dis,"%i",Second);
- lcd_gotoxy(13,0); lcd_puts(" ");
- lcd_gotoxy(13,0); lcd_puts(dis);
- if(Second==0) Display(); //moi phut cap nhat 1 lan
- }
- Time_count=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement