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

📄 95sport1-1.c

📁 PIC18系列与RF315MHz连接透过RF传输数值资料
💻 C
字号:
// crystal 10MHz
#include "p18f242.h"
#include "type_define.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 bit_tr_one_time		flag1.bit0
#define bit_check_even		flag1.bit1

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;

uns16 isr_temp16;
uns16 test_count,test;
uns8 test_re,i,j,byte_temp,tx_high8,tx_low8,tx_buf,check_num,check_sum,tx_number;

#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;
		test_count++;
		TMR0IF=0;
	}
	if(ADIE && ADIF)
	{
		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();
	while(1)
	{
		if(test_count>=10000)
		{
			test++;
			test_count=0;
			bin_to_bcd();
			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;
							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;
					}
				}
			}
		}
	}
}

void mcu_io_init(void)
{
//I/O setting
	// PORTA setting
	PORTA=0x00;
	LATA=0x00;
	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

	//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)
{
	test_count=0;
	test=0;
	i=0;
	j=0;
	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 + -