// *
// *	Serial port driver (uses bit-banging)
// *	for 16Cxx series parts.
// *
// * 	IMPORTANT: Compile this file with FULL optimization
// *	
// *	Copyright (C)1996 HI-TECH Software.
// *	Freely distributable.
// *    Adapted for use with the PIC10F20X by Peter Best

#include	<pic.h>
__CONFIG(MCLRDIS & WDTDIS & UNPROTECT);

//Tunable parameters
//Transmit and Receive port bits  
#define SERIAL_PORT	GPIO
#define SERIAL_TRIS	TRIS
#define	TX_PIN		2	//GP2
#define RX_PIN		3	//GP3	

//Xtal frequency  
#define	XTAL	4000000

//Baud rate	 
#define	BRATE	9600

//Don't change anything else  
#define SCALER			10000000
#define ITIME			4*SCALER/XTAL	// Instruction cycle time  
#if BRATE > 1200
 #define	DLY		3		// cycles per null loop  
 #define	TX_OHEAD 	13		// overhead cycles per loop  
#else
 #define	DLY		9		// cycles per null loop  
 #define TX_OHEAD  		14
#endif
#define	RX_OHEAD		12		// receiver overhead per loop  

#define	DELAY(ohead)		(((SCALER/BRATE)-(ohead*ITIME))/(DLY*ITIME))

static bit	TxData @ (unsigned)&SERIAL_PORT*8+TX_PIN;	// Map TxData to pin  
static bit	RxData @ (unsigned)&SERIAL_PORT*8+RX_PIN;	// Map RxData to pin  
#define	INIT_PORT		SERIAL_TRIS = 1<<RX_PIN		// set up I/O direction  

void putch(char c)
{
 unsigned char	bitno;
 #if BRATE > 1200
  unsigned char	dly;
 #else
  unsigned int	dly;
 #endif

 INIT_PORT;
 TxData = 0;			// start bit  
 bitno = 12;
 do
 {
  dly = DELAY(TX_OHEAD);	// wait one bit time  
   do 				// waiting in delay loop   
    while(--dly);
  if(c & 1)
   TxData = 1;
  if(!(c & 1))
   TxData = 0;
  c = (c >> 1) | 0x80;
 }while(--bitno);
 NOP();
}

char getch(void)
{
 unsigned char	c, bitno;
 #if BRATE > 1200
  unsigned char	dly;
 #else
  unsigned int	dly;
 #endif

  for(;;)
  {
   while(RxData)
    continue;		// wait for start bit  
   dly = DELAY(3)/2;
   do			// waiting in delay loop   
    while(--dly);
   if(RxData)
    continue;		// twas just noise  
   bitno = 8;
   c = 0;
   do
   {
    dly = DELAY(RX_OHEAD);
    do			// waiting in delay loop   
     while(--dly);
    c = (c >> 1) | (RxData << 7);
   }while(--bitno);
   return c;
  }
}

char getche(void)
{
 char c;

 putch(c = getch());
 return c;
}

//*********************************************************************
//*  HI-TECH C SOURCE CODE FOR RS-232 MODULE
//*********************************************************************

const char * string = "NUTS & VOLTS";

void main()
{
 unsigned char x;
 unsigned int y,z;

 FOSC4 = 0;
 CMCON  = 0b11110111;
 OPTION = 0b11001111;
    
 while(1)			//loop forever
 {
  x = 0;			//initialize character index
  do
  {
   putch(string[x++]);		//send character indexed by x: increment x
  }while(string[x]!= 0);	//look for null character at end of string
  putch(0x0D);			//send carriage return
  putch(0x0A);			//send line feed
  for(y=0;y<0xFFFF;++y)		//delay for a while	
   ++z;
 }
}
//*********************************************************************
//*  HOW THE STRING IS STORED IN FLASH
//*********************************************************************
   248                           	psect	strings
   249  018                     u19
   250  018  84E                	retlw	78	;'N'
   251  019  855                	retlw	85	;'U'
   252  01A  854                	retlw	84	;'T'
   253  01B  853                	retlw	83	;'S'
   254  01C  820                	retlw	32
   255  01D  826                	retlw	38	;'&'
   256  01E  820                	retlw	32
   257  01F  856                	retlw	86	;'V'
   258  020  84F                	retlw	79	;'O'
   259  021  84C                	retlw	76	;'L'
   260  022  854                	retlw	84	;'T'
   261  023  853                	retlw	83	;'S'
   262  024  800                	retlw	0
