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

📄 main.c

📁 whereavr是一个用ATMEL M8实现的AX.25精简协议栈,搜了很多资料,这个是对学习自动位置报告系统最好的实例
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************File:			Main.c				Main WhereAVR function library.Functions:	extern int	main(void)				extern void mainTransmit(void)				extern void mainReceive(void)				extern void ax25rxByte(unsigned char rxbyte)				extern void mainDelay(unsigned int timeout)				extern void Delay(unsigned int timeout)				SIGNAL(SIG_OVERFLOW0)				SIGNAL(SIG_OVERFLOW1)				SIGNAL(SIG_OVERFLOW2)				SIGNAL(SIG_COMPARATOR)Created:		1.00	10/05/04	GND	Gary DionRevisions:	1.01	11/02/04	GND	Major modification on all fronts				1.02	12/01/04	GND	Continued optimization				1.03	06/23/05	GND	Converted to C++ comment style and cleaned upCopyright:	(c)2005, Gary N. Dion (me@garydion.com). All rights reserved.				This software is available only for non-commercial amateur radio				or educational applications.  ALL other uses are prohibited.				This software may be modified only if the resulting code be				made available publicly and the original author(s) given credit.*******************************************************************************/// OS headers#include <avr/eeprom.h>#include <avr/interrupt.h>#include <avr/io.h>#include <avr/signal.h>// General purpose include files#include "StdDefines.h"// App required include files#include "ADC.h"#include "ax25.h"#include "Main.h"#include "Messaging.h"#include "Serial.h"#define	RXSIZE (256)#define	WatchdogReset() asm("wdr")		// Macro substitution to kick the dog// Static functions and variablesvolatile unsigned char delay;				// State of Delay functionvolatile unsigned char maindelay;		// State of mainDelay functionstatic unsigned char	rxbytes[RXSIZE];	// Incoming character arraystatic unsigned char msg_start;			// Index of message start after headerstatic unsigned char	msg_end;				// Index of message ending characterstatic unsigned char	command;				// Used just for togglingstatic unsigned char	transmit;			// Keeps track of TX/RX statestatic unsigned char	rxtoggled;			// Signals frequency just toggledstatic unsigned short crc;					// Current checksum for incoming messagestatic unsigned short dcd;					// Carrier detect of sortsvolatile char busy;							// Carrier detect of sorts/******************************************************************************/extern int	main(void)/******************************************************************************** ABSTRACT:	Main program entry function.** INPUT:		None* OUTPUT:	None* RETURN:	None*/{	static unsigned short loop;			// Generic loop variable	static unsigned short servo_loop;	// Generic loop variable	static unsigned char	ones_seconds;	// Remembers tens digit of seconds	static unsigned char seconds;			// Holds seconds calculated from GPS	//Initialize serial communication functions	ADCInit();	SerInit();	MsgInit();	// PORT B - Sinewave Generation, and	//	Bit/Pin 5 (out) connected to a 1k ohm resistor	//	Bit/Pin 4 (out) connected to a 2k ohm resistor	//	Bit/Pin 3 (out) connected to a 3.9k ohm resistor	//	Bit/Pin 2 (out) connected to an 8.2k ohm resistor	//	Bit/Pin 1 (out) connected to the PTT circuitry	//	Bit/Pin 0 (out) DCD LED line	PORTB = 0x00;							// Initial state is everything off	DDRB  = 0x3F;							// Data direction register for port B	// PORT D - General use port	//	Bit/Pin 2 (out) connected the control line on a servo	PORTD = 0x00;							// Initial state is everything off	DDRD  = 0x04;							// Data direction register for port D	// Initialize the Analog Comparator	SFIOR = 0;								// Select AIN1 as neg. input	ACSR = (1<<ACBG) | (1<<ACIE);		// Select Bandgap for pos. input	//	Initialize the 8-bit Timer0 to clock at 1.8432 MHz	TCCR0 = 0x02; 							// Timer0 clock prescale of 8	// Use the 16-bit Timer1 to measure frequency; set it to clock at 1.8432 MHz	TCCR1B = 0x02;							// Timer2 clock prescale of 8	//	Initialize the 8-bit Timer2 to clock at 1.8432 MHz	TCCR2 = 0x07; 							// Timer2 clock prescale of 1024	// Enable Timer interrupts	TIMSK = 1<<TOIE0 | 1<<TOIE2; // | 1<<TOIE2;	// Enable the watchdog timer	WDTCR	= (1<<WDCE) | (1<<WDE);		// Wake-up the watchdog register	WDTCR	= (1<<WDE) | 7;				// Enable and timeout around 2.1s	// Enable interrupts	sei();	// Reset watchdog	WatchdogReset();	// First move the servo to the starting position	for (servo_loop = 0 ; servo_loop < 200 ; servo_loop++)	{		PORTD |= 0x04;	// servo signal on pin 4 PORT D2//		Delay(5);			// 1.17 ms pulse - fully clockwise		Delay(23);			// 1.51 ms pulse - center position - latch locked//		Delay(32);			// 1.87 ms pulse - fully anticlockwise - release!!!!		PORTD &= ~0x04;		Delay(50);			// 18.0 ms between servo pulses		WatchdogReset();	}	// Initialization complete - system ready.  Run program loop indefinitely.	while (TRUE)	{//		txtone = SPACE;						// Debug tone for testing (MARK or SPACE)//		while(1) WatchdogReset();			// Debug with a single one tone//		while(1) ax25sendByte(0);			// Debug with a toggling tone		Delay(250);		Delay(250);		Delay(250);		Delay(250);		Delay(250);//		while(busy)	Delay(250);			// Wait for break (not on balloons!!!)		MsgPrepare();							// Prepare variables for APRS position		mainTransmit();						// Enable transmitter		if (command == 0)						// Default message to be sent		{			MsgSendPos();						// Send Position Report and comment		}		else		{			if (command == 'S')			{				ax25sendEEPROMString(48);	// Send ">See garydion.com"			}			if (command == 'T')			{				MsgSendTelem();				// Send Telemetry and comment			}		}		mainReceive();							// Disable transmitter		command = 0;		for (loop = 0 ; loop < 3900 ; loop++)	// Wait a maximum of 30 seconds		{			Delay(112);									// Delay is 10ms with 112			if (msg_end)								// Meanwhile, if we get a packet			{				if (rxbytes[msg_start] == ':')		// Look for message character				if (rxbytes[msg_start + 1] == 'N')	// Look for my callsign				if (rxbytes[msg_start + 2] == '4')				if (rxbytes[msg_start + 3] == 'T')				if (rxbytes[msg_start + 4] == 'X')				if (rxbytes[msg_start + 5] == 'I')				if (rxbytes[msg_start + 6] == '-')				if (rxbytes[msg_start + 7] == '1')	// And my SSID				if (rxbytes[msg_start + 8] == '1')				if (rxbytes[msg_start + 9] == ' ')				if (rxbytes[msg_start + 10] == ':')				{					command = rxbytes[msg_start + 11];	// Grab command character					if (rxbytes[msg_start + 12] == '{')	// Sender expects acknowledge					{						Delay(250);				// Pause						Delay(250);						Delay(250);						Delay(250);						Delay(250);//						while(busy) Delay(250);			// Wait for clear channel						mainTransmit();						// Enable transmitter						MsgSendAck (rxbytes,msg_start);	// Send Ack text						mainReceive();							// Disable transmitter					}					if (command == 'P')					{						loop = 32766;			// Exit timeout loop to transmit					}					if (command == 'R')		// "R" means release					{						for (servo_loop = 0 ; servo_loop < 200 ; servo_loop++)						{							PORTD |= 0x04;		// servo signal on pin 4 PORT D2							Delay(32);			// latch released							PORTD &= ~0x04;							Delay(50);			// 18.0 ms between servo pulses						}					}					if (command == 'L')		// "L" means lock					{						for (servo_loop = 0 ; servo_loop < 200 ; servo_loop++)						{							PORTD |= 0x04;		// servo signal on pin 4 PORT D2							Delay(23);			// latch locked							PORTD &= ~0x04;							Delay(50);			// 18.0 ms between servo pulses						}					}				}		// end if (rxbytes[msg_start + 10] == ':')				msg_end = 0;					// Forget there was a message				ACSR |= (1<<ACIE);			// Re-enable the comparator			}		// end if (msg_end)			// The following condition is true once per second.			if (Time_Temp[5] != ones_seconds)	// Has ones digit changed?			{				ones_seconds = Time_Temp[5];		// Remember new ones digit				// Convert 'seconds' from ascii to use in time slotting functions				seconds = (Time_Temp[4] - 48) * 10 + Time_Temp[5] - 48;				if (seconds == 5 || seconds == 35)	// Check for transmit slots				{					loop = 32766;						// Exit timeout loop to transmit				}				if (seconds == 42)				{					// do something else interesting				}			}		}		// end for (loop = 0 ; loop < 3900 ; loop++)	}	return(1);}		// End main/******************************************************************************/extern void mainTransmit(void)/******************************************************************************** ABSTRACT:	Do all the setup to transmit.** INPUT:		None* OUTPUT:	None* RETURN:	None*/{	UCSRB &= ~((1<<RXCIE)|(1<<TXCIE));	// Disable the serial interrupts	ACSR &= ~(1<<ACIE);						// Disable the comparator	TCCR0 = 0x03; 								// Timer0 clock prescale of 64	TCCR1B = 0x02;								// Timer1 clock prescale of 8	TCCR2 = 0x02; 								// Timer2 clock prescale of 8	transmit = TRUE;							// Enable the transmitter	ax25sendHeader();							// Send APRS header	return;}		// End mainTransmit(void)/******************************************************************************/extern void mainReceive(void)/******************************************************************************** ABSTRACT:	Do all the setup to receive or wait.** INPUT:		None* OUTPUT:	None* RETURN:	None*/{	ax25sendFooter();							// Send APRS footer	transmit = FALSE;							// Disable transmitter	PORTB &= 0x3D;								// Make sure the transmitter is disabled	TCCR0 = 0x02; 								// Timer0 clock prescale of 8	TCCR1B = 0x02;								// Timer1 clock prescale of 8	TCCR2 = 0x07;								// Timer2 clock prescale of 1024	ACSR |= (1<<ACIE);						// Re-enable the comparator	UCSRB |= (1<<RXCIE)|(1<<TXCIE);		// Re-enable the serial interrupts	MsgHandler(0);								// Reset the GPS decoding engine

⌨️ 快捷键说明

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