EDF - OnePIC MCU  Preliminary v0.7.0
8bit_IR/irda_uart.c
Go to the documentation of this file.
00001 
00018 #include <htc.h>   //PIC specific hardware
00019 #include <stdio.h> //Used for printf and sprintf
00020 
00021 #include "bsp.h"   //XTAL Frequ
00022 #include "irda_uart.h" //prototypes
00023 
00033 void irda_putch(char c) {
00034     unsigned char i;
00035 
00036     IRDA_TX = 0; 
00037     for (i = NUM_DATA_BITS + 1; i != 0; i--) {
00038         __delay_us(BIT_UDLY);
00039         if (c & 1)
00040             IRDA_TX = 1;
00041         if (!(c & 1))
00042             IRDA_TX = 0;
00043         c = (c >> 1) | 0x80;
00044     }
00045     IRDA_TX = 1; 
00046 
00047 }
00048 
00059 char irda_getch(void) {
00060     char c = 0; //returned character received
00061     uint8_t i;
00062 
00063     //Worst case is that start bit is caught 20us from inital start, depending on rate of sampling start bit
00064     //Want to sample in the middle( 104us/2)
00065 
00066     __delay_us(5);
00067     if (IRDA_RX)
00068         goto noise; // twas just noise
00069     for(i=NUM_DATA_BITS+1;i!=0;i--){
00070         c = (c >> 1) | (IRDA_RX << 7); //LSB is transmitted first
00071         __delay_us(BIT_UDLY);
00072     }
00073     return c;   //return the char
00074 
00075 noise:
00076     NOP();
00077 }
00078 
00083 char irda_getche(void) {
00084     char c;
00085 
00086     irda_putch(c = irda_getch());
00087     char c;
00088 }