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
#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 */
#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 */
#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
/******************************************************************************
* 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;
}
/*
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++;
}
}
#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 */
#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 */
/******************************************************************************
* 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;
}
#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 */
#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 */
/*
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