/* * File: main.c * Author: oncley * * This for use to get the Crista IMU data into TRAM without losing characters. * Extremely simple baud rate down-scaler: * input stream at 115200 baud, output stream at 19200 baud * * Created on November 18, 2011, 11:04 PM * Various edits through Dec, 2011. * Fixed April 28, 2011 [had been removing 0 values in que_out! now using outflag] * * Dec 27, 2013: removed serial data checking that seems to throw out too much. */ #include #define XTAL_FREQ 8MHZ #define CLRWDT() {__asm__ volatile ("clrwdt");} #define DI() {__asm__ volatile ("disi #16383");} // I this disables all interrupts /* internal FRC clock, clock output enabled, fail-safe disabled */ _FOSCSEL(FNOSC_FRC); _FOSC(OSCIOFNC_ON & FCKSM_CSDCMD); /* WDT enabled with long timeouts */ _FWDT(WDTPS_PS32768 & FWPSA_PR128 & FWDTEN_ON & WINDIS_OFF); #define QTOP 999 #define QBOT 0 #define QLEN 1000 char buf[QLEN]; int rfpop, rfpush, outflag; char que_out() { char c; if (rfpush != rfpop) { outflag = 1; c = buf[rfpop]; if (rfpop == QTOP) rfpop = QBOT; else rfpop++; } else { outflag = 0; c = 0; } return c; } void que_in(char qdata) { buf[rfpush] = qdata; if (rfpush == QTOP) rfpush = QBOT; else rfpush++; } int main(void) { char c; int rstat; /* Pins used: * U1Tx - pin11; RB7; CN23 * U1Rx - pin6; RB2; SDA2; AN4; CTED13, CN6 * U2Tx - pin4; RB0; SDI2; AN2; CN4 * U2Rx - pin5; RB1; AN3; CTED12; CN5 */ DI(); // disable all interrupts (forever!) /* initialize variables */ rfpush = QBOT; rfpop = QBOT; outflag = 0; /* initialize ports */ ANSA = 0x0000; // turn off portA A/Ds ANSB = 0x0000; // turn off portB A/Ds SPI1STAT = 0x0000; // SPI1 disabled SPI2STAT = 0x0000; // SPI2 disabled I2C1CON = 0x0000; // I2C1 disabled I2C2CON = 0x0000; // I2C2 disabled CTMUCON1 = 0x0000; // CTMU disabled CNEN1 = 0x0000; // Change Notification bits disabled CNEN2 = 0x0000; // Change Notification bits disabled TRISA = 0xFFFF; // all portA bits are input TRISB = 0x3F7E; // all portB bits are input except the 2 Tx pins (0,7) and debug (15) OSCTUN = 0x0020; // lower oscillator to improve baud rate match [WORKS!!!] /* UART1 to 115200 baud */ U1BRG = 1; // 8.5% high U1MODE = 0x8800; // Enabled, 8-N-1, no hardware flow control U1STA = 0x0400; // Enable transmit [not yet used] /* UART2 to 19200 baud */ U2BRG = 12; // 0.2% high U2MODE = 0x8800; // Enabled, 8-N-1, no hardware flow control U2STA = 0x0400; // Enable transmit /* Continuous loop */ while (1) { /* Poll input UART for byte; put into queue if available */ rstat = U1STA; if ((rstat & 0x0001)==0x0001) { // if ((rstat & 0x000E)==0x0000) { // only save if no read errors [ignore errors for now] c = (char)(U1RXREG & 0x00FF); que_in(c); // } // else U1STA = 0x0400; // clear error flag } /* Poll output UART; if empty, write byte from queue */ if ((U2STA & 0x0100)==0x0100) { c = que_out(); if (outflag==1) U2TXREG = (int)c; } CLRWDT(); } return 0; // never gets here }