Using output compare as interrupt timebase?
I just have to ask as i cannot seem to wrap my head around this :) I need to create a bitbanged, interrupted UART that can do 15 bit data. I thought I'd set up two timebases, one for transmit and one for receive. On transmit of an integer I shove it into the TCX buffer zero the OC counter and start the OC timer and then just clock out each bit, one per interrupt and when done I turn off the OC timer. For reception I have one interrupt that catches the start bit and starts the timer, initially with 1.5 bit time then 1 bit time and clock in the data. PIC 24FJ64GB00464MHz
// Output compare 1 interrupt. Transmit timer
// Shift out the bits in TXRegister and turn off timer interrupt when done.
void __attribute__((__interrupt__, auto_psv)) _OC1Interrupt( void )
LATBbits.LATB5 = TXRegister & 1;
TXRegister = TXRegister >> 1;
if(TXCounter == 0)
LATBbits.LATB5 = 1;
IEC0bits.OC1IE = 0; // All done?
IFS0bits.OC1IF = 0; // clear the interrupt bit
Problem I have is that I do not quite understand the OC initialisation, even if it works. I just need an OC interrupt, nothing else and this is what I have:
// Testing 115200 baud. Bit time 8,68us
// Timer clock Fosc/2 = 32MHz, 278 ticks
OC1CON1 = 0;
OC1CON2 = 0;
OC1CON1bits.OCTSEL = 7; // System clock
OC1CON1bits.OCM = 0b011;
OC1CON2bits.SYNCSEL = 0b11111;
OC1CON2bits.OCTRIS = 1;
OC1RS = 138;
OC1R = 0;
I thought I'd have to put the timer value in OC1R, but not so. It goes into OC1RS and the value is not what I have calculated, so I must be doing something wrong. Can anyone enlighten me?