Chat
Danh mục
Mạch đồng hồ dùng ATMEGA16+DS1307+LCD+7SEG

Mạch đồng hồ dùng ATMEGA16+DS1307+LCD+7SEG

Số lượng:
Thêm vào giỏ
Mạch đồng hồ dùng ATMEGA16+DS1307+LCD+7SEG đã được thêm vào giỏ hàng


Click vào ảnh để xem full




/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 30/11/2010
Author  : F4CG                            
Company : F4CG                            
Comments: 


Chip type           : ATmega16
Program type        : Application
Clock frequency     : 4,000000 MHz
Memory model        : Small
External SRAM size  : 0                    
Data Stack size     : 256                     

        void qpm(void)
{

if(MODE&&UP&&DOWN&&SET&&RING&&SPEAKER)
  {
bpm = 0;
  }
else if((SET == 0)&&(bpm != 1))
{
bpm = 1; 
}
else if((MODE == 0)&&(bpm != 2))
{
bpm = 2; 
}
else if((UP == 0)&&(bpm != 3))
{
bpm = 3; 
}
else if((DOWN == 0)&&(bpm != 4))
{
bpm = 4; 
}

}
*****************************************************/
#include <mega16.h> 
#include <delay.h>
#include <spi.h>  
#asm
   .equ __lcd_port=0x12 ;PORTD
   .equ __i2c_port=0x15 ;PORTC
   .equ __sda_bit=1
   .equ __scl_bit=0
#endasm
#include <ds1307.h>

#include <lcd.h>       

/*------------------------------------------------------------------------------------------------*/
// phan ct cho ADC
#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 0
unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x20                          
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
register static unsigned int input_index=0;
// Read the 8 most significant bits
// of the AD conversion result
adc_data[input_index]=ADCH;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
   input_index=0;
ADMUX=(FIRST_ADC_INPUT|ADC_VREF_TYPE)+input_index;
// Start the AD conversion       
ADCSRA|=0x40;
}         
/*------------------------------------------------------------------------------------------------*/

        
/*------------------------------------------------------------------------------------------------*/
// khai báo bien, mang toan cuc 
#define latch         PORTB.4  
#define MODE     PINC.2
#define UP            PINC.3
#define DOWN     PINC.4
#define SET          PINC.5
#define RING       PINC.6
#define SPEAKER  PORTC.7     
bit AP;                           // che do 24h (1), che do 12h(0)
bit sb;                            // sb=1: phim duoc nhan; sb=0; phim khong dc nhan
unsigned int a,b,c,d,j,k=0;
unsigned char hour,min,sec,day,date,month,year,temp,i,n,mode,h,No_date;   
unsigned char array_led[6];
unsigned char array_msg[]=
{  
  " WELCOME !  ",
  " DAI HOC KINH TE KY THUAT-CONG NGHIEP UNETI ",
  " DIEN TU 2A1 HE LIEN THONG CD-DH ",
  " DE TAI: THIET KE DONG HO THOI GIAN THUC (RTC) HIEN THI TREN LCD 16x2, LED7SEG SU DUNG VDK ATMEGA16  ",
  " GVHD: DANG THI HUONG GIANG  ",
  " NHOM SVTH: NHOM 6 "             
  " HAN HANH DUOC CO CUNG CAC BAN THEO DOI CHUONG TRINH CUA NHOM CHUNG EM  ",
  " RAT MONG NHAN DUOC SU UNG HO NHIET TINH CUA CO & CAC BAN ",
  " XIN CHAN THANH CAM ON !. ", 
    "$"    // ki tu ket thuc chuoi msg
};  

unsigned char led7seg[13]=
{
  // MANG CÁC GIA TRI LED 7SEG TU SO 0- SO 9,ki tu "C" ,"N","NULL" 
  // Dau cham mac dinh khong sang nên bit 8 de gia tri"1"
  0b11000000, // so "0"
  0b11111001, // so "1"
  0b10100100, // so "2"
  0b10110000, // so "3"
  0b10011001, // so "4"
  0b10010010, // so "5"
  0b10000010, // so "6"
  0b11011000, // so "7"
  0b10000000, // so "8"
  0b10010000, // so "9"
  0b11000110, // chu "C"
  0b11001000, // chu "N"
  0b11111111  // "NULL"
}; 

/*------------------------------------------------------------------------------------------------*/                        
                                                                                                    

/*------------------------------------------------------------------------------------------------*/ 
// ctc hien thi thu theo ten tieng anh
void display_day(unsigned char x)
{
 switch(x)
 {     
 case 1: lcd_putsf("Sun");
         break;
 case 2: lcd_putsf("Mon");
         break;
 case 3: lcd_putsf("Tue");
         break; 
 case 4: lcd_putsf("Wed");
         break;               
 case 5: lcd_putsf("Thi");
         break;         
 case 6: lcd_putsf("Fri");
         break; 
 case 7: lcd_putsf("Sat");
         break; 
 default:
 };           
}                      

/*------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------*/
void check_conveter_day(void) 
{      
 if(day==0)                                                 // Neu ngay bang 0 
 {
  day=1;
  rtc_set_date(day,date,month,year);
 }
/*------------------------------------------------------------------------------------------------*/    

/*------------------------------------------------------------------------------------------------*/
// chuyen doi che do gio 24h <=> 12h
// AP=1: che do 24h; AP=0: che do 12h
void check_conveter_hour(void)
 // kiem tra che do gio: 24h or 12h
 if(AP==0)       // che do 12h
 {   
  if(hour==0)
  { 
   h=12;
  }
  else
  {
   if(hour>12)
   {
    h=hour-12;
   }
   else
   {
     h=hour;
   };
  };
 }
 else               // che do 24h
 {
  h=hour;
 };   


/*------------------------------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------------------------*/
// ctc hien thi nhiet do len lcd
void display_temp_lcd(unsigned int x, unsigned int y)
{
 //temp=2*adc_data[0];                           // Nhiet do, giá tri 0,49 khi chon ADC 10bit
 temp=26;
 a=temp/1000;
 b=(temp-1000*a)/100;
 c=(temp-1000*a-100*b)/10;
 d=(temp-1000*a-100*b-10*c);
 lcd_gotoxy(x,y);
 if(a!=0){
  /*lcd_putchar(a+48);lcd_putchar(b+48);*/lcd_putchar(c+48);lcd_putchar(d+48);
 }
 else {
  if(b!=0){
   /*lcd_putchar(0);lcd_putchar(b+48);*/lcd_putchar(c+48);lcd_putchar(d+48);
  }
  else{
   if(c!=0){
    /*lcd_putchar(0);lcd_putchar(0);*/lcd_putchar(c+48);lcd_putchar(d+48);
   }
   else{
    /*lcd_putchar(0);lcd_putchar(0);*/lcd_putsf(" ");lcd_putchar(d+48);
   };
  };
 };                                          
 lcd_putchar(0b11011111);        // ki tu do
 lcd_putsf("C"); 
}                
/*------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------*/
// ctc hien thi time len lcd
void display_time_lcd(unsigned char x,unsigned char y)
{
  lcd_gotoxy(x,y);
  lcd_putchar(48+h/10);
  lcd_putchar(48+h%10); 
  lcd_putsf(":"); 
  lcd_putchar(48+min/10);
  lcd_putchar(48+min%10);
  lcd_putsf(":");  
  lcd_putchar(48+sec/10);
  lcd_putchar(48+sec%10);
/*------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------*/
// ctc hien thi date len lcd
void display_date_lcd(unsigned char x,unsigned char y)
{
  lcd_gotoxy(x,y);
  lcd_putchar(48+date/10);
  lcd_putchar(48+date%10); 
  lcd_putsf("/");       
  lcd_putchar(48+month/10);
  lcd_putchar(48+month%10);
  lcd_putsf("/");        
  lcd_putchar(48+year/10);
  lcd_putchar(48+year%10); 
  display_day(day);
}
/*------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------*/
// ctc chay chuoi msg tren LCD   
void display_msg_lcd(void)
{           
 if(n==1)                                          // Thoi gian dich ki tu chuyen dong
 {     
  for(j=0;j<7;j++)                            // 7 ki tu dc nhin thay tren lcd
  {                                         
   lcd_gotoxy(9+j,0);                        // xoa 7 ki tu danh cho msg
   lcd_putchar(0);
  };
  for(j=0;j<7;j++)                            // 7 ki tu danh cho msg
  {
    if(array_msg[k+j]=='$')               // neu gap ki tu "$" thi ki ket thuc msg
    {
     j=7;                                             // thoat khoi vong for
    }
    else
    {
     lcd_gotoxy(9+j,0);                     // dua con tro toi vi tri xy(10+j,0) de hien thi
     lcd_putchar(array_msg[k+j]);   // hien thi ki tu thu k+j cua array_msg[]
    }; 
  }; 
  if(array_msg[k]=='$')                   // tiep tuc chay lai chuoi msg khi gap ki tu '$'
  { 
   k=0;
  }
  else
  {
   k++;
  };
  j=0;
  n=0;
 }
 else
 {
  n++;
 };
/*------------------------------------------------------------------------------------------------*/ 

/*------------------------------------------------------------------------------------------------*/
// ctc hien thi  len lcd
void display_lcd(void)                    
{                                         
  display_temp_lcd(12,1);                              // Hien thi nhiet do len lcd 
  display_time_lcd(0,0);                                 // Hien thi time len lcd       
  display_date_lcd(0,1);                                 // Hien thi date len lcd   
  display_msg_lcd();                                      // chay msg tren LCD     
/*------------------------------------------------------------------------------------------------*/ 

/*------------------------------------------------------------------------------------------------*/  
// ctc chot du lieu 74HC595
void latch_data(void)
{
 latch=0;
 latch=1;
}
/*------------------------------------------------------------------------------------------------*/                                 

/*------------------------------------------------------------------------------------------------*/  
// ctc chuyen doi thoi gian  sang ma 7seg
void bin2_7seg(void)      
{                        
  
  array_led[3]=led7seg[min%10];        // phan don vi cua phut         
  array_led[2]=led7seg[min/10];        // phan chuc cua phut
  
  array_led[1]=led7seg[h%10];          // phan don vi cua gio
  array_led[0]=led7seg[h/10];          // phan chuc cua gio
  
  if(a==0&&b==0&&c==0)                 // Nêu nhiet do nho hon 10 thi hien thi 1 chu so
  {
   array_led[5]=led7seg[d];            // phan don vi cua nhiet do
   array_led[4]=led7seg[12];           // phan chuc cua nhiet do 
  }
  else
  { 
   array_led[5]=led7seg[d];            // phan don vi cua nhiet do
   array_led[4]=led7seg[c];            // phan chuc cua nhiet do 
  };
}
/*------------------------------------------------------------------------------------------------*/    


/*------------------------------------------------------------------------------------------------*/  
void display_led7seg(void)         // ctc hien thi len led7seg      
{                    
  PORTB.3=0;                                  
  bin2_7seg();                     // chuyen doi tu bin sang ma 7seg                          
  spi(array_led[i]);               // xuat du lieu ra 595 dua den led7seg thu i 
  latch_data();                    // chot du lieu ra 595  
  PORTB=i;                
  PORTB.3=1;                       // enable 74hc138 chon led can hien thi                                             
  if(i==6)
  {
   i=0;
  }
  else
  { 
   i++;
  };
}
/*------------------------------------------------------------------------------------------------*/                                                          

/*------------------------------------------------------------------------------------------------*/  
// chuong trinh con kiem tra trang thai cua phim nhan : bit trang thai sb
void status_button(void)
{                                         
  if(MODE&&UP&&DOWN&&SET&&RING&&SPEAKER&&sb==1)
  {
   sb=0;  //  kiem tra phim da nha hay chua?
  }
}
/*------------------------------------------------------------------------------------------------*/  

/*------------------------------------------------------------------------------------------------*/  
void Ndate_in_month(void)
{
  //////////////////////////////////////////////////////////////////////////////////////  
  // doan chuong trinh kiem tra so ngay trong thang            
  if(month==2)                           // thang 2 nam nhuan co 29 ngay, nam thuong co 28 ngay
  {
   if(year%4==0)
   {
    No_date=28;
   }
   else
   { 
    No_date=29;     
   };
  }
  /////////////////////////////////////////////////////////////////////////////////////// 
  else
  { 
   if(month==4||month==6||month==9||month==11)
   {
    No_date=30;
   }  
  /////////////////////////////////////////////////////////////////////////////////////// 
   else
   {
    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    {
     No_date=31;
    }
   }; 
  };
  ///////////////////////////////////////////////////////////////////////////////////////           
}                                                                                                                                        
/*------------------------------------------------------------------------------------------------*/    


/*------------------------------------------------------------------------------------------------*/    
// ctc test phim ( cai dat thoi gian, bao thuc )
void set_alarm_time(void)
{                                                  
 /*****************************************************/                                           
  status_button();            // goi ctc kiem tra trang thai phim nhan
                                                                                                              
 /*****************************************************/  
 // doan chuong trinh chuyen doi che do gio: 24h/12h
 // sb=1: phim duoc nhan; sb=0; phim khong dc nhan
  if(SET==0&&sb==0)
  {
   AP=~AP;
   sb=1;         // bat trang thai co phim nhan  sb=1;
  }                                                                                                             
 /*****************************************************/                                                                                                            
 // doan chuong trinh chon che do
 if(MODE==0&&sb==0)
 {
   if(mode==5)            //  che do nguoi dung
   {
    mode=0;
   }
   else
   {
     lcd_clear();
     mode++;
   };              
   sb=1;                 // bao co phim nhan
 }                                                                                                              
 /*****************************************************/ 
 // doan chuong trinh cho che do 1(mode=1):
 if(mode==1)                               // chinh gio
 {
  lcd_gotoxy(0,0);
  lcd_putsf("Please,you set  Hour:");
  display_time_lcd(5,1);      
  //////////////////////////////////////////////////////////////////////////////
  if(UP==0&&sb==0)                // phim "UP" nhan
  {
   if(hour==23)
   {
    hour=0;
   }
   else
   { 
    hour++;
   };           
   rtc_set_time(hour,min,sec);    // dat lai gio 
   sb=1;                                       // bao co phim nhan
  }
  ////////////////////////////////////////////////////////////////////////////////
  if(DOWN==0&&sb==0)        // phim "DOWN" nhan
  {
   if(hour==0)
   {
    hour=23;
   }
   else
   { 
    hour--;
   };         
   rtc_set_time(hour,min,sec);    // dat lai time   
   sb=1;
  }   
 }     
 /*****************************************************/ 
 // doan chuong trinh cho che do 2(mode=2):
 if(mode==2)                               // chinh phut
 {
  lcd_gotoxy(0,0);
  lcd_putsf("Please,you set  Min:");
  display_time_lcd(5,1);
  ////////////////////////////////////////////////////////////////////////////////
  if(UP==0&&sb==0)                // phim "UP" nhan
  {
   if(min==59)
   {
    min=0;
   }
   else
   { 
    min++;
   };         
   rtc_set_time(hour,min,sec);    // dat lai gio   
  sb=1;                                       // bao co phim nhan
 }                                                                               
  ////////////////////////////////////////////////////////////////////////////////
 if(DOWN==0&&sb==0)        // phim "DOWN" nhan
 {
  if(min==0)
  {
   min=59;
  }
  else
  { 
   min--;
  };       
  rtc_set_time(hour,min,sec);    // dat lai gio   
  sb=1;
 }   
 }
/*****************************************************/ 
// doan chuong trinh cho che do 3(mode=3):
if(mode==3)                               // chinh ngay
{
  lcd_gotoxy(0,0);
  lcd_putsf("Please,you set  Date:");
  display_date_lcd(5,1); 
  Ndate_in_month();
  //////////////////////////////////////////////////////////////////////////////////////////
 if(UP==0&&sb==0)                // phim "UP" nhan
 {
  if(day==7)
  {
   day=1;
  }
  else
  {     
   day++;
  };   
  if(date==No_date)
  {
   date=1;
  }
  else
  { 
   date++;
  };
  rtc_set_date(day,date,month,year);  
  sb=1;                                       // bao co phim nhan
 }
 if(DOWN==0&&sb==0)        // phim "DOWN" nhan
 {                 
  if(day==1)
  {
   day=7;
  }
  else
  {     
   day--;
  };  
  if(date==1)
  {
   date=No_date;
  }
  else
  { 
   date--;
  };    
  rtc_set_date(day,date,month,year);  
  sb=1;
 }   
}
/*****************************************************/  

/*****************************************************/ 
// doan chuong trinh cho che do 4(mode=4):
if(mode==4)                               // chinh thang
{
  lcd_gotoxy(0,0);
  lcd_putsf("Please,you set  Mont:");
  display_date_lcd(5,1); 
  Ndate_in_month();
  //////////////////////////////////////////////////////////////////////////////////////////
 if(UP==0&&sb==0)                // phim "UP" nhan
 {
  if(month==12)
  {
   month=1;
  }
  else
  {     
   month++;
  };

  if((day+No_date%7)>7)
  {
   day=day+(day+No_date%7)-7;
  }
  else
  { 
   day=(day+No_date%7);
  }; 
  
  if(date>No_date)
  {
   date=No_date;
  }
  
  rtc_set_date(day,date,month,year);  
  sb=1;                                       // bao co phim nhan
 }
 if(DOWN==0&&sb==0)        // phim "DOWN" nhan
 {                 
  if(month==1)
  {
   month=12;
  }
  else
  {     
   month--;
  };  
                                
   Ndate_in_month();
   
  if(day>No_date%7)
  {
   day-=No_date%7;
  }
  else
  { 
   day=7-(No_date%7-day);
  }; 
  
  if(date>No_date)                 // vi so ngay moi thang khac nhau
  {
   date=No_date;
  } 

  rtc_set_date(day,date,month,year);  
  sb=1;
 }   
}
/*****************************************************/

}   
/*------------------------------------------------------------------------------------------------*/



/*------------------------------------------------------------------------------------------------*/
// Timer1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
 display_led7seg();                             // quet led 7seg khi co ngat
}
/*------------------------------------------------------------------------------------------------*/ 

// Declare your global variables here  


void main(void)
{    
PORTA=0xFE;   
DDRA=0xFF;

PORTB=0x00;   
DDRB=0xFF;           // PORTB lam dau ra

PORTC=0b11111111;   
DDRC=0b10000011;     // dat cac chan co gia tri 0 de nhan tin hieu tu nut nhan

PORTD=0xFF;   
DDRD=0xF7;               


// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// 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=0x0C;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x0B;
OCR1BH=0x00;
OCR1BL=0x00; 
OCR1BH=0x00;
OCR1BL=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;

// ADC initialization
// ADC Clock frequency: 1000,000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=FIRST_ADC_INPUT | ADC_VREF_TYPE;
ADCSRA=0xCA;        


// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2*1000.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x01;

// 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);

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

lcd_clear();      
//rtc_get_time(&hour,&min,&sec);                   // Doc gio, phut, giay tu ds1307
//if(sec==0)                                                         // ds bi mat nguon cap
//{                                                    
 //rtc_set_time(19,15,00);                                        // Dat lai Gio, Phut, Giay
 //rtc_set_date(7,26,02,11);                                     // Dat lai Thu, Ngay, Thang, Nam               
//}                                                                                                                                                                                       

while (1)
      {                         
       rtc_get_time(&hour,&min,&sec);                   // Doc gio, phut, giay tu ds1307
       rtc_get_date(&day,&date,&month,&year);    // Doc thu, ngay, thang, nam tu ds1307  
       check_conveter_day();                                    // kiem tra va kiem chuyen thu, che do gio ve dang chuan 
       check_conveter_hour();                                                          
       set_alarm_time();                                            // goi ctc thuc hien cai dat
       if(mode==0)                                                    // O che do hien thi binh thuong
       {
        display_lcd();                                                 // Hien thi thoi gian len lcd         
       }
       else                                                                 // chuyen sang che do set
       { 
        set_alarm_time();                                         // goi ctc thuc hien cai dat
       };
      };
           
}                       





Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút  hiện ra ở góc phải phía trên, nhấn vào để đến trang download

(pass:chipkool)

Nguồn duythien88hn@gmail.com