1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/main.c Tue Oct 16 23:26:17 2018 +0200
1.3 @@ -0,0 +1,85 @@
1.4 +#include "pic32_c.h"
1.5 +#include "init.h"
1.6 +
1.7 +static void uart_write(char c)
1.8 +{
1.9 + while (REG(U1STA) & (1 << 9)); /* UTXBF (buffer full) */
1.10 +
1.11 + REG(U1TXREG) = c;
1.12 +}
1.13 +
1.14 +static void bits(uint32_t reg)
1.15 +{
1.16 + uint32_t mask;
1.17 +
1.18 + for (mask = (1 << 31); mask; mask >>= 1)
1.19 + if (REG(reg) & mask)
1.20 + uart_write('1');
1.21 + else
1.22 + uart_write('0');
1.23 +
1.24 + uart_write('\r');
1.25 + uart_write('\n');
1.26 +}
1.27 +
1.28 +static void blink(uint32_t delay, uint32_t port, uint32_t pins)
1.29 +{
1.30 + uint32_t counter;
1.31 +
1.32 + /* Clear outputs (LED). */
1.33 +
1.34 + CLR_REG(port, pins);
1.35 +
1.36 + while (1)
1.37 + {
1.38 + counter = delay;
1.39 +
1.40 + while (counter--) __asm__(""); /* retain loop */
1.41 +
1.42 + /* Invert outputs (LED). */
1.43 +
1.44 + INV_REG(port, pins);
1.45 + bits(IFS1);
1.46 + }
1.47 +}
1.48 +
1.49 +void main(void)
1.50 +{
1.51 + init_memory();
1.52 + init_pins();
1.53 + init_outputs();
1.54 +
1.55 + unlock_config();
1.56 + config_uart();
1.57 + lock_config();
1.58 +
1.59 + init_uart(7, 3);
1.60 +
1.61 + interrupts_on();
1.62 +
1.63 + blink(3 << 24, PORTA, 1 << 3);
1.64 +}
1.65 +
1.66 +void exception_handler(void)
1.67 +{
1.68 + blink(3 << 12, PORTA, 1 << 3);
1.69 +}
1.70 +
1.71 +void interrupt_handler(void)
1.72 +{
1.73 + /* Check for a UART receive interrupt condition (U1RIF). */
1.74 +
1.75 + if (!(REG(IFS1) & (1 << 8)))
1.76 + return;
1.77 +
1.78 + /* Write the received data back. */
1.79 +
1.80 + INV_REG(PORTA, 1 << 2);
1.81 +
1.82 + while (REG(U1STA) & 1)
1.83 + uart_write((char) REG(U1RXREG));
1.84 +
1.85 + /* Clear the UART interrupt condition. */
1.86 +
1.87 + CLR_REG(IFS1, 1 << 8);
1.88 +}