DS1302,MEGA16,带74HC595的LED数码管显示屏制作万年历

</pre><pre name="code" class="cpp">#include <mega16.h>
#include <delay.h>
// DS1302 Real Time Clock functions
#asm
   .equ __ds1302_port=0x1B ;PORTA
   .equ __ds1302_io=5
   .equ __ds1302_sclk=4
   .equ __ds1302_rst=6
#endasm                                                                                                                                 ,
#include <ds1302.h>


// SPI functions
#include <spi.h>
#define RCK PORTB.4
#define nOE PORTB.3
#define set PIND.2
#define add PIND.3
// Declare your global variables here


void main(void)
{
    int i;
    unsigned char h,m,s,flag;
    unsigned char date,date1,date2,month,month1,month2,year;
    unsigned char hour1,hour2,minute1,minute2;
    unsigned char a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x48,0x40};
// 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=0x00;


// Port B initialization
// Func7=Out Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In 
// State7=0 State6=T State5=0 State4=0 State3=0 State2=P State1=P State0=T 
PORTB=0x06;
DDRB=0xB8;


// 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=0x00;


// 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=0x00;
DDRD=0x00;


// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
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: Timer2 Stopped
// 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=0x00;


// 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 Type: Master
// SPI Clock Rate: 2000.000 kHz
// SPI Clock Phase: Cycle Start
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x00;


// TWI initialization
// TWI disabled
TWCR=0x00;


// DS1302 Real Time Clock initialization
// Trickle charger: Off
rtc_init(0,0,0);
nOE = 0;
flag=0;
//rtc_set_time(17,19,30);
//rtc_set_date(4,11,14);
while (1)
      {
      // Place your code here


      end1:;
          for(i=0;i<50;i++)
            {   
                rtc_get_time(&h,&m,&s);
                hour1=h/10;   //小时十位
                hour2=h%10;   //个位
                minute1=m/10;
                minute2=m%10;
                if(set==0)
                {
                    flag=(flag+1)%5;              
                }


                //设置小时,闪烁
                if((s%2==0)&&flag==1)
                { 
                    spi(a[hour1]);
                    spi(a[hour2]);
                    //调时间
                    if(add==0)
                    {
                        rtc_get_time(&h,&m,&s);
                        h=(h+1)%24;
                        rtc_set_time(h,m,s);    
                    }
                }
                else if((s%2==1)&&flag==1)
                {
                    spi(0);
                    spi(0);
                }else
                {
                    spi(a[hour1]);
                    spi(a[hour2]);
                }
                
                //冒号
                if(s%2)        
                {
                    spi(a[10]);
                }else
                {
                    spi(0);
                }
                
                
                //设置分钟,闪烁
                if((s%2==0)&&flag==2)
                {
                    spi(a[minute1]);
                    spi(a[minute2]);
                    if(add==0)
                    {
                        rtc_get_time(&h,&m,&s);
                        m=(m+1)%60;
                        rtc_set_time(h,m,s);   
                    }
                }else if((s%2==1)&&flag==2)
                {
                    spi(0);
                    spi(0); 
                }else
                {
                    spi(a[minute1]);
                    spi(a[minute2]);
                }
                //spi(1);
                RCK = 1;       //上升沿触发
                delay_us(100);
                RCK = 0;
                      
                delay_ms(100);
                if(flag==1||flag==2)
                {
                    goto end1;
                }else if(flag==3||flag==4)
                {
                    goto end2;
                }
            }
        
        


        //显示年月日
        end2:;
            for(i=0;i<50;i++)
            {
                rtc_get_time(&h,&m,&s);
                rtc_get_date(&date,&month,&year);
                month1=month/10;
                month2=month%10;
                date1=date/10;
                date2=date%10;
                if(set==0)
                {
                    flag=(flag+1)%5;              
                }
                
                //设置月份,闪烁
                if((s%2==0)&&flag==3)
                {
                    spi(a[month1]);
                    spi(a[month2]);
                    if(add==0)
                    {
                        rtc_get_date(&date,&month,&year);
                        month=(month+1)%12;
                        if(month==0)
                        {
                            month=12;
                        }
                        rtc_set_date(date,month,year);   
                    }
                }else if((s%2==1)&&flag==3)
                {
                    spi(0);
                    spi(0); 
                }else
                {
                    spi(a[month1]);
                    spi(a[month2]);
                }
                spi(a[11]);
                
                //设置日期
                if((s%2==0)&&flag==4)
                {
                    spi(a[date1]);
                    spi(a[date2]);
                    if(add==0)
                    {
                        rtc_get_date(&date,&month,&year);
                        //date=(date+1)%31;
                        switch(month)
                        {
                            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                            date=(date+1)%31;
                            if(date==0)
                            {
                                date=31;
                            }
                            break;
                            case 4: case 6: case 9: case 11:
                            date=(date+1)%30;
                            if(date==0)
                            {
                                date=30;
                            }
                            break;
                            case 2:
                            if((year%4==0)&&(year%100!=0))
                            {
                                date=(date+1)%28;
                                if(date==0)
                                {
                                    date=28;
                                }
                            }else
                            {
                                date=(date+1)%29;
                                if(date==0)
                                {
                                    date=29;
                                }
                            }break;
                            
                        }
                        
                        rtc_set_date(date,month,year);   
                    }
                }else if((s%2==1)&&flag==4)
                {
                    spi(0);
                    spi(0); 
                }else
                {
                    spi(a[date1]);
                    spi(a[date2]);    
                }
                RCK = 1;
                delay_us(100);
                RCK = 0;
                      
                delay_ms(100);
                if(flag==3||flag==4)
                {
                    goto end2;
                }else if(flag==1||flag==2)
                {
                    goto end1;
                }
            }
            
        
      }
}


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值