[AC2] Aula10 Part1 fixes

Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2024-05-17 10:57:51 +01:00
parent ddc4715b30
commit fae75c9376
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
3 changed files with 22 additions and 14 deletions

View File

@ -4,7 +4,7 @@ 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);
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)

View File

@ -21,7 +21,7 @@ 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)
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)

View File

@ -12,12 +12,9 @@ void putc(char byte) {
U2TXREG = byte;
}
void sendCounter(int counter) {
while (counter != 0) {
char bit = counter & 0x1;
putc(bit);
counter >>= 1;
}
void putstr(char *str) {
// use putc() function to send each charater ('\0' should not be sent)
while (*str != '\0') putc(*str++);
}
int main() {
@ -38,14 +35,25 @@ int main() {
// 4 - Enable UART2 (see register U2MODE)
U2MODEbits.ON = 1;
int counter = 0;
int i, count = 0;
while (1) {
sendCounter(counter);
delay(5000);
counter = (counter + 1) % 10;
while(1) {
// Incrementa o contador módulo 10
count = (count + 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;
}