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

📄 user.c

📁 ADS-B接收机DIY全套资料
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************
 * FileName:        user.c
 * Dependencies:    See INCLUDES section below
 * Processor:       PIC18
 * Compiler:        C18 2.30.01+
 * Company:         sprut
 * Copyright:       2007-2010 Joerg Bredendiek (sprut)
 *
 *
 ********************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */



/*** Pinbelegung*************************************************************
*
*	PORTA	0	Comp-Eingang (-) (mit RA2 verbinden)
*			1	ADC-Eingang
*			2	Uref-Ausgang (mit RA0 verbinden)
*			3	Comp-Eingang (+) mit miniadsb verbinden
*			4	Comp-Ausgang -> RC0
*
*	PORTB	0..3	SWITCH
*			5..7	LEDs
*
*	PORTC	0	TTL-Eingang (mit PA4 verbinden)
*			4	USB D-
*			5	USB	D+
*
*****************************************************************************/

//#define	NO_CDC	//MCD
//#define	CDC_HEX	// nonsens
#define	CDC_ASCII


/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include <usart.h>
#include "system\typedefs.h"
#include "system\usb\usb.h"
#include "io_cfg.h"             // I/O pin mapping

#include "delays.h"
//#include "system\interrupt\interrupt.h"
#include "user\user.h"
#include "user\adsbin.h"

//#include "timers.h"
//#include "i2c.h"
//#include "pwm.h"
//#include "mwire.h"
//#include "spi.h"


/** V A R I A B L E S ********************************************************/
#pragma udata

#ifdef	CDC_ASCII
char	input_buffer[64];
byte	rgetascii;
byte 	hb_counter 		= 0;
byte	ib_counter 		= 0;
char	asciiKor   		= 0;
byte	befehl     		= 0;	//eine gueltige Befehlszeile wurde empfangen
byte	wertgueltig  	= 0; 	//steht in rgetascii schon was?
byte	zeileangefangen = 0; 	//kam schon mal ein #?
#endif	//CDC_ASCII


byte				counter;
DATA_PACKET 		dataPacket;

byte				adsb[14];
byte				AAA0;
byte				AAA1;


//ADC
byte				AcdMaskeA	= 0;	// von ADC verwendete Pins


/** D E C L A R A T I O N S **************************************************/
#pragma code
//Aufruf nach Reset
void UserInit(void)
{
	// init COMP: Mode 001


	// init ADC: input ist RA1



	// init pins
	TRISA 	= 0xFF;
	TRISB 	= 0xFF;
	TRISC 	= 0xFF;

}//end UserInit




/******************************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void)
{   
    // User Application USB tasks
    if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return;
    ServiceRequests();
}//end ProcessIO



// A S C I I *****************************************************************************
#ifdef CDC_ASCII

// ermittelt, ob es sich bei einem Zeichen um ein Trennzeichen handelt
// ermittle den noetigen Wert fuer asciiKor
// alles ausser 0..9/a..f/A.F sind trennzeichen
// aus A..F/a..F wird 10..15 (0x0A..0x0F)
// input: zeichen
// output:	-1 : Trennzeichen
//			-2 : Zeilenende
//			-3 : Zeilenanfang
//			 0 : ASCII-Zeichen
byte	trennzeichen(char	zeichen)
{
	if (zeichen== '#') return -3; //SOL
	if (zeichen==0x00) return -2; //EOL
	if (zeichen==0x0A) return -2; //EOL
	if (zeichen==0x0D) return -2; //EOL

	asciiKor = 0x00;
	if (zeichen < '0') return -1;	//   .. /
	if (zeichen <='9') 				// 0 .. 9
	{
		asciiKor = '0';
		return 0;
	}
	if (zeichen < 'A') return -1;	// : .. @
	if (zeichen <='F') 				// A .. F
	{
		asciiKor = 'A'-10;
		return 0;
	}
	if (zeichen < 'a') return -1;	// G .. '
	if (zeichen <='f') 				// a .. f
	{
		asciiKor = 'a'-10;
		return 0;
	}
	return -1;						// g .. 
} //trennzeichen


// input_buffer[ib_counter] steht auf letztem Trennzeichen
// input_buffer lesen bis zum n鋍hsten Trennzeichen
// wandeln in Hex -> rgetascii
// bei Zeilenende  -1
byte	getascii(void)
{
	rgetascii = 0;
	while (trennzeichen(input_buffer[ib_counter]) == -1) ib_counter++;  // trennzeichen 黚ersprinegn
	if    (trennzeichen(input_buffer[ib_counter]) == -2) return (-1);	//EOL
//	if (ib_counter >= mCDCGetRxLength())               return (-1);	    //EOL
	while (trennzeichen(input_buffer[ib_counter]) ==  0)
	{
		rgetascii <<= 4;
		rgetascii += (input_buffer[ib_counter] - asciiKor);	
		ib_counter++;
	}
	return(0);
}	//getascii


//input_buffer -> dataPacket
//  #xx-xx-xx-xx-xx-xx
// einlesen der per RS232 empfangenen Zeichen
// beim Zeilenende wird befehl=1 gesetzt
void	AsciiToHexParser(void)
{
	for (ib_counter = 0; ib_counter < mCDCGetRxLength(); ib_counter++)
	{
		switch (trennzeichen(input_buffer[ib_counter]))
		{
			case -3:									//zeilenanfang
				for (hb_counter = 0; hb_counter < 64; hb_counter++)
					dataPacket._byte[hb_counter] = 0;
				hb_counter  = 0;
				befehl      = 0;
				rgetascii   = 0;
				wertgueltig = 0;
				zeileangefangen = 1;
				break;
			case -2:									//zeilenende
				if (wertgueltig == 1)
				{
					dataPacket._byte[hb_counter] = rgetascii;
					hb_counter++;
					wertgueltig = 0;
				}
				rgetascii = 0;
				if (zeileangefangen == 1) befehl = 1;
				break;
			case -1:									//trennzeichen
				if (wertgueltig == 1)
				{
					dataPacket._byte[hb_counter] = rgetascii;
					hb_counter++;
					wertgueltig = 0;
				}
				rgetascii = 0;
				break;
			case 0: 									//ziffer
				rgetascii <<= 4;
				rgetascii += (input_buffer[ib_counter] - asciiKor);	
				wertgueltig = 1;						
				break;
		} //switch
	}
}	//AsciiToHexParser


// wandelt Zahl (0..15) in Zeichen ('0' .. 'A')
char	zeichen( byte wert)
{
	if (wert < 10) return (wert+'0');
	return (wert-10+'A');
}


//dataPacket -> input_buffer
void	HexToAsciiParser(void)
{
	char	puffer;
	for (ib_counter= 0; ib_counter<64; ib_counter++)
		input_buffer[ib_counter] = 0;
	ib_counter = 1;
	hb_counter = 0;
	input_buffer[0] = '@';
	for (hb_counter= 0; hb_counter<16; hb_counter++)
	{
		puffer = dataPacket._byte[hb_counter];
		input_buffer[ib_counter] = zeichen((puffer & 0xF0) >> 4);
		ib_counter++;
		input_buffer[ib_counter] = zeichen( puffer & 0x0F);
		ib_counter++;
		input_buffer[ib_counter] = '-';
		ib_counter++;
	}
	input_buffer[ib_counter] = 0x0A;
	ib_counter++;
	input_buffer[ib_counter] = 0x0D;
	ib_counter++;
	input_buffer[ib_counter] = 0x00;
	counter = ib_counter;
}

#endif //CDC_ASCII



// Kommando auswerten und ausf黨ren **********************
void ServiceRequests(void)
{
    byte	index;
	word	big_counter;
	WORD	uidaten;
  
    // wurde via USB etwas empfangen?

	#ifdef NO_CDC	// MCD Treiber
    if(USBGenRead((byte*)&dataPacket,sizeof(dataPacket)))  //byte USBGenRead(byte *buffer, byte len)	//normal
	#endif	//NO_CDC

	#ifdef CDC_HEX	//CDC-Treiber im HEX-Format (nonsens)
    if(getsUSBUSART((char*)&dataPacket,sizeof(dataPacket)))  //byte getsUSBUSART(char *buffer, byte len)	//cdc hex
	#endif	//CDC_HEX

	#ifdef CDC_ASCII	// CDC Treiber
    if(getsUSBUSART((char*)&input_buffer,sizeof(input_buffer)))  //byte getsUSBUSART(char *buffer, byte len)	//cdc ascii
		AsciiToHexParser();
	if (befehl==1)
	#endif	//CDC_ASCII

    {
        counter = 0;

		#ifdef CDC_ASCII
		befehl = 0;
		#endif	//CDC_ASCII

		switch(dataPacket.CMD) 		//das ist in Assembler ein riesieger Sprungverteiler 
        {
			// eine Kennummer aus dem PIC auslesen
            case READ_VERSION:
                //dataPacket._byte[1] is len
                dataPacket._byte[2] = MINOR_VERSION; // Firmware-Version steht in user.h

⌨️ 快捷键说明

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