EDF - OnePIC MCU  Preliminary v0.7.0
8bit_IR/demo_main.c
Go to the documentation of this file.
00001 
00010 #include <htc.h>   //PIC specific hardware defintions
00011 #include <string.h> //Used for sprintf
00012 #include <stdio.h> //Used for sprintf
00013 
00014 #include "int.h"
00015 #include "OnePIC_timer.h"
00016 #include "OnePIC_lcd.h"
00017 #include "OnePIC_led.h"
00018 #include "OnePIC_switch.h"
00019 #include "Compiler.h"
00020 #include "mTouchCVD.h"
00021 #include "TimeDelay.h"
00022 
00023 //Custom Demo Routines
00024 #include "Demo_isr.h"
00025 #include "Demo_mtouch.h"
00026 #include "demo_main.h"
00027 #include "mcp2200_uart.h"
00028 #include "irda_uart.h"
00029 
00038 IR_DEMO_FLAG rx_tx_flag; 
00039 IR_DEMO_RX rx_byte;  
00047 void demo_main_loop(void) {
00048     uint8_t status_led = 0; // status_led used as a delay for the status led @note this is hardcode and dependent on Fosc */
00049     uint8_t blink_tx = 0; // blink_tx blink tx status LED this many times */
00050     uint8_t blink_rx = 0; // blink_rx blink rx status LED this many times */
00051     uint16_t counter = 0; // counter hardcoded delay routine @note this is depeendent on Fosc */
00052 
00053 
00054     irda_demo_init();
00055 
00056     while (1) {
00057         if (counter++ == 100) { //hardcoded delay loop to blink TX/RX status LEDs
00058             counter = 0;
00059             if (blink_tx != 0) {
00060                 gpLED_1 ^= 1; //blink RX LED
00061                 blink_tx--;
00062             }
00063             if (blink_rx != 0) {
00064                 gpLED_2 ^= 1; //blink TX LED
00065                 blink_rx--;
00066             }
00067             if (blink_tx == 0)
00068                 gpLED_1 = 0; //Reset
00069             if (blink_rx == 0)
00070                 gpLED_2 = 0; //Reset
00071 
00072             if (status_led++ == 90) {
00073                 gpLED_6 = 1; //set the "I'm Alive" LED indicator
00074             }
00075         }
00076         if (status_led == 95) { //only allow this LED to be on for a short while
00077             status_led = 0; //clear the "I'm Alive" LED indicator
00078             gpLED_6 = 0;
00079         }
00080 
00081         if (rx_tx_flag.TOUCH_TX_FLAG) { //was a button pressed?
00082             touch_service();
00083             blink_tx = 5;
00084 
00085         }
00086         if (rx_tx_flag.MCP2200_RX_FLAG) { //was something received from USB?
00087             mcp2200_service();
00088             blink_tx = 5;
00089             blink_rx = 5;
00090         }
00091         if (rx_tx_flag.IRDA_RX_FLAG) { //was someting received from the PICtail?
00092             ir_service();
00093             blink_tx = 5;
00094             blink_rx = 5;
00095         }
00096         Demo_mTouchPoll(); //keep on polling the buttons
00097     }
00098 }
00099 
00108 void touch_service() {
00109     mcp2200_tx(rx_byte.touch_tx); //send the button press byte to the PC
00110     irda_tx(rx_byte.touch_tx);   //send the button press byte to the IR link
00111     if (rx_byte.rx_tx_old_byte != rx_byte.touch_tx) { //Only enter here to refresh the LCD....don't want to update unless new byte sent
00112         EDF_INT_Disable(); //disable interrupts before updating or else risk failing the write
00113         sprintf(LCDBuffer[0], "PC-RX:  IR-RX: ");
00114         sprintf(LCDBuffer[1], "PC-TX:%c IR-TX:%c", rx_byte.touch_tx, rx_byte.touch_tx);
00115         rx_byte.rx_tx_old_byte = rx_byte.touch_tx; //save the current byte for checking next time through
00116         LCDCommand(CLEAR_DISPLAY);
00117         LCDUpdate(); //write both LCD buffers
00118         EDF_INT_Enable(); //Enable again now that it is safe
00119     }
00120     rx_tx_flag.TOUCH_TX_FLAG = false; //Don't enter here again unless a touch button is pressed again
00121 }
00122 
00130 void mcp2200_service() {
00131 
00132     mcp2200_tx(rx_byte.mcp2200_rx); //echo received byte back
00133     irda_tx(rx_byte.mcp2200_rx); //send received byte to IR channel
00134     if (rx_byte.rx_tx_old_byte != rx_byte.mcp2200_rx) { //Only enter here to refresh the LCD....don't want to update unless new byte sent
00135         sprintf(LCDBuffer[0], "PC-RX:%c IR-RX: ", rx_byte.mcp2200_rx); //send and receive
00136         sprintf(LCDBuffer[1], "PC-TX:%c IR-TX:%c", rx_byte.mcp2200_rx, rx_byte.mcp2200_rx);
00137         rx_byte.rx_tx_old_byte = rx_byte.mcp2200_rx;
00138         EDF_INT_Disable();
00139         LCDCommand(CLEAR_DISPLAY);
00140         LCDUpdate(); //update the LCD
00141         EDF_INT_Enable();
00142     }
00143     rx_tx_flag.MCP2200_RX_FLAG = false; //don't enter here agin until new data received
00144 }
00145 
00152 void ir_service() {
00153 
00154     mcp2200_tx(rx_byte.irda_rx);
00155     if (rx_byte.rx_tx_old_byte != rx_byte.irda_rx) {
00156         sprintf(LCDBuffer[0], "PC-RX:  IR-RX:%c", rx_byte.irda_rx);
00157         sprintf(LCDBuffer[1], "PC-TX:%c IR-TX: ", rx_byte.irda_rx);
00158         rx_byte.rx_tx_old_byte = rx_byte.irda_rx;
00159         EDF_INT_Disable();
00160         LCDCommand(CLEAR_DISPLAY);
00161         LCDUpdate();
00162         EDF_INT_Enable();
00163     }
00164     rx_tx_flag.IRDA_RX_FLAG = false; 
00165 }
00166 
00173 void irda_demo_init(void) {
00174 
00175     //0x20 is ASCII for blank space
00176     rx_byte.irda_rx = 0x20;
00177     rx_byte.mcp2200_rx = 0x20;
00178     rx_byte.touch_tx = 0x20;
00179 
00180     EDF_pot2bar(0); //disable the POT
00181     mcp2200_init(); //Initilize the onboard hardware UART to communicate with MCP2200
00182     CCPTMRS0 = 0b00001000; //CCP2 => TMR6 || CCP4 => TMR4
00183 
00184     //SETUP 16x clock for MCP2122
00185     EDF_TMR_ClockSourceSelect(TMR_6, TMR_SOURCE_INS_CLK);
00186     EDF_TMR_PrescalerSet(TMR_6, TMR246_PRESCALE_1_4);
00187     EDF_INT_SourceFlagClear(INT_TMR_6); //Don't enable the interrupt flag - let the module take care of it
00188     EDF_TMR_CounterSet(TMR_6,12); //Must be 16 * (baud rate).
00189     CCPR2L = 6;
00190     CCP2CON = 0b00101100;
00191 
00192 
00193     //use TIMR4 for IRDA_RX polling for the START bit
00194     EDF_TMR_PrescalerSet(TMR_4, TMR246_PRESCALE_1_1);
00195     EDF_INT_SourceEnable(INT_TMR_4); //enter the ISR when this rolls over to check IrDA RX pin for start bit (LOW when starting transmission)
00196     EDF_INT_SourceFlagClear(INT_TMR_4);
00197     EDF_TMR_CounterSet(TMR_4,50); //@todo document this better
00198 
00199     EDF_INT_Disable();
00200     LCDCommand(CLEAR_DISPLAY);
00201     sprintf(LCDBuffer[0], " IR Demo Active");
00202     LCDUpdate();
00203     EDF_INT_Enable();
00204 
00205     IRDA_16X_CLK_TRIS = 0; //Clear clock output
00206     IRDA_TX = 1; //IrDA devices transmit when TX is LOW..so set HIGH to avoid this
00207     EDF_TMR_Start(TMR_4); //Start the start bit polling
00208     EDF_TMR_Start(TMR_6); //Start the PWM module for the MCP2200 CLK
00209 }
00210 
00215 void irda_tx(char c) {
00216     EDF_INT_Disable();
00217     irda_putch(c);
00218     EDF_INT_Enable();
00219 }
00224 void mcp2200_tx(char c) {
00225     EDF_INT_Disable();
00226     putch(c); //echo it back to PC just for fun
00227     EDF_INT_Enable();
00228 
00229 }