秒表

目的:利用定时器和数码管显示时钟,按键0停止时钟,按键1调整秒,按键2调整分,按键3开启时钟,重新开始计时

 

nios代码

 

sopc.h
#ifndef SOPC_H_
#define  SOPC_H_

#include 
" system.h "
#include 
" alt_types.h "

#define  _KEY
#define  _SEG0
#define  _SEG1
#define  _SEG2
#define  _SEG3
#define  _SEG4
#define  _SEG5
#define  _SEG6
#define  _SEG7

typedef 
struct
{
    alt_u32 DATA;
    alt_u32 DIRECTION;
    alt_u32 INTERRUPT_MASK;
    alt_u32 EDGE_CAPTURE;
    
}PIO_STR;

#ifdef _KEY
#define  KEY ((PIO_STR *)KEY4_PIO_BASE)
#endif

#ifdef _SEG0
#define  SEG0 ((PIO_STR *)SEG0_PIO_BASE)
#endif

#ifdef _SEG1
#define  SEG1 ((PIO_STR *)SEG1_PIO_BASE)
#endif

#ifdef _SEG2
#define  SEG2 ((PIO_STR *)SEG2_PIO_BASE)
#endif

#ifdef _SEG3
#define  SEG3 ((PIO_STR *)SEG3_PIO_BASE)
#endif

#ifdef _SEG4
#define  SEG4 ((PIO_STR *)SEG4_PIO_BASE)
#endif

#ifdef _SEG5
#define  SEG5 ((PIO_STR *)SEG5_PIO_BASE)
#endif

#ifdef _SEG6
#define  SEG6 ((PIO_STR *)SEG6_PIO_BASE)
#endif

#ifdef _SEG7
#define  SEG7 ((PIO_STR *)SEG7_PIO_BASE)
#endif

#endif  /*SOPC_H_*/

 

 

 

 

main.c
/*  
* ==================================================== 
* Filename: main.c 
* Description:  
* Version: 
* Created: 
* Revision: none 
* Compiler: Nios II IDE 
* Author: lasers 
* Company: 441lab
* ===================================================== 
*/  

// #include "system.h"
#include  " ../inc/sopc.h "
#include 
" altera_avalon_pio_regs.h "
#include 
" altera_avalon_timer_regs.h "

// #include "alt_types.h"
#include  " sys/alt_irq.h "

#include 
< stdio.h >
#include 
< unistd.h >

alt_u32 hour 
=   23 ;
alt_u32 minute 
=   58 ;
alt_u32 second 
=   45 ;
alt_u32 secL,secH,minuL,minuH,hourL,hourH;

void  divide_time();
void  display();

void  key_irq( void   * context, alt_u32 id)  // 按键中断函数
{
    
switch (KEY -> DATA)
    {
        
case   0x0e :                       // 停止定时器计数
        {
            
// 停止Timer计数
            IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x0008 );
            
break ;
        }
        
case   0x0d :                       // 调整秒
        {
            
if (second  <   60 )
            {
                second
++ ;
            }
            
else
            {
                second 
=   0 ;
            }
            
break ;
        }
        
case   0x0b :                       // 调整分钟
        {
            
if (minute  <   60 )
            {
                minute
++ ;
            }
            
else
            {
                minute 
=   0 ;
            }
            
break ;
        }
        
case   0x07 :                       // 开启定时器
        {
            
// 开启Timer计数,允许中断
            IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x0007 );
            
break ;
        }
    }
    
    KEY
-> EDGE_CAPTURE  =   0x0000 ;               // 边沿捕获寄存器清零
}

int  init_key()                           // 按键中断初始化函数
{
//     void * edge_capture_ptr = (void *) &edge_capture;
    
    KEY
-> INTERRUPT_MASK  =   0x000f ;             // 开按键中断
    
    KEY
-> EDGE_CAPTURE  =   0x0000 ;               // 边沿捕获寄存器清零
    
    
return  alt_irq_register(KEY4_PIO_IRQ,NULL,key_irq);       // 注册中断服务函数,成功则返回0
}



void  ISR_timer( void   *  context, alt_u32 id)
{
    second
++ ;
    
    
if (second  ==   60 )
    {
        minute
++ ;
        second 
=   0 ;
    }
    
    
if (minute  ==   60 )
    {
        hour
++ ;
        minute 
=   0 ;
    }
    
    
if (hour  ==   24 )
    {
        hour 
=   0 ;
    }
    
    divide_time();
    
    display();
    
    
// 清除Timer中断标志寄存器
    IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE,  0x0000 );
}

int  init_timer( void )
{
    
// 清除Timer中断标志寄存器
    IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0x0000 );
    
    
// 允许Timer中断
    IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x0007 );
    
    
// 注册Timer中断
     return  alt_irq_register(TIMER_IRQ,( void   * )TIMER_BASE,ISR_timer);
}

void  divide_time()
{
    hourH 
=  hour / 10 ;
    hourL 
=  hour % 10 ;
    
    minuH 
=  minute / 10 ;
    minuL 
=  minute % 10 ;
    
    secH 
=  second / 10 ;
    secL 
=  second % 10 ;
}

void  display()
{
    alt_u8 segtab[
10 ] = { 0x40 , 0x79 , 0x24 , 0x30 , 0x19 , 0x12 , 0x02 , 0x78 , 0x00 , 0x18 };
    
    SEG0
-> DATA  =  segtab[secL];
    SEG1
-> DATA  =  segtab[secH];
    SEG2
-> DATA  =  segtab[minuL];
    SEG3
-> DATA  =  segtab[minuH];
    SEG4
-> DATA  =  segtab[hourL];
    SEG5
-> DATA  =  segtab[hourH];
}

int  main()
{
    
if ( ! init_timer())
    {
        printf(
" Timer initialize seccess!\n " );
    }
    
else
    {
        printf(
" Error:Timer initialize failed!\n " );
    }
    
    
if ( ! init_key())
    {
        printf(
" Key interrupt seccess!\n " );
    }
    
else
    {
        printf(
" Error:key interrupt failed!\n " );
    }
    
    
while ( 1 )
    {
        divide_time();
        display();
    }
    
    
return   0 ;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值