/**
 * This example for the PIC16f628a repeats data read through serial, back
 * at a given baud.  It has been tested on a PIC16f628a operating 
 * at 4MHz with at 9600 baud.  See the PIC16f628a manual for tables of 
 * allowable clock rate and baud rate combinations.
 *
 * NOTE:  The PIC's serial port has the same timings as a normal PC
 * serial port, but the wrong voltages.  A PIC only has 0V and 5V, while
 * a serial port produces, and expects +12V/-12V!  You can't hook it up
 * directly, a translator chip is needed.
 *
 * The MAX232 IC and its clones are among the most popular for this.
 * They have built-in voltage inverters and doublers allowing them to
 * run off the same 5V supply as your PIC, and are really simple to use.
 * See http://burningsmell.org/biollante/max232.gif for an outline.
 */
#define __16f628a
#include "pic14/pic16f628a.h"
#include "tsmserial.h"
#include "tsmcrc16.h"
#include "tsmtypes.h"
#include "tsmasm.h"

#undef BAUD
#define BAUD 19200L

// Set the __CONFIG word:
Uint16 at 0x2007  __CONFIG = CONFIG_WORD;

// These are fixed.  The 16f628a can only use these as transmit and recieve.
#define TX_PORT	2
#define RX_PORT	1
#define TX_BIT	(1<<TX_PORT)
#define RX_BIT	(1<<RX_PORT)

// Resulting CRC from these bytes should be 0x079d
const unsigned char d[8]={0x01, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
crc16 crc;

void main(void)
{
	static unsigned char i;

	TRISB=TX_BIT|RX_BIT;	// These need to be 1 for USART to work

	ASYNC_INIT();

	CRC16_INIT(crc);

	for(i=0; i<sizeof(d); i++)
	{
		CRC16_NEXT(crc,d[i]);
		SENDHEX(d[i]);
	}

	CRC16_FINISH(crc);
	SEND(':');
	SENDHEX(crc.bcclo);
	SENDHEX(crc.bcchi);
	SEND('\r');

	while(1)
	{	}
}

/*#include <stdint.h>
#include <stdio.h>
#include "au_crc16.h"

int main(void)
{
	crc16 crc;
	int n;
	
	CRC16_INIT(crc);

	for(n=0; n<sizeof(data); n++)
		CRC16_NEXT(crc, data[n]);

	CRC16_FINISH(crc);

	printf("C\t%02x%02x\n", crc.bcclo, crc.bcchi);
	printf("W\t%04x\n", htons(crc.word));
}*/

