Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / apps / demo_msp / clock / mcu /

main.c

   1  /*******************************************************************************
   2     main: interrupt service routine (ISR) of timer A0 utilized for LED control
   3     Copyright (C) 2009 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Josef Strnadel <strnadel AT stud.fit.vutbr.cz>
   6  
   7     LICENSE TERMS
   8  
   9     Redistribution and use in source and binary forms, with or without
  10     modification, are permitted provided that the following conditions
  11     are met:
  12     1. Redistributions of source code must retain the above copyright
  13        notice, this list of conditions and the following disclaimer.
  14     2. Redistributions in binary form must reproduce the above copyright
  15        notice, this list of conditions and the following disclaimer in
  16        the documentation and/or other materials provided with the
  17        distribution.
  18     3. All advertising materials mentioning features or use of this software
  19        or firmware must display the following acknowledgement:
  20  
  21          This product includes software developed by the University of
  22          Technology, Faculty of Information Technology, Brno and its
  23          contributors.
  24  
  25     4. Neither the name of the Company nor the names of its contributors
  26        may be used to endorse or promote products derived from this
  27        software without specific prior written permission.
  28  
  29     This software or firmware is provided ``as is'', and any express or implied
  30     warranties, including, but not limited to, the implied warranties of
  31     merchantability and fitness for a particular purpose are disclaimed.
  32     In no event shall the company or contributors be liable for any
  33     direct, indirect, incidental, special, exemplary, or consequential
  34     damages (including, but not limited to, procurement of substitute
  35     goods or services; loss of use, data, or profits; or business
  36     interruption) however caused and on any theory of liability, whether
  37     in contract, strict liability, or tort (including negligence or
  38     otherwise) arising in any way out of the use of this software, even
  39     if advised of the possibility of such damage.
  40  
  41     $Id$
  42  
  43  
  44  *******************************************************************************/
  45  
  46  #include <fitkitlib.h>
  47  #include <lcd/display.h>
  48  #include "user_defined.h"
  49  
  50  #include <string.h>
  51  
  52  unsigned char clk_h, clk_m, clk_s;
  53  unsigned short flag_timer;
  54  
  55  /*******************************************************************************
  56   * Vypis uzivatelske napovedy (funkce se vola pri vykonavani prikazu "help")
  57  *******************************************************************************/
  58  void print_user_help(void)
  59  {
  60    term_send_str_crlf(" SET HH:MM:SS  ... zmena casu");
  61  }
  62  
  63  
  64  /*******************************************************************************
  65   * Dekodovani a vykonani uzivatelskych prikazu
  66  *******************************************************************************/
  67  unsigned char decode_user_cmd(char *cmd_ucase, char *cmd)
  68  {
  69    unsigned char data[8];
  70  
  71    if (strcmp4(cmd_ucase, "SET "))
  72    {
  73      term_send_str("Hodiny byly nastaveny na: ");
  74      strcpy(data, get_data(cmd, 8));
  75      term_send_str_crlf(data);
  76  
  77      // konverze ascii retezce hh:mm:ss na hodnoty hh, mm, ss
  78      clk_h = (data[0] - 48) * 10 + (data[1] - 48);   // hh
  79      clk_m = (data[3] - 48) * 10 + (data[4] - 48);   // mm
  80      clk_s = (data[6] - 48) * 10 + (data[7] - 48);   // ss
  81    }
  82    else
  83    {
  84      return CMD_UNKNOWN;
  85    }
  86    return USER_COMMAND;
  87  }
  88  
  89  /*******************************************************************************
  90   * Inicializace periferii/komponent po naprogramovani FPGA
  91  *******************************************************************************/
  92  void fpga_initialized()
  93  {
  94    term_send_str_crlf("----------------------------------------------------");
  95    term_send_str_crlf("Aplikace je pripravena k behu.");
  96    term_send_str_crlf("Zadejte prosim 'help' pro napovedu.");
  97    term_send_str_crlf("----------------------------------------------------");
  98  
  99    LCD_init();                          // inicializuj LCD
 100    LCD_append_string("FITkit: clock");  // zobraz text na LCD
 101    LCD_send_cmd(LCD_DISPLAY_ON_OFF | LCD_DISPLAY_ON | LCD_CURSOR_OFF, 0); // vypni kurzor
 102  }
 103  
 104  /*******************************************************************************
 105   * Zobrazeni casu (ve formatu HH:MM:SS) na LCD
 106  *******************************************************************************/
 107  void display_clock() {
 108    LCD_send_cmd(LCD_SET_DDRAM_ADDR | LCD_SECOND_HALF_OFS, 0); // kurzor na druhou polovinu
 109    delay_ms(2);
 110  
 111    LCD_send_cmd((unsigned char)(clk_h / 10) + 48, 1); delay_ms(2);     // zobraz hh
 112    LCD_send_cmd((unsigned char)(clk_h % 10) + 48, 1); delay_ms(2);
 113    LCD_send_cmd(0x3A, 1); delay_ms(2);                                 // :
 114    LCD_send_cmd((unsigned char)(clk_m / 10) + 48, 1); delay_ms(2);     // zobraz mm
 115    LCD_send_cmd((unsigned char)(clk_m % 10) + 48, 1); delay_ms(2);
 116    LCD_send_cmd(0x3A, 1); delay_ms(2);                                 // :
 117    LCD_send_cmd((unsigned char)(clk_s / 10) + 48, 1); delay_ms(2);     // zobraz ss
 118    LCD_send_cmd((unsigned char)(clk_s % 10) + 48, 1); delay_ms(2);
 119  }
 120  
 121  
 122  /*******************************************************************************
 123   * Hlavni funkce
 124  *******************************************************************************/
 125  
 126  int main(void)
 127  {
 128    flag_timer = 0;
 129    initialize_hardware();
 130    WDTCTL = WDTPW + WDTHOLD;                // zastav watchdog
 131  
 132    P1DIR |= 0x01;                            // nastav P1.0 na vystup (budeme ovladat zelenou LED D5)
 133  
 134    CCTL0 = CCIE;                             // povol preruseni pro casovac (rezim vystupni komparace)
 135    CCR0 = 0x8000;                            // nastav po kolika ticich (32768 = 0x8000, tj. za 1 s) ma dojit k preruseni
 136    TACTL = TASSEL_1 + MC_2;                  // ACLK (f_tiku = 32768 Hz = 0x8000 Hz), nepretrzity rezim
 137  
 138    P1OUT ^= 0x01;                            // inicializuj P1.0 na 1 (D5 zhasnuta)
 139  
 140    clk_h = 0;                                // inicializuj hodiny
 141    clk_m = 0;                                // inicializuj minuty
 142    clk_s = 0;                                // inicializuj sekundy
 143  
 144    while (1)
 145    {
 146      terminal_idle();                       // obsluha terminalu
 147      if (flag_timer > 0) {
 148        display_clock();                    // zobraz aktualizovane hh:mm:ss
 149        flag_timer--;
 150      }
 151    }
 152  }
 153  
 154  /*******************************************************************************
 155   * Obsluha preruseni casovace timer A0
 156  *******************************************************************************/
 157  interrupt (TIMERA0_VECTOR) Timer_A (void)
 158  {
 159    P1OUT ^= 0x01;      // invertuj bit P1.0 (kazdou 1/2 s)
 160    CCR0 += 0x8000;     // nastav po kolika ticich (32768 = 0x8000, tj. za 1 s) ma dojit k dalsimu preruseni
 161  
 162    clk_s++;
 163  
 164    if(clk_s==60) { clk_m++; clk_s=0; }     // kazdou 60. sekundu inkrementuj minuty
 165    if(clk_m==60) { clk_h++; clk_m=0; }     // kazdou 60. minutu inkrementuj hodiny
 166    if(clk_h==24)                         // kazdych 24 hodin nuluj h,m,s
 167    {
 168      clk_h = clk_m = clk_s = 0;
 169    }
 170  
 171    flag_timer += 1;
 172  }
 173  
Zobrazeno: 356166x Naposledy: 28.2.2020 12:30:26