⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g20serialdrv.c

📁 使用avr--cc2420模块程序,需要原理图的联系 QQ:120304948
💻 C
字号:
#include <mega128.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include "typedef.h"

#include "app.h"
#include "attention.h"
#include "g20modem.h"
#include "g20serialdrv.h"

#include "rtc.h"
#include "serialdrv.h"
#include "timer.h"
#include "UDP.h"
#include "protocol.h"

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 >= G20MAXLEN)
	{
		my_g20_sp_status.empty = 0;
	}
	
	if(my_g20_sp_status.empty > SPOVERFLOW)
	{
	    my_g20_sp_status.overflow = TRUE;
	}
}


void g20_udp_getip()
{
	g20_count_comma = 0;
	
	while(g20_count_comma <4)
	{
        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)
	{
        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)
			{
                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 [USART0_RXC] void usart0_rx_isr(void)
{
    unsigned char status,data;
    
    G20_RTS = 1;     // disable rts
	status=UCSR0A;
	data=UDR0;
	if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
	{
		//UDR1 = data;
		UCSR0B &= 0x7F;     //disable uart0 interrupt
	    EINT;              //enable all interrupt
	    g20_rcv_pre_proc(data);	
		DINT;              //disable all interrupt
        UCSR0B |= 0x80;     //enable uart0 interrupt
#if defined (MONITOR_G20_SP)		
		while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
		UDR1 = data;
#endif
	}
	G20_RTS = 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, SPMAXLEN);
	
// 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;
	UCSR0C=0x06;
	UBRR0H=0x00;
	UBRR0L=0x03;

	// set rts
    DDRA.7 = 1;
//    PORTA.7 = 1;
	G20_RTS = 1;
	
	G20_RTS = 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 >= G20MAXLEN)
		{
			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)
	{
        wdr();
		while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
		UDR0 = *pBuf ++;	
	}
}

void g20_sp_sendb(BYTE * pBuf)
{
	while(*pBuf)
	{
        wdr();
		while ((UCSR0A & DATA_REGISTER_EMPTY)==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 = NULL;
	pTimer = timer_settimer(timeouts);
	if(pTimer == NULL)
		return FALSE;
	while(timer_checktimeout(pTimer)!= TRUE)
	{
        wdr();
        while(g20_sp_not_empty())
        {
            wdr();
            c = g20_sp_getchar();
            if(pExpect != NULL)
            {
                if( c == pExpect[expoff])
                {
                    expoff ++;
                    if(pExpect[expoff] == 0x00)
                    {
						pTimer->bUsed = FALSE;
						pTimer->ltimes = 0;
                        return TRUE;
                    }
                }
                else
                {
                    expoff =0;
                }
            }
            
            if(pExpect1 != NULL)
            {
                if(c == pExpect1[expoff1])
                {
                    expoff1 ++;
                    if(pExpect1[expoff1] == 0x00)
                    {
						pTimer->bUsed = FALSE;
						pTimer->ltimes = 0;
                        return TRUE;
                    }
                }
                else
                {
                    expoff1 = 0;
                }
            }
    
            if(pExcept1 != NULL)
            {
                if( c == pExcept1[excoff1])
                {
                    excoff1++;
                    if(pExcept1[excoff1] == 0x00)
                    {
						pTimer->bUsed = FALSE;
						pTimer->ltimes = 0;
                        return FALSE;
                    }
                }
                else
                {
                    excoff1 = 0;
                }
            }
    
            if(pExcept2 != NULL)
            {
                if( c == pExcept2[excoff2])
                {
                    excoff2++;
                    if(pExcept2[excoff2] == 0x00)
                    {
						pTimer->bUsed = FALSE;
						pTimer->ltimes = 0;
                        return FALSE;
                    }
                }
                else
                {
                    excoff2 = 0;
                }
            }
            
            if(pExcept3 != NULL)
            {
                if( c == pExcept3[excoff3])
                {
                    excoff3++;
                    if(pExcept3[excoff3] == 0x00)
                    {
						pTimer->bUsed = FALSE;
						pTimer->ltimes = 0;
                        return FALSE;
                    }
                }
                else
                {
                    excoff3 = 0;
                }
            }
            if(timer_checktimeout(pTimer) == TRUE)
            {
				pTimer->bUsed = FALSE;
				pTimer->ltimes = 0;
                return FALSE;
            }
        }
    }
    pTimer->bUsed = FALSE;
	pTimer->ltimes = 0;
	return FALSE;
}

void g20_sp_writebyte(BYTE c)
{
	while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
	UDR0 = c;		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -