9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit 75fef584b8d767e6f175875be75bed8f248b4211
parent 9e864f7eb82b6ff4d5afeb1281bbcf148524c526
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Tue, 22 Jun 2021 20:44:19 -0300

Updated: completed guia07 exercises

Diffstat:
Aguia07/ex09.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aguia07/ex10.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aguia07/ex11.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aguia07/ex12.c | 35+++++++++++++++++++++++++++++++++++
4 files changed, 246 insertions(+), 0 deletions(-)
diff --git a/guia07/ex09.c b/guia07/ex09.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+
+#define MASK_RXCIE    0x80
+#define MASK_TXCIE    0x40
+#define MASK_UDRIE    0x20
+#define MASK_RXEN    0x10
+#define MASK_TXEN    0x08
+#define MASK_CHR9    0x04
+#define    MASK_RXB8    0x02
+#define MASK_TXB8    0x01
+
+#define SHIFT_RXCIE    7
+#define SHIFT_TXCIE    6
+#define SHIFT_UDRIE    5
+#define SHIFT_RXEN    4
+#define SHIFT_TXEN    3
+#define SHIFT_CHR9    2
+#define    SHIFT_RXB8    1
+#define SHIFT_TXB8    0
+
+#define MSG_TX_COMPLETE "Transmission Completed"
+#define MSG_TX_UNCOMPLETE "Transmission Incomplete"
+
+typedef unsigned char uchar;
+typedef enum { LO, HI } bit_t;
+typedef enum { FALSE, TRUE } bool_t;
+typedef enum {
+    OK,
+    ERROR_TRANSMISSION_UNCOMPLETE,
+    ERROR_NULL_POINTER
+} status_t;
+
+bit_t tx_complete(uchar);
+status_t tx_complete2(uchar, bit_t *state);
+
+void print_reg(uchar reg)
+{
+    for(size_t i = ((sizeof(reg) * 8)); i > 0; i--)
+        putchar((reg & (0x01 << (i - 1))) ? '1' : '0');
+
+    putchar('\n');
+}
+
+int main (void)
+{
+    uchar reg = 0x42;
+    status_t st;
+    bit_t tx;
+
+    print_reg(reg);
+
+    printf("%s\n", tx_complete(reg) ? MSG_TX_UNCOMPLETE : MSG_TX_COMPLETE);
+
+    if((st = tx_complete2(reg, &tx))) return st;
+    printf("%s\n", tx ? MSG_TX_COMPLETE : MSG_TX_UNCOMPLETE);
+
+    return OK;
+}
+
+bit_t tx_complete(uchar reg)
+{
+    return ((reg & MASK_TXCIE) >> SHIFT_TXCIE) ? LO : HI;
+}
+
+status_t tx_complete2(uchar reg, bit_t *state)
+{
+    if(!state) return ERROR_NULL_POINTER;
+
+    *state = ((reg & MASK_TXCIE) >> SHIFT_TXCIE);
+
+    return OK;
+}
diff --git a/guia07/ex10.c b/guia07/ex10.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+
+#define MASK_AFT    0x80
+#define MASK_BAND    0x40
+#define MASK_FDIV    0x3E    /* 0011 1110 */
+#define MASK_SYN    0x01
+
+#define SHIFT_AFT    7
+#define SHIFT_BAND    6
+#define SHIFT_FDIV    1
+
+typedef unsigned char uchar;
+typedef enum { BAND_FM, BAND_AM } band_t;
+typedef enum { OK, ERROR_NULL_POINTER } status_t;
+
+uchar get_synthesizer_divider(uchar control);
+
+status_t set_synthesizer_divider(uchar fdiv, uchar *control);
+
+band_t get_band(uchar control);
+
+uchar set_band(uchar control, band_t new_band);
+
+int main (void)
+{
+    uchar reg = 0x3C;    /* 0011 1100 */
+
+    printf("%d\n", get_synthesizer_divider(reg));
+
+    set_synthesizer_divider(4, &reg);
+
+    printf("%d\n", get_synthesizer_divider(reg));
+
+    printf("%s\n", get_band(reg) ? "BAND: AM" : "BAND: FM");
+
+    reg = set_band(reg, BAND_AM);
+
+    printf("%s\n", get_band(reg) ? "BAND: AM" : "BAND: FM");
+
+    return 0;
+}
+
+uchar get_synthesizer_divider(uchar control)
+{
+    return (((control & MASK_FDIV) >> SHIFT_FDIV) + 1);
+}
+
+status_t set_synthesizer_divider(uchar fdiv, uchar *control)
+{
+    if(!control) return ERROR_NULL_POINTER;
+
+    *control = (*control & ~MASK_FDIV) + ((fdiv - 1) << SHIFT_FDIV);
+
+    return OK;
+}
+
+band_t get_band(uchar control)
+{
+    return (control & MASK_BAND) >> SHIFT_BAND;
+}
+
+uchar set_band(uchar control, band_t new_band)
+{
+    return ((control & ~MASK_BAND) + (new_band << SHIFT_BAND));
+}
diff --git a/guia07/ex11.c b/guia07/ex11.c
@@ -0,0 +1,74 @@
+#include <stdio.h>
+
+#define MASK_SPIE    0x80
+#define MASK_SPE    0x40
+#define MASK_CPOL    0x08
+#define MASK_SPR1    0x04
+#define MASK_SPR0    0x02
+
+#define SHIFT_SPIE    7
+#define SHIFT_SPE    6
+#define SHIFT_CPOL    3
+#define SHIFT_SPR1    2
+#define SHIFT_SPR0    1
+
+typedef unsigned char uchar;
+typedef enum { LO, HI } bit_t;
+
+bit_t getSPIE(uchar);
+bit_t getCPOL(uchar);
+uchar getPrescalingFactor(uchar SPCR);
+
+void getCOMControl(uchar control, uchar *prescalingFactor, uchar *divisionFactor);
+void setCPOL(uchar *, bit_t);
+
+int main (void)
+{
+    uchar SPCR = 0xCE; /* 1100 1110 */
+
+    printf("Interrupciones: %s\n", getSPIE(SPCR) ? "habilitadas" : " deshabilitadas");
+    printf("Reloj activo: %s\n", getCPOL(SPCR) ? "nivel bajo" : "nivel alto");
+    printf("Factor de division: %d\n", getPrescalingFactor(SPCR));
+    
+    printf("0x%X\n", SPCR);
+
+    /* Sets CPOL bit to LOW */
+    setCPOL(&SPCR, LO);
+
+    printf("0x%X\n", SPCR);
+
+    return 0;
+}
+
+bit_t getSPIE(uchar SPCR)
+{
+    return (SPCR & MASK_SPIE) >> SHIFT_SPIE;
+}
+
+bit_t getCPOL(uchar SPCR)
+{
+    return (SPCR & MASK_CPOL) >> SHIFT_CPOL;
+}
+
+uchar getPrescalingFactor(uchar SPCR)
+{
+    static uchar divFactor[4] = { 2, 4, 16, 32 };
+    return divFactor[(SPCR & (MASK_SPR0 + MASK_SPR1)) >> SHIFT_SPR0];
+}
+
+void getCOMControl(uchar control, uchar *prescalingFactor, uchar *divisionFactor)
+{
+    if(!prescalingFactor || !divisionFactor) return;
+
+    static uchar divFactor[4] = { 2, 4, 16, 32 };
+
+    *prescalingFactor = ((control & (MASK_SPR0 + MASK_SPR1)) >> SHIFT_SPR0);
+    *divisionFactor = divFactor[*prescalingFactor];
+}
+
+void setCPOL(uchar *control, bit_t CPOL)
+{
+    if(!control) return;
+
+    *control = ((*control & ~MASK_CPOL) + (CPOL << SHIFT_CPOL));
+}
diff --git a/guia07/ex12.c b/guia07/ex12.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+
+#define MASK_BIT 0x01
+
+typedef unsigned char uchar;
+
+uchar set_bit(uchar byte, short linea);
+uchar clear_bit(uchar byte, short linea);
+
+int main (void)
+{
+    uchar byte = 0x81; /* 1000 0001 */
+
+    printf("0x%X\n", byte);
+
+    byte = set_bit(byte, 3);
+
+    printf("0x%X\n", byte);
+
+    byte = clear_bit(byte, 3);
+
+    printf("0x%X\n", byte);
+
+    return 0;
+}
+
+uchar set_bit(uchar byte, short linea)
+{
+    return (byte | (MASK_BIT << linea));
+}
+
+uchar clear_bit(uchar byte, short linea)
+{
+    return (byte & ~(MASK_BIT << linea));
+}