Compare commits

..

2 Commits

Author SHA1 Message Date
Tiago Garcia 8e0ffb6f84
[AC2] Add Aula11
Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
2024-05-17 10:58:02 +01:00
Tiago Garcia e35addbf00
[AC2] Aula10 Part1 fixes
Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
2024-05-17 10:57:51 +01:00
7 changed files with 284 additions and 14 deletions

View File

@ -4,7 +4,7 @@ int main() {
// Configure UART2: // Configure UART2:
// 1 - Configure BaudRate Generator // 1 - Configure BaudRate Generator
U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1; U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1;
U2MODEbits.BRGH = 0 // 16x baud clock enabled (1 -> 4x baud clock); U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock);
// 2 - Configure number of data bits, parity and number of stop bits // 2 - Configure number of data bits, parity and number of stop bits
// (see U2MODE register) // (see U2MODE register)

View File

@ -21,7 +21,7 @@ int main() {
// Configure UART2: // Configure UART2:
// 1 - Configure BaudRate Generator // 1 - Configure BaudRate Generator
U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1; U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1;
U2MODEbits.BRGH = 1; // 16x baud clock enabled (1 -> 4x baud clock) U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock)
// 2 - Configure number of data bits, parity and number of stop bits // 2 - Configure number of data bits, parity and number of stop bits
// (see U2MODE register) // (see U2MODE register)

View File

@ -12,12 +12,9 @@ void putc(char byte) {
U2TXREG = byte; U2TXREG = byte;
} }
void sendCounter(int counter) { void putstr(char *str) {
while (counter != 0) { // use putc() function to send each charater ('\0' should not be sent)
char bit = counter & 0x1; while (*str != '\0') putc(*str++);
putc(bit);
counter >>= 1;
}
} }
int main() { int main() {
@ -38,14 +35,25 @@ int main() {
// 4 - Enable UART2 (see register U2MODE) // 4 - Enable UART2 (see register U2MODE)
U2MODEbits.ON = 1; U2MODEbits.ON = 1;
int counter = 0; int i, count = 0;
while(1) { while(1) {
sendCounter(counter); // Incrementa o contador módulo 10
delay(5000); count = (count + 1) % 10;
counter = (counter + 1) % 10;
}
// Converte o valor do contador para binário
char binary[5];
for (i = 0; i < 4; i++) {
binary[3 - i] = (count & (1 << i)) ? '1' : '0';
}
binary[4] = '\0';
// Count modulo 10 em binário:
putstr(binary);
putc('\n');
delay(200); // 5hz = 200ms
}
return 0; return 0;
} }

Binary file not shown.

View File

@ -0,0 +1,50 @@
#include <detpic32.h>
int main() {
TRISCbits.TRISC14 = 0;
U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1;
U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock)
U2MODEbits.PDSEL = 00; // 8 data bits, without parity
U2MODEbits.STSEL = 0; // 1 stop bit
U2STAbits.URXEN = 1; // Enable Receiver
U2STAbits.UTXEN = 1; // Enable Transmitter
IEC1bits.U2RXIE = 1;
IEC1bits.U2TXIE = 0;
IPC8bits.U2IP = 2;
IFS1bits.U2RXIF = 0;
U2MODEbits.ON = 1;
EnableInterrupts();
while (1);
return 0;
}
void putc(char byte) {
// wait while UTXBF == 1
while (U2STAbits.UTXBF == 1);
// Copy byte to the UxTXREG register
U2TXREG = byte;
}
void _int_(32) isr_uart2rx() {
// If OERR == 1 then reset OERR
if (U2STAbits.OERR == 1)
U2STAbits.OERR = 0;
// Wait while URXDA == 0
while (U2STAbits.URXDA == 0);
// Save U2RXREG value
char c = U2RXREG;
if (c == 'T') LATCbits.LATC14 = 1;
else if (c == 't') LATCbits.LATC14 = 0;
putc(c);
IFS1bits.U2RXIF = 0;
}

View File

@ -0,0 +1,57 @@
#include "p32mx795f512h.h"
#include <detpic32.h>
volatile struct {
char mem[100];
int nchar;
int posrd;
} txbuf;
void putStrInt(char *s) {
while (txbuf.nchar > 0);
while (*s != '\0')
txbuf.nchar++[txbuf.mem] = *s++;
txbuf.posrd = 0;
IEC1bits.U2TXIE = 1;
}
int main() {
U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1;
U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock)
U2MODEbits.PDSEL = 00; // 8 data bits, without parity
U2MODEbits.STSEL = 0; // 1 stop bit
U2STAbits.URXEN = 1; // Enable Receiver
U2STAbits.UTXEN = 1; // Enable Transmitter
IEC1bits.U2RXIE = 0;
IEC1bits.U2TXIE = 0;
IPC8bits.U2IP = 2;
IFS1bits.U2TXIF = 0;
U2STAbits.UTXISEL = 00;
U2MODEbits.ON = 1;
EnableInterrupts();
txbuf.nchar = 0;
while (1) {
putStrInt("Test string which can be as long as you like, up to a maximum of 100 characters\n");
}
return 0;
}
void _int_(32) isr_uart2tx() {
if (!IFS1bits.U2TXIF) return;
if (txbuf.nchar-- > 0) {
U2TXREG = txbuf.posrd++[txbuf.mem];
} else {
IEC1bits.U2TXIE = 0;
}
IFS1bits.U2TXIF = 0;
}

View File

@ -0,0 +1,155 @@
#include <detpic32.h>
#define SAMPLES 4
volatile int voltage = 0;
volatile int voltMin = 33;
volatile int voltMax = 0;
const unsigned int dis7Scodes[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D,
0xFD, 0x07, 0x7F, 0x6F, 0x77, 0xFC,
0x39, 0x5E, 0xF9, 0xF1};
typedef enum { HIGH, LOW } display;
unsigned char toBdc(unsigned char value) {
return ((value / 10) << 4) + (value % 10);
}
void send2displays(unsigned char value) {
static display flag = LOW;
unsigned char high = dis7Scodes[value >> 4];
unsigned char low = dis7Scodes[value & 0x0F];
if (flag == HIGH) {
LATD = (LATD & 0xFF9F) | 0x0040;
LATB = (LATB & 0x80FF) | high << 8;
flag = LOW;
} else {
LATD = (LATD & 0xFF9F) | 0x0020;
LATB = (LATB & 0x80FF) | low << 8;
flag = HIGH;
}
}
void putc(char byte) {
// wait while UTXBF == 1
while (U2STAbits.UTXBF == 1);
// Copy byte to the UxTXREG register
U2TXREG = byte;
}
void putstr(char *str) {
// use putc() function to send each charater ('\0' should not be sent)
while (*str != '\0') putc(*str++);
}
int main() {
TRISB &= 0x80FF;
TRISD &= 0xFF9F;
TRISBbits.TRISB4 = 1;
AD1PCFGbits.PCFG4 = 0;
AD1CON1bits.SSRC = 7;
AD1CON1bits.CLRASAM = 1;
AD1CON3bits.SAMC = 16;
AD1CON2bits.SMPI = SAMPLES - 1;
AD1CHSbits.CH0SA = 4;
AD1CON1bits.ON = 1;
IPC6bits.AD1IP = 2;
IFS1bits.AD1IF = 0;
IEC1bits.AD1IE = 1;
// Configure Timer T1 with 5Hz frequency
T1CONbits.TCKPS = 2;
PR1 = 62499;
TMR1 = 0;
T1CONbits.TON = 1;
// Configure Timer T3 with 100Hz frequency
T3CONbits.TCKPS = 2;
PR3 = 49999;
TMR3 = 0;
T3CONbits.TON = 1;
IPC1bits.T1IP = 2;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;
IPC3bits.T3IP = 2;
IFS0bits.T3IF = 0;
IEC0bits.T3IE = 1;
U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1;
U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock)
U2MODEbits.PDSEL = 00; // 8 data bits, without parity
U2MODEbits.STSEL = 0; // 1 stop bit
U2STAbits.URXEN = 1; // Enable Receiver
U2STAbits.UTXEN = 1; // Enable Transmitter
IEC1bits.U2RXIE = 1;
IEC1bits.U2TXIE = 0;
IPC8bits.U2IP = 2;
IFS1bits.U2RXIF = 0;
U2MODEbits.ON = 1;
EnableInterrupts();
while (1);
return 0;
}
void _int_(4) isr_T1(void) {
AD1CON1bits.ASAM = 1;
IFS0bits.T1IF = 0;
}
void _int_(12) isr_T3(void) {
send2displays(toBdc(voltage));
IFS0bits.T3IF = 0;
}
void _int_(27) isr_adc(void) {
int *p = (int *)(&ADC1BUF0);
int media = 0;
for (; p <= (int *)(&ADC1BUFF); p++)
media += *p;
media /= SAMPLES;
voltage = (media * 33 + 511) / 1023;
voltMin = voltage < voltMin ? voltage : voltMin;
voltMax = voltage > voltMax ? voltage : voltMax;
IFS1bits.AD1IF = 0;
}
void _int_(32) isr_uart2rx() {
// If OERR == 1 then reset OERR
if (U2STAbits.OERR == 1)
U2STAbits.OERR = 0;
// Save U2RXREG value
char c = U2RXREG;
if (c == 'M') {
char v = toBdc(voltMax);
putstr("VMax=");
putc((v >> 4) + 0x30);
putc('.');
putc((v & 0x0F) + 0x30);
putc('V');
putc('\n');
} else if (c == 'm') {
char v = toBdc(voltMin);
putstr("VMin=");
putc((v >> 4) + 0x30);
putc('.');
putc((v & 0x0F) + 0x30);
putc('V');
putc('\n');
}
IFS1bits.U2RXIF = 0;
}