diff --git a/2ano/2semestre/ac2/aula10/AC2-P-Aula10.pdf b/2ano/2semestre/ac2/aula10/AC2-P-Aula10.pdf new file mode 100644 index 0000000..11698d1 Binary files /dev/null and b/2ano/2semestre/ac2/aula10/AC2-P-Aula10.pdf differ diff --git a/2ano/2semestre/ac2/aula10/part1-ex1.c b/2ano/2semestre/ac2/aula10/part1-ex1.c new file mode 100644 index 0000000..bbc8ed3 --- /dev/null +++ b/2ano/2semestre/ac2/aula10/part1-ex1.c @@ -0,0 +1,22 @@ +#include + +int main() { + // Configure UART2: + // 1 - Configure BaudRate Generator + U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1; + U2MODEbits.BRGH = 0 // 16x baud clock enabled (1 -> 4x baud clock); + + // 2 - Configure number of data bits, parity and number of stop bits + // (see U2MODE register) + U2MODEbits.PDSEL = 00; // 8 data bits, without parity + U2MODEbits.STSEL = 0; // 1 stop bit + + // 3 - Enable the transmitter and receiver modules (see register U2STA) + U2STAbits.URXEN = 1; // Enable Receiver + U2STAbits.UTXEN = 1; // Enable Transmitter + + // 4 - Enable UART2 (see register U2MODE) + U2MODEbits.ON = 1; + + return 0; +} diff --git a/2ano/2semestre/ac2/aula10/part1-ex2.c b/2ano/2semestre/ac2/aula10/part1-ex2.c new file mode 100644 index 0000000..633ceda --- /dev/null +++ b/2ano/2semestre/ac2/aula10/part1-ex2.c @@ -0,0 +1,39 @@ +#include + +void delay(unsigned int ms) { + resetCoreTimer(); + while (readCoreTimer() < ms * 20000); +} + +void putc(char byte) { + // wait while UTXBF == 1 + while (U2STAbits.UTXBF == 1); + // Copy byte to the UxTXREG register + U2TXREG = byte; +} + +int main() { + // Configure UART2: + // 1 - Configure BaudRate Generator + U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1; + U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock) + + // 2 - Configure number of data bits, parity and number of stop bits + // (see U2MODE register) + U2MODEbits.PDSEL = 00; // 8 data bits, without parity + U2MODEbits.STSEL = 0; // 1 stop bit + + // 3 - Enable the transmitter and receiver modules (see register U2STA) + U2STAbits.URXEN = 1; // Enable Receiver + U2STAbits.UTXEN = 1; // Enable Transmitter + + // 4 - Enable UART2 (see register U2MODE) + U2MODEbits.ON = 1; + + while (1) { + putc('+'); + delay(1000); + } + + return 0; +} diff --git a/2ano/2semestre/ac2/aula10/part1-ex4.c b/2ano/2semestre/ac2/aula10/part1-ex4.c new file mode 100644 index 0000000..153904a --- /dev/null +++ b/2ano/2semestre/ac2/aula10/part1-ex4.c @@ -0,0 +1,44 @@ +#include + +void delay(unsigned int ms) { + resetCoreTimer(); + while (readCoreTimer() < ms * 20000); +} + +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() { + // Configure UART2: + // 1 - Configure BaudRate Generator + U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1; + U2MODEbits.BRGH = 1; // 16x baud clock enabled (1 -> 4x baud clock) + + // 2 - Configure number of data bits, parity and number of stop bits + // (see U2MODE register) + U2MODEbits.PDSEL = 00; // 8 data bits, without parity + U2MODEbits.STSEL = 0; // 1 stop bit + + // 3 - Enable the transmitter and receiver modules (see register U2STA) + U2STAbits.URXEN = 1; // Enable Receiver + U2STAbits.UTXEN = 1; // Enable Transmitter + + // 4 - Enable UART2 (see register U2MODE) + U2MODEbits.ON = 1; + + while (1) { + putstr("String de teste\n"); + delay(1000); + } + + return 0; +} diff --git a/2ano/2semestre/ac2/aula10/part1-ex6.c b/2ano/2semestre/ac2/aula10/part1-ex6.c new file mode 100644 index 0000000..7a744d4 --- /dev/null +++ b/2ano/2semestre/ac2/aula10/part1-ex6.c @@ -0,0 +1,49 @@ +#include + +void delay(unsigned int ms) { + resetCoreTimer(); + while (readCoreTimer() < ms * 20000); +} + +void putc(char byte) { + // wait while UTXBF == 1 + while (U2STAbits.UTXBF == 1); + // Copy byte to the UxTXREG register + U2TXREG = byte; +} + +char getc() { + // If OERR == 1 then reset OERR + if (U2STAbits.OERR == 1) + U2STAbits.OERR = 0; + // Wait while URXDA == 0 + while (U2STAbits.URXDA == 0); + // Return U2RXREG + return U2RXREG; +} + +int main() { + // Configure UART2: + // 1 - Configure BaudRate Generator + U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1; + U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock) + + // 2 - Configure number of data bits, parity and number of stop bits + // (see U2MODE register) + U2MODEbits.PDSEL = 00; // 8 data bits, without parity + U2MODEbits.STSEL = 0; // 1 stop bit + + // 3 - Enable the transmitter and receiver modules (see register U2STA) + U2STAbits.URXEN = 1; // Enable Receiver + U2STAbits.UTXEN = 1; // Enable Transmitter + + // 4 - Enable UART2 (see register U2MODE) + U2MODEbits.ON = 1; + + while (1) { + putc(getc()); + } + + return 0; +} + diff --git a/2ano/2semestre/ac2/aula10/part1-ex7.c b/2ano/2semestre/ac2/aula10/part1-ex7.c new file mode 100644 index 0000000..efaa3a8 --- /dev/null +++ b/2ano/2semestre/ac2/aula10/part1-ex7.c @@ -0,0 +1,51 @@ +#include + +void delay(unsigned int ms) { + resetCoreTimer(); + while (readCoreTimer() < ms * 20000); +} + +void putc(char byte) { + // wait while UTXBF == 1 + while (U2STAbits.UTXBF == 1); + // Copy byte to the UxTXREG register + U2TXREG = byte; +} + +void sendCounter(int counter) { + while (counter != 0) { + char bit = counter & 0x1; + putc(bit); + counter >>= 1; + } +} + +int main() { + // Configure UART2: + // 1 - Configure BaudRate Generator + U2BRG = ((PBCLK + 8 * 115200) / (16 * 115200)) - 1; + U2MODEbits.BRGH = 0; // 16x baud clock enabled (1 -> 4x baud clock) + + // 2 - Configure number of data bits, parity and number of stop bits + // (see U2MODE register) + U2MODEbits.PDSEL = 00; // 8 data bits, without parity + U2MODEbits.STSEL = 0; // 1 stop bit + + // 3 - Enable the transmitter and receiver modules (see register U2STA) + U2STAbits.URXEN = 1; // Enable Receiver + U2STAbits.UTXEN = 1; // Enable Transmitter + + // 4 - Enable UART2 (see register U2MODE) + U2MODEbits.ON = 1; + + int counter = 0; + + while (1) { + sendCounter(counter); + delay(5000); + counter = (counter + 1) % 10; + } + + return 0; +} +