📄 g20serialdrv.i
字号:
void uploadAlarmValue();
void uploadRealtimeData();
void uploadAlarmData(unsigned char value);
/* hex to bcd */
unsigned char hex2bcd(unsigned char hdata);
/* bcd to hex */
unsigned int bcd2hex(BYTE bdata);
void initial_protocol(void);
void ProtocolProc(void);
void SerialPortWrite(BYTE *pBuf, int nLength);
SPSTATUS my_g20_sp_status;
BYTE g20_sp_rcv_buffer[200];
UINT g20_sp_rcv_empty = 0;
UINT g20_sp_rcv_data = 0;
UINT g20_count_comma = 0;
BYTE Hex2Char(BYTE ch)
{
volatile BYTE chTemp;
chTemp = 0x00;
if(ch < 0x0A)
{
chTemp = ch + 0x30 ;
}
if(ch > 0x09)
{
chTemp = ch + 0x40 - 0x09;
}
return chTemp;
}
BYTE Char2Hex(BYTE ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
return ch-'A'+10;
else if((ch>='a')&&(ch<='f'))
return ch-'a'+10;
else return 1;
}
void g20_rcv_proc(BYTE data)
{
my_g20_sp_status.buffer[my_g20_sp_status.empty ++] = data;
if(my_g20_sp_status.empty >= 80 )
{
my_g20_sp_status.empty = 0;
}
if(my_g20_sp_status.empty > 8)
{
my_g20_sp_status.overflow = 1;
}
}
void g20_udp_getip()
{
g20_count_comma = 0;
while(g20_count_comma <4)
{
#asm("wdr");
if(g20_sp_rcv_buffer[g20_sp_rcv_data++] == ',')
{
g20_count_comma++;
}
}
}
void g20_upd_package()
{
volatile UINT nCount = 0;
volatile BYTE chTemp = 0x00;
volatile BYTE ch = 0x00;
g20_udp_getip();
while(g20_sp_rcv_data < g20_sp_rcv_empty)
{
#asm("wdr");
chTemp = g20_sp_rcv_buffer[g20_sp_rcv_data++];
if(nCount%2)
{
ch |= (Char2Hex(chTemp));
udp_rcv_proc(ch);
timer_set_g20_sp();
}
else
{
ch = (Char2Hex(chTemp)<<4)&0xF0;
}
nCount++;
}
}
void g20_rcv_pre_proc(BYTE data)
{
if((data == 0x0D)||(data == 0x0A))
{//+MIPRUDP:
if((g20_sp_rcv_buffer[0] == '+')
&&(g20_sp_rcv_buffer[1] == 'M')
&&(g20_sp_rcv_buffer[2] == 'I')
&&(g20_sp_rcv_buffer[3] == 'P')
&&(g20_sp_rcv_buffer[4] == 'R')
&&(g20_sp_rcv_buffer[5] == 'U')
&&(g20_sp_rcv_buffer[6] == 'D')
&&(g20_sp_rcv_buffer[7] == 'P')
&&(g20_sp_rcv_buffer[8] == ':'))
{
g20_sp_rcv_data = 9;
g20_upd_package();
g20_sp_rcv_empty = 0;
g20_sp_rcv_data = 0;
}
else if(g20_sp_rcv_empty >0 )
{
while(g20_sp_rcv_data < g20_sp_rcv_empty)
{
#asm("wdr");
g20_rcv_proc(g20_sp_rcv_buffer[g20_sp_rcv_data++]);
}
g20_sp_rcv_empty = 0;
g20_sp_rcv_data = 0;
}
else
{
g20_sp_rcv_empty = 0;
g20_sp_rcv_data = 0;
}
return;
}
else
{
g20_sp_rcv_buffer[g20_sp_rcv_empty ++] = data;
}
}
//#pragma vector = USART0_RXC_vect
//__interrupt void usart0_rx_isr(void)
interrupt [19] void usart0_rx_isr(void)
{
unsigned char status,data;
PORTA.7 = 1; // disable rts
status=UCSR0A;
data=UDR0;
if ((status & ((1<<4) | (1<<2) | (1<<3)))==0)
{
//UDR1 = data;
UCSR0B &= 0x7F; //disable uart0 interrupt
#asm("sei"); //enable all interrupt
g20_rcv_pre_proc(data);
#asm("cli");; //disable all interrupt
UCSR0B |= 0x80; //enable uart0 interrupt
}
PORTA.7 = 0; // enable rts
}
void g20_sp_init()
{
my_g20_sp_status.buffer[0] = (BYTE) 0x00;
my_g20_sp_status.data = 0;
my_g20_sp_status.empty = 0;
memset(my_g20_sp_status.buffer, 0x00, 80 );
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud rate: 115200
UCSR0A=0x00;
UCSR0B=0x98;
(*(unsigned char *) 0x95)=0x06;
(*(unsigned char *) 0x90)=0x00;
UBRR0L=0x03;
// set rts
DDRA.7 = 1;
// PORTA.7 = 1;
PORTA.7 = 1;
PORTA.7 = 0; // enable rts
}
unsigned char g20_sp_not_empty()
{
return !(my_g20_sp_status.data == my_g20_sp_status.empty);
}
BYTE g20_sp_getchar()
{
BYTE c = 0;
if(my_g20_sp_status.empty != my_g20_sp_status.data)
{
c = my_g20_sp_status.buffer[my_g20_sp_status.data ++];
if(my_g20_sp_status.data >= 80 )
{
my_g20_sp_status.data = 0;
}
return (c);
}
else
{
return (BYTE)0x00;
}
}
void g20_sp_flush()
{
my_g20_sp_status.data = my_g20_sp_status.empty;
}
void g20_sp_init_buf()
{
my_g20_sp_status.data = 0;
my_g20_sp_status.empty = 0;
}
void g20_sp_send(flash BYTE * pBuf)
{
while(*pBuf)
{
#asm("wdr");
while ((UCSR0A & (1<<5))==0);
UDR0 = *pBuf ++;
}
}
void g20_sp_sendb(BYTE * pBuf)
{
while(*pBuf)
{
#asm("wdr");
while ((UCSR0A & (1<<5))==0);
UDR0 = *pBuf ++;
}
}
unsigned char g20_sp_waitfor(flash BYTE * pExpect,
flash BYTE * pExpect1,
flash BYTE *pExcept1,
flash BYTE * pExcept2,
flash BYTE *pExcept3,
DWORD timeouts)
{
BYTE c = 0x00;
UINT expoff = 0;
UINT expoff1 = 0;
UINT excoff1 = 0;
UINT excoff2 = 0;
UINT excoff3 = 0;
TIMERTICK *pTimer = 0;
pTimer = timer_settimer(timeouts);
if(pTimer == 0)
return 0;
while(timer_checktimeout(pTimer)!= 1)
{
#asm("wdr");
while(g20_sp_not_empty())
{
#asm("wdr");
c = g20_sp_getchar();
if(pExpect != 0)
{
if( c == pExpect[expoff])
{
expoff ++;
if(pExpect[expoff] == 0x00)
{
pTimer->bUsed = 0;
pTimer->ltimes = 0;
return 1;
}
}
else
{
expoff =0;
}
}
if(pExpect1 != 0)
{
if(c == pExpect1[expoff1])
{
expoff1 ++;
if(pExpect1[expoff1] == 0x00)
{
pTimer->bUsed = 0;
pTimer->ltimes = 0;
return 1;
}
}
else
{
expoff1 = 0;
}
}
if(pExcept1 != 0)
{
if( c == pExcept1[excoff1])
{
excoff1++;
if(pExcept1[excoff1] == 0x00)
{
pTimer->bUsed = 0;
pTimer->ltimes = 0;
return 0;
}
}
else
{
excoff1 = 0;
}
}
if(pExcept2 != 0)
{
if( c == pExcept2[excoff2])
{
excoff2++;
if(pExcept2[excoff2] == 0x00)
{
pTimer->bUsed = 0;
pTimer->ltimes = 0;
return 0;
}
}
else
{
excoff2 = 0;
}
}
if(pExcept3 != 0)
{
if( c == pExcept3[excoff3])
{
excoff3++;
if(pExcept3[excoff3] == 0x00)
{
pTimer->bUsed = 0;
pTimer->ltimes = 0;
return 0;
}
}
else
{
excoff3 = 0;
}
}
if(timer_checktimeout(pTimer) == 1)
{
pTimer->bUsed = 0;
pTimer->ltimes = 0;
return 0;
}
}
}
pTimer->bUsed = 0;
pTimer->ltimes = 0;
return 0;
}
void g20_sp_writebyte(BYTE c)
{
while ((UCSR0A & (1<<5))==0);
UDR0 = c;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -