pic24_serial.c

Go to the documentation of this file.
00001 /*
00002  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
00003  * All rights reserved.
00004  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
00005  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
00006  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
00007  *
00008  * Permission to use, copy, modify, and distribute this software and its
00009  * documentation for any purpose, without fee, and without written agreement is
00010  * hereby granted, provided that the above copyright notice, the following
00011  * two paragraphs and the authors appear in all copies of this software.
00012  *
00013  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
00014  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00015  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
00016  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00019  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00021  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
00022  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
00023  *
00024  * Please maintain this header in its entirety when copying/modifying
00025  * these files.
00026  *
00027  *
00028  */
00029 
00030 
00031 // Documentation for this file. If the \file tag isn't present,
00032 // this file won't be documented.
00037 #include "pic24_all.h"
00038 #include <libpic30.h>
00039 
00040 /*********************************************************
00041  * Public functions intended to be called by other files *
00042  *********************************************************/
00043 
00044 #ifdef BUILT_ON_ESOS
00045 #define outChar                 esos_PutUint8ToCommOut
00046 #else
00047 
00054 void outChar(uint8 u8_c) {
00055   switch (__C30_UART) {
00056 #if (NUM_UART_MODS >= 1)
00057     case 1 :
00058       outChar1(u8_c);
00059       break;
00060 #endif
00061 #if (NUM_UART_MODS >= 2)
00062     case 2 :
00063       outChar2(u8_c);
00064       break;
00065 #endif
00066 #if (NUM_UART_MODS >= 3)
00067     case 3 :
00068       outChar3(u8_c);
00069       break;
00070 #endif
00071 #if (NUM_UART_MODS >= 4)
00072     case 4 :
00073       outChar4(u8_c);
00074       break;
00075 #endif
00076     default :
00077       REPORT_ERROR("Invalid UART");
00078   }
00079 }
00080 #endif
00081 
00086 void outString(const char* psz_s) {
00087   while (*psz_s) {
00088 
00089 #if (SERIAL_EOL_DEFAULT==SERIAL_EOL_CR_LF)
00090         if (*psz_s == '\n') outChar(0x0D);
00091     outChar(*psz_s);    
00092 #endif
00093 #if (SERIAL_EOL_DEFAULT==SERIAL_EOL_CR)
00094     if (*psz_s == '\n') outChar(0x0D);
00095     else outChar(*psz_s);
00096 #endif
00097 #if (SERIAL_EOL_DEFAULT==SERIAL_EOL_LF)
00098 //no translation
00099     outChar(*psz_s);
00100 #endif
00101     psz_s++;
00102   }
00103 }
00104 
00105 
00106 
00107 
00108 static uint16 inStringInternal (char *psz_buff, uint16 u16_maxCount, uint8 echoFlag) {
00109   uint8 u8_c;
00110   uint16 u16_i;
00111 
00112   if (!u16_maxCount) return 0;
00113   u16_i = 0;
00114   for (u16_i = 0; u16_i < u16_maxCount; u16_i++) {
00115     if (echoFlag) u8_c = inCharEcho();
00116     else u8_c = inChar();
00117     if (u8_c == '\n' ||u8_c == '\r' ) break; //terminate loop
00118     *psz_buff = u8_c; //save character
00119     psz_buff++;
00120   }
00121   //stop reading, terminate, return characters read.
00122   *psz_buff = 0;
00123   return(u16_i);
00124 }
00125 
00136 uint16 inString (char *psz_buff, int16 u16_maxCount) {
00137   return inStringInternal(psz_buff,u16_maxCount,0);
00138 }
00139 
00143 uint16 inStringEcho (char *psz_buff, int16 u16_maxCount) {
00144   return inStringInternal(psz_buff,u16_maxCount,1);
00145 }
00146 
00147 
00148 void outUint8NoLeader(uint8 u8_x) {
00149   uint8 u8_c;
00150   u8_c = (u8_x>>4)& 0xf;
00151   if (u8_c > 9) outChar('A'+u8_c-10);
00152   else outChar('0'+u8_c);
00153   //LSDigit
00154   u8_c= u8_x & 0xf;
00155   if (u8_c > 9) outChar('A'+u8_c-10);
00156   else outChar('0'+u8_c);
00157 }
00158 
00163 void outUint8(uint8 u8_x) {
00164   outString("0x");
00165   outUint8NoLeader(u8_x);
00166 }
00167 
00172 void outUint16(uint16 u16_x) {
00173   uint8 u8_c;
00174 
00175   outString("0x");
00176   u8_c = (u16_x >> 8);
00177   outUint8NoLeader(u8_c);
00178   u8_c = (uint8) u16_x;
00179   outUint8NoLeader(u8_c);
00180 }
00181 
00186 void outUint32(uint32 u32_x) {
00187   uint8 u8_c;
00188   outString("0x");
00189   u8_c = (u32_x >> 24);
00190   outUint8NoLeader(u8_c);
00191   u8_c = (u32_x >> 16);
00192   outUint8NoLeader(u8_c);
00193   u8_c = (u32_x >> 8);
00194   outUint8NoLeader(u8_c);
00195   u8_c = u32_x;
00196   outUint8NoLeader(u8_c);
00197 }
00198 
00203 void outUint8Decimal( uint8 u8_x ) {
00204   static uint8  u8_d[]={50, 30, 20, 10, 5, 3, 2, 1 };
00205   static uint8  u8_f[]={5, 3, 2, 1, 5, 3, 2, 1 };
00206 
00207   char      psz_out[5];
00208   uint8     u8_i, u8_destroy;
00209 
00210   u8_i = 0;
00211   u8_destroy = u8_x;
00212   psz_out[0] = '0';
00213   psz_out[1] = '0';
00214   psz_out[2] = '0';
00215   psz_out[3] = 0;
00216   if (u8_destroy >= 200) {
00217     psz_out[0] += 200;
00218     u8_destroy -= 200;
00219   } // end if()
00220   if (u8_destroy >= 100) {
00221     psz_out[0] += 100;
00222     u8_destroy -= 100;
00223   } // end if()
00224   for (u8_i=0; u8_i<8; u8_i++) {
00225     if (u8_destroy >= u8_d[u8_i]) {
00226       psz_out[1+(u8_i/4)] += u8_f[u8_i];
00227       u8_destroy -= u8_d[u8_i];
00228     }
00229   } //end for()
00230   psz_out[3] = 0;
00231   outString( psz_out );
00232 }
00233 
00238 void outUint16Decimal( uint16 u16_x ) {
00239   static uint16  u16_d[]={50000, 30000, 20000, 10000, 5000, 3000, 2000, 1000, \
00240                           500, 300, 200, 100, 50, 30, 20, 10, 5, 3, 2, 1
00241                          };
00242   static uint8   u8_f[]={5, 3, 2, 1 };
00243 
00244   uint8     u8_i;
00245   uint16    u16_destroy;
00246   char      psz_out[5];
00247 
00248   u8_i = 0;
00249   u16_destroy = u16_x;
00250   psz_out[0] = '0';
00251   psz_out[1] = '0';
00252   psz_out[2] = '0';
00253   psz_out[3] = '0';
00254   psz_out[4] = '0';
00255 
00256   for (u8_i=0; u8_i<20; u8_i++) {
00257     if (u16_destroy >= u16_d[u8_i]) {
00258       psz_out[u8_i/4] += u8_f[u8_i % 4];
00259       u16_destroy -= u16_d[u8_i];
00260     }
00261   } //end for()
00262   psz_out[5] = 0;
00263   outString( psz_out );
00264 }
00265 
00273 uint8 inChar(void) {
00274   switch (__C30_UART) {
00275 #if (NUM_UART_MODS >= 1)
00276     case 1 :
00277       return inChar1();
00278 #endif
00279 #if (NUM_UART_MODS >= 2)
00280     case 2 :
00281       return inChar2();
00282 #endif
00283 #if (NUM_UART_MODS >= 3)
00284     case 3 :
00285       return inChar3();
00286 #endif
00287 #if (NUM_UART_MODS >= 4)
00288     case 4 :
00289       return inChar4();
00290 #endif
00291     default :
00292       REPORT_ERROR("Invalid UART");
00293       return 0;
00294   }
00295 }
00296 
00301 uint8 inCharEcho(void) {
00302   uint8 u8_c;
00303   u8_c = inChar(); //get character
00304   outChar(u8_c);   //echo
00305   return u8_c;
00306 }
00307 
00312 uint8 isCharReady(void) {
00313   switch (__C30_UART) {
00314 #if (NUM_UART_MODS >= 1)
00315     case 1 :
00316       return isCharReady1();
00317 #endif
00318 #if (NUM_UART_MODS >= 2)
00319     case 2 :
00320       return isCharReady2();
00321 #endif
00322 #if (NUM_UART_MODS >= 3)
00323     case 3 :
00324       return isCharReady3();
00325 #endif
00326 #if (NUM_UART_MODS >= 4)
00327     case 4 :
00328       return isCharReady4();
00329 #endif
00330     default :
00331       REPORT_ERROR("Invalid UART");
00332       return 0;
00333   }
00334 }
00335 
00336 
00337 
00343 void configDefaultUART(uint32 u32_baudRate) {
00344   switch (DEFAULT_UART) {
00345 #if (NUM_UART_MODS >= 1)
00346     case 1 :
00347       __C30_UART = 1;   //this is the default UART
00348       configUART1(u32_baudRate);
00349       break;
00350 #endif
00351 #if (NUM_UART_MODS >= 2)
00352     case 2 :
00353       __C30_UART = 2;   //this is the default UART
00354       configUART2(u32_baudRate);
00355       break;
00356 #endif
00357 #if (NUM_UART_MODS >= 3)
00358     case 3 :
00359       __C30_UART = 3;   //this is the default UART
00360       configUART3(u32_baudRate);
00361       break;
00362 #endif
00363 #if (NUM_UART_MODS >= 4)
00364     case 4 :
00365       __C30_UART = 4;   //this is the default UART
00366       configUART4(u32_baudRate);
00367       break;
00368 #endif
00369     default :
00370       REPORT_ERROR("Invalid UART");
00371   }
00372 }
00373 

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9