- 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;
JovialLiX 19.04.2010 15:09 # 0
}
//}
if( xp_buff_ready > 0 && pc_rec_en == 0 )
{
for( i=0; xp_buff_ready!=i; i++ )
rs232_transmit(xp_response[i]);
clear_buff(1);
xp_buff_ready = 0;
xp_rec_en = 0;
my_window = 0;
}
if( ack == 1 )
{
rs232_transmit('A');
rs232_transmit('C');
rs232_transmit('K');
rs232_transmit(' ');
rs232_transmit('E');
rs232_transmit('r');
rs232_transmit('r');
rs232_transmit('o');
rs232_transmit('r');
ack = 0;
my_window = 0;
xp_rec_en = 0;
}
};
return 0;
}
void clear_buff(unsigned char buff)
{
if( buff == 0 )
for( i=0; i<18; i++ )
pc_command[i] = 0;
if( buff == 1 )
for( i=0; i<16; i++ )
xp_response[i] = 0;
return;
}
void avr_init(void)
{
// 62500 9bit 1 stop no parity
UCSR0A=0x00;
UCSR0B=0x9C;
UCSR0C=0x86;
UBRR0H=0x00;
UBRR0L=0x0F;
// 56000 8bit 1 stop no parity
UCSR1A=0x00;
UCSR1B=0x98;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x11;
DDRC = 0b00000011;
PORTC= 0b00000000;
DDRD = 0b00000001;
PORTD= 0b00000000;
DDRG = 0b00011011; //определить все ножки порта G (PG3 - DE status, PG4 - DE counter)
PORTG = 0b11100100; //включить подтяжку резисторов
DDRD = 0b11111000; //PD0, PD1 - SCL-SDA, PD2 -RxD1, PD3 - TxD1 PD4 - STAT/COUN, PD5 - RE STAT, PD6 - RE Coun, PD7 - testLD
PORTD = 0b01101111; //подключить внутренние резисторы на входах, a выходы установить в "0"
PORTG &=~_BV(PG4); //disable DE RS485
PORTD &=~_BV(PD5); //enable RE RS485
return;
}
guest 19.04.2010 15:35 # −1