- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
if( pc_rbyte == 0xFA && pc_buff_ready == 0 && pc_rec_pos == 0)
{
pc_rec_en = 1;
return;
}
return;
}
ISR(USART0_RX_vect)
{
cli();
rbyte = rs485_reciv();
if( COMM == 1 && rbyte != 0x42 )
xp_rec_en = 1;
if(COMM == 1 && rbyte == 0x42)
{
//xp_rec_pos++;
xp_response[xp_rec_pos] = xp_xor;
COMM = 0;
xp_rec_en = 0;
xp_buff_ready = xp_rec_pos;
xp_rec_pos = 0;
xp_rec_en = 0;
xp_xor = 0;
}
if( xp_rec_en == 1 )
{
if( rbyte != xp_xor && xp_rec_pos<16 )
{
xp_response[xp_rec_pos] = rbyte;
xp_xor = xp_xor^rbyte;
xp_rec_pos++;
}else{
xp_buff_ready = xp_rec_pos;
xp_rec_pos = 0;
xp_rec_en = 0;
xp_xor = 0;
}
}
if( rbyte == 0x60 )
{
xp_rec_en = 1;
xp_response[0] = rbyte;
xp_rec_pos++;
}
if( rbyte == 0x41 && my_window!=1)
COMM = 1;
if( (bit9&0b00000010) == 0b00000010 && (rbyte&0b00011111) == myXPaddress )
switch( (rbyte&0b01100000) )
{
case 0b01000000: ack = 0; if( pc_buff_ready == 1 ) my_window = 1; xp_rec_en = 0;break; // NormalInquary
case 0b01100000: ack = 0; my_window = 0; xp_rec_en = 1;break; // CallByte
case 0b00000000: ack = 1; my_window = 0; xp_rec_en = 0;break; // ACK!
}
rbyte = 0;
sei();
return;
}
int main(void)
{
avr_init();
sei();
//PORTD &=~(1<<transmit_en);
//pc_buff_ready = 1;
while (1)
{
//if( pc_buff_ready == 1 ) //pc_buff_ready == 1 &&
//{
if( my_window == 1 )
{
cli();
_delay_us(30);
//PORTD |= (1<<transmit_en);
PORTG |=_BV(PG4); //disable DE RS485
PORTD |=_BV(PD5); //enable RE RS485
//_delay_us(5);
for( i=0; pc_rec_pos != i; i++ )
rs485_ransmit(pc_command[i]);
while ( !( UCSR0A & (1<<UDRE0)) );
_delay_us(160);
PORTG &=~_BV(PG4); //disable DE RS485
PORTD &=~_BV(PD5); //enable RE RS485
my_window = 0;
clear_buff(0);
pc_buff_ready = 0;
pc_rec_pos = 0;