PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
soft_uart.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 
32 /** \file
33  * Demonstrates a software driven UART TX/RX using PIO pins
34 */
35 
36 /// Soft UART Config
37 #define CONFIG_STX() CONFIG_RB2_AS_DIG_OUTPUT()
38 #define STX _LATB2 //STX state
39 #define CONFIG_SRX() CONFIG_RB3_AS_DIG_INPUT()
40 #define SRX _RB3 //SRX state
41 #define DEFAULT_SOFT_BAUDRATE 19200
42 uint16_t u16_softBaudRate = DEFAULT_SOFT_BAUDRATE;
43 
44 void doBitDelay (uint16_t u16_baudRate) {
45  if (u16_baudRate == 9600) {
46  DELAY_US(106);
47  } else if (u16_baudRate == 19200) DELAY_US(52);
48 }
49 
50 void doBitHalfDelay (uint16_t u16_baudRate) {
51  if (u16_baudRate == 9600) {
52  DELAY_US(53);
53  } else if (u16_baudRate == 19200) DELAY_US(26);
54 }
55 
56 void outCharSoft(uint8_t u8_c) {
57  uint8_t u8_i;
58  STX = 0;
59  doBitDelay(u16_softBaudRate);
60  for (u8_i=0; u8_i<8; u8_i++) {
61  if (u8_c & 0x01)
62  STX = 1;
63  else STX = 0;
64  doBitDelay(u16_softBaudRate);
65  u8_c = u8_c >> 1;
66  }
67  STX = 1;
68  doBitDelay(u16_softBaudRate);
69 }
70 
71 uint8_t inCharSoft(void) {
72  uint8_t u8_i, u8_c;
73 
74  u8_c = 0x00;
75  while (SRX) doHeartbeat();
76  doBitHalfDelay(u16_softBaudRate);
77  for (u8_i=0; u8_i<8; u8_i++) {
78  doBitDelay(u16_softBaudRate);
79  if (SRX) u8_c = u8_c | 0x80;
80  if (u8_i != 7) u8_c = u8_c >> 1;
81  }
82  doBitDelay(u16_softBaudRate);
83  return(u8_c);
84 }
85 
86 
87 
88 int main (void) {
89  uint8_t u8_c;
90 
91  configClock();
93  /** GPIO config ***************************/
94  CONFIG_STX(); //software TX
95  STX = 1; //should be high
96  while (1) {
97  u8_c = inCharSoft(); //get character
98  u8_c++; //increment the character
99  outCharSoft(u8_c); //echo the character
100  doHeartbeat();
101  }
102  // End program
103  return 0;
104 }