//#pragma CLOCK_FREQ 3579545  // Processor clock frequency in Hz. 
                            // Default value for PIC is 4000000 (4MHz) 
                            // and for SX 50000000 (50MHz).
                            
                            // This will not affect the comms speed !
                            // if you use a different frequency crystal then change SPBRG

#ifdef ICD
// ICD reserved locations

// Also RB6 & RB7 are reserved for the ICD

char RSVRD1@0x70;

char RSVRD2@0x1EB;
char RSVRD3@0x1EC;
char RSVRD4@0x1ED;
char RSVRD5@0x1EE;
char RSVRD6@0x1EF;

// Program locations 0x1F00 - 0x1FFFF are reserved for debugger code
#endif

// My PORT Configuration    

#define PortAConfig 0x01        // RA0 as an Input, RA1 as an output
#define PortBConfig 0xf1        // RB0 as an Input, RB4-7 as inputs
#define PortCConfig 0x99        // SCL & SDA as Inputs
#define PortDConfig 0x01
#define PortEConfig 0x01


// USART Register bits
#define CSCR 7
#define TX9  6
#define TXEN 5
#define SYNC 4
#define BRGH 2
#define TRMT 1
#define TX9D 0
#define SPEN 7
#define RX9  6
#define SREN 5
#define CREN 4
#define ADDEN 3
#define FERR 2
#define OERR 1
#define RX9D 0  
  
#define TRMT_MASK 2

// Masks for INTCON

#define RBIF_MASK       1   //00000001b       // 1
#define INTF_MASK       2   //00000010b       // 2
#define T0IF_MASK       4   //00000100b       // 3
#define TOIF_MASK       4   //00000100b       // Handle misspelling zero with the letter O
#define RBIE_MASK       8   //00001000b       // 4
#define INTE_MASK       16  //00010000b       // 5
#define T0IE_MASK       32  //00100000b       // 6
#define TOIE_MASK       32  //00100000b       // Handle misspelling zero with the letter O
#define PEIE_MASK       64  //01000000b       // 7
#define GIE_MASK        128 //10000000b       // 8

// Masks for PIR1

#define PSPIF_MASK 0x80
#define ADIF_MASK  0x40
#define RCIF_MASK  0x20  
#define TXIF_MASK  0x10


// Registers for I2C

char SSPSTAT@0x94;  // Bank 1
char SSPCON@0x14;   // Bank 0
char SSPCON2@0x91;  // Bank 1
char SSPBUF@0x13;   // I2C Buffer
char SSPADD@0x93;   // I2C Slave Address register


// Bits of SSPSTAT

#define SMP 7
#define CKE 6
#define D_A 5
#define P   4
#define S   3
#define R_W 2
#define R_W_MASK 0x04
#define UA  1
#define BF  0


// Bits of SSPCON2 

#define GCEN    7
#define ACKSTAT 6
#define ACKDT   5
#define ACKEN   4
#define RCEN    3
#define PEN     2
#define RSEN    1
#define SEN     0


// Bits of PIR1

#define PSPIF  7
#define ADIF   6
#define RCIF   5 
#define TXIF   4
#define SSPIF  3
#define SSPIF_MASK 0x08
#define CCP1IF 2
#define TMR2IF 1
#define TMR1IF 0  

// Bits of SSPCON 

#define WCOL  7
#define SSPOV 6
#define SSPEN 5
#define CKP   4
#define SSPM3 3
#define SSPM2 2
#define SSPM1 1
#define SSPM0 0


// Port addresses
char PORTC@0x07;
char PORTD@0x08;
char PORTE@0x09;
 
// USART Registers
char TXREG@0x19;
char RCREG@0x1a;
char TXSTA@0x98;
char RCSTA@0x18;
char SPBRG@0x99;
                  
// Extra Ports on PIC16F877
char TRISC@0x87;
char TRISD@0x88;
char TRISE@0x89;

// Other regs

char PIE1@0x8c;
char PIE2@0x8d;
char PIR1@0x0c;
char PIR2@0x0d;
char PCON@0x8e;

//char TMR0@0x01;                       // Timer 0 register (0x01 & 0x101)

// ADC bits  
char ADCON0@0x1f;
char ADCON1@0x9f;

// Local stuff for my use **************************************************

#define ZC      RB0                     // RB0/INT

// Bit position

#define INTF    1                       // External Interrupt Flag
#define T0IF    2                       // Timer 0 Interrupt Flag

#define INTE    4                       // External Interrupt Enable bit
#define T0IE    5                       // Timer 0 Interrupt Enable bit

#define RCEF    5                       // Receive Char Interrupt Flag

#define Rx      0                       // PortA PIN 0
#define Tx      1                       // PortB PIN 1

#define CR      13                      // Carriage return ^M
#define NL      10                      // New Line        ^J

#define Opt1 0x80
#define Opt2 0xC0

#define BufferReady 0              // Bit 0 of MyFlags
#define BufferReadyMask 0x01
// Function Declarations  

void Setup(void);                  // Setup the PIC
void ConfigureComms(void);         // Configure the comms
void SendChar(char);               // Send a character

char RxChars(void);                // Receive a characters when RX Interrupt occurs

void SendString( const char *ptr );// Send a const string
void buf_char( char c );
void timer0( int i );

// Variables

char Opt;
char zero_bits;                    // 6 continuous 0 1/2 bits =
                                   // end of a 1/2 or whole command
char BufferIndex;                  // Yep! an index to the buffer
char chcount;                      // Current characters avaiable

// Arrays
#define RX_BUFFER_SIZE 20
#define TX_BUFFER_SIZE 10

char RxFifo[RX_BUFFER_SIZE];       // Receive data buffer.
char TxFifo[TX_BUFFER_SIZE];       // Transmit data buffer.

