diff options
author | Nao Pross <naopross@thearcway.org> | 2018-02-23 16:31:32 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-02-23 16:31:32 +0100 |
commit | a588c9f6e6f0946844ed27e618d792cd025d4362 (patch) | |
tree | d16bbb2b5d99ec50036cc78884abde5a67fab798 /src/main.c | |
parent | Fix rs232 interface for MIDI serial (diff) | |
download | Xilofono-a588c9f6e6f0946844ed27e618d792cd025d4362.tar.gz Xilofono-a588c9f6e6f0946844ed27e618d792cd025d4362.zip |
No time for commit msg
Diffstat (limited to '')
-rw-r--r-- | src/main.c | 141 |
1 files changed, 119 insertions, 22 deletions
@@ -84,34 +84,62 @@ #include <xc.h> #include <stdio.h> #include <stdlib.h> +#include <stdint.h> #include <string.h> -int eusart_write_midi(const midi_message_t *pkt) +#define NOTES 15 +#define NOTE_MASK 0xFF00 + + +/* global variables */ +volatile uint16_t keypresses[NOTES]; + +/* function prototypes */ +int eusart_write_midi(const midi_message_t *pkt); + +/* interrupt service routine */ +interrupt void isr(void) { - size_t length; - uint8_t *data; - - if (pkt == NULL) { - return -1; - } - - length = pkt->data_size; - data = (uint8_t *) pkt->data; - - putch((char)((pkt->status << 4) | pkt->channel)); + unsigned char i, data; + + PORTDbits.RD3 = 0; - while (length--) { - putch((char) *(data++)); + if (PIR1bits.TMR2IF) { + // PORTA + data = PORTA; + i = 7; + do { + keypresses[i] = (keypresses[i] << 1) | ((data >> i) & 0x01); + } while (i--); + + data = PORTB; + i = 7; + do { + keypresses[i] = (keypresses[i] << 1) | ((data >> i) & 0x01); + } while (i-- - 8); + + // PORTB + // for (i = 8; i < NOTES; i++) { + // keypresses[i] = (keypresses[i] << 1) | ((PORTB & (1 << i)) >> i); + // } + + // TODO same for PORTD when the steps board is printed + + // debug stuff + PORTDbits.RD4 = PORTAbits.RA0; + PORTDbits.RD2 = (keypresses[0] && !(keypresses[0] & NOTE_MASK)); + + // reset interrupt flag + PIR1bits.TMR2IF = 0; } - return 0; + PORTDbits.RD3 = 1; } - -/* main program */ -void main(void) +/* hardware configuration (inlined) */ +inline void init_hw(void) { - midi_message_t sample_message; + di(); /* PLL / FOSC configuration */ // enable PLL @@ -130,14 +158,62 @@ void main(void) ANSELC = 0x00; ANSELD = 0x00; + // TODO: remove demo + TRISA = 0xFF; + TRISB = 0xFF; + + TRISDbits.TRISD1 = 0; + TRISDbits.TRISD2 = 0; + TRISDbits.TRISD3 = 0; + TRISDbits.TRISD4 = 0; + + // LED + PORTDbits.RD1 = 1; + // TEST OUTPUT 1 + PORTDbits.RD2 = 0; + // TEST OUTPUT 2 + PORTDbits.RD3 = 1; + // TEST OUTPUT 3 + PORTDbits.RD4 = 0; + + /* timer configuration */ + // timer 2 comp value + PR2 = 128; + // postscaler 1:4 + T2CONbits.T2OUTPS = 0b0011; + // prescaler 1:16 + T2CONbits.T2CKPS = 0b11; + // start timer + T2CONbits.TMR2ON = 1; + + // timer 2 interrupts + PIE1bits.TMR2IE = 1; + PIR1bits.TMR2IF = 0; + + // enable peripheral interrupts + INTCONbits.PEIE = 1; /* serial configuration */ eusart1_init(); - /* demo code */ - midi_note_on(&sample_message, 0x0, 0x3C, 0x7F); + ei(); +} - TRISDbits.TRISD1 = 0; + +/* main program */ +void main(void) +{ + unsigned char i, data; + midi_message_t sample_message; + + /* setup hardware */ + init_hw(); + + /* setup software */ + memset(keypresses, 0, sizeof(keypresses)); + + /* TODO remove demo code */ + midi_note_on(&sample_message, 0x0, 0x3C, 0x7F); PORTDbits.RD1 = 0; /* main loop */ @@ -146,3 +222,24 @@ void main(void) } } + +int eusart_write_midi(const midi_message_t *pkt) +{ + size_t length; + uint8_t *data; + + if (pkt == NULL) { + return -1; + } + + length = pkt->data_size; + data = (uint8_t *) pkt->data; + + putch((char)((pkt->status << 4) | pkt->channel)); + + while (length--) { + putch((char) *(data++)); + } + + return 0; +}
\ No newline at end of file |