Spliter_by_Waldo_v1.5_source_codes.zip

Splitter Waldo modyfikacja wsadów

Witam z uwagi na to że z nowymi kartami C+ przestały działać splittery postanowiłem założyć nowy temat. Mianowicie w załacznikach są schematy, wzory płytek, stare wsady (juz nie działające) i co najważniejsze kody źrodłowe w C dla tych wsadów do Slitera by WALDO tzn serwera i klienta. I tutaj moja prośba do bardziej obeznanych w temacie co należałoby zmodyfikowac aby spliter znowu nam zadziałał. Proszę o wypowiedzi co zmienił matrix że stare wsady sa już bezużyteczne i w jaki sposób to poprawić. Najpierw myślałem że sam dam rade coś wykombinowac no ale okazuje sie że jestem zbyt słaby w programowaniu aby się za to zabrać samemu, więc wszystkie sugestie/wskazówki mile widziane. Aha jakby ktoś posiadał źródła wsadów do splitera Bezerkera prosiłbym o podzielenie się nimi. P.S. Myśle że spliter Waldo byłby lepszy ponieważ posiada PIC'a 16F73 (posiada więcej pamięci), można zastosowac dłuższe przewody (max485) oraz posiada 8 wyjść. Pozdrawiam i liczę na pomoc Patol

  • Spliter_by_Waldo_v1.5_source_codes.zip
    • smart_card.h
    • types.h
    • smart_card.c
    • always.h
    • serial.h
    • main.c
    • delay.h
    • tr_485.c
    • serial.c
    • lerrno.h
    • commands.h
    • delay.c
    • main.h
    • tr_485.h


Download file - link to post

Spliter_by_Waldo_v1.5_source_codes.zip > smart_card.h

#ifndef _SMART_CARD_H_
#define _SMART_CARD_H_

/******************************************************************************
* File: smart_card.h
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
//PIC CLK = 20MHz
#define TICKS_CARD_ETU_9600 500
#define TICKS_CARD_STEP 5
#define TICKS_CARD_ETU_MAX 560
#define TICKS_CARD_ETU_MIN 290

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/


/******************************************************************************
* External variable declarations
******************************************************************************/
extern u16 ETU_CARD;

/******************************************************************************
* Function prototypes
******************************************************************************/
void card_init();
u8 card_reset(u8 *ATR);
u8 card_read(u8 * data, u8 * number_bytes);
u8 card_write(u8 * cmd, u8 number_bytes);

/******************************************************************************
* End of file
******************************************************************************/
#endif /* _SMART_CARD_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > types.h

#ifndef _TYPES_H_
#define _TYPES_H_

/******************************************************************************
* File: types.h
* Software:
* Module:
* SubModule:
* Description: Standard types
*
* Created: 2003-03-05
* Author:
* mail:
*
*****************************************************************************/

/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
typedef unsigned char u8;
typedef unsigned char u08;
typedef unsigned short u16;
typedef unsigned long u32;
typedef signed char s8;
typedef signed char s08;
typedef signed short s16;
typedef signed long s32;

#define U16_DEFINED
typedef u8 bitfield;

typedef unsigned char bool;

#ifndef NULL
#define NULL (0L)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif

#define max(val1,val2) (((val1) & gt; (val2)) ? (val1) : (val2))
#define min(val1,val2) (((val1) & lt; (val2)) ? (val1) : (val2))

#define BITFIELD8L(start_bit,value) ((value) & lt; & lt; (start_bit))
#define ONEBIT8L(start_bit) BITFIELD8L(start_bit,1)


/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/
/* Definitions for variables with bit and byte access ================= */
typedef struct BYTE_BIT_TAG {
bitfield b0:1;
bitfield b1:1;
bitfield b2:1;
bitfield b3:1;
bitfield b4:1;
bitfield b5:1;
bitfield b6:1;
bitfield b7:1;
}TYPE_U8_BOOL;

typedef struct WORD_BIT_TAG {
bitfield b0:1;
bitfield b1:1;
bitfield b2:1;
bitfield b3:1;
bitfield b4:1;
bitfield b5:1;
bitfield b6:1;
bitfield b7:1;
bitfield b8:1;
bitfield b9:1;
bitfield b10:1;
bitfield b11:1;
bitfield b12:1;
bitfield b13:1;
bitfield b14:1;
bitfield b15:1;
}TYPE_U16_BOOL;


/******************************************************************************
* External variable declarations
******************************************************************************/


/******************************************************************************
* Function prototypes
******************************************************************************/
// Bit manipulation macros
#define SetBit8(u8ptr,bit) ((((TYPE_U8_BOOL*)u8ptr)- & gt; b##bit) = 1)
#define ClrBit8(u8ptr,bit) ((((TYPE_U8_BOOL*)u8ptr)- & gt; b##bit) = 0)
#define SetBit16(u16ptr,bit) ((((TYPE_U16_BOOL*)u16ptr)- & gt; b##bit) = 1)
#define ClrBit16(u16ptr,bit) ((((TYPE_U16_BOOL*)u16ptr)- & gt; b##bit( = 0)


/******************************************************************************
* End of file
******************************************************************************/
#endif /* _TYPES_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > smart_card.c

/******************************************************************************
* File: smart_card.c
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#include & lt; pic.h & gt;
#include " types.h "
#include " main.h "
#include " delay.h "
#include " lerrno.h "
#include " smart_card.h "

//lines
#define CARD_RST RA1
#define CARD_RST_N ONEBIT8L(1)
#define CARD_RST_REG TRISA

#define CARD_IO RA0
#define CARD_IO_N ONEBIT8L(0)
#define CARD_IO_REG TRISA

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* Global variables
******************************************************************************/
u16 ETU_CARD;

/******************************************************************************
* Local variables
******************************************************************************/


/******************************************************************************
* Local functions
******************************************************************************/


/*.****************************************************************************
*. Function: card_init
*. Description: Initialization
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void card_init()
{
//data line as input
CARD_IO_REG = CARD_IO_REG | CARD_IO_N;

//data line as output
CARD_RST_REG = CARD_RST_REG & ~CARD_RST_N;

CARD_RST = 0;
ETU_CARD = TICKS_CARD_ETU_9600;
}

/*.****************************************************************************
*. Function: read_byte
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
u8 read_byte(u8 *data, u8 timeout)
{
u8 i;
u16 temp;
u8 parity = 0;
u8 data_temp;
u8 data_end;

CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input
if(timeout & gt; 0)
{
temp = timeout;
}
else
{
temp = 0x0FFF;
}
SET_ETU(ETU_CARD); // baud rate
SET_TMR1(ETU);
CLR_FLAG(ETU_flag);
TMR1_ON();

while(1 == CARD_IO) //wait for start bit
{
while(1 == CARD_IO) // check port two times. check - & gt; delay 10usec - & gt; check again
{ // eliminate faulse data
if(1 == ETU_flag)
{
CLR_FLAG(ETU_flag);
temp--;
}
if(0 == temp)
{
TMR1_OFF();
return ETIMEOUT;
}
}
SET_TMR1(ETU);
DelayUs(10);
}
CLR_FLAG(ETU_flag);
data_end = 0;
for(i = 0; i & lt; 8; i++)
{
WAIT_FLAG(ETU_flag);
data_temp = CARD_IO;
data_end = data_end & gt; & gt; 1;
data_end = data_end | (data_temp & lt; & lt; 7);
parity = parity + data_temp;
}
WAIT_FLAG(ETU_flag);
data_temp = CARD_IO;
if((parity % 2) != data_temp)
{
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR1_OFF();
return EPARITY;
}
*data = data_end;
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR1_OFF();
return EOK;
}

/*.****************************************************************************
*. Function: write_byte
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void write_byte(u8 data)
{
u8 i;
u8 parity = 0;
u8 data_temp;

CARD_IO = 1;
CARD_IO_REG = CARD_IO_REG & ~CARD_IO_N; // output
SET_ETU(ETU_CARD); // baud rate 9600
SET_TMR1(ETU);
CLR_FLAG(ETU_flag);
TMR1_ON();
WAIT_FLAG(ETU_flag);
CARD_IO = 0; // start bit
for(i = 0; i & lt; 8; i++)
{
data_temp = (data & 0x01);
WAIT_FLAG(ETU_flag);
CARD_IO = data_temp;
parity = parity + data_temp;
data = data & gt; & gt; 1;
}
data_temp = (parity % 2);
WAIT_FLAG(ETU_flag);
CARD_IO = data_temp;
WAIT_FLAG(ETU_flag);
CARD_IO = 1;
CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR1_OFF();
}


/*.****************************************************************************
*. Function: card_reset
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
u8 card_reset(u8 *ATR)
{
u8 i;
u8 temp = 0;

CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input

CARD_RST = 0;
DelayMs(1);
CARD_RST = 1;

for(i = 0; i & lt; 16; i++)
{
temp = read_byte( & ATR[i], 0);
if(temp != 0)
{
return temp; // NOT OK
}
}
return EOK;
}

/*.****************************************************************************
*. Function: card_read
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
u8 card_read(u8 *data, u8 *number_bytes)
{
u8 i;
u8 temp, number_bytes_local;
u8 number_bytes_temp;

number_bytes_temp = *number_bytes;
if(number_bytes_temp & gt; MAX_BUFFER_SIZE)
{
return EFULL_BUFF;
}

number_bytes_local = 0;
temp = read_byte( & data[0], 0);
if(ETIMEOUT == temp)
{
return ELONG_TIMEOUT;
}
else if(0 != temp)
{
return temp; // parity
}

number_bytes_local++;
for(i = 1; i & lt; number_bytes_temp; i++)
{
temp = read_byte( & data[i], 0);
number_bytes_local++;
if(temp != EOK)
{
number_bytes_local--;
*number_bytes = number_bytes_local;
return temp;
}
}
return temp;
}

/*.****************************************************************************
*. Function: card_write
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
u8 card_write(u8 *cmd, u8 number_bytes)
{
u8 i;

for(i = 0; i & lt; number_bytes; i++)
{
write_byte(*(cmd + i));
}
return EOK;
}


Spliter_by_Waldo_v1.5_source_codes.zip > always.h

/*

Servo PIC

Compiled with Hitech-C v8.00

Usage: #include in all " .c " files in project along with " pic.h "

*/

//warning about #if statements: if any of the arguments are spelled wrong or
//unrecognised by the compiler, it will not generate a warning, but not include code

#ifndef ALWAYS_H
#define ALWAYS_H

#define us_to_ticks(x) (x*(PIC_CLK/4)/(TMR1_PRESCALE)) //converting microseconds to tmr1 clock ticks
#define ticks_to_us(x) ((x*4/PIC_CLK)*(TMR1_PRESCALE)) //converting tmr1 clock ticks to microseconds

#define hibyte(x) ((unsigned char)(x & gt; & gt; 8))
#define lobyte(x) ((unsigned char)(x & 0xff))


/*
given variable of any type (char, uchar, int, uint, long) it modifies
the unsigned char residing at that memory location
for ints, byte1 is msb, byte0 is lsb
for longs byte3 is msb, byte0 is lsb

ie: sample C code

unsigned int myint=0x4321;
long mylong=0x87654321;

//for myint byte1(myint)=0x43; (msb) and byte0(myint)=0x21; (lsb)
//for mylong byte3(mylong)=0x87; (msb), byte2(mylong)=0x65;
byte2(mylong)=0x43; and byte0(mylong)=0x21; (lsb)

note: to avoid fixup overflow errors add bankX if the target variable
resides in banks 1, 2 or 3
*/

//#define byte0(x) (unsigned char)(*(((unsigned char *) & x)+0))
//#define byte1(x) (unsigned char)(*(((unsigned char *) & x)+1))
//#define byte2(x) (unsigned char)(*(((unsigned char *) & x)+2))
//#define byte3(x) (unsigned char)(*(((unsigned char *) & x)+3))

#define byte0_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+0))
#define byte1_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+1))
#define byte2_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+2))
#define byte3_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+3))

#define byte0_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+0))
#define byte1_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+1))
#define byte2_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+2))
#define byte3_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+3))

#define byte0_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+0))
#define byte1_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+1))
#define byte2_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+2))
#define byte3_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+3))

/*
given variable of any type (char, uchar, int, uint, long) it modifies
the int residing at that memory location
ie: sample C code

unsigned char array[4];
unsigned int test;

uint_atbyteaddr( & array[0])=0x4321; //now array[0- & gt; 3]={0x21,0x43,0,0};
uint_atbyteaddr( & array[0+2])=0x8765; //now array[0- & gt; 3]={0x21,0x43,0x65,0x87};
test=uint_atbyteaddr( & array[0+2]) //now test=0x8765

note: do NOT use & (array[0]+1) to reference the int stored at array[1] as it will
reference the int after array[0] in pointer arithmetic. This will
result with the int at array[2]
instead use & array[0+1] to reference the int at array[1]

note: to avoid fixup overflow errors add bankX if the target variable
resides in banks 1, 2 or 3
*/

#define uint_atbyteaddr(x) (unsigned int)(*(((unsigned int *)x)))
#define uint_atbank1byteaddr(x) (unsigned int)(*(((bank1 unsigned int *)x)))
#define uint_atbank2byteaddr(x) (unsigned int)(*(((bank2 unsigned int *)x)))
#define uint_atbank3byteaddr(x) (unsigned int)(*(((bank3 unsigned int *)x)))

/*
these two structures allow access to 2 byte word, high and low bytes of variable
declaration: union wordtype x;
usage: x.word=0xABCD; x.byte.high=0xAB; x.byte.low=0xCD;
x.part.bit15=1; (msb), x.part.bit0=1; (lsb)
declaration: union chartype x;
usage: x.byte=0xAB;
x.part.bit7=1; (msb), x.part.bit0=1; (lsb)
*/

struct sixteen_bits
{
unsigned char bit0 :1;
unsigned char bit1 :1;
unsigned char bit2 :1;
unsigned char bit3 :1;
unsigned char bit4 :1;
unsigned char bit5 :1;
unsigned char bit6 :1;
unsigned char bit7 :1;
unsigned char bit8 :1;
unsigned char bit9 :1;
unsigned char bit10 :1;
unsigned char bit11 :1;
unsigned char bit12 :1;
unsigned char bit13 :1;
unsigned char bit14 :1;
unsigned char bit15 :1;
};

struct eight_bits
{
unsigned char bit0 :1;
unsigned char bit1 :1;
unsigned char bit2 :1;
unsigned char bit3 :1;
unsigned char bit4 :1;
unsigned char bit5 :1;
unsigned char bit6 :1;
unsigned char bit7 :1;
};

struct two_bytes
{
unsigned char low;
unsigned char high;
};

union wordtype
{
unsigned int word;
struct two_bytes byte;
struct sixteen_bits part;
};

union chartype
{
unsigned char byte;
struct eight_bits part;
};

//defines
#define INPUT 1 //port directions
#define OUTPUT 0
#define TRUE 1
#define FALSE 0
#define HIGH 1
#define LOW 0
#define hi 1
#define lo 0

#define b asm( " nop " ) //convenient point for breakpoint (debugging)
#define l while(1) //loop for ever (debugging)

//see AM576. If interrupt occurs just when gie gets set to zero, it won't be cleared

#define gie_on GIE=1
#define gie_off while(GIE==1) GIE=0

#endif


Spliter_by_Waldo_v1.5_source_codes.zip > serial.h

//*****************
//function prototypes

void serial_setup(void);
void putch(unsigned char c);
unsigned char getch(void);
void putst(register const char * str);
unsigned char usart_timeout(void);
void putchdec(unsigned char c);
void putchhex(unsigned char c);
void putinthex(unsigned int c);

#define putlf putst( " \n " ) //put line feed


Spliter_by_Waldo_v1.5_source_codes.zip > main.c

/******************************************************************************
* File: main.c
* Software:
* Module: main
* Description: Main file.
*
* Created: 2004-04-23
* Author:
* mail:
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#include & lt; pic.h & gt;
#include " delay.h "
#include " types.h "
#include " main.h "
#include " smart_card.h "
#include " tr_485.h "
#include " lerrno.h "
#include " commands.h "

#define SERIAL_DEBUG
#ifdef SERIAL_DEBUG
#include " serial.h "
#endif

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* Global variables
******************************************************************************/
u16 ETU = 0xFFFF; // global ETU for serial transmit
bit ETU_flag;
bool more_data_to_receive = FALSE;

/******************************************************************************
* Local variables
******************************************************************************/


/******************************************************************************
* Local functions
******************************************************************************/
void init(void);

__CONFIG(WDTDIS & PWRTEN & BOREN & UNPROTECT & HS); // 8.00

/*.****************************************************************************
*. Function: main
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void main(void)
{
static bank1 u8 buffor[MAX_BUFFER_SIZE];
enum {WAIT_FOR_CARD, SCAN_PORTS, RECEIVE, WRITE, READ, SEND} main_state = WAIT_FOR_CARD;
u8 number_bytes, i;
u8 count_error; // number of error reading from card
u8 number_receives; // number receive from client to do in one cycle
bool direct_response = FALSE;
u8 temp;

init();
tr_init();
card_init();

TMR1_OFF();

#ifdef SERIAL_DEBUG
serial_setup();
//could use printf() here but putst saves 1k of rom space
putst( " \n\n\nPIC Splitter Card 2004 Waldo\n " );
putst( " Starting up serial @ 19200 baud, N,8,1, no flow control ...\n\n " );
#endif

while(1)
{
switch(main_state)
{
case WAIT_FOR_CARD:
LED_ON();

#ifdef SERIAL_DEBUG
putst( " \nATR: " );
#endif

temp = card_reset(buffor);
if(EOK == temp)
{
#ifdef SERIAL_DEBUG
for(i = 0; i & lt; 16; i++)
{
putchhex(buffor[i]);
putst( " " );
}
#endif

if(0x3B == buffor[0]) //first byte of ATR
{
count_error = 0;
number_receives = 0;
more_data_to_receive = FALSE;
main_state = SCAN_PORTS;
LED_OFF();
}
}
else
{
#ifdef SERIAL_DEBUG
putst( " Error = " );
putchdec(temp);
#endif
if(EPARITY == temp)
{
count_error++;
if(count_error & gt; = 2)
{
count_error = 0;
ETU_CARD = ETU_CARD + TICKS_CARD_STEP;
if(ETU_CARD & gt; TICKS_CARD_ETU_MAX)
{
ETU_CARD = TICKS_CARD_ETU_MIN;
}
#ifdef SERIAL_DEBUG
putst( " \nETU CARD = " );
putchdec(ETU_CARD / 100);
putchdec(ETU_CARD % 100);
#endif
}
}
DelayMs(50);
}
break;

case SCAN_PORTS:
#ifdef SERIAL_DEBUG
putst( " \nClient number = " );
putchdec(client_number);
putst( " \nBAUD RATE TICKS = " );
putchdec(ETU_TR / 100);
putchdec(ETU_TR % 100);
putst( " \nSCAN PORTS " );
#endif
LED_OFF();
if(tr_scan_ports() == EOK)
{
number_receives = 0;
more_data_to_receive = FALSE;
main_state = RECEIVE;
}
LED_ON();
break;

case RECEIVE:
temp = 0; // timeout during receive
if((COMMAND_05 == buffor[0]) || (COMMAND_10 == buffor[0]) || (COMMAND_12 == buffor[0]) || (COMMAND_07 == buffor[0]))
{
number_bytes = buffor[4];
}
else
{
number_bytes = 5; // normal command
}

for(i = 0; i & lt; number_bytes; i++)
{
temp = tr_read_byte( & buffor[i], temp);
if(temp != EOK)
{
#ifdef SERIAL_DEBUG
putst( " \nReceive error = " );
putchdec(temp);
putst( " \ni = " );
putchdec(i);
#endif
temp = 0xAA;
break;
}
temp = 50; // only for first byte long timeout
}
if(0xAA == temp)
{
DelayMs(100);
tr_write_byte(0x67);
tr_write_byte(0x00);
main_state = WAIT_FOR_CARD;
}
else
{
#ifdef SERIAL_DEBUG
putst( " \nReceived Number of bytes : " );
putchhex(number_bytes);
putst( " \nDATA: " );
for(i = 0; i & lt; number_bytes; i++)
{
putchhex(buffor[i]);
putst( " " );
}
#endif
if((0xC1 == buffor[0]) & & (COMMAND_12 == buffor[1])
& & (TRUE == EMM_BLOCK))
{
buffor[0] = COMMAND_12;
main_state = SEND;
number_bytes = 1;
more_data_to_receive = TRUE;
direct_response = TRUE;
#ifdef SERIAL_DEBUG
putst( " \nEMM block is ON " );
#endif
DelayMs(50);
}
else if(TRUE == direct_response)
{
buffor[0] = 0x90;
buffor[1] = 0x24;
main_state = SEND;
number_bytes = 2;
more_data_to_receive = FALSE;
direct_response = FALSE;
DelayMs(50);
}
else
{
main_state = WRITE;
more_data_to_receive = FALSE;
}
}
break;

case WRITE:
card_write(buffor, number_bytes);
main_state = READ;
break;

case READ:
// number bytes to read
if(buffor[0] != 0xC1)
{
number_bytes = 2;
}
else
{
if((COMMAND_05 == buffor[1]) || (COMMAND_10 == buffor[1]))
{
number_receives = 1; // arguments for command and next command_06
more_data_to_receive = TRUE;
number_bytes = 1;
}
else if((COMMAND_12 == buffor[1]) || (COMMAND_07 == buffor[0]))
{
number_receives = 0;
more_data_to_receive = TRUE;
number_bytes = 1;
}
else
{
number_receives = 0;
more_data_to_receive = FALSE;
number_bytes = buffor[4] + 3; // 3 = command + status 90 00
}
}
i = card_read(buffor, & number_bytes);
#ifdef SERIAL_DEBUG
putst( " \nREAD: " );
if(i != EOK)
{
putchdec(i);
DelayMs(100);
tr_write_byte(0x67);
tr_write_byte(0x00);
main_state = WAIT_FOR_CARD;
break;
}
else
{
for(i = 0; i & lt; number_bytes; i++)
{
putchhex(buffor[i]);
putst( " " );
}
}
#endif
main_state = SEND;
break;

case SEND:
for(i = 0; i & lt; number_bytes; i++)
{
tr_write_byte(buffor[i]);
}
if(TRUE == more_data_to_receive)
{
main_state = RECEIVE;
}
else if(number_receives != 0)
{
number_receives--;
main_state = RECEIVE;
}
else
{
main_state = SCAN_PORTS;
}
break;

default:
main_state = WAIT_FOR_CARD;
break;
}
}
}

/*.****************************************************************************
*. Function: init
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void init(void)
{
ADCON1 = 0x06; //portA digital I/O
TRISA = TRISA & ~(ONEBIT8L(5)); // RA5 output - LED
TRISA = TRISA | (ONEBIT8L(4)); // RA4 input - EMM block
RBPU = 1; //pull up resistors for portB OFF
T0CS = 0; //timer0 internal

TMR1ON = 1; //timer1 ON
PEIE = 1; //enable interups from device
TMR1IE = 1; //enable interup from TIMER1
GIE = 1; //enable interupts
}

/*.****************************************************************************
*. Function: interrupt timer_int
*. Description: handle interrupts - should be placed on end of file
*. (better handle context saving)
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void interrupt timer_int() // Here be interrupt function - the name is unimportant.
{
if(TMR1IF)
{
SET_TMR1(ETU);
SET_FLAG(ETU_flag);
TMR1IF = 0; // clear the interrupt flag
}
}


Spliter_by_Waldo_v1.5_source_codes.zip > delay.h

/*

lowlevel delay routines

For Microchip PIC and Hi-Tech C

Example C:

#define PIC_CLK 8000000

#include " delay.h "

unsigned int timeout_int, timeout_char;

timeout_char=timeout_char_us(1147);
while(timeout_char-- & & (RA1==0)); //wait up to 1147us for port RA1 to go high
// - this is the max timeout

timeout_int=timeout_int_us(491512);
while(timeout_int-- & & (RA1==0)); //wait up to 491512us for port RA1 to go high
// - this is the max timeout

dly250n; //delay 250ns
dly1u; //delay 1us
DelayUs(40); //do not do DelayUs(0) or else it bombs :)
DelayUs(255); //max

*/

#ifndef __DELAY_H
#define __DELAY_H

#define PIC_CLK 20000000

extern unsigned char delayus_variable;
/*void DelayBigUs(unsigned int cnt);*/
void DelayMs(unsigned char cnt);
/*void DelayBigMs(unsigned int cnt);*/


#if (PIC_CLK == 4000000) || (PIC_CLK == 3686400) || (PIC_CLK == 3579000) //3686400 is not entirely accurate, but it works
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n please remove; for 8Mhz+ only
#define dly1u asm( " nop " )
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 8000000)
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n asm( " nop " )
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif ( (PIC_CLK == 16000000) || (PIC_CLK == 16257000) )
#define dly125n please remove; for 32Mhz+ only
#define dly250n asm( " nop " )
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 20000000)
#define dly200n asm( " nop " )
#define dly400n dly250n;dly250n
#define dly2u dly400n;dly400n;dly400n;dly400n;dly400n
#elif (PIC_CLK == 32000000)
#define dly125n asm( " nop " )
#define dly250n dly125n;dly125n
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#else
#error please define pic_clk correctly
#endif

//*****
//delay routine

#if PIC_CLK == 4000000 || (PIC_CLK == 3686400) || (PIC_CLK == 3579000) //3686400 is not entirely accurate, but it works
#define DelayDivisor 4
#define WaitFor1Us asm( " nop " )
#define Jumpback asm( " goto $ - 2 " )
#elif PIC_CLK == 8000000
#define DelayDivisor 2
#define WaitFor1Us asm( " nop " )
#define Jumpback asm( " goto $ - 2 " )
#elif ( (PIC_CLK == 16000000) || (PIC_CLK==16257000) )
#define DelayDivisor 1
#define WaitFor1Us asm( " nop " )
#define Jumpback asm( " goto $ - 2 " )
#elif PIC_CLK == 20000000
#define DelayDivisor 1
#define WaitFor1Us asm( " nop " ); asm( " nop " ); asm( " nop " ); asm( " nop " ); asm( " nop " )
#define Jumpback asm( " goto $ - 6 " )
#elif PIC_CLK == 32000000
#define DelayDivisor 1
#define WaitFor1Us asm( " nop " ); asm( " nop " ); asm( " nop " ); asm( " nop " ); asm( " nop " )
#define Jumpback asm( " goto $ - 6 " )
#else
#error please define pic_clk correctly
#endif

#define DelayUs(x) { \
delayus_variable=(unsigned char)((x)/DelayDivisor); \
WaitFor1Us; } \
asm( " decfsz _delayus_variable,f " ); \
Jumpback;

/*

timeouts:

C code for testing with ints:

unsigned int timeout;
timeout=4000;
PORT_DIRECTION=OUTPUT;
while(1)
{
PORT=1;
timeout=8000;
while(timeout-- & gt; = 1); //60ms @ 8Mhz, opt on, 72ms @ 8Mhz, opt off
PORT=0;
}

Time taken: optimisations on: 16cyc/number loop, 8us @ 8Mhz
optimisations off: 18cyc/number loop, 9us @ 8Mhz
with extra check ie: & & (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz

C code for testing with chars:

similar to above

Time taken: optimisations on: 9cyc/number loop, 4.5us @ 8Mhz
with extra check ie: & & (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz

Formula: rough timeout value = ( & lt; us desired & gt; / & lt; cycles per loop & gt; ) * (PIC_CLK/4.0)

To use: //for max timeout of 1147us @ 8Mhz
#define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on
#define timeout_char_us(x) (unsigned char)((x/LOOP_CYCLES_CHAR)*(PIC_CLK/4.0))
unsigned char timeout;
timeout=timeout_char_us(1147); //max timeout allowed @ 8Mhz, 573us @ 16Mhz
while((timeout-- & gt; = 1) & & ( & lt; extra condition & gt; )); //wait

To use: //for max 491512us, half sec timeout @ 8Mhz
#define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on
#define timeout_int_us(x) (unsigned int)((x+/LOOP_CYCLES_INT)*(PIC_CLK/4.0))
unsigned int timeout;
timeout=timeout_int_us(491512); //max timeout allowed @ 8Mhz
while((timeout-- & gt; = 1) & & ( & lt; extra condition & gt; )); //wait
*/
#define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on
#define timeout_char_us(x) (unsigned char)((x/LOOP_CYCLES_CHAR)*(PIC_CLK/4000000.0))

#define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on
#define timeout_int_us(x) (unsigned int)((x/LOOP_CYCLES_INT)*(PIC_CLK/4000000.0))

#endif


Spliter_by_Waldo_v1.5_source_codes.zip > tr_485.c

/******************************************************************************
* File: tr_485.c
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#include & lt; pic.h & gt;
#include & lt; stdlib.h & gt;
#include " types.h "
#include " main.h "
#include " delay.h "
#include " lerrno.h "

#define TRAN_IO_PORT PORTB
#define TRAN_IO RB0
#define TRAN_IO_N ONEBIT8L(0)
#define TRAN_IO_REG TRISB

#define TRAN_ST_PORT PORTC
#define TRAN_ST RC0
#define TRAN_ST_N ONEBIT8L(0)
#define TRAN_ST_REG TRISC

#define TRAN_SET_RECEIVE(number) TRAN_IO_REG = (TRAN_IO_REG | (ONEBIT8L(number)));TRAN_ST_PORT = (TRAN_ST_PORT & ~(ONEBIT8L(number)))
#define TRAN_SET_TRANSMIT(number) TRAN_ST_PORT = (TRAN_ST_PORT | (ONEBIT8L(number)));TRAN_IO_REG = (TRAN_IO_REG & ~(ONEBIT8L(number)))

#define TRAN_READ(number) ((TRAN_IO_PORT & ONEBIT8L(number)) ? 1 : 0)
#define TRAN_WRITE(number, data) if(1 == (data)){TRAN_IO_PORT = TRAN_IO_PORT | (ONEBIT8L(number));}else{TRAN_IO_PORT = TRAN_IO_PORT & ~(ONEBIT8L(number));}

#define MAX_NUMBER_CLIENTS 4
#define LOCAL_TIMEOUT1 500

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* Global variables
******************************************************************************/
u16 ETU_TR;
u8 client_number;

/******************************************************************************
* Local variables
******************************************************************************/
u16 ETU_client[MAX_NUMBER_CLIENTS];

/******************************************************************************
* Local functions
******************************************************************************/


/*.****************************************************************************
*. Function: tr_init
*. Description: Initialization
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void tr_init()
{
u8 i;

//485 transciver control pins as output
TRAN_ST_REG = 0x00;
TRAN_SET_RECEIVE(0);
TRAN_SET_RECEIVE(1);
TRAN_SET_RECEIVE(2);
TRAN_SET_RECEIVE(3);
TRAN_SET_RECEIVE(4);
TRAN_SET_RECEIVE(5);
TRAN_SET_RECEIVE(6);
TRAN_SET_RECEIVE(7);

for(i = 0; i & lt; MAX_NUMBER_CLIENTS; i++)
{
ETU_client[i] = 0;
}

}

/*.****************************************************************************
*. Function: tr_read_byte
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
u8 tr_read_byte(u8 *data, u8 timeout)
{
u8 i;
u16 temp;
u8 parity = 0;
u8 temp2;
u8 count_parity = 0;
u8 data_temp;

while(0 == parity)
{
TRAN_SET_RECEIVE(client_number);
if(timeout & gt; 0)
{
temp = timeout;
}
else
{
temp = 0x0FFF;
}
SET_ETU((ETU_TR * 3) / 5);
TMR1_ON();
while(0 == TRAN_READ(client_number)) //wait for start bit
{
while(0 == TRAN_READ(client_number)) // check port two times. check - & gt; delay 10usec - & gt; check again
{ // eliminate faulse data
if(1 == ETU_flag)
{
CLR_FLAG(ETU_flag);
temp--;
}
if(0 == temp)
{
TMR1_OFF();
return ETIMEOUT;
}
}
SET_TMR1(ETU);
DelayUs(10);
}
CLR_FLAG(ETU_flag);
data_temp = 0;
for(i = 0; i & lt; 8; i++)
{
data_temp = data_temp & gt; & gt; 1;
temp = 0x0FFF;
WAIT_FLAG(ETU_flag);
temp2 = TRAN_READ(client_number);
TMR1_OFF();
SET_TMR1(ETU);
while(temp2 == TRAN_READ(client_number))
{
temp--;
if(0 == temp)
{
return ETIMEOUT;
}
}
TMR1_ON();
parity = parity + temp2;
if(1 == temp2)
{
temp2 = 0x80;
}
else
{
temp2 = 0x00;
}
data_temp = data_temp | temp2;
}
temp = 0x0FFF;
WAIT_FLAG(ETU_flag);
temp2 = TRAN_READ(client_number);
TMR1_OFF();
while(temp2 == TRAN_READ(client_number))
{
temp--;
if(0 == temp)
{
return ETIMEOUT;
}
}
SET_ETU(ETU_TR);
TMR1_ON();
if(FALSE == more_data_to_receive)
{
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
if((parity % 2) != temp2)
{
//parity error
count_parity++;
if(count_parity & gt; 2)
{
parity = 1;
}
else
{
TRAN_SET_TRANSMIT(client_number);
TRAN_WRITE(client_number, 1);
parity = 0;// 0
}
}
else
{
parity = 1;
}
}
else
{
parity = 1;
}
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR1_OFF();
}
*data = data_temp;
return EOK;
}

/*.****************************************************************************
*. Function: tr_write_byte
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void tr_write_byte(u8 data)
{
u8 i;
u8 parity = 0;
u16 temp;
u8 count_parity = 0;
u8 data_temp;
u8 data_backup;

data_backup = data;
temp = 0x0FFF;
TRAN_SET_RECEIVE(client_number);
SET_ETU(ETU_TR / 2);
TMR1_ON();
CLR_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
while(1 == TRAN_READ(client_number)) //wait for ACK
{
if(1 == ETU_flag)
{
CLR_FLAG(ETU_flag);
temp--;
}
if(0 == temp)
{
break;
}
}
TMR1_OFF();

while(0 == parity)
{
data = data_backup;
TRAN_WRITE(client_number, 0);
TRAN_SET_TRANSMIT(client_number);
SET_TMR1(ETU);
TMR1_ON();
CLR_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TRAN_WRITE(client_number, 1); // start bit
for(i = 0; i & lt; 8; i++)
{
data_temp = (data & 0x01);
data = data & gt; & gt; 1;
WAIT_FLAG(ETU_flag);
TRAN_WRITE(client_number, data_temp);
parity = parity + data_temp;
data_temp = ((~data_temp) & 0x01);
WAIT_FLAG(ETU_flag);
TRAN_WRITE(client_number, data_temp);
}
data_temp = (parity % 2);
WAIT_FLAG(ETU_flag);
TRAN_WRITE(client_number, data_temp);
data_temp = ((~data_temp) & 0x01);
WAIT_FLAG(ETU_flag);
TRAN_WRITE(client_number, data_temp);
WAIT_FLAG(ETU_flag);
TRAN_WRITE(client_number, 0);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TRAN_SET_RECEIVE(client_number);
if(1 == TRAN_READ(client_number))
{
parity = 0;
count_parity++;
if(count_parity & gt; 5)
{
parity = 1;
}
}
else
{
parity = 1;
}
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR1_OFF();
}
}


/*.****************************************************************************
*. Function: tr_scan_ports
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
u8 tr_scan_ports()
{
u16 temp;

TRAN_SET_RECEIVE(0);
TRAN_SET_RECEIVE(1);
TRAN_SET_RECEIVE(2);
TRAN_SET_RECEIVE(3);
TRAN_SET_RECEIVE(4);
TRAN_SET_RECEIVE(5);
TRAN_SET_RECEIVE(6);
TRAN_SET_RECEIVE(7);

while(1)
{
client_number = client_number++;
if(client_number & gt; = MAX_NUMBER_CLIENTS)
{
client_number = 0;
}
if(1 == TRAN_READ(client_number))
{
break;
}
}

// --------- time out 300 usec - & gt; temp = 1500;
temp = LOCAL_TIMEOUT1;
while(1 == TRAN_READ(client_number))
{
temp--;
if(0 == temp)
{
return ETIMEOUT;
}
}
// check client's baud rate
TMR1L = 0;
TMR1H = 0;
TMR1_ON();
temp = LOCAL_TIMEOUT1;
while(0 == TRAN_READ(client_number))
{
temp--;
if(0 == temp)
{
return ETIMEOUT;
}
}
temp = LOCAL_TIMEOUT1;
while(1 == TRAN_READ(client_number))
{
temp--;
if(0 == temp)
{
return ETIMEOUT;
}
}
temp = LOCAL_TIMEOUT1;
while(0 == TRAN_READ(client_number))
{
temp--;
if(0 == temp)
{
return ETIMEOUT;
}
}
temp = LOCAL_TIMEOUT1;
while(1 == TRAN_READ(client_number))
{
temp--;
if(0 == temp)
{
return ETIMEOUT;
}
}
TMR1_OFF();
// end baud rate check
ETU_TR = (((u16)(TMR1H & lt; & lt; 8) | (u16)(TMR1L)) / 4);

TRAN_WRITE(client_number, 1); // ACK bit
TRAN_SET_TRANSMIT(client_number);

if((ETU_TR & gt; 400) & & (ETU_TR & lt; 700))
{
if(((float)ETU_TR & lt; ((float)ETU_client[client_number] * (float)1.2)) & & ((float)ETU_TR & gt; ((float)ETU_client[client_number] * (float)0.8)))
{
ETU_client[client_number] = ((ETU_client[client_number] * 3) + ETU_TR) / 4;
}
else
{
ETU_client[client_number] = ETU_TR;
}
}

if(0 == ETU_client[client_number])
{
ETU_client[client_number] = ETU_TR;
}
else
{
ETU_TR = ETU_client[client_number];
}

TMR1_ON();
SET_ETU(ETU_TR);
SET_TMR1(ETU);
CLR_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR1_OFF();
TRAN_WRITE(client_number, 0);
TRAN_SET_RECEIVE(client_number);
return EOK;
}


Spliter_by_Waldo_v1.5_source_codes.zip > serial.c

/*

Interface PIC

Overall goal: serial comms using USART to comm port of an ibm pc compatible computer

*/

#include & lt; pic.h & gt;
#include & lt; conio.h & gt;
#include & lt; stdio.h & gt;
#include " always.h "
#include " delay.h "

void serial_setup(void)
{
/* relates crystal freq to baud rate - see above and PIC16F87x data sheet under 'USART async. modes'

BRGH=1, Fosc=3.6864MHz BRGH=1, Fosc=4MHz BRGH=1, Fosc=8MHz BRGH=1, Fosc=16MHz
---------------------- ----------------- ----------------- ------------------
Baud SPBRG Baud SPBRG Baud SPBRG Baud SPBRG
1200 191 1200 207.3 1200 415.7 9600 103
2400 95 2400 103.2 2400 207.3 19200 51
4800 47 4800 51.1 4800 103.2 38400 25
9600 23 9600 25.0 9600 51.1 57600 16
19200 11 19200 12.0 19200 25.0 115200 8
38400 5 38400 5.5 38400 12.0
57600 3 57600 3.3 57600 7.7
115200 1 115200 1.2 115200 3.3

*/

/*
* Comms setup:
*/

#define BAUD 19200
#define HIGH_SPEED 1
#if PIC_CLK==3579000 & & BAUD==19200
#define DIVIDER 11
#else
#define DIVIDER ((PIC_CLK/(16UL * BAUD) -1))
#endif

//you can comment these #assert statements out if you dont want error checking
#if PIC_CLK==3579000 & & BAUD==19200
#assert DIVIDER==11
#elif PIC_CLK==3686400 & & BAUD==19200
#assert DIVIDER==11
#elif PIC_CLK==4000000 & & BAUD==19200
#assert DIVIDER==12
#elif PIC_CLK==16000000 & & BAUD==19200
#assert DIVIDER==51
#elif PIC_CLK==20000000 & & BAUD==19200
#assert DIVIDER==64
#endif

SPBRG=DIVIDER;
BRGH=HIGH_SPEED; //data rate for sending
SYNC=0; //asynchronous
SPEN=1; //enable serial port pins
CREN=1; //enable reception
SREN=0; //no effect
TXIE=0; //disable tx interrupts
RCIE=0; //disable rx interrupts
TX9=0; //8-bit transmission
RX9=0; //8-bit reception
TXEN=0; //reset transmitter
TXEN=1; //enable the transmitter
}

unsigned char dummy;

#define clear_usart_errors_inline \
if (OERR) \
{ \
TXEN=0; \
TXEN=1; \
CREN=0; \
CREN=1; \
} \
if (FERR) \
{ \
dummy=RCREG; \
TXEN=0; \
TXEN=1; \
}

//writes a character to the serial port
void putch(unsigned char c)
{
while(!TXIF) //set when register is empty
{
clear_usart_errors_inline;
CLRWDT();
}
TXREG=c;
DelayUs(250);
DelayUs(250);
}

//gets a character from the serial port without timeout
unsigned char getch(void)
{
while(!RCIF)
{
CLRWDT();
clear_usart_errors_inline;
}
return RCREG;
}

unsigned char getch_timeout(void)
{
unsigned char i;
unsigned int timeout_int;

// retrieve one byte with a timeout
for (i=2;i!=0;i--)
{
timeout_int=timeout_int_us(240000);

while (hibyte(timeout_int)!=0) //only check the msb of the int for being 0, it saves space, see always.h for macro
{
CLRWDT();
timeout_int--;
if (RCIF)
{
return RCREG;
}
}
}
return 0;
}


void clear_usart_errors(void)
{
clear_usart_errors_inline;
}



/*
writes a character to the serial port in hex
if serial lines are disconnected, there are no errors
*/

void putchhex(unsigned char c)
{
unsigned char temp;

// transmits in hex

temp=c;

c=(c & gt; & gt; 4);
if (c & lt; 10) c+=48; else c+=55;
putch(c);

c=temp;

c=(c & 0x0F);
if (c & lt; 10) c+=48; else c+=55;
putch(c);
}

void putinthex(unsigned int c)
{
#define ramuint(x) (*((unsigned int *) (x)))
#define ramuint_hibyte(x) (*(((unsigned char *) & x)+1))
#define ramuint_lobyte(x) (*(((unsigned char *) & x)+0))
#define ramuchar(x) (*((unsigned char *) (x)))

putchhex(ramuint_hibyte(c));
putchhex(ramuint_lobyte(c));

#undef ramuint(x)
#undef ramuint_hibyte(x)
#undef ramuint_lobyte(x)
#undef ramuchar(x)
}

//if there has been a previous timeout error from getch_timeout, this returns TRUE
unsigned char usart_timeout(void)
{
// return usart_timeout_error;
return FALSE;
}

/*
writes a character to the serial port in decimal
if serial lines are disconnected, there are no errors
*/
void putchdec(unsigned char c)
{
unsigned char temp;

temp=c;
//hundreds
if ((c/100) & gt; 0) putch((c/100)+'0');
c-=(c/100)*100;

//tens
if (((temp/10) & gt; 0) || ((temp/100) & gt; 0)) putch((c/10)+'0');
c-=(c/10)*10;

//ones
putch((c/1)+'0');
}

void putst(register const char *str)
{
while((*str)!=0)
{
putch(*str);
if (*str==13) putch(10);
if (*str==10) putch(13);
str++;
}
}


Spliter_by_Waldo_v1.5_source_codes.zip > lerrno.h

#ifndef _LERRNO_H_
#define _LERRNO_H_

/******************************************************************************
* File: LERRNO.h
* Software:
* Module: main
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#define EOK 0
#define EPARITY 1
#define ETIMEOUT 2
#define ELONG_TIMEOUT 3
#define EFULL_BUFF 4

/******************************************************************************
* End of file
******************************************************************************/
#endif /* _LERRNO_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > commands.h

#ifndef _COMMANDS_H_
#define _COMMANDS_H_

/******************************************************************************
* File: commands.h
* Software:
* Module: main
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
// commands for SECA
#define COMMAND_01 0x0A
#define COMMAND_02 0x0E
#define COMMAND_03 0x12
#define COMMAND_04 0x16
#define COMMAND_05 0x34
#define COMMAND_06 0x32
#define COMMAND_07 0x30
#define COMMAND_08 0x1A
#define COMMAND_09 0x5A
#define COMMAND_10 0x3C
#define COMMAND_11 0x3A
#define COMMAND_12 0x40

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* External variable declarations
******************************************************************************/

/******************************************************************************
* Function prototypes
******************************************************************************/

/******************************************************************************
* End of file
******************************************************************************/
#endif /* _COMMAND_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > delay.c

#ifndef __DELAY_C
#define __DELAY_C

#include & lt; pic.h & gt;
#include " always.h "
#include " delay.h "

unsigned char delayus_variable;
/*
void DelayBigUs(unsigned int cnt)
{
unsigned char i;

i = (unsigned char)(cnt & gt; & gt; 8);
while(i & gt; =1)
{
i--;
DelayUs(253);
CLRWDT();
}
DelayUs((unsigned char)(cnt & 0xFF));
}*/


void DelayMs(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
/*
//this copy is for the interrupt function
void DelayMs_interrupt(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
} while(--i);
} while(--cnt);
}
*/
/*
void DelayBigMs(unsigned int cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}*/

/*
void DelayS(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayMs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
*/

#endif


Spliter_by_Waldo_v1.5_source_codes.zip > main.h

#ifndef _MAIN_H_
#define _MAIN_H_

/******************************************************************************
* File: main.h
* Software:
* Module: main
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#define MAX_BUFFER_SIZE 95

#define SET_FLAG(data) (data = 1)
#define CLR_FLAG(data) (data = 0)
#define WAIT_FLAG(data) while(!(data)){};(data = 0)
#define SET_TMR1(data) TMR1H = (u8)((0xFFFF - (data)) & gt; & gt; 8); TMR1L = (u8)(0xFFFF - (data))
#define SET_ETU(data) ETU = (data) - 45

#define TMR1_ON() TMR1ON = 1
#define TMR1_OFF() TMR1ON = 0

#define LED_ON() RA5 = 0
#define LED_OFF() RA5 = 1

#define EMM_BLOCK ((RA4 == 0) ? TRUE : FALSE)

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/


/******************************************************************************
* External variable declarations
******************************************************************************/
extern u16 ETU;
extern bit ETU_flag;
extern bool more_data_to_receive;

/******************************************************************************
* Function prototypes
******************************************************************************/


/******************************************************************************
* End of file
******************************************************************************/
#endif /* _MAIN_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > tr_485.h

#ifndef _TR_485_H_
#define _TR_485_H_

/******************************************************************************
* File: tr_485.h
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/


/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/


/******************************************************************************
* External variable declarations
******************************************************************************/
extern u16 ETU_TR;
extern u8 client_number;

/******************************************************************************
* Function prototypes
******************************************************************************/
void tr_init();
u8 tr_scan_ports();
u8 tr_read_byte(u8 * data, u8 timeout);
void tr_write_byte(u8 data);

/******************************************************************************
* End of file
******************************************************************************/
#endif /* _TR_485_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > smart_card.h

#ifndef _SMART_CARD_H_
#define _SMART_CARD_H_

/******************************************************************************
* File: smart_card.h
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#define CARD_IO RB7
#define CARD_IO_N ONEBIT8L(7)
#define CARD_IO_REG TRISB

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/


/******************************************************************************
* External variable declarations
******************************************************************************/


/******************************************************************************
* Function prototypes
******************************************************************************/
void card_init();
void send_ATR();
void read_byte(u8 * data);
void write_byte(u8 data);

/******************************************************************************
* End of file
******************************************************************************/
#endif /* _SMART_CARD_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > types.h

#ifndef _TYPES_H_
#define _TYPES_H_

/******************************************************************************
* File: types.h
* Software:
* Module:
* SubModule: Main
* Description: Standard types
*
* Created: 2003-03-05
* Author:
* mail:
*
*****************************************************************************/

/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
typedef unsigned char u8;
typedef unsigned char u08;
typedef unsigned short u16;
typedef unsigned long u32;
typedef signed char s8;
typedef signed char s08;
typedef signed short s16;
typedef signed long s32;

#define U16_DEFINED
typedef u8 bitfield;

typedef unsigned char bool;

#ifndef NULL
#define NULL (0L)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif

#define max(val1,val2) (((val1) & gt; (val2)) ? (val1) : (val2))
#define min(val1,val2) (((val1) & lt; (val2)) ? (val1) : (val2))

#define BITFIELD8L(start_bit,value) ((value) & lt; & lt; (start_bit))
#define ONEBIT8L(start_bit) BITFIELD8L(start_bit,1)


/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/
/* Definitions for variables with bit and byte access ================= */
typedef struct BYTE_BIT_TAG {
bitfield b0:1;
bitfield b1:1;
bitfield b2:1;
bitfield b3:1;
bitfield b4:1;
bitfield b5:1;
bitfield b6:1;
bitfield b7:1;
}TYPE_U8_BOOL;

typedef struct WORD_BIT_TAG {
bitfield b0:1;
bitfield b1:1;
bitfield b2:1;
bitfield b3:1;
bitfield b4:1;
bitfield b5:1;
bitfield b6:1;
bitfield b7:1;
bitfield b8:1;
bitfield b9:1;
bitfield b10:1;
bitfield b11:1;
bitfield b12:1;
bitfield b13:1;
bitfield b14:1;
bitfield b15:1;
}TYPE_U16_BOOL;


/******************************************************************************
* External variable declarations
******************************************************************************/


/******************************************************************************
* Function prototypes
******************************************************************************/
// Bit manipulation macros
#define SetBit8(u8ptr,bit) ((((TYPE_U8_BOOL*)u8ptr)- & gt; b##bit) = 1)
#define ClrBit8(u8ptr,bit) ((((TYPE_U8_BOOL*)u8ptr)- & gt; b##bit) = 0)
#define SetBit16(u16ptr,bit) ((((TYPE_U16_BOOL*)u16ptr)- & gt; b##bit) = 1)
#define ClrBit16(u16ptr,bit) ((((TYPE_U16_BOOL*)u16ptr)- & gt; b##bit( = 0)


/******************************************************************************
* End of file
******************************************************************************/
#endif /* _TYPES_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > smart_card.c

/******************************************************************************
* File: smart_card.c
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#include & lt; pic.h & gt;
#include " types.h "
#include " main.h "
#include " delay.h "
#include " smart_card.h "

const u8 ATR[] = {0x3B, 0xF7, 0x11, 0x00, 0x01, 0x40, 0x96, 0x70, 0x70, 0x07, 0x0E, 0x6C, 0xB6, 0xD6, 0x90, 0x00};

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* Global variables
******************************************************************************/


/******************************************************************************
* Local variables
******************************************************************************/


/******************************************************************************
* Local functions
******************************************************************************/


/*.****************************************************************************
*. Function: card_init
*. Description: Initialization
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void card_init()
{
//data line as input
CARD_IO_REG = CARD_IO_REG | CARD_IO_N;
}

/*.****************************************************************************
*. Function: read_byte
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void read_byte(u8 *data)
{
u8 i;
u8 temp2;
u8 data_temp;

CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input
SET_TMR0(ETU);
CLR_FLAG(ETU_flag);
while(1 == CARD_IO)
{
}
TMR0_ON();
data_temp = 0;
for(i = 0; i & lt; 8; i++)
{
data_temp = data_temp & gt; & gt; 1;
WAIT_FLAG(ETU_flag);
temp2 = CARD_IO;
if(temp2)
{
temp2 = 0x80;
}
else
{
temp2 = 0x00;
}
data_temp = data_temp | temp2;
}
*data = data_temp;
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR0_OFF();
}

/*.****************************************************************************
*. Function: write_byte
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void write_byte(u8 data)
{
u8 i;
u8 parity = 0;
u8 temp;

CARD_IO_REG = CARD_IO_REG & ~CARD_IO_N; // output
SET_TMR0(ETU);
CLR_FLAG(ETU_flag);
TMR0_ON();
WAIT_FLAG(ETU_flag);
CARD_IO = 0; // start bit
for(i = 0; i & lt; 8; i++)
{
temp = (data & 0x01);
WAIT_FLAG(ETU_flag);
CARD_IO = temp;
parity = parity + temp;
data = data & gt; & gt; 1;
}
temp = (parity % 2);
WAIT_FLAG(ETU_flag);
CARD_IO = temp;
WAIT_FLAG(ETU_flag);
CARD_IO = 1;
CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input
WAIT_FLAG(ETU_flag);
TMR0_OFF();
}


/*.****************************************************************************
*. Function: send_ATR
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void send_ATR()
{
u8 i;

CARD_IO_REG = CARD_IO_REG & ~CARD_IO_N; // output
DelayUs(250);

SET_ETU(ETU_372);
for(i = 0; i & lt; 16; i++)
{
DelayUs(250);
write_byte(ATR[i]);
}
CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input
}


Spliter_by_Waldo_v1.5_source_codes.zip > always.h

/*

Servo PIC

Compiled with Hitech-C v8.00

Usage: #include in all " .c " files in project along with " pic.h "

*/

//warning about #if statements: if any of the arguments are spelled wrong or
//unrecognised by the compiler, it will not generate a warning, but not include code

#ifndef ALWAYS_H
#define ALWAYS_H

#define us_to_ticks(x) (x*(PIC_CLK/4)/(TMR1_PRESCALE)) //converting microseconds to tmr1 clock ticks
#define ticks_to_us(x) ((x*4/PIC_CLK)*(TMR1_PRESCALE)) //converting tmr1 clock ticks to microseconds

#define hibyte(x) ((unsigned char)(x & gt; & gt; 8))
#define lobyte(x) ((unsigned char)(x & 0xff))


/*
given variable of any type (char, uchar, int, uint, long) it modifies
the unsigned char residing at that memory location
for ints, byte1 is msb, byte0 is lsb
for longs byte3 is msb, byte0 is lsb

ie: sample C code

unsigned int myint=0x4321;
long mylong=0x87654321;

//for myint byte1(myint)=0x43; (msb) and byte0(myint)=0x21; (lsb)
//for mylong byte3(mylong)=0x87; (msb), byte2(mylong)=0x65;
byte2(mylong)=0x43; and byte0(mylong)=0x21; (lsb)

note: to avoid fixup overflow errors add bankX if the target variable
resides in banks 1, 2 or 3
*/

//#define byte0(x) (unsigned char)(*(((unsigned char *) & x)+0))
//#define byte1(x) (unsigned char)(*(((unsigned char *) & x)+1))
//#define byte2(x) (unsigned char)(*(((unsigned char *) & x)+2))
//#define byte3(x) (unsigned char)(*(((unsigned char *) & x)+3))

#define byte0_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+0))
#define byte1_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+1))
#define byte2_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+2))
#define byte3_atbank1(x) (unsigned char)(*(((bank1 unsigned char *) & x)+3))

#define byte0_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+0))
#define byte1_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+1))
#define byte2_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+2))
#define byte3_atbank2(x) (unsigned char)(*(((bank2 unsigned char *) & x)+3))

#define byte0_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+0))
#define byte1_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+1))
#define byte2_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+2))
#define byte3_atbank3(x) (unsigned char)(*(((bank3 unsigned char *) & x)+3))

/*
given variable of any type (char, uchar, int, uint, long) it modifies
the int residing at that memory location
ie: sample C code

unsigned char array[4];
unsigned int test;

uint_atbyteaddr( & array[0])=0x4321; //now array[0- & gt; 3]={0x21,0x43,0,0};
uint_atbyteaddr( & array[0+2])=0x8765; //now array[0- & gt; 3]={0x21,0x43,0x65,0x87};
test=uint_atbyteaddr( & array[0+2]) //now test=0x8765

note: do NOT use & (array[0]+1) to reference the int stored at array[1] as it will
reference the int after array[0] in pointer arithmetic. This will
result with the int at array[2]
instead use & array[0+1] to reference the int at array[1]

note: to avoid fixup overflow errors add bankX if the target variable
resides in banks 1, 2 or 3
*/

#define uint_atbyteaddr(x) (unsigned int)(*(((unsigned int *)x)))
#define uint_atbank1byteaddr(x) (unsigned int)(*(((bank1 unsigned int *)x)))
#define uint_atbank2byteaddr(x) (unsigned int)(*(((bank2 unsigned int *)x)))
#define uint_atbank3byteaddr(x) (unsigned int)(*(((bank3 unsigned int *)x)))

/*
these two structures allow access to 2 byte word, high and low bytes of variable
declaration: union wordtype x;
usage: x.word=0xABCD; x.byte.high=0xAB; x.byte.low=0xCD;
x.part.bit15=1; (msb), x.part.bit0=1; (lsb)
declaration: union chartype x;
usage: x.byte=0xAB;
x.part.bit7=1; (msb), x.part.bit0=1; (lsb)
*/

struct sixteen_bits
{
unsigned char bit0 :1;
unsigned char bit1 :1;
unsigned char bit2 :1;
unsigned char bit3 :1;
unsigned char bit4 :1;
unsigned char bit5 :1;
unsigned char bit6 :1;
unsigned char bit7 :1;
unsigned char bit8 :1;
unsigned char bit9 :1;
unsigned char bit10 :1;
unsigned char bit11 :1;
unsigned char bit12 :1;
unsigned char bit13 :1;
unsigned char bit14 :1;
unsigned char bit15 :1;
};

struct eight_bits
{
unsigned char bit0 :1;
unsigned char bit1 :1;
unsigned char bit2 :1;
unsigned char bit3 :1;
unsigned char bit4 :1;
unsigned char bit5 :1;
unsigned char bit6 :1;
unsigned char bit7 :1;
};

struct two_bytes
{
unsigned char low;
unsigned char high;
};

union wordtype
{
unsigned int word;
struct two_bytes byte;
struct sixteen_bits part;
};

union chartype
{
unsigned char byte;
struct eight_bits part;
};

//defines
#define INPUT 1 //port directions
#define OUTPUT 0
#define TRUE 1
#define FALSE 0
#define HIGH 1
#define LOW 0
#define hi 1
#define lo 0

#define b asm( " nop " ) //convenient point for breakpoint (debugging)
#define l while(1) //loop for ever (debugging)

//see AM576. If interrupt occurs just when gie gets set to zero, it won't be cleared

#define gie_on GIE=1
#define gie_off while(GIE==1) GIE=0

#endif


Spliter_by_Waldo_v1.5_source_codes.zip > main.c

/******************************************************************************
* File: main.c
* Software:
* Module: main
* Description: Main file.
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#include & lt; pic.h & gt;
#include " delay.h "
#include " types.h "
#include " main.h "
#include " smart_card.h "
#include " tr_485.h "
#include " commands.h "

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* Global variables
******************************************************************************/
bit ETU_flag;
u8 ETU = 250;

/******************************************************************************
* Local variables
******************************************************************************/


/******************************************************************************
* Local functions
******************************************************************************/
void init(void);

__CONFIG(WDTDIS & PWRTDIS & UNPROTECT & XT); // 8.00

/*.****************************************************************************
*. Function: main
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void main(void)
{
enum {READ, WAIT_SERVER, SEND, RECEIVE_WRITE, READ_SEND} main_state = READ;
u8 buffor[MAX_BUFFER_SIZE];
u8 number_bytes;
u8 temp, i;
u8 number_direct_sends = 0; // number direst sends to server to do
bool more_data_to_send = FALSE;

init();

card_init();
tr_init();

TMR0_OFF();

send_ATR();

while(1)
{
switch(main_state)
{
// -------- READ ------------------
case READ:
STATUS(0);
SET_ETU(ETU_372);
number_bytes = 5;
read_byte( & buffor[0]);
if(0xC1 == buffor[0])
{
for(i = 1; i & lt; number_bytes; i++)
{
read_byte( & buffor[i]);
}
if(number_direct_sends != 0)
{
number_direct_sends--;
main_state = SEND;
}
else
{
main_state = WAIT_SERVER;
}
}
break;

// ----------- WAIT SERVER -----------------
case WAIT_SERVER:
STATUS(1);
SET_ETU(ETU_372);
tr_wait_server();
main_state = SEND;
break;

// -------- SEND --------------------
case SEND:
STATUS(2);
SET_ETU(ETU_372 / 2);
for(i = 0; i & lt; number_bytes; i++)
{
tr_write_byte(buffor[i]); // data
}
main_state = RECEIVE_WRITE;
break;

// ----------- RECEIVE_WRITE ----------------------------------
case RECEIVE_WRITE:
STATUS(3);
// number bytes to receive
if(buffor[0] != 0xC1)
{
number_bytes = 2;
}
else
{
if((COMMAND_05 == buffor[1]) || (COMMAND_10 == buffor[1])) // 0x34 & ECM
{
number_direct_sends = 1; // next command_06 or command_11
more_data_to_send = TRUE;
number_bytes = 1;
}
else if((COMMAND_12 == buffor[1]) || (COMMAND_07 == buffor[1])) // EMM & PIN
{
more_data_to_send = TRUE;
number_bytes = 1;
}
else
{
number_bytes = buffor[4] + 3; // 3 = command + status 90 00
}
}

temp = 0; // check first byte only for 0x60
for(i = 0; i & lt; number_bytes; i++)
{
tr_read_byte( & buffor[0]);
if((0 == temp) & & ((buffor[0] & 0xF0) == 0x60))
{
number_bytes = 2;
number_direct_sends = 0;
more_data_to_send = FALSE;
}
temp = 1;
if((number_bytes - 1) == i)
{
TRAN_IO = 0;
}
else
{
TRAN_IO = 1;
}
SET_ETU(ETU_372);
write_byte(buffor[0]);
}

CARD_IO = 1;
CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input
TRAN_485_TRANSMIT();
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
if(TRUE == more_data_to_send)
{
more_data_to_send = FALSE;
main_state = READ_SEND;
}
else
{
main_state = READ;
}
break;

// ---------- READ_SEND ----------------------------------
case READ_SEND:
STATUS(4);
SET_ETU(ETU_372 / 2);
// number bytes to read
number_bytes = buffor[4];

TRAN_485_TRANSMIT();
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input

for(i = 0; i & lt; number_bytes; i++)
{
SET_TMR0(ETU);
CLR_FLAG(ETU_flag);
while(1 == CARD_IO) // wait for start bit of next byte
{
}
TMR0_ON();
WAIT_FLAG(ETU_flag);
TRAN_IO = 1;
for(temp = 0; temp & lt; 9; temp++)
{
WAIT_FLAG(ETU_flag);
TRAN_IO = CARD_IO;
WAIT_FLAG(ETU_flag);
TRAN_IO = !TRAN_IO;
}
WAIT_FLAG(ETU_flag);
TRAN_IO = 0;
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR0_OFF();
}
TRAN_485_TRANSMIT();
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
CARD_IO = 1;
CARD_IO_REG = CARD_IO_REG | CARD_IO_N; // input
main_state = RECEIVE_WRITE;
break;
}
}
}

/*.****************************************************************************
*. Function: init
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void init(void)
{
T0CS = 0; //timer0 internal
RBPU = 0;

TRISB = TRISB & 0x8F; // LEDs on port RB4, RB5, RB6
STATUS(7);

T0IE = 1; //enable interup from TIMER0
GIE = 1; //enable interupts
}

/*.****************************************************************************
*. Function: interrupt timer_int
*. Description: handle interrupts - should be placed on end of file
*. (better handle context saving)
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void interrupt timer_int() // Here be interrupt function - the name is unimportant.
{
if(T0IF)
{
SET_TMR0(ETU);
SET_FLAG(ETU_flag);
T0IF = 0; // clear the interrupt flag
}
}


Spliter_by_Waldo_v1.5_source_codes.zip > delay.h

/*

lowlevel delay routines

For Microchip PIC and Hi-Tech C

Example C:

#define PIC_CLK 8000000

#include " delay.h "

unsigned int timeout_int, timeout_char;

timeout_char=timeout_char_us(1147);
while(timeout_char-- & & (RA1==0)); //wait up to 1147us for port RA1 to go high
// - this is the max timeout

timeout_int=timeout_int_us(491512);
while(timeout_int-- & & (RA1==0)); //wait up to 491512us for port RA1 to go high
// - this is the max timeout

dly250n; //delay 250ns
dly1u; //delay 1us
DelayUs(40); //do not do DelayUs(0) or else it bombs :)
DelayUs(255); //max

*/

#ifndef __DELAY_H
#define __DELAY_H

#define PIC_CLK 3579000

extern unsigned char delayus_variable;
//void DelayBigUs(unsigned int cnt);
//void DelayMs(unsigned char cnt);
//void DelayBigMs(unsigned int cnt);


#if (PIC_CLK == 4000000) || (PIC_CLK == 3686400) || (PIC_CLK == 3579000) //3686400 is not entirely accurate, but it works
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n please remove; for 8Mhz+ only
#define dly1u asm( " nop " )
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 8000000)
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n asm( " nop " )
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif ( (PIC_CLK == 16000000) || (PIC_CLK == 16257000) )
#define dly125n please remove; for 32Mhz+ only
#define dly250n asm( " nop " )
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 20000000)
#define dly200n asm( " nop " )
#define dly400n dly250n;dly250n
#define dly2u dly400n;dly400n;dly400n;dly400n;dly400n
#elif (PIC_CLK == 32000000)
#define dly125n asm( " nop " )
#define dly250n dly125n;dly125n
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#else
#error please define pic_clk correctly
#endif

//*****
//delay routine

#if PIC_CLK == 4000000 || (PIC_CLK == 3686400) || (PIC_CLK == 3579000) //3686400 is not entirely accurate, but it works
#define DelayDivisor 4
#define WaitFor1Us asm( " nop " )
#define Jumpback asm( " goto $ - 2 " )
#elif PIC_CLK == 8000000
#define DelayDivisor 2
#define WaitFor1Us asm( " nop " )
#define Jumpback asm( " goto $ - 2 " )
#elif ( (PIC_CLK == 16000000) || (PIC_CLK==16257000) )
#define DelayDivisor 1
#define WaitFor1Us asm( " nop " )
#define Jumpback asm( " goto $ - 2 " )
#elif PIC_CLK == 20000000
#define DelayDivisor 1
#define WaitFor1Us asm( " nop " )
#define WaitFor1Us asm( " nop " )
#define Jumpback asm( " goto $ - 2 " )
#elif PIC_CLK == 32000000
#define DelayDivisor 1
#define WaitFor1Us asm( " nop " ); asm( " nop " ); asm( " nop " ); asm( " nop " ); asm( " nop " )
#define Jumpback asm( " goto $ - 6 " )
#else
#error please define pic_clk correctly
#endif

#define DelayUs(x) { \
delayus_variable=(unsigned char)(x/DelayDivisor); \
WaitFor1Us; } \
asm( " decfsz _delayus_variable,f " ); \
Jumpback;

/*

timeouts:

C code for testing with ints:

unsigned int timeout;
timeout=4000;
PORT_DIRECTION=OUTPUT;
while(1)
{
PORT=1;
timeout=8000;
while(timeout-- & gt; = 1); //60ms @ 8Mhz, opt on, 72ms @ 8Mhz, opt off
PORT=0;
}

Time taken: optimisations on: 16cyc/number loop, 8us @ 8Mhz
optimisations off: 18cyc/number loop, 9us @ 8Mhz
with extra check ie: & & (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz

C code for testing with chars:

similar to above

Time taken: optimisations on: 9cyc/number loop, 4.5us @ 8Mhz
with extra check ie: & & (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz

Formula: rough timeout value = ( & lt; us desired & gt; / & lt; cycles per loop & gt; ) * (PIC_CLK/4.0)

To use: //for max timeout of 1147us @ 8Mhz
#define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on
#define timeout_char_us(x) (unsigned char)((x/LOOP_CYCLES_CHAR)*(PIC_CLK/4.0))
unsigned char timeout;
timeout=timeout_char_us(1147); //max timeout allowed @ 8Mhz, 573us @ 16Mhz
while((timeout-- & gt; = 1) & & ( & lt; extra condition & gt; )); //wait

To use: //for max 491512us, half sec timeout @ 8Mhz
#define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on
#define timeout_int_us(x) (unsigned int)((x+/LOOP_CYCLES_INT)*(PIC_CLK/4.0))
unsigned int timeout;
timeout=timeout_int_us(491512); //max timeout allowed @ 8Mhz
while((timeout-- & gt; = 1) & & ( & lt; extra condition & gt; )); //wait
*/
#define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on
#define timeout_char_us(x) (unsigned char)((x/LOOP_CYCLES_CHAR)*(PIC_CLK/4000000.0))

#define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on
#define timeout_int_us(x) (unsigned int)((x/LOOP_CYCLES_INT)*(PIC_CLK/4000000.0))

#endif


Spliter_by_Waldo_v1.5_source_codes.zip > tr_485.c

/******************************************************************************
* File: tr_485.c
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#include & lt; pic.h & gt;
#include " types.h "
#include " main.h "
#include " delay.h "
#include " tr_485.h "

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* Global variables
******************************************************************************/


/******************************************************************************
* Local variables
******************************************************************************/


/******************************************************************************
* Local functions
******************************************************************************/


/*.****************************************************************************
*. Function: tr_init
*. Description: Initialization
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void tr_init()
{
//485 transciver control pin as output
TRAN_ST_REG = TRAN_ST_REG & ~TRAN_ST_N;
TRAN_485_TRANSMIT();
//data line as output
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N;
TRAN_IO = 0;
}

/*.****************************************************************************
*. Function: tr_read_byte
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void tr_read_byte(u8 *data)
{
u8 i;
u8 parity = 0;
u8 temp2;
u8 data_temp;
u8 count_parity = 0;

while(0 == parity)
{
TRAN_IO_REG = TRAN_IO_REG | TRAN_IO_N; // input
TRAN_485_RECEIVE();
SET_ETU((ETU_372 * 3) / 5);
SET_TMR0(ETU);
CLR_FLAG(ETU_flag);
TMR0_ON();
while(0 == TRAN_IO) // wait for start bit
{
TMR0_OFF();
SET_TMR0(ETU);
while(0 == TRAN_IO)
{
}
TMR0_ON();
DelayUs(10);
}
data_temp = 0;
for(i = 0; i & lt; 8; i++)
{
data_temp = data_temp & gt; & gt; 1;
WAIT_FLAG(ETU_flag);
temp2 = TRAN_IO;
TMR0_OFF();
SET_TMR0(ETU);
CLR_FLAG(ETU_flag);
if(temp2)
{
while(1 == TRAN_IO)
{
}
}
else
{
while(0 == TRAN_IO)
{
}
}
TMR0_ON();
parity = parity + temp2;
if(temp2)
{
temp2 = 0x80;
}
else
{
temp2 = 0x00;
}
data_temp = data_temp | temp2;
}
WAIT_FLAG(ETU_flag);
temp2 = TRAN_IO;
TMR0_OFF();
if(temp2)
{
while(1 == TRAN_IO)
{
}
}
else
{
while(0 == TRAN_IO)
{
}
}
SET_ETU(ETU_372);
SET_TMR0(ETU);
TMR0_ON();
TRAN_485_TRANSMIT();
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
if((parity % 2) != temp2)
{
//parity error
count_parity++;
if(count_parity & gt; 2)
{
parity = 1;
}
else
{
TRAN_IO = 1;
parity = 0;
}
}
else
{
parity = 1;
}
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TMR0_OFF();
}
*data = data_temp;
}

/*.****************************************************************************
*. Function: tr_write_byte
*. Description: code manchester. log " 1 " = 1- & gt; 0 ; log " 0 " = 0- & gt; 1
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void tr_write_byte(u8 data_org)
{
u8 i;
u8 parity = 0;
u8 temp;
u8 data;

while(0 == parity)
{
TRAN_485_TRANSMIT();
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
SET_TMR0(ETU);
CLR_FLAG(ETU_flag);
data = data_org;
TMR0_ON();
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TRAN_IO = 1; // start bit " 0 "
for(i = 0; i & lt; 8; i++)
{
temp = data & 0x01;
WAIT_FLAG(ETU_flag);
TRAN_IO = temp;
parity = parity + temp;
data = data & gt; & gt; 1;
WAIT_FLAG(ETU_flag);
TRAN_IO = !TRAN_IO;
}
temp = (parity % 2);
WAIT_FLAG(ETU_flag);
TRAN_IO = temp;
WAIT_FLAG(ETU_flag);
TRAN_IO = !TRAN_IO;
WAIT_FLAG(ETU_flag);
TRAN_IO = 0;
WAIT_FLAG(ETU_flag);
TRAN_IO_REG = TRAN_IO_REG | TRAN_IO_N; // input
TRAN_485_RECEIVE();
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
if(1 == TRAN_IO)
{
parity = 0;
}
else
{
parity = 1;
}
WAIT_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TRAN_485_TRANSMIT();
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
TMR0_OFF();
}
}

/*.****************************************************************************
*. Function: tr_wait_server
*. Description:
*.
*. Input:
*. Output:
*. Return:
******************************************************************************/
void tr_wait_server()
{
u8 temp = 0;

SET_TMR0(ETU);
CLR_FLAG(ETU_flag);
TMR0_ON();
WAIT_FLAG(ETU_flag);
do // generate ETU on bus - server will response with '1'
{
TRAN_485_TRANSMIT();
TRAN_IO = 1;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
CLR_FLAG(ETU_flag);
WAIT_FLAG(ETU_flag);
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG | TRAN_IO_N; // input
TRAN_485_RECEIVE();
DelayUs(20);
while(!(ETU_flag))
{
if(1 == TRAN_IO)
{
temp = 0xFF;
}
}
}while(0 == temp);
CLR_FLAG(ETU_flag);

while(1 == TRAN_IO)
{
}
TRAN_485_TRANSMIT();
TRAN_IO = 0;
TRAN_IO_REG = TRAN_IO_REG & ~TRAN_IO_N; // output
TMR0_OFF();
}


Spliter_by_Waldo_v1.5_source_codes.zip > commands.h

#ifndef _COMMANDS_H_
#define _COMMANDS_H_

/******************************************************************************
* File: commands.h
* Software:
* Module: main
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
// commands for SECA
#define COMMAND_01 0x0A
#define COMMAND_02 0x0E
#define COMMAND_03 0x12
#define COMMAND_04 0x16
#define COMMAND_05 0x34
#define COMMAND_06 0x32
#define COMMAND_07 0x30
#define COMMAND_08 0x1A
#define COMMAND_09 0x5A
#define COMMAND_10 0x3C
#define COMMAND_11 0x3A
#define COMMAND_12 0x40

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/

/******************************************************************************
* External variable declarations
******************************************************************************/

/******************************************************************************
* Function prototypes
******************************************************************************/

/******************************************************************************
* End of file
******************************************************************************/
#endif /* _COMMAND_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > delay.c

#ifndef __DELAY_C
#define __DELAY_C

#include & lt; pic.h & gt;
#include " always.h "
#include " delay.h "

unsigned char delayus_variable;

/*void DelayBigUs(unsigned int cnt)
{
unsigned char i;

i = (unsigned char)(cnt & gt; & gt; 8);
while(i & gt; =1)
{
i--;
DelayUs(253);
CLRWDT();
}
DelayUs((unsigned char)(cnt & 0xFF));
}*/

/*
void DelayMs(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}*/
/*
//this copy is for the interrupt function
void DelayMs_interrupt(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
} while(--i);
} while(--cnt);
}
*/
/*
void DelayBigMs(unsigned int cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}*/

/*
void DelayS(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayMs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
*/

#endif


Spliter_by_Waldo_v1.5_source_codes.zip > main.h

#ifndef _MAIN_H_
#define _MAIN_H_

/******************************************************************************
* File: main.h
* Software:
* Module: main
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#define MAX_BUFFER_SIZE 40

#define SET_FLAG(data) (data = 1)
#define CLR_FLAG(data) (data = 0)
#define WAIT_FLAG(data) while(!(data)){};data = 0
#define SET_TMR0(data) (TMR0 = 255 - (data))
#define SET_ETU(data) ETU = (data) - 15
#define TMR0_ON() (T0CS = 0)
#define TMR0_OFF() (T0CS = 1)

#define ETU_372 (372/4)

#define LED1 RB4
#define LED2 RB5
#define LED3 RB6
#define STATUS(data) LED1 = (data & 0x01) ? 0 : 1; LED2 = (data & 0x02) ? 0 : 1; LED3 = (data & 0x04) ? 0 : 1

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/


/******************************************************************************
* External variable declarations
******************************************************************************/
extern bit ETU_flag;
extern u8 ETU;

/******************************************************************************
* Function prototypes
******************************************************************************/


/******************************************************************************
* End of file
******************************************************************************/
#endif /* _MAIN_H_ not defined */


Spliter_by_Waldo_v1.5_source_codes.zip > tr_485.h

#ifndef _TR_485_H_
#define _TR_485_H_

/******************************************************************************
* File: tr_485.h
* Software:
* Module:
* Description:
*
* Created: 2004-04-23
* Author:
* mail:
*
*
*****************************************************************************/


/******************************************************************************
* Pre-processor directives (#include, #define, #ifdef, ...)
******************************************************************************/
#define TRAN_IO RB0
#define TRAN_IO_N ONEBIT8L(0)
#define TRAN_IO_REG TRISB

#define TRAN_ST RB1
#define TRAN_ST_N ONEBIT8L(1)
#define TRAN_ST_REG TRISB

#define TRAN_485_RECEIVE() TRAN_ST = 0
#define TRAN_485_TRANSMIT() TRAN_ST = 1

/******************************************************************************
* Declarations (enum, union, struct, typedef, class)
******************************************************************************/


/******************************************************************************
* External variable declarations
******************************************************************************/


/******************************************************************************
* Function prototypes
******************************************************************************/
void tr_init();
void tr_wait_server();
void tr_read_byte(u8 * data);
void tr_write_byte(u8 data);

/******************************************************************************
* End of file
******************************************************************************/
#endif /* _TR_485_H_ not defined */