Čeština / English
Login

SVN Repository / Prohlížení

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

main.c

   1  /*******************************************************************************
   2     main: main for 'demo_msp_signal_generator' app
   3     Copyright (C) 2009 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Jan Krepsky <xkreps00 AT stud.fit.vutbr.cz>
   6                Josef Strnadel <strnadel AT stud.fit.vutbr.cz>
   7  
   8     LICENSE TERMS
   9  
  10     Redistribution and use in source and binary forms, with or without
  11     modification, are permitted provided that the following conditions
  12     are met:
  13     1. Redistributions of source code must retain the above copyright
  14        notice, this list of conditions and the following disclaimer.
  15     2. Redistributions in binary form must reproduce the above copyright
  16        notice, this list of conditions and the following disclaimer in
  17        the documentation and/or other materials provided with the
  18        distribution.
  19     3. All advertising materials mentioning features or use of this software
  20        or firmware must display the following acknowledgement:
  21  
  22          This product includes software developed by the University of
  23          Technology, Faculty of Information Technology, Brno and its
  24          contributors.
  25  
  26     4. Neither the name of the Company nor the names of its contributors
  27        may be used to endorse or promote products derived from this
  28        software without specific prior written permission.
  29  
  30     This software or firmware is provided ``as is'', and any express or implied
  31     warranties, including, but not limited to, the implied warranties of
  32     merchantability and fitness for a particular purpose are disclaimed.
  33     In no event shall the company or contributors be liable for any
  34     direct, indirect, incidental, special, exemplary, or consequential
  35     damages (including, but not limited to, procurement of substitute
  36     goods or services; loss of use, data, or profits; or business
  37     interruption) however caused and on any theory of liability, whether
  38     in contract, strict liability, or tort (including negligence or
  39     otherwise) arising in any way out of the use of this software, even
  40     if advised of the possibility of such damage.
  41  
  42     $Id$
  43  
  44  
  45  *******************************************************************************/
  46  
  47  #include <fitkitlib.h>
  48  #include <lcd/display.h>
  49  #include <stdlib.h>
  50  #include <string.h>
  51  
  52  #define TICKS_PER_SECOND 32768  // pocet tiku za 1s
  53  #define SIN_SAMPLES 32    // pocet vzorku harmonickeho signalu (sinus)
  54  #define SAW_SAMPLES 32    // pocet vzorku piloviteho signalu
  55  #define SQUARE_SAMPLES 2  // pocet vzorku obdelnikoveho signalu
  56  #define CONST_SAMPLES 1  // pocet vzorku konstantniho signalu
  57  /*
  58   * ----------------------------------------------------------------------------
  59   */
  60  enum SIG_ID {SIG_CONST, SIG_HARM, SIG_SAW, SIG_SQUARE} sig_type;
  61  
  62  // pole se vzorky signalu
  63  unsigned char arr_sin[SIN_SAMPLES] = {
  64     128, 153, 177, 199, 219, 234, 246, 254, 255, 254, 246, 234, 219, 199, 177,
  65     153, 128, 103, 79, 57, 37, 22, 10, 2, 0, 2, 10, 22, 37, 57, 79, 103
  66  };
  67  
  68  unsigned char arr_saw[SAW_SAMPLES] = {
  69    0, 8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140,
  70    148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247, 255
  71  };
  72  
  73  unsigned char arr_square[SQUARE_SAMPLES] = {
  74    0, 255
  75  };
  76  
  77  unsigned char arr_const[CONST_SAMPLES] = {
  78    255
  79  };
  80  /*
  81   * ----------------------------------------------------------------------------
  82   */
  83  unsigned char *signal_arr = arr_sin;  // ukazatel na pole vzorku zvoleneho signalu
  84  int signal_arr_index = 0;   // index aktualniho vzorku, ktery se bude prevadet
  85  int frequency = 100;        // frekvence signalu
  86  unsigned char samples = SIN_SAMPLES;  // pocet vzorku zvoleneho signalu
  87  int ticks;                  // pocet tiku, po kterych ma dojit k preruseni od casovace
  88  int freq_max;               // maximalni frekvence zvoleneho signalu, kterou lze generovat
  89  unsigned char y_scale = 100;          // zmena meritka osy Y na dany pocet % (0...100)
  90  
  91  unsigned char str_tmp[MAX_COMMAND_LEN+1];
  92  /*
  93   * ----------------------------------------------------------------------------
  94   */
  95  void get_data(unsigned char *str, int from);
  96  void set_ticks(void);
  97  void set_freq_max(void);
  98  
  99  /*******************************************************************************
 100   * Vypis uzivatelske napovedy (funkce se vola pri vykonavani prikazu "help")
 101  *******************************************************************************/
 102  void print_user_help(void)
 103  {
 104    term_send_str_crlf("Napoveda pro generator signalu:");
 105    term_send_str_crlf(" SIG harm ..... harmonicky signal");
 106    term_send_str_crlf(" SIG saw ...... pilovity signal");
 107    term_send_str_crlf(" SIG square ... obdelnikovy signal");
 108    term_send_str_crlf(" SIG const ... konstantni signal");
 109    term_send_str_crlf("");
 110    term_send_str_crlf(" SCALE x ... zmena amplitudy napeti na x = 0 ... 100 [%]");
 111    term_send_str_crlf(" FREQ f ....... zmena frekvence [Hz]");
 112    term_send_str_crlf("");
 113    term_send_str_crlf("Napeti je meritelne mezi vyvodem 31 (konektoru JP9) a zemi FITkitu");
 114  }
 115  
 116  
 117  /*******************************************************************************
 118   * Dekodovani a vykonani uzivatelskych prikazu
 119  *******************************************************************************/
 120  unsigned char decode_user_cmd(char *UString, char *String)
 121  {
 122  	if (strcmp4(UString, "SIG "))  // zmena typu signalu
 123  	{
 124        get_data(String, 4);  // do str_tmp ulozi UString od 4. znaku dal
 125  		unsigned char *msg;
 126  
 127  	  if (strcmp3(str_tmp, "harm"))  // harmonicky
 128  	  {
 129  	    sig_type = SIG_HARM;
 130  	    signal_arr = arr_sin;
 131  	    signal_arr_index = 0;
 132  	    samples = SIN_SAMPLES;
 133     	  msg = "Generuju harmonicky signal...";
 134  	  }
 135  	  else if (strcmp3(str_tmp, "saw"))  // pilka
 136  	  {
 137  	    sig_type = SIG_SAW;
 138  	    signal_arr = arr_saw;
 139  	    signal_arr_index = 0;
 140  	    samples = SAW_SAMPLES;
 141  	    msg = "Generuju pilovity signal...";
 142  	  }
 143  	  else if (strcmp6(str_tmp, "square"))  // obdelnik
 144  	  {
 145  	    sig_type = SIG_SQUARE;
 146  	    signal_arr = arr_square;
 147  	    signal_arr_index = 0;
 148  	    samples = SQUARE_SAMPLES;
 149  	    msg = "Generuju obdelnikovy signal...";
 150  	  }
 151  	  else if (strcmp6(str_tmp, "const"))  // konstanta
 152  	  {
 153  	    sig_type = SIG_CONST;
 154  	    signal_arr = arr_const;
 155  	    signal_arr_index = 0;
 156  	    samples = CONST_SAMPLES;
 157  	    msg = "Generuju konstantni signal...";
 158  	  }
 159  	  else
 160  	  {
 161  	    term_send_str_crlf("Neznamy signal.");
 162  	    return USER_COMMAND;
 163  	  }
 164  
 165      set_freq_max();
 166  
 167  	  // zkontrolujeme, jestli aktualni frekvence neni vyssi, nez maximalni
 168  	  // povolena pro vybrany druh signalu
 169  	  if (frequency <= freq_max)
 170  	  {
 171  	    set_ticks();
 172      }
 173      else
 174  	  {
 175  	    frequency = freq_max;
 176  	    set_ticks();
 177  
 178  	    itoa(freq_max, str_tmp, 10);
 179  
 180  	    term_send_str("Frekvence snizena na ");
 181        term_send_str(str_tmp);
 182        term_send_str_crlf(" Hz.");
 183  	  }
 184  
 185  	  term_send_str_crlf(msg);
 186    }
 187    else if (strcmp6(UString, "SCALE "))  // zmena meritka osy Y
 188  	{
 189       get_data(String, 6);  // do str_tmp ulozi UString od 6. znaku dal
 190  	  y_scale = atoi(str_tmp);
 191  
 192      if(y_scale < 0)
 193      {
 194        term_send_str_crlf("Chybne meritko (< 0) zmeneno na hodnotu 0 %.");
 195        y_scale = 0;
 196      }
 197      else if(y_scale > 100)
 198      {
 199        term_send_str_crlf("Chybne meritko (> 100) zmeneno na hodnotu 100 %.");
 200        y_scale = 100;
 201      }
 202      else
 203      {
 204        term_send_str("Amplituda nastavena na ");
 205        term_send_str(str_tmp);
 206        term_send_str_crlf(" %.");
 207      }
 208    }
 209    else if (strcmp5(UString, "FREQ "))  // zmena frekvence
 210  	{
 211      get_data(String, 5);  // do str_tmp ulozi UString od 5. znaku dal
 212  	  int freq_tmp = atoi(str_tmp);
 213  
 214  	  if (freq_tmp > 0 && freq_tmp <= freq_max)
 215  	  {
 216  	    frequency = freq_tmp;
 217  	    set_ticks();
 218  
 219        term_send_str("Frekvence zmenena na ");
 220        term_send_str(str_tmp);
 221        term_send_str_crlf(" Hz.");
 222      }
 223      else if (freq_tmp > freq_max)
 224      {
 225        itoa(freq_max, str_tmp, 10);
 226  
 227        term_send_str("Chybna frekvence. Maximalni hodnota pro tento signal je ");
 228        term_send_str(str_tmp);
 229        term_send_str_crlf(" Hz.");
 230      }
 231      else
 232      {
 233        term_send_str_crlf("Chybna frekvence.");
 234      }
 235  
 236    }
 237  	else { return (CMD_UNKNOWN); }
 238  
 239    return USER_COMMAND;
 240  }
 241  
 242  /*******************************************************************************
 243   * Inicializace periferii/komponent po naprogramovani FPGA
 244  *******************************************************************************/
 245  void fpga_initialized()
 246  {
 247     LCD_init();
 248     LCD_append_string("Signal-generator");
 249     LCD_send_cmd(LCD_DISPLAY_ON_OFF | LCD_DISPLAY_ON | LCD_CURSOR_OFF, 0);  // vypni kurzor
 250  
 251    term_send_str_crlf("Napetovy prubeh, ktery chcete generovat, nastavte pomoci prikazu (viz help).");
 252    term_send_str_crlf("Napeti je meritelne mezi vyvodem 31 (konektoru JP9) a zemi FITkitu");
 253  }
 254  
 255  
 256  /*******************************************************************************
 257   * Hlavni funkce
 258  *******************************************************************************/
 259  int main(void)
 260  {
 261    initialize_hardware();
 262    WDTCTL = WDTPW + WDTHOLD;	 // zastav watchdog
 263  
 264    // nastaveni maximalni frekvence signalu a poctu tiku do preruseni
 265    set_freq_max();
 266    set_ticks();
 267  
 268    // nastaveni casovace
 269  	CCTL0 = CCIE;  // povol preruseni pro casovac (rezim vystupni komparace)
 270    CCR0 = ticks; // nastav po kolika ticich ma dojit k preruseni
 271    TACTL = TASSEL_1 + MC_2;  // ACLK (f_tiku = 32768 Hz = 0x8000 Hz), nepretrzity rezim
 272  
 273    // nastaveni DAC
 274    ADC12CTL0 |= 0x0020;    // nastaveni referencniho napeti z ADC na 1,5 V
 275    DAC12_0CTL |= 0x1060;   // nastaveni kontrolniho registru DAC (8-bitovy rezim, medium speed)
 276    DAC12_0CTL |= 0x100;    // vystupni napeti nasobit 1x
 277    DAC12_0DAT = 0;         // vynulovani vystupni hodnoty DAC
 278  
 279    // hlavni smycka
 280    while (1)
 281  	{
 282      terminal_idle();      // obsluha terminalu
 283    }
 284  }
 285  
 286  /*******************************************************************************
 287   * Obsluha preruseni casovace timer A0
 288  *******************************************************************************/
 289  interrupt (TIMERA0_VECTOR) Timer_A (void)
 290  {
 291    if (++signal_arr_index >= samples){ signal_arr_index = 0; }
 292    unsigned int smpl = (signal_arr[signal_arr_index]*y_scale)/100;
 293    DAC12_0DAT = smpl;  // nahrani dalsiho vzorku pro prevod
 294  
 295    CCR0 += ticks;  // nastav po kolika ticich ma dojit k dalsimu preruseni
 296  }
 297  
 298  /*******************************************************************************
 299   * Funkce ulozi do str_tmp retezec str od znaku from do konce
 300  *******************************************************************************/
 301  void get_data(unsigned char *str, int from)
 302  {
 303  	int i, j = 0;
 304  	for (i = from; i < strlen(str); i++){	str_tmp[j++] = str[i]; }
 305  	str_tmp[j] = 0;
 306  }
 307  
 308  /*******************************************************************************
 309   * Funkce nastavi pocet tiku (do promenne ticks), za kolik dojde k preruseni
 310   * od casovace na zaklade zadane frekvence a funkce (jejich vzorku)
 311  *******************************************************************************/
 312  void set_ticks(void)
 313  {
 314    ticks = TICKS_PER_SECOND / frequency / samples;
 315  }
 316  
 317  /*******************************************************************************
 318   * Funkce nastavi do promenne freq_max maximalni hodnotu frekvence pro zvoleny signal
 319  *******************************************************************************/
 320  void set_freq_max(void)
 321  {
 322    // maximalni frekvence je odvozena od poctu vzorku zvoleneho signalu
 323    freq_max = TICKS_PER_SECOND / samples / 2;
 324  }
 325  
 326  
Zobrazeno: 595814x Naposledy: 22.1.2022 08:56:52