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

📄 95sport1-1.c.bak

📁 PIC18系列与RF315MHz连接透过RF传输数值资料
💻 BAK
字号:
// crystal 10MHz
#include "p18f242.h"
#include "type_define.h"
#include "math.h"

#define AXIS_X_TOP	PORTAbits.RA0	//analog input
#define AXIS_Y_TOP	PORTAbits.RA1	//analog input
#define AXIS_X_BOT	PORTAbits.RA2	//analog input
#define AXIS_Y_BOT	PORTAbits.RA3	//analog input
#define AXIS_Z_TOP	PORTAbits.RA4	//analog input
#define AXIS_Z_BOT	PORTAbits.RA5	//analog input

#define RB0			PORTBbits.RB0	//output
#define RB1			PORTBbits.RB1	//output
#define RB2			PORTBbits.RB2	//output
#define RB3			PORTBbits.RB3	//output
#define TE			PORTBbits.RB4	//output

#define RC0			PORTCbits.RC0	//input
#define RC1			PORTCbits.RC1	//input
#define RC2			PORTCbits.RC2	//input
#define RC3			PORTCbits.RC3	//input
#define VT			PORTCbits.RC4	//input

#define RF_TR_DISABLE	1
#define RF_TR_ENABLE	0
#define TRUE			1
#define FAULT			0
#define START_CHAR		0xaa
//#define END_CHAR		0xaa

#define bit_tr_one_time		flag1.bit0
#define bit_check_even		flag1.bit1

//#define bit_hr_lo_pulse_width_count_begin		flag6.bit3
//#define bit_hr_hi_pulse_width_count_begin		flag6.bit4
void isr_high_code(void);
void isr_high(void);
void mcu_io_init(void);
void init_val(void);
void bin_to_bcd(void);
void calc_check_sum(void);

typedef struct
{
	unsigned bit0:1;
    unsigned bit1:1;
    unsigned bit2:1;
    unsigned bit3:1;
    unsigned bit4:1;
    unsigned bit5:1;
    unsigned bit6:1;
    unsigned bit7:1;
}uns8_flag;
uns8_flag flag1;

uns8 ad_count;
uns16 isr_temp16,axis_x_top_ad_val,axis_x_bot_ad_val,axis_y_top_ad_val,axis_y_bot_ad_val;
float32 x,y,z;
uns16 test_count,test;
uns8 test_re,i,j,byte_temp,tx_high8,tx_low8,tx_buf,check_num,check_sum,tx_number;
typedef struct uns32
{
	uns8 byte1;
	uns8 byte2;
	uns8 byte3;
	uns8 byte4;
}uns32_byte;
//uns32_byte test_32;
union
{
	uns32_byte test_32_bytes;
	uns32 test_32;
}union_test_32;

//uns32 test_32;

#pragma code hi_vector=0x0008
void isr_high_code(void)
{
	_asm
		goto	isr_high
	_endasm
}
#pragma code	/* return to the default code section */

#pragma interrupt isr_high //save=PROD//,section(".tmpdata"),TABLAT,TBLPTR,PCLATH,PCLATU
void isr_high(void)
{
	if(TMR0IF && TMR0IE)	//Timer0 interrupt,100ns interrupt one time
	{
		TMR0H=0xfa;
		TMR0L=0x80;
		ad_count++;
		test_count++;
//		RB0=!RB0;
		if(ad_count<=4)
			GO=1;
		if(ad_count==10)
		{
			ad_count=0;
		}
		TMR0IF=0;
	}
	if(ADIE && ADIF)
	{
		isr_temp16=ADRESH;
		isr_temp16=isr_temp16<<8;
		isr_temp16=isr_temp16 + ADRESL;
		if(CHS1==0)
		{
			if(CHS0==0)	//axis x top
			{
				axis_x_top_ad_val=isr_temp16;
			}
			else		//axis y top
			{
				axis_y_top_ad_val=isr_temp16;
			}
		}
		else
		{
			if(CHS0==0)	//axis x bot
			{
				axis_x_bot_ad_val=isr_temp16;
			}
			else		//axis y bot
			{
				axis_y_bot_ad_val=isr_temp16;
			}
		}
		ADIF=0;
	}
	if(TMR1IE && TMR1IF)
	{
		TMR1IF=0;
	}
	if(INT0IE && INT0IF)
	{
		INT0IF=0;
	}
	if(INT1IE && INT1IF)
	{
		INT1IF=0;
	}
}	
#pragma code

void main(void)
{
	uns8 uns8_temp1;
	mcu_io_init();
	init_val();
	x=3;
	y=4;
	x=x*x;
	y=y*y;
	x=x+y;
	z=sqrt(x);
	CHS0=1;
	while(1)
	{
//		if(TRMT==1 && TXIF==1)
//			TXREG=0x55;
		//PORTB=0b00000001;
		if(test_count>=10000)
		{
			test++;
//			PORTB=test | 0b00010000;
//			RB0=1;
//			RB1=0;
//			RB2=0;
//			RB3=0;
//			TE=RF_TR_ENABLE;
//			TE=RF_TR_DISABLE;
			test_count=0;
			bin_to_bcd();
			//check_num++;
			uns8_temp1=check_num & 0b00001111;
			if(uns8_temp1!=9)
				check_num++;
			else
			{
				uns8_temp1=check_num & 0b11110000;
				if(uns8_temp1!=0x90)
					check_num=check_num + 0x07;
				else
					check_num=0x00;
			}
			calc_check_sum();
			for(tx_number=0;tx_number<10;tx_number++)
			{
				j=5;
				tx_buf=START_CHAR;
				while(j!=0)
				{
					if(TRMT==1 && TXIF==1)
					{
						bit_check_even=TRUE;
						for(i=0;i<8;i++)
						{
							uns8_temp1=tx_buf << i;//test << i;
							uns8_temp1=uns8_temp1 & 0x80;
							if(uns8_temp1!=0)
								bit_check_even=!bit_check_even;
						}
						if(bit_check_even==TRUE)
							TX9D=0;
						else
							TX9D=1;
						TXREG=tx_buf;//test;
						j--;
						if(j==4)
							tx_buf=check_num;
						else if(j==3)
								tx_buf=tx_high8;
						 	 else if(j==2)
									tx_buf=tx_low8;
							  	  else
									tx_buf=check_sum;
					}
				}
			}
			//PORTBbits.RB7=!PORTBbits.RB7;
/*			test_count=20000;
			if(i<1)//8)
			{
				if(bit_tr_one_time==FAULT)
				{
					j=i%2;
					if(j==0)
					{
						if(i==0)
							byte_temp=union_test_32.test_32_bytes.byte1;
						else if(i==2)
								byte_temp=0;//union_test_32.test_32_bytes.byte2;
	        			 	else if(i==4)
									byte_temp=0;//union_test_32.test_32_bytes.byte3;
							  	else
									byte_temp=0;//union_test_32.test_32_bytes.byte4;
				//test++;
						test=byte_temp & 0b00001111;
					}
					else
					{
						test=byte_temp>>4;
					}
				//PORTB=0b00010001;
				//RB0=1;
				//RB1=0;
				//RB2=0;
				//RB3=0;
				//PORTBbits.RB7=!PORTBbits.RB7;
					if(PORTBbits.RB7==1)
						test=test | 0b10000000;
					PORTB=test | 0b00010000;
					TE=RF_TR_ENABLE;
					bit_tr_one_time=TRUE;
				}
				//TE=RF_TR_ENABLE;
				//for(i=0;i<250;i++)
				//	Nop();
				//TE=RF_TR_DISABLE;
			//	test_count=0;
				if(VT==1)// && TE==RF_TR_ENABLE)
				{
					PORTBbits.RB7=1;
					if(bit_tr_one_time==TRUE)
					{
						test_re=PORTC & 0b00001111;
						if(test_re==test)
						{
							TE=RF_TR_DISABLE;
							i++;
							bit_tr_one_time=FAULT;
						}
					}
				}
			}
			else
			{
				//PORTBbits.RB7=!PORTBbits.RB7;
				i=0;
				test_count=0;
				//union_test_32.test_32++;
				union_test_32.test_32_bytes.byte1++;
			}*/
		}
	}
}

void mcu_io_init(void)
{
//I/O setting
	// PORTA setting
	PORTA=0x00;
	LATA=0x00;
	ADCON1=0x89;	//RA0~RA5 analog input
	TRISA=0xff;
	// PORTB setting
	PORTB=0x00;
	LATB=0x00;
	TRISB=0x00;
	TE=RF_TR_DISABLE;
	// PORTC setting
	PORTC=0x00;
	LATC=0x00;
	TRISC=0b10111111;
//I/O setting

	//A/D setting
	ADCON0=0b11000001;	//Clock conversion=RC
						//select channel 0
						//ADON=1 A/D converter module is powered up
	ADIF=0;
	ADIE=1;
	ADIP=1;
	//A/D setting

	//Timer0 setting
	T0CS=0;		//TIMER0 clock=Fosc/4=40MHz/4=10MHz
				//10M/10k=1000
	T08BIT=0;	//16 bit counter mode
	TMR0H=0xfa;
	TMR0L=0x80;	//65536-1408=0xfa80
	PSA=1;		//no use prescaler
	T0PS2=0;
	T0PS1=0;
	T0PS0=0;
	TMR0ON=1;
	TMR0IE=1;	//TIMER0 enable
	//Timer0 setting

	//USART setting
	SPBRG=186;
	SPEN=1;
	TXSTA=0b01100000;//0x20;
	//USART setting

	//Timer1 setting
	//Timer1 setting

	//INT0 setting
	//INT0 setting

	//INT1 setting
	//INT1 setting
	IPEN=1;		//Enable priority levels on interrupts
	TMR0IP=1;	//High priority
	GIEH=1;		//Enables all high priority interrupts
}

void init_val(void)
{
	ad_count=0;
	test_count=0;
	test=0;
//	test_32=0;
	i=0;
	j=0;
	union_test_32.test_32=1;
	bit_tr_one_time=FAULT;
	check_num=0;
}

void bin_to_bcd(void)
{
	uns8 uns8_temp_1000,uns8_temp_100,uns8_temp_10,uns8_temp_1,uns8_temp1;
	uns16 uns16_temp1;
	uns8_temp_1000=test/1000;
	uns16_temp1=test%1000;
	uns8_temp_100=uns16_temp1/100;
	uns8_temp1=uns16_temp1%100;
	uns8_temp_10=uns8_temp1/10;
	uns8_temp_1=uns8_temp1%10;
	tx_high8=uns8_temp_1000<<4;
	tx_high8=tx_high8 | uns8_temp_100;
	tx_low8=uns8_temp_10<<4;
	tx_low8=tx_low8 | uns8_temp_1;
}

void calc_check_sum(void)
{
	uns8 uns8_temp1,uns8_temp_10;
	check_sum=check_num & 0b00001111;
	uns8_temp1=check_num>>4;
	check_sum=check_sum + uns8_temp1;
	uns8_temp1=tx_high8 & 0b00001111;
	check_sum=check_sum + uns8_temp1;
	uns8_temp1=tx_high8>>4;
	check_sum=check_sum + uns8_temp1;
	uns8_temp1=tx_low8 & 0b00001111;
	check_sum=check_sum + uns8_temp1;
	uns8_temp1=tx_low8>>4;
	check_sum=check_sum + uns8_temp1;
	uns8_temp_10=check_sum/10;
	uns8_temp1=check_sum%10;
	check_sum=uns8_temp_10<<4;
	check_sum=check_sum | uns8_temp1;
}

⌨️ 快捷键说明

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