Chat
Danh mục
[Share] - Đồng hồ Giờ : Phút : Giây AVR - ATmega16

[Share] - Đồng hồ Giờ : Phút : Giây AVR - ATmega16

Số lượng:
Thêm vào giỏ
[Share] - Đồng hồ Giờ : Phút : Giây AVR - ATmega16 đã được thêm vào giỏ hàng




Code C:
/*****************************************************
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
External 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>
#define led6 PORTC.7
#define led5 PORTC.2
#define led1 PORTC.6
#define led4 PORTC.5
#define led3 PORTC.4
#define led2 PORTC.3
#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]={0x81,0xb7,0x8a,0x92,0xb4,0xd0,0xC0,0x97,0x80,0x90};
unsigned char h,m,s;
unsigned char t_view,index,F_set;
//unsigned char B_nhay,get_t,time ;
//char hh,mm,ss;
//unsigned char k;
//// Declare your global variables here
// void quetled(unsigned char so1, unsigned char so2, unsigned char so3)
// {
//led1=0;led2=0;led3=0;led4=0;led5=0;led6=0;
//delay_us(10);
//led1=1;led2=0;led3=0;led4=0;led5=0;led6=0;
//
//PORTA=font[so1/10];
//delay_ms(2);
//led1=0;led2=0;led3=0;led4=0;led5=0;led6=0;
//delay_us(10);
//led1=0;led2=1;led3=0;led4=0;led5=0;led6=0;
//PORTA=font[so1%10];
//delay_ms(2);
//led1=0;led2=0;led3=0;led4=0;led5=0;led6=0;
//delay_us(10);
//led1=0;led2=0;led3=1;led4=0;led5=0;led6=0;
//PORTA=font[so2/10];
//delay_ms(2);
//led1=0;led2=0;led3=0;led4=0;led5=0;led6=0;
//delay_us(10);
//led1=0;led2=0;led3=0;led4=1;led5=0;led6=0;
//PORTA=font[so2%10];
//delay_ms(2);
//led1=0;led2=0;led3=0;led4=0;led5=0;led6=0;
//delay_us(10);
//led1=0;led2=0;led3=0;led4=0;led5=1;led6=0;
//PORTA=font[so3/10];
//delay_ms(2);
//led1=0;led2=0;led3=0;led4=0;led5=0;led6=0;
//delay_us(10);
//led1=0;led2=0;led3=0;led4=0;led5=0;led6=1;
//PORTA=font[so3%10];
//delay_ms(2);
// }

//void setgio(void)
//{
// if((menu==0)&&(k==0)){
//                      while(menu==0);
//                      if(down==0){while(down==0);h--; if(hh<0){hh=24;}}
//                      }
//}

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;
}
void Keypad(void)    // Kiem tra phim nhan.
{
if(!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
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);
}


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 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
// 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;
TCNT2=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;

// 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<12)){PORTB.0=1;}
       else{PORTB.0=0;}
     
        }
   // quetled(h,m,s);
      // Place your code here

      }
}


( Nguyên lý + PCB Altium)
( Mô phỏng Protues 8.0)
( Code Codevision AVR)
Theo codientu - Lanhcdt

Điện Tử | Tin Học - Chia sẻ kiến thức - Kết nối đam mê điện tử