9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia07/ex09.c (1398B)
   1 #include <stdio.h>
   2 
   3 #define MASK_RXCIE    0x80
   4 #define MASK_TXCIE    0x40
   5 #define MASK_UDRIE    0x20
   6 #define MASK_RXEN    0x10
   7 #define MASK_TXEN    0x08
   8 #define MASK_CHR9    0x04
   9 #define    MASK_RXB8    0x02
  10 #define MASK_TXB8    0x01
  11 
  12 #define SHIFT_RXCIE    7
  13 #define SHIFT_TXCIE    6
  14 #define SHIFT_UDRIE    5
  15 #define SHIFT_RXEN    4
  16 #define SHIFT_TXEN    3
  17 #define SHIFT_CHR9    2
  18 #define    SHIFT_RXB8    1
  19 #define SHIFT_TXB8    0
  20 
  21 #define MSG_TX_COMPLETE "Transmission Completed"
  22 #define MSG_TX_UNCOMPLETE "Transmission Incomplete"
  23 
  24 typedef unsigned char uchar;
  25 typedef enum { LO, HI } bit_t;
  26 typedef enum { FALSE, TRUE } bool_t;
  27 typedef enum {
  28     OK,
  29     ERROR_TRANSMISSION_UNCOMPLETE,
  30     ERROR_NULL_POINTER
  31 } status_t;
  32 
  33 bit_t tx_complete(uchar);
  34 status_t tx_complete2(uchar, bit_t *state);
  35 
  36 void print_reg(uchar reg)
  37 {
  38     for(size_t i = ((sizeof(reg) * 8)); i > 0; i--)
  39         putchar((reg & (0x01 << (i - 1))) ? '1' : '0');
  40 
  41     putchar('\n');
  42 }
  43 
  44 int main (void)
  45 {
  46     uchar reg = 0x42;
  47     status_t st;
  48     bit_t tx;
  49 
  50     print_reg(reg);
  51 
  52     printf("%s\n", tx_complete(reg) ? MSG_TX_UNCOMPLETE : MSG_TX_COMPLETE);
  53 
  54     if((st = tx_complete2(reg, &tx))) return st;
  55     printf("%s\n", tx ? MSG_TX_COMPLETE : MSG_TX_UNCOMPLETE);
  56 
  57     return OK;
  58 }
  59 
  60 bit_t tx_complete(uchar reg)
  61 {
  62     return ((reg & MASK_TXCIE) >> SHIFT_TXCIE) ? LO : HI;
  63 }
  64 
  65 status_t tx_complete2(uchar reg, bit_t *state)
  66 {
  67     if(!state) return ERROR_NULL_POINTER;
  68 
  69     *state = ((reg & MASK_TXCIE) >> SHIFT_TXCIE);
  70 
  71     return OK;
  72 }