⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : main.c
//* Object              : Test Real Time Clock RTC for the AT91EB55
//*
//* 1.0 25/08/00 JPP    : Creation
//*----------------------------------------------------------------------------

#ifndef AT91_DEBUG_NONE
#include <stdio.h>
#endif

#include "parts/m55800/lib_m55800.h"
#include "targets/eb55/eb55.h"

#include "drivers/com/com.h"

#include "drivers/time_rtc/time_rtc.h"
extern TypeAICHandler at91_asm_time_rtc_irq_handler;

extern menu_rtc_it(void);

//* Global Variable
char message[512];
ComDesc COM;
TimeDescRtc  rtc;

//* local variable
char main_menu[]=
{
  "\n\r  COMMAND MENU: Base\n\r"
  "  0)  Info\n\r"
  "  1)  Get new date\n\r"
  "  2)  Read RTC\n\r"
  "  3)  Write RTC\n\r"
  "  4)  set in mode 24 h\n\r"
  "  5)  set in mode AM/PM\n\r"
  "  6)  get Mcki (ROUND)\n\r"
  "  7)  get Mcki\n\r"
  "  9)  Menu Interruptm\n\r"
  "Enter command to execute: "
};


//*----------------------------------------------------------------------------
//* Function Name       : get_val
//* Object              : get a integer or no update the value
//* Input Parameters    : <val> value to update
//* Output Parameters   : value updated
//*----------------------------------------------------------------------------
int  get_val( int val )
{
    int read;

    read = 0xFF;
    at91_scanf(&COM,"%d",&read);
    if ( read != 0xFF ) val=read;
    at91_print_crlf(&COM);
    return val;
}
//*----------------------------------------------------------------------------
//* Function Name       : print_date
//* Object              : Print date value
//* Input Parameters    : none
//* Output Parameters   : none
//*----------------------------------------------------------------------------
void  print_date(void )
{
    u_int read;
    //* bcd date
    sprintf(message,"date bcd: DD-MM-YY %02X-%02X-%02X%02X  %02X\n\r",
    rtc.time_bcd.date,rtc.time_bcd.month,rtc.time_bcd.cent,rtc.time_bcd.year,rtc.time_bcd.day );
    at91_print(&COM,message);
    //* bcd time
    if ( at91_time_rtc_24mode(&rtc) == RTC_24_HRMOD)
         at91_print(&COM,"time in 24 h \n\r");
    else
        at91_print(&COM,"time in AM / PM \n\r");
    sprintf(message,"TIME bcd: HH:MM:SS %02X-%02X-%02X  AM/PM %02X\n\r",
    rtc.time_bcd.hour,rtc.time_bcd.min,rtc.time_bcd.sec,rtc.time_bcd.ampm );
    at91_print(&COM,message);
    // check validity
    read = at91_time_rtc_validity(&rtc);
    if ( (read & RTC_NVT) == RTC_NVT )
        at91_print(&COM,"INVALID TIME ");
    else
        at91_print(&COM,"  VALID TIME ");

    if ( (read & RTC_NVTAL) == RTC_NVTAL )
        at91_print(&COM,"INVALID TIME Alarm\n\r");
    else
        at91_print(&COM,"  VALID TIME Alarm\n\r");

    if ( (read & RTC_NVC) == RTC_NVC )
        at91_print(&COM,"INVALID CALENDAR");
    else
        at91_print(&COM,"  VALID CALENDAR");

    if ( (read & RTC_NVCAL) == RTC_NVCAL )
        at91_print(&COM,"INVALID CALENDAR Alarm\n\r");
    else
        at91_print(&COM,"  VALID CALENDAR Alarm\n\r");
}
//*----------------------------------------------------------------------------
//* Function Name       : Get_Command
//* Object              : Get command val
//* Input Parameters    : None
//* Output Parameters   : int : Command num
//*----------------------------------------------------------------------------
int Get_Command(void)
{
  int command;
  at91_print_frame(&COM,main_menu,sizeof(main_menu));
  at91_scanf(&COM,"%d", &command);
  return command;
}

//*----------------------------------------------------------------------------
//* Function Name       : menu
//* Object              : check general menu
//* Input Parameters    : None
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void menu(void)
{
int      command = 0;
u_int    read;
for(;;)
  {

    command = Get_Command();
    switch(command)
    {
     case 0:          /* Info */
         print_date();
         break;
     case 1:          /*Get new date  */
         at91_print(&COM,"\n\rEnter Century: ");
         rtc.time_int.cent = get_val(rtc.time_int.cent);

         at91_print(&COM,"Enter year: ");
         rtc.time_int.year = get_val(rtc.time_int.year);

         at91_print(&COM,"Enter mouth: ");
         rtc.time_int.month = get_val(rtc.time_int.month);

         at91_print(&COM,"Enter date: ");
         rtc.time_int.date = get_val(rtc.time_int.date);

         at91_print(&COM,"Enter day in week: ");
         rtc.time_int.day = get_val(rtc.time_int.day);

         at91_print(&COM,"Enter hour: ");
         rtc.time_int.hour = get_val(rtc.time_int.hour);

         at91_print(&COM,"Enter minute: ");
         rtc.time_int.min = get_val(rtc.time_int.min);

         at91_print(&COM,"Enter seconde: ");
         rtc.time_int.sec = get_val(rtc.time_int.sec);

         at91_print(&COM,"Enter 0=>AM / 1=>PM: ");
         rtc.time_int.ampm = get_val(rtc.time_int.ampm);
        // Set date & time in bcd
         at91_time_rtc_int_bdc(&rtc);

         at91_print_crlf(&COM);
         break;
     case 2:         /* Read RTC */
          at91_time_rtc_read(&rtc);
        break;

     case 3:         /* Write RTC  */
          at91_time_rtc_write_bcd(&rtc);
        break;
     case 4:         /* mode 24*/
        at91_rtc_set_24(*(&rtc.rtc_desc));
        at91_print_frame(&COM,"OK\n\r",4);
        break;
     case 5:         /* mode 24*/
        at91_rtc_clear_24(*(&rtc.rtc_desc));
        at91_print_frame(&COM,"OK\n\r",4);
        break;
     case 6:         /* mode 24*/
        read = at91_time_rtc_mcki_round(&rtc, &TC0_DESC );
        sprintf(message," MCKI = %d \n\r",read);
        at91_print(&COM,message);
        break;
     case 7:
         read = at91_time_rtc_mcki(&rtc, &TCB0_DESC );
        sprintf(message," MCKI = %d \n\r",read);
        at91_print(&COM,message);
        break;

     case 9:         /* mode 24*/
        menu_rtc_it();
        break;

    default:         /*  invalid command  */
        at91_print(&COM,"Invalid command entered. Please enter again!\n\r");
    }
  }
}
//*----------------------------------------------------------------------------
//* Function Name       : main
//* Object              : Main function of sinus with led blink
//* Input Parameters    : none
//* Output Parameters   : True
//*----------------------------------------------------------------------------
int main( void )
//* Begin
{
    u_short cd_baud,error;
    u_int mcki;

    //* open terminal for console
    COM.usart=&USART0_DESC;
    error=0;
    //* Open the RTC time
    rtc.rtc_desc=&RTC_DESC;
    rtc.AsmRtcHandler=at91_asm_time_rtc_irq_handler;
    at91_time_rtc_open(&rtc);

    // get the mcki
    mcki = at91_time_rtc_mcki(&rtc, &TCB0_DESC );
    cd_baud = at91_baud_com(mcki, 115200);
    error = at91_error_baud_com(mcki, 115200);
    at91_open_com(&COM,(COM_7_BIT|COM_PAR_NONE|COM_NBSTOP_1|COM_FLOW_CONTROL_NONE), cd_baud);

    at91_pio_open ( &PIOB_DESC, LED_MASK, PIO_OUTPUT ) ;
    at91_pio_write (&PIOB_DESC, LED_MASK, LED_OFF ) ;

    //* read RTC value
    at91_time_rtc_read(&rtc);
    at91_time_rtc_read_time_alarm(&rtc);
    at91_time_rtc_read_cal_alarm(&rtc);

    at91_print_frame(&COM,"\n\r  *** ATMEL RTC test ***\n\r",29);
    at91_print_frame(&COM,"Copyright (C) 1999 ATMEL Corporations Version: 1.0\n\r",53);
    at91_pio_write (&PIOB_DESC, LED1|LED3 , LED_ON ) ;
    menu();

    return(TRUE);
//* End
}
//*----------------------------------------------------------------------------
//* Function Name       : at91_time_rtc_c_handler
//* Object              : C handler interrup function called by the interrups
//*                       assembling routine
//* Input Parameters    : <RTC_pt> time rtc descriptor
//* Output Parameters   : none
//*----------------------------------------------------------------------------
void at91_time_rtc_c_handler(TimeDescRtc *RTC_pt)
{
    u_int imr,status;
    //get curent interrupt
    imr = RTC_pt->rtc_desc->rtc_base->RTC_IMR;
    //* driver user defined
    //get curent interrupt
    status = RTC_pt->rtc_desc->rtc_base->RTC_SR;
    //* driver user defined

    sprintf(message,"\n\rInterrupt detected IMR: %02X Status %02X\n\r",imr,status);
    at91_print(&COM,message);
    // Read time
    at91_time_rtc_read(RTC_pt);
    print_date();
    //* Enable next interrpt
    RTC_pt->rtc_desc->rtc_base->RTC_SCR = imr;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -