diff --git a/2ano/2semestre/ac2/aula06/AC2-P-Aula06.pdf b/2ano/2semestre/ac2/aula06/AC2-P-Aula06.pdf new file mode 100644 index 0000000..1c464d3 Binary files /dev/null and b/2ano/2semestre/ac2/aula06/AC2-P-Aula06.pdf differ diff --git a/2ano/2semestre/ac2/aula06/ex01.c b/2ano/2semestre/ac2/aula06/ex01.c new file mode 100644 index 0000000..6679621 --- /dev/null +++ b/2ano/2semestre/ac2/aula06/ex01.c @@ -0,0 +1,26 @@ +#include + +int main() { + TRISBbits.TRISB4 = 1; // RB4 digital output disconnected + AD1PCFGbits.PCFG4 = 0; // RB4 configured as analog input (AN4) + AD1CON1bits.SSRC = 7; // Conversion trigger selection bits: in this + // mode an internal counter ends sampling and + // starts conversion + AD1CON1bits.CLRASAM = 1; // Stop conversions when the 1st A/D converter + // interrupt is generated. At the same time, hardware + // clears the ASAM bit + AD1CON3bits.SAMC = 16; // Sample time is 16 TAD (TAD = 100 ns) + AD1CON2bits.SMPI = 0; // Interrupt is generated after 1 sample + AD1CHSbits.CH0SA = 4; // Selects AN4 as input for the A/D converter + AD1CON1bits.ON = 1; // Enable A/D converter + + while (1) { + AD1CON1bits.ASAM = 1; // Start conversion + while (IFS1bits.AD1IF == 0); // Wait while conversion not done + printInt(ADC1BUF0, 16 | 3 << 16); // Print ADC1BUF0 value + putChar('\r'); + IFS1bits.AD1IF = 0; // Reset AD1IF + } + + return 0; +} diff --git a/2ano/2semestre/ac2/aula06/ex02.c b/2ano/2semestre/ac2/aula06/ex02.c new file mode 100644 index 0000000..86460b8 --- /dev/null +++ b/2ano/2semestre/ac2/aula06/ex02.c @@ -0,0 +1,26 @@ +#include + +int main() { + TRISBbits.TRISB4 = 1; + AD1PCFGbits.PCFG4 = 0; + AD1CON1bits.SSRC = 7; + AD1CON1bits.CLRASAM = 1; + AD1CON3bits.SAMC = 16; + AD1CON2bits.SMPI = 0; + AD1CHSbits.CH0SA = 4; + AD1CON1bits.ON = 1; + TRISDbits.TRISD11 = 0; + + volatile int aux; + + while (1) { + AD1CON1bits.ASAM = 1; + LATDbits.LATD11 = 1; + while (IFS1bits.AD1IF == 0); + LATDbits.LATD11 = 0; + aux = ADC1BUF0; + IFS1bits.AD1IF = 0; + } + + return 0; +} diff --git a/2ano/2semestre/ac2/aula06/ex03.c b/2ano/2semestre/ac2/aula06/ex03.c new file mode 100644 index 0000000..a13dc04 --- /dev/null +++ b/2ano/2semestre/ac2/aula06/ex03.c @@ -0,0 +1,30 @@ +#include + +int main() { + TRISBbits.TRISB4 = 1; // RB4 digital output disconnected + AD1PCFGbits.PCFG4 = 0; // RB4 configured as analog input (AN4) + AD1CON1bits.SSRC = 7; // Conversion trigger selection bits: in this + // mode an internal counter ends sampling and + // starts conversion + AD1CON1bits.CLRASAM = 1; // Stop conversions when the 1st A/D converter + // interrupt is generated. At the same time, hardware + // clears the ASAM bit + AD1CON3bits.SAMC = 16; // Sample time is 16 TAD (TAD = 100 ns) + AD1CON2bits.SMPI = 15; // Interrupt is generated after 16 samples + AD1CHSbits.CH0SA = 4; // Selects AN4 as input for the A/D converter + AD1CON1bits.ON = 1; // Enable A/D converter + + while (1) { + AD1CON1bits.ASAM = 1; // Start conversion + while (IFS1bits.AD1IF == 0); // Wait while conversion not done + int *p = (int *)(&ADC1BUF0); + for (; p <= (int *)(&ADC1BUFF); p+=4) { + printInt(*p, 10 | 4 << 16); // Print ADC1BUF0 value + putChar(' '); + } + putChar('\r'); + IFS1bits.AD1IF = 0; // Reset AD1IF + } + + return 0; +} diff --git a/2ano/2semestre/ac2/aula06/ex05.c b/2ano/2semestre/ac2/aula06/ex05.c new file mode 100644 index 0000000..f08858a --- /dev/null +++ b/2ano/2semestre/ac2/aula06/ex05.c @@ -0,0 +1,37 @@ +#include + +#define SAMPLES 16 + +int main() { + TRISBbits.TRISB4 = 1; // RB4 digital output disconnected + AD1PCFGbits.PCFG4 = 0; // RB4 configured as analog input (AN4) + AD1CON1bits.SSRC = 7; // Conversion trigger selection bits: in this + // mode an internal counter ends sampling and + // starts conversion + AD1CON1bits.CLRASAM = 1; // Stop conversions when the 1st A/D converter + // interrupt is generated. At the same time, hardware + // clears the ASAM bit + AD1CON3bits.SAMC = 16; // Sample time is 16 TAD (TAD = 100 ns) + AD1CON2bits.SMPI = SAMPLES - 1; // Interrupt is generated after 16 samples + AD1CHSbits.CH0SA = 4; // Selects AN4 as input for the A/D converter + AD1CON1bits.ON = 1; // Enable A/D converter + + while (1) { + AD1CON1bits.ASAM = 1; // Start conversion + while (IFS1bits.AD1IF == 0); // Wait while conversion not done + int total = 0; + int *p = (int *)(&ADC1BUF0); + for (; p <= (int *)(&ADC1BUFF); p+=4) + total += *p; + int val_ad = total / SAMPLES; + int v = (val_ad * 33 + 511) / 1023; + printStr("VAL_AD value: "); + printInt(val_ad, 10 | 4 << 16); // Print ADC1BUF0 value + printStr(" | V value: "); + printInt(v, 10 | 4 << 16); // Print ADC1BUF0 value + putChar('\r'); + IFS1bits.AD1IF = 0; // Reset AD1IF + } + + return 0; +} diff --git a/2ano/2semestre/ac2/aula06/ex06.c b/2ano/2semestre/ac2/aula06/ex06.c new file mode 100644 index 0000000..6ec716d --- /dev/null +++ b/2ano/2semestre/ac2/aula06/ex06.c @@ -0,0 +1,77 @@ +#include + +#define SAMPLES 4 + +const unsigned int dis7Scodes[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, + 0xFD, 0x07, 0x7F, 0x6F, 0x77, 0xFC, + 0x39, 0x5E, 0xF9, 0xF1}; + +typedef enum { HIGH, LOW } flag; + +unsigned char toBdc(unsigned char value) { + return ((value / 10) << 4) + (value % 10); +} + +void send2displays(unsigned char value) { + static flag f = LOW; + value = toBdc(value); + unsigned char high = dis7Scodes[value >> 4]; + unsigned char low = dis7Scodes[value & 0x0F]; + + if (f == HIGH) { + LATD = (LATD & 0xFF9F) | 0x0040; + LATB = (LATB & 0x80FF) | high << 8; + f = LOW; + } else { + LATD = (LATD & 0xFF9F) | 0x0020; + LATB = (LATB & 0x80FF) | low << 8; + f = HIGH; + } +} + +void delay(unsigned int ms) { + resetCoreTimer(); + while (readCoreTimer() < ms * 20000); +} + +int main() { + TRISBbits.TRISB4 = 1; // RB4 digital output disconnected + AD1PCFGbits.PCFG4 = 0; // RB4 configured as analog input (AN4) + AD1CON1bits.SSRC = 7; // Conversion trigger selection bits: in this + // mode an internal counter ends sampling and + // starts conversion + AD1CON1bits.CLRASAM = 1; // Stop conversions when the 1st A/D converter + // interrupt is generated. At the same time, hardware + // clears the ASAM bit + AD1CON3bits.SAMC = 16; // Sample time is 16 TAD (TAD = 100 ns) + AD1CON2bits.SMPI = SAMPLES - 1; // Interrupt is generated after 16 samples + AD1CHSbits.CH0SA = 4; // Selects AN4 as input for the A/D converter + AD1CON1bits.ON = 1; // Enable A/D converter + + TRISB &= 0x80FF; // Configure RB8-RB14 as outputs + TRISD &= 0xFF9F; // Configure RD5-RD6 as outputs + + int i = 0; + int v = 0; + + while (1) { + if (i == 0){ + AD1CON1bits.ASAM = 1; // Start conversion + while (IFS1bits.AD1IF == 0); // Wait while conversion not done + int total = 0; + int *p = (int *)(&ADC1BUF0); + for (; p <= (int *)(&ADC1BUFF); p+=4) + total += *p; + int val_ad = total / SAMPLES; + v = (val_ad * 33 + 511) / 1023; + } + send2displays(v); + delay(10); + + i = (i + 1) % 20; + + IFS1bits.AD1IF = 0; // Reset AD1IF + } + + return 0; +}