summaryrefslogtreecommitdiff
path: root/pic/softuart_pic.h
diff options
context:
space:
mode:
authorukw2014-07-21 09:05:03 +0000
committerukw2014-07-21 09:05:03 +0000
commit7fe8188d6871371baf83a08d1ab42c094525c04b (patch)
treebcb9ca7c6706c3e0bd101c530c779f1a9d20c40b /pic/softuart_pic.h
parentc6a60200d5a77ef8011dba4523a92c9f09569e1b (diff)
downloadirmp-7fe8188d6871371baf83a08d1ab42c094525c04b.zip
Version 2.6.2: port to PIC 12F1840
git-svn-id: svn://mikrocontroller.net/irmp@143 aeb2e35e-bfc4-4214-b83c-9e8de998ed28
Diffstat (limited to 'pic/softuart_pic.h')
-rw-r--r--pic/softuart_pic.h151
1 files changed, 151 insertions, 0 deletions
diff --git a/pic/softuart_pic.h b/pic/softuart_pic.h
new file mode 100644
index 0000000..3d40bc8
--- /dev/null
+++ b/pic/softuart_pic.h
@@ -0,0 +1,151 @@
+/*
+ W. Strobl, Bonn, March 2014
+
+Simple minded software bit banging async RS232 implementation for Microchip XC8
+
+http://en.wikipedia.org/wiki/Bit_banging
+
+Tested with 2400, 9600 and 19200 baud on a 4 MHz 16F675 so far
+Tested with 9600 baud on a 32 MHz 12F1840.
+19200 softuard doesn't work.
+
+ PIC12F1840
+
+ ___ __
+ 10k -|___|-+ Vdd -o| o|o- Vss
+ ___ +-RS232in / GP5 -o| |o- GP0 / ICSPDAT
+ 1k -|___|-- RS232out/ GP4 -o| |o- GP1 / ICSPCLK
+ Vpp / GP3 -o|__|o- GP2 /
+
+Necessary definitions (examples)
+
+#define FCY 1000000 // cycles per second (4 MHz PIC)
+#define SOFTUART_RXPIN GPIO5 // if input is desired
+#define SOFTUART_TXPIN GPIO4 // if output is desired
+
+Optional definitions
+
+#define SOFTUART_BAUD 19200 // default: 9600
+#define SOFTUART_STDIO 1 // if definition for getch, putch is desired
+ // default: 0
+#define SOFTUART_DI 1 // if interrupts are to be disabled during IO
+ // default: 0
+#define SOFTUART_MARK 0 // 0: not inverted (default: 1)
+
+
+Typical:
+
+
+#define SOFTUART_RXPIN GPIO5
+#define SOFTUART_TXPIN GPIO4
+#define SOFTUART_STDIO 1
+#define SOFTUART_DI 1
+#include "softuart.h"
+
+#define kbhit softuartkbhit
+
+*/
+
+/******************************************************************************/
+// Software UART
+/******************************************************************************/
+// FCY == instructions per second, see system.h for xc8
+#ifndef SOFTUART_BAUD // default baudrate
+#define SOFTUART_BAUD 9600
+#endif
+#define SOFTUART_BITLEN (FCY/SOFTUART_BAUD)
+#define SOFTUART_DELAY (SOFTUART_BITLEN/5)
+
+#ifndef SOFTUART_MARK
+#define SOFTUART_MARK 1 // 0: not inverted (default: 1)
+#endif
+
+
+// Input Pin defined?
+#ifdef SOFTUART_RXPIN
+/******************************************************************************/
+// Input
+/******************************************************************************/
+
+char softuartgetch(void)
+{
+ char rcvd,i;
+#if SOFTUART_DI
+ di();
+#endif
+ rcvd=0;
+ _delay(SOFTUART_BITLEN/2-10); // wait half a startbit
+ if (SOFTUART_RXPIN != SOFTUART_MARK)
+ {
+#if SOFTUART_DI
+ ei();
+#endif
+ return 0; // glitch
+ }
+ _delay(SOFTUART_BITLEN/2-10); // wait half a startbit
+ for (i=0;i<8;i++)
+ {
+ rcvd >>= 1; // shift previous bits, LSB comes first
+ _delay(SOFTUART_BITLEN/2-12); // ADJUST
+ rcvd |= ((SOFTUART_RXPIN != SOFTUART_MARK)?0x80:0);
+ _delay(SOFTUART_BITLEN/2-10); // ADJUST
+ }
+#ifdef SOFTUART_DI
+ ei();
+#endif
+ _delay(SOFTUART_BITLEN); // stopbit
+ return rcvd;
+}
+
+#define softuartkbhit() (SOFTUART_RXPIN == SOFTUART_MARK)
+
+#endif
+
+// Output Pin defined?
+#ifdef SOFTUART_TXPIN
+/******************************************************************************/
+// Output
+//******************************************************************************/
+
+#if defined(SOFTUART_TXPIN) || defined(SOFTUART_STDIO)
+#define softuartputch putch
+#endif
+
+void softuartputch(char c)
+{
+ char i;
+#ifdef SOFTUART_DI
+ di();
+#endif
+ SOFTUART_TXPIN = SOFTUART_MARK; // startbit
+ _delay(SOFTUART_BITLEN-20); // Adjust Schleifeninit braucht
+ for (i=0;i<8;i++)
+ {
+ SOFTUART_TXPIN = c; // checken, ob da das untere Bit verwendet wird sonst
+#if SOFTUART_MARK
+ SOFTUART_TXPIN = ~(c&1);
+#else
+ SOFTUART_TXPIN = c&1;
+#endif
+ c >>=1;
+ _delay(SOFTUART_BITLEN-20); // Adjust
+ }
+ SOFTUART_TXPIN = ~SOFTUART_MARK;
+ _delay(SOFTUART_BITLEN*2); // two stop bits
+#ifdef SOFTUART_DI
+ ei();
+#endif
+}
+#endif
+
+/******************************************************************************/
+// Utility
+/******************************************************************************/
+#if defined(SOFTUART_RXPIN) || defined(SOFTUART_STDIO)
+// getch with wait
+char getch(void)
+{
+ while (!softuartkbhit()) _delay(1);
+ return softuartgetch();
+}
+#endif