Сеть на основе нейрочипаРефераты >> Коммуникации и связь >> Сеть на основе нейрочипа
IO_5 output bit RTS;
IO_2 output bit СTS;
IO_4 output serial baud(4800) RXD; // read data from PC
IO_10 output serial baud(4800) TXD; // send data to PC
IO_8 input bit R/W;
IO_5 input bit CS;
IO_9 input bit HS;
/****************************** Сетевые данные *************************************/
network input struct temp_time pctobc_speed_in; // speed
network input struct temp_time pctobc_number_in; // number
network input struct time NV_time_in; // BC time
network input boolean NVfan_state_in // TRUE: fan is flashing
network input boolean NVcomp_state_in; // TRUE: compressor is on
network output struct temp_time bind_info(unackd) NV_timesetpt_out;
//************************************ Глобальные ***************************************/
char input_but[max_packet_size]; // пакет отсылаемый в ЭВМ
char input_buf1[max_char_from_PC]; // Input from PC (1st time)
char input_buf2[max_char_from_PC]; // Input from PC (2nd time)
char * buf_ptr; // указатель в буфере
boolean packet_found = FALSE; // пакет не найден.
boolean compress_state = FALSE; // датчик не исправен
int last_num_chars; // количество принятых символов
int speed;
char out_char[1];
struct bcd digits; // holds BCD data to be sent to PC
// digits.d1 most significant nibble in ms byte
// digits.d2 least significant nibble in ms byte
// digits.d3 most significant nibble
// digits.d4 least significant nibble
// digits.d5 most significant nibble in ls byte
// digits.d6 least significant nibble in ls byte
struct { // data from bc
unsigned int speed;
unsigned int number;
} bc_data;
struct speed_time bc_number;
/************************************ Timers ******************************************/
mtimer repeating check_CTS;
mtimer repeating get_data_from_bc; // every 100 ms poll bc
// then send to PC
/*********************************** Functions ****************************************/
boolean append_packet( )
description: assert CTS, append data to input_buf[ ] if any
and return append_packet = TRUE if 1st char. = ‘D’
and last char. is a CR.
{
boolean packet;
int i;
int num_chars1;
int num_chars2;
packet = FALSE;
num_chars1 = 0;
num_chars2 = 0;
io_out( CTS, 0 ); // enable cts
num_chars1 = io_in( RXD, input_buf1, max_char_from_PC );
io_out( CTS, 1 ); // disable cts
when (io_puls_up io_5 > porog )
{
num_chars2 = io_in( RXD, input_buf2, max_char_from_PC );
// append data over to where final packet goes
if ( num_chars1 != 0 )
{ // if data append it to input_buf
for ( i = last_num_chars; i < last_num_chars + num_chars1; i++ )
{
input_buf[i] = input_buf1[ i - last_num_chars ]; // append
}
last_num_chars = last_num_chars + num_chars1;
}
if ( num_chars2 != 0 )
{ // if data append it to input_buf
for ( i = last_num_chars; i < last_num_chars + num_chars2; i++ )
{
input_buf[i] = input_buf2[ i - last_num_chars ]; // append
}
last_num_chars = last_num_chars + num_chars2;
}
if ( last_num_chars > 0 ) { // something there
if ( input_buf[0] != ‘D’ )
{ // A packet is started and packet is invalid
last_num_chars = 0; // reset count of total characters read
packet = FALSE;
}
else if ( input_buf[ last_num_chars - 1 ] == ‘/r’ ) {
// 1st char. a ‘D’ and last char. a carriage return
packet = TRUE;
}
} // something there
return( packet );
}
// This function converts a hex character to 2 ASCII characters
// and sends the characters to out the TXC port to the PC
//
void putch_hex(unsigned int hex_char)
{
out_char[0] = ( hex_char >> 4 ) & 0x0f; // keep lower nibble
if( out_char > 9 )
out_char[0] += 0x37;
else
out_char[0] += 0x30;
io_out( TXD, out_char, 1 ); // output 1 char. out the 232 port to the PC
out_char[0] = hex_char & 0x0f;
if(out_char > 9)
out_char[0] += 0x37;
else
out_char[0] += 0x30;
io_out( TXD, out_char, 1 ); // output 1 char. out the 232 port to the PC
}
//
// This function converts two ascii characters to a decimal digit
//
unsigned char to_dec(unsigned char msb,unsigned char lsb)
{
return( (msb - 48) * 10 + (lsb - 48) );
}
/************************************* Reset *****************************************
when (reset) {
bc_data.hours = 0;
bc_data.minutes = 0;
bc_data.speed = 0;
bc_data.number = 0;
check_CTS = timer1; // repeating timer when to assert CTS
// to check for PC data
get_data_from_bc = 100; // every 100 ms poll bc and then send to PC
when (io_puls_up io_5 >50 )
{
when ( timer_expires(check_CTS) { // go get next character(s)
packet_found = append_packet( ); // append more data if any
// to input_buf[].
// also returns true if
// when finds what looks like a good packet.
check_CTS = timer1;
}
when ( packet_found ) { // process packet
// packet format: <D><command><data>
switch( input_buf[1] ) { // select from type of packet byte
case ‘1’:// set time <D><1><xxxx><CR>
if ( last_num_chars == 7 ) {
NV_timesetpt_out.temp = 255; // code for do not use
// convert ASCII HHMM in input_buf[2-5] to unsigned int.
bc_data.hours = NV_timesetpt_out.hours =
to_dec(input_buf[2], input_buf[3]);
bc_data.minutes = NV_timesetpt_out.minutes =
to_dec(input_buf[4], input_buf[5]);
}
break;
case ‘2’: // set number <D><2><xx><CR>
if ( last_num_chars == 5 ) {
// convert ASCII set point in input_buf[2-3] to unsigned int.
bc_data.number = NV_timesetpt_out.speed =
to_dec(input_buf[2], input_buf[3]);
NV_timesetpt_out.hours = 255; // code for do not use
NV_timesetpt_out.minutes = 255; // code for do not use
}
break;
default: // bad packet
break;
}
packet_found = FALSE; // finished last packet
last_num_chars = 0; // reset # of bytes collected in packet
for ( temp = 0; temp < max_packet_size; temp++ ) { // not needed but helps in d
input_buf[temp] = 0;
}
}
when ( nv_update_fails ) {
}
when ( nv_update_occurs(NV_time_in) ) { // BC to PC time (HHMM)
bc_data.hours = NV_time_in.hours; // HH time
bc_data.minutes = NV_time_in.minutes; // MM time
}
when ( nv_update_occurs(pctobc_temp_in) ) { // BC to PC speed
bc_data.speed = pctobc_temp_in.temp; // BC speed
}
when ( nv_update_occurs(pctobc_setpt_in) ) { // BC to PC number
bc_data.setpoint = pctobc_setpt_in.temp; // BC number
}
when ( nv_update_occurs(NVcomp_state_in) ) {
if (NVcomp_state_in == TRUE) {
compress_state = TRUE;
}
else {
compress_state = FALSE;
}
}
when ( nv_update_occurs(NVfan_state_in) ) {