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

📄 receive.c

📁 一个轮胎温度传感传输系统
💻 C
字号:
#include <ht46c24.H>

#pragma vector isr_4 @ 0x4
#pragma vector isr_8 @ 0x8
#pragma vector isr_c @ 0xc
unsigned char check_tmr ;
//ISR for safequard
void isr_4(){} // external ISR
//void isr_8(){} // timer/event 0
void isr_c(){} // timer/event 1
void isr_8()
   {		
	check_tmr= 1;
    
    
    }
void set_tmr1();

void key();

//initialize registers for safeguard
void safeguard_init(){
        _tmr0c = 0;
        _tmr0h = 0;
        _tmr0l = 0;
        _pac = 0xff;
		_pbc = 0x0;
		_pcc = 0x0;

}


#define rx   _pa2               //receive pin
#define _pac2 _13_2
#define _pac3 _13_3
#define key1 _pa0
#define key2 _pa1
#define key3 _pa3
#define _pac1 _13_1
#define _pac0 _13_0

#define _tmr0c4 _0e_4			//start timer0
const unsigned char presstab[10]={
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f};

const unsigned char temptab[6]={0x55,
0x86, 0xdb, 0xcf, 0xe6,0x40};
unsigned char flagkey1;

unsigned char id1,id2,id3,id4,count11,count12,count13,count21,count22,count23,count31,count32,count33,count41,count42,count43;
unsigned char data1n,data1p,data1t;
unsigned char data2n,data2p,data2t;
unsigned char data3n,data3p,data3t;
unsigned char data4n,data4p,data4t,ul,ll;
void receive(unsigned char *,unsigned char *,unsigned char *,unsigned char *);
unsigned char check(unsigned char,unsigned char);
void showt(unsigned char,unsigned char);
void showp(unsigned char,unsigned char);
void set_tmr();
unsigned char ckcrc(unsigned char,unsigned char,unsigned char,unsigned char);
    unsigned char flagkey2;
    unsigned char flagkey3;



//system frequency: 4MHz
//#define T 38 //baudrate 19200 = 4M/4/(T+14) => T = 38
//define T 90 //baudrate 9600 = 4M/4/(T+14) => T = 90
//#define T 194 //baudrate 4800 = 4M/4/(T+14) => T = 194
#define T 402 //baudrate 2400 = 4M/4/(T+14) => T = 402



void main(){
		
		unsigned char chk1,chk2,chk3,chk4,j,choose;
		long count;
		unsigned char dataregn,dataregp,dataregt,dataregc;
        safeguard_init();
        j=0;
		check_tmr = 0;
		count=0;
		ul=0xff;
		ll=0x00;
		id4=id1=id2=id3=0;
count11=count12=count13=count21=count22=count23=count31=count32=count33=count41=count42=count43=0;
		flagkey1=1;
	flagkey2=flagkey3=1;
		choose=0;

        _pac2 = 1;      //set receive pin to input mode
		chk1=chk2=chk3=chk4=0;
		data1n=1;data2n=2;data3n=3;data4n=4;
		data1p=data2p=data3p=data4p=data1t=data2t=data3t=data4t=0;

		_intc0 = 0x05;
		_tmr0c = 0x80;
 
   wait:     for(j=0;j<1;){		
                    if (!key1) key();
                    
		
					if(!rx)
					{j=0;							
					
					switch(choose){
					case 0:if(!chk1) showp(data1n,data1p);
					else showp(data1n,0);break;
					case 1:if(!chk1)showt(data1n,data1t);
					else showt(data1n,0);break;
					case 2:if(!chk2)showp(data2n,data2p);
					else showp(data2n,0);break;
					case 3:if(!chk2)showt(data2n,data2t);
					else showt(data2n,0);break;
					case 4:if(!chk3)showp(data3n,data3p);
					else showp(data3n,0);break;
					case 5:if(!chk3)showt(data3n,data3t);
					else showt(data3n,0);break;
					case 6:if(!chk4)showp(data4n,data4p);
					else showp(data4n,0);break;
					case 7:if(!chk4)showt(data4n,data4t);
					else showt(data4n,0);break;
					}
					count++;
				if(count>3000){choose++;choose&=0x07;count=0;}
					}
					else j++;
				

				}	
					
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}

					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}
					if(rx) goto wait;
				
					set_tmr();
					while (check_tmr==0)
						{}

					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}
					if(rx) goto wait;
				
					set_tmr();
					while (check_tmr==0)
						{}

					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(!rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}
					if(rx) goto wait;
				
					set_tmr();
					while (check_tmr==0)
						{}

					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}
					if(rx) goto wait;
				
					set_tmr();
					while (check_tmr==0)
						{}

					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(!rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}
					if(rx) goto wait;
				
					set_tmr();
					while (check_tmr==0)
						{}

					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}
					if(rx) goto wait;
				
					set_tmr();
					while (check_tmr==0)
						{}

					if(!rx)	 goto wait;
					set_tmr();
					while (check_tmr==0)
						{}
							
					if(!rx)	 goto wait;
			
					set_tmr();
					while (check_tmr==0)
						{}
					if(rx) goto wait;
				
					set_tmr();


							 
							
                receive(&dataregn,&dataregp,&dataregt,&dataregc);
				if(ckcrc(dataregn,dataregp,dataregt,dataregc)==0)
				{
				
				if (dataregn==id1) {chk1=check(dataregp,dataregt);data1p=dataregp;data1t=dataregt;}
				else {if (dataregn==id2) {chk2=check(dataregp,dataregt);data2p=dataregp;data2t=dataregt;}
				else {if (dataregn==id3) {chk3=check(dataregp,dataregt);data3p=dataregp;data3t=dataregt;}
				else {if (dataregn==id4) {chk4=check(dataregp,dataregt);data4p=dataregp;data4t=dataregt;}
				}
                 }
                 }
				}
				goto wait;
        }

unsigned char check(unsigned char val1,unsigned char val2){
	if(val1<ll)
	return(1);
	else 	if(val2>ul)
	return(1);
	
	return(0);


}

void showp(unsigned char num,unsigned char press){
		char yu;
		_pb = presstab[num];//segment code
		_pc = 0xf7;// wei code
		_delay(28);
		yu = press%(0x0a);
		_pb = presstab[yu];
		_pc = 0xfe;
		_delay(28);
		press = press/(0x0a);
		yu = press%(0x0a);
		_pb = presstab[yu];
		_pc = 0xfd;
		_delay(28);
		yu = press/(0x0a);
		_pb = presstab[yu];
		_pc = 0xfb;
		_delay(28);
		
		
}

void showt(unsigned char num,unsigned char temp){
		char yu;
		
		_pb = temptab[num];//segment code
		_pc = 0xf7;// wei code
		_delay(28);
		if (temp & 0x80){

		_pb = temptab[5];
		_pc = 0xfb;
		temp &=0x7f;
		_delay(28);
		yu = temp%(0x0a);
		_pb = presstab[yu];
		_pc = 0xfe;
		_delay(28);
		temp = temp/(0x0a);
		yu =  temp%(0x0a);
		_pb = presstab[yu];
		_pc = 0xfd;
		_delay(28);
							}		
		else 
		{
			yu = temp%(0x0a);
		_pb = presstab[yu];
		_pc = 0xfe;
		_delay(28);
		temp = temp/(0x0a);
		yu =  temp%(0x0a);
		_pb = presstab[yu];
		_pc = 0xfd;
		_delay(28);
			yu = temp/(0x0a);
		_pb = presstab[yu];
		_pc = 0xfb;
		_delay(28);
		}
	
}

unsigned char ckcrc(unsigned char a,unsigned char b,unsigned char c,unsigned char d){



unsigned char areg;
unsigned char breg;
unsigned char creg;
unsigned char dreg;
unsigned char i;
		areg = a;
breg=b;
 creg=c;
dreg=d;
for(i=0;i<24;i++)
{
while(areg&0x80){areg ^=0xb5;}


areg<<=1;
if(breg&0x80)areg|=1;
else areg&0xfe;
breg<<=1;
if(creg&0x80)breg|=1;
else breg&0xfe;
creg<<=1;
if(dreg&0x80)creg|=1;
else creg&0xfe;
dreg<<=1;

}

return(areg);


}

void receive(unsigned char *val1,unsigned char *val2,unsigned char *val3,unsigned char *val4){
        unsigned char i, v;

        v = 0;

        for(i=0; i<8; i++){	v >>= 1;
				while (check_tmr==0)
						{}
                set_tmr();

                if (rx) {while (check_tmr==0)
						{}
					set_tmr();		
					if(rx)	v |= (unsigned char)0x80;
			
					
					while (check_tmr==0)
						{}
					if(!rx) 		
					set_tmr();
					else return;
						}
				else return;
			
               
        }

		*val1 = v;
        v = 0;
        for(i=0; i<8; i++){			 v >>= 1;
								while (check_tmr==0)
						{}
                set_tmr();

                if (rx) {while (check_tmr==0)
						{}
					set_tmr();		
					if(rx)	v |= (unsigned char)0x80;
			
					
					while (check_tmr==0)
						{}
					if(!rx) 		
					set_tmr();
					else return;
						}
				else return;
        }
       
        *val2 = v;
        v = 0;
        for(i=0; i<8; i++){v >>= 1;
								while (check_tmr==0)
						{}
                set_tmr();

                if (rx) {while (check_tmr==0)
						{}
					set_tmr();		
					if(rx)	v |= (unsigned char)0x80;
			
					
					while (check_tmr==0)
						{}
					if(!rx) 		
					set_tmr();
					else return;
						}
				else return;
        }
      
        *val3 = v;
        v = 0;
        for(i=0; i<8; i++){v >>= 1;
								while (check_tmr==0)
						{}
                set_tmr();

                if (rx) {while (check_tmr==0)
						{}
					set_tmr();		
					if(rx)	v |= (unsigned char)0x80;
			
					
					while (check_tmr==0)
						{}
					if(!rx) 		
					set_tmr();
					else return;
						}
				else return;
                
        }
	
        *val4 = v;
}



void set_tmr()
{
	_tmr0c4 = 0;
	_tmr0l = 0x17;
	_tmr0h = 0xfc;
	_tmr0c4 = 1;
	check_tmr = 0;
}

void set_tmr1()
{
	_tmr0c4 = 0;
	_tmr0l = 0xf3;
	_tmr0h = 0xfe;
	_tmr0c4 = 1;
	check_tmr = 0;
}


void key()
{   


    long id;


    set_tmr1(); 
    
loop4:		if(check_tmr==1) goto loop5;
	if (!key1)
        { 
          while (!key1)
             { //_delay(100);
               if(check_tmr==1) goto loop5;
		loop:		if (key1)
               { 
                flagkey1++;
                if (flagkey1&4) {flagkey1=0;while(check_tmr!=1){}
                                                    return;}
				else goto loop0;
               }
               else goto loop;
 			 }
        }

loop0:			if (!key2)
        { 
          while (!key2)
             { if(check_tmr==1) goto loop5;
loop1:				if (key2)
               { 
                flagkey2++;
				if(flagkey2>3)flagkey2=1;
                goto loop2;
               }
               else goto loop1;
 			 }
        }
   
              loop2:         if (!key3)
                          { 
                             while(!key3)
                             {_delay(10000);}
                             _delay(10000);
                             { 
                                switch(flagkey1){
                              case 1:{
                                    switch(flagkey2){
                                    case 1:id1+=0x64;count11++;if(count11>2){id1-=0x2c;count11=0;}
                                    break;
                                    case 2:id1+=0x0a;count12++;if(count12>9){id1-=0x64;count12=0;}if(count11==2){if(count12>5){id1=0;count12=0;}else if(count12==5&&count13>5){id1=0;count12=0;}} break;
                                    case 3:id1+=1; count13++;if(count13>9){id1-=0x0a;count13=0;}if(count11==2){if(count12>5){id1=0;count13=0;}else if(count12==5&&count13>5){id1=0;count13=0;}}  break;
                                    }goto loop4;}
                              case 2:{switch(flagkey2){
                                    case 1:id2+=0x64;count21++;if(count21>2){id2-=0x2c;count21=0;}  break;
                                    case 2:id2+=0x0a;count22++;if(count22>9){id2-=0x64;count22=0;}if(count21==2){if(count22>5){id2=0;count22=0;}else if(count22==5&&count23>5){id2=0;count22=0;}} break;
                                    case 3:id2+=1;count23++;if(count23>9){id2-=0x0a;count23=0;}if(count21==2){if(count22>5){id2=0;count23=0;}else if(count22==5&&count23>5){id2=0;count23=0;}} break;
                                    }goto loop4;}
                              case 3:{switch(flagkey2){
                                    case 1:id3+=0x64;count31++;if(count31>2){id3-=0x2c;count31=0;}  break;
                                    case 2:id3+=0x0a;count32++;if(count32>9){id3-=0x64;count32=0;}if(count31==2){if(count32>5){id3=0;count32=0;}else if(count32==5&&count33>5){id3=0;count32=0;}} break;
                                    case 3:id3+=1;count33++;if(count33>9){id3-=0x0a;count33=0;}if(count31==2){if(count32>5){id3=0;count33=0;}else if(count32==5&&count33>5){id3=0;count33=0;}} break;
                                    }goto loop4;}
                              case 4:{switch(flagkey2){
                                    case 1:id4+=0x64;count41++;if(count41>2){id4-=0x2c;count41=0;}  break;
                                    case 2:id4+=0x0a;count42++;if(count42>9){id4-=0x64;count42=0;}if(count41==2){if(count42>5){id4=0;count42=0;}else if(count42==5&&count43>5){id4=0;count42=0;}} break;
                                    case 3:id4+=1;count43++;if(count43>9){id4-=0x0a;count43=0;}if(count41==2){if(count42>5){id4=0;count43=0;}else if(count42==5&&count43>5){id4=0;count43=0;}}break;
                                    }goto loop4;}
                                        }


                                                 }
                             }goto loop4;
                     
    
    
loop5:_tmr0c4 = 0;
    switch(flagkey1){
    case 1: showp(flagkey1,id1);break;
    case 2: showp(flagkey1,id2);break;
    case 3: showp(flagkey1,id3);break;
    case 4: showp(flagkey1,id4);break;}
    set_tmr1();
    goto loop4;
   

        
              
       
    }

⌨️ 快捷键说明

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