9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia07/ex10.c (1337B)
   1 #include <stdio.h>
   2 
   3 #define MASK_AFT    0x80
   4 #define MASK_BAND    0x40
   5 #define MASK_FDIV    0x3E    /* 0011 1110 */
   6 #define MASK_SYN    0x01
   7 
   8 #define SHIFT_AFT    7
   9 #define SHIFT_BAND    6
  10 #define SHIFT_FDIV    1
  11 
  12 typedef unsigned char uchar;
  13 typedef enum { BAND_FM, BAND_AM } band_t;
  14 typedef enum { OK, ERROR_NULL_POINTER } status_t;
  15 
  16 uchar get_synthesizer_divider(uchar control);
  17 
  18 status_t set_synthesizer_divider(uchar fdiv, uchar *control);
  19 
  20 band_t get_band(uchar control);
  21 
  22 uchar set_band(uchar control, band_t new_band);
  23 
  24 int main (void)
  25 {
  26     uchar reg = 0x3C;    /* 0011 1100 */
  27 
  28     printf("%d\n", get_synthesizer_divider(reg));
  29 
  30     set_synthesizer_divider(4, &reg);
  31 
  32     printf("%d\n", get_synthesizer_divider(reg));
  33 
  34     printf("%s\n", get_band(reg) ? "BAND: AM" : "BAND: FM");
  35 
  36     reg = set_band(reg, BAND_AM);
  37 
  38     printf("%s\n", get_band(reg) ? "BAND: AM" : "BAND: FM");
  39 
  40     return 0;
  41 }
  42 
  43 uchar get_synthesizer_divider(uchar control)
  44 {
  45     return (((control & MASK_FDIV) >> SHIFT_FDIV) + 1);
  46 }
  47 
  48 status_t set_synthesizer_divider(uchar fdiv, uchar *control)
  49 {
  50     if(!control) return ERROR_NULL_POINTER;
  51 
  52     *control = (*control & ~MASK_FDIV) + ((fdiv - 1) << SHIFT_FDIV);
  53 
  54     return OK;
  55 }
  56 
  57 band_t get_band(uchar control)
  58 {
  59     return (control & MASK_BAND) >> SHIFT_BAND;
  60 }
  61 
  62 uchar set_band(uchar control, band_t new_band)
  63 {
  64     return ((control & ~MASK_BAND) + (new_band << SHIFT_BAND));
  65 }