From ccae98208a35a18c8a33ad3f0f05a0ab36258ec4 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Mon, 8 Apr 2024 00:18:38 +0100 Subject: [PATCH] =?UTF-8?q?[AC2]=20Exerc=C3=ADcios=20adicionais=20(ex5-7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: TiagoRG --- 2ano/2semestre/ac2/extras_aula01-06/ex05.c | 76 ++++++++++++++++++++++ 2ano/2semestre/ac2/extras_aula01-06/ex07.c | 45 +++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 2ano/2semestre/ac2/extras_aula01-06/ex05.c create mode 100644 2ano/2semestre/ac2/extras_aula01-06/ex07.c diff --git a/2ano/2semestre/ac2/extras_aula01-06/ex05.c b/2ano/2semestre/ac2/extras_aula01-06/ex05.c new file mode 100644 index 0000000..f20be53 --- /dev/null +++ b/2ano/2semestre/ac2/extras_aula01-06/ex05.c @@ -0,0 +1,76 @@ +#include + +const unsigned int dis7Scodes[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, + 0xFD, 0x07, 0x7F, 0x6F, 0x77, 0xFC, + 0x39, 0x5E, 0xF9, 0xF1}; + +void delay(unsigned int ms) { + resetCoreTimer(); + while (readCoreTimer() < ms * 20000); +} + +typedef enum { HIGH, LOW } flag; + +void send2displays(unsigned char value) { + static flag f = LOW; + + if (f == HIGH) { + LATD = (LATD & 0xFF9F) | 0x0040; + LATB = (LATB & 0x80FF) | dis7Scodes[value >> 4] << 8; + f = LOW; + } else { + LATD = (LATD & 0xFF9F) | 0x0020; + LATB = (LATB & 0x80FF) | dis7Scodes[value & 0x0F] << 8; + f = HIGH; + } +} + +void clearDisplays() { + LATD = (LATD & 0xFF9F) | 0x0040; + LATB &= 0x80FF; + LATD = (LATD & 0xFF9F) | 0x0020; + LATB &= 0x80FF; +} + +unsigned char toBdc(unsigned char value) { + return ((value / 10) << 4) + (value % 10); +} + +int main() { + TRISB &= 0x80FF; + TRISD &= 0xFF9F; + + int counter = 0; + char factor = 0; + int display_freq = 0; + int i = 0; + + while (1) { + putChar('\r'); + printInt(counter, 10 | 2 << 16); + char key = inkey(); + if ('0' <= key && key <= '4') { + factor = key - '0'; + } + if (key == '\n') { + display_freq = !display_freq; + } + if (display_freq) { + printStr(" -> "); + printInt(2 * (1 + factor), 10 | 2 << 16); + printStr(" Hz"); + send2displays(toBdc(2 * (1 + factor))); + } else { + printStr(" \r"); + clearDisplays(); + } + if (i++ >= 50 / (2 * (1 + factor))) { + counter = (counter + 1) % 100; + i = 0; + } + delay(20); + + } + + return 0; +} diff --git a/2ano/2semestre/ac2/extras_aula01-06/ex07.c b/2ano/2semestre/ac2/extras_aula01-06/ex07.c new file mode 100644 index 0000000..d80492e --- /dev/null +++ b/2ano/2semestre/ac2/extras_aula01-06/ex07.c @@ -0,0 +1,45 @@ +#include + +void delay(unsigned int ms) { + resetCoreTimer(); + while (readCoreTimer() < ms * 20000); +} + +int main() { + TRISBbits.TRISB4 = 1; + AD1PCFGbits.PCFG4 = 0; + AD1CON1bits.SSRC = 7; + AD1CON1bits.CLRASAM = 1; + AD1CON3bits.SAMC = 16; + AD1CON2bits.SMPI = 15; + AD1CHSbits.CH0SA = 4; + AD1CON1bits.ON = 1; + + TRISB |= 0x000F; + + int c = 0; + int factor = 1; + while (1) { + if (c++ >= 100 / factor) { + c = 0; + putChar('\r'); + printStr("DS="); + char input = PORTB & 0x000F; + int i = 0; + for (; i < 4; i++) { + putChar((input >> (3 - i) & 0x0001) + '0'); + } + } + AD1CON1bits.ASAM = 1; + while (IFS1bits.AD1IF == 0); + int value = ADC1BUF0; + int total = 0; + int *p = (int *)(&ADC1BUF0); + for (; p <= (int *)(&ADC1BUFF); p+=4) + total += *p; + factor = (total / 16) / 205 + 1; + delay(10); + } + + return 0; +}