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

📄 picmainrec.c

📁 Received Signal Strength Indicator in PIC24 with wireless communication. Ping a char and add RSSI in
💻 C
字号:
#include "p24fj128ga010.h"
#include "CONU2.h"
#include "CONU1.h"
#include "ADC.h"
#include "lcd.h"


_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) 
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRIPLL)

#define AINPUTS 0xFFC0

// RSSI Variables
unsigned int rise_edge=0, pwm_up1=0, pwm_up2=0, pwm_down=0;
// UART Variables
unsigned char u1char, u2char;
// ADC Variables
unsigned int adc_temp, adc_rssi;
// Two Way Ranging Variables
unsigned char source_id, my_id=0x03;
unsigned int ranging=0, rssi_base, ping_in, ping_out, pwm_detected, ton, toff;

// Auxiliary Variables
unsigned int aux;

//**************************************************************************************
// Initializations
void initAll(void)
{
	initU1();					// UART1
	initU2();					// UART2
	initLCD();					// LCD
	initADC(AINPUTS);			// ADC
	
	// Timer2 Configuration        
	T2CON = 0x8010;				// Enable TMR2, Tcy, Prescaler 1:8, 500ns Tick
	_T2IE=1;

	// UART1 RX Interrupt Enable
	_U1RXIE = 1;
	// UART2 RX Interrupt Enable
	_U2RXIE = 1;

	// Input Capture Configuration [Timer2 as the time base]
    IC1CON= 0x008B;
	
	TRISAbits.TRISA0=0;			// PortoA como saida
    TRISAbits.TRISA1=0;
    TRISAbits.TRISA2=0;
    TRISAbits.TRISA3=0;
    TRISAbits.TRISA4=0;
    TRISAbits.TRISA5=0;
    TRISAbits.TRISA6=0;
    TRISAbits.TRISA7=0;
}

// UART1 Interrupt Service Routine
void _ISR _U1RXInterrupt (void)
{
	u1char = U1RXREG;	// Get Char
	U1TXREG = u1char;
	source_id=u1char;

	_IC1IE=1;

	ping_in=1;

	_RA7^=1;
	_U1RXIF = 0;				// Clean UART1 Interrupt Flag
}
void _ISR _U2RXInterrupt (void)
{
	u2char = U2RXREG;
	_U1RXIF = 0;				// Clean UART2 Interrupt Flag
}

// Timer2 Interrupt Service Routine
void _ISR _T2Interrupt(void)
{
	TMR2=0;
	_IC1IE=1;

	pwm_detected=0;

	_RA0^=1;
	_T2IF=0;
}

// Input Capture Interrupt Service Routines
void _ISR _IC1Interrupt (void)
{
	pwm_detected=1;

	if (rise_edge==0)
	{
		TMR2=0;
		rise_edge=1;
		IC1CONbits.ICM=0b10;	// Change Input Capture to Falling Edge
		pwm_up1=IC1BUF;

		aux=IC1BUF;
		aux=IC1BUF;
		aux=IC1BUF;
	}
	else if (rise_edge==1)
	{
		rise_edge=2;
		IC1CONbits.ICM=0b11;	// Change Input Capture to Rising Edge
		pwm_down=IC1BUF;

		aux=IC1BUF;
		aux=IC1BUF;
		aux=IC1BUF;
	}
	else
	{
		rise_edge=0;
		IC1CONbits.ICM=0b10;	// Change Input Capture to Falling Edge
		pwm_up2=IC1BUF;

		aux=IC1BUF;
		aux=IC1BUF;
		aux=IC1BUF;
	}
	_IC1IF=0;
}
//**************************************************************************************
// Main
void main (void)
{
	char term[90];
	float temp, rssi_bot;

    initAll();

    putsLCD("[Bot] RSSI Test");
    while(1)
    {
		while(!ping_in)

		toff=pwm_up2-pwm_down;
		ton=(pwm_up1-pwm_down)-toff;

		if ((ton+toff)>=130 || (pwm_up1==0 || pwm_down==0 || pwm_up2==0))
		{
			ton=128;
			toff=0;
		}

		sprintf(term,",%d,%.2f,%.2f,\n",my_id,ton*500e-3,toff*500e-3);
		putsU1(term);

		clrLCD();
		sprintf(term,"[Bot] RSSI Test");
		putsLCD(term);
		setLCDC(0x40);
		sprintf(term,"%d %.2f %.2f",my_id,ton*500e-3,toff*500e-3);
		putsLCD(term);
		ping_in=0;
    }
} // Main

⌨️ 快捷键说明

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