www.pudn.com > digital_duoji.rar > main.c


////////////////////////////// 
//////////////////////////////   61493为近似180度 
 
#include  
#include  
 
#define PREVSONG   BIT(2) 
#define NEXTSONG   BIT(3) 
#define VOLDOWN    BIT(4) 
#define VOLUP      BIT(5) 
#define STOP       BIT(6) 
 
#define R_pwm0       BIT(4) 
#define R_pwm1       BIT(5) 
#define L_pwm0       BIT(6) 
#define L_pwm1       BIT(7) 
 
 
long ADCjohu=0; 
int ms=0; 
long xx=0; 
int adc=0; 
 
#define REF 483 
unsigned char lvbo=0; 
long lvbo_num=0; 
 
unsigned char pwm_pingjun=100;//平均速度 
unsigned char Rpwm0=0; 
unsigned char Rpwm1=0; 
unsigned char Lpwm0=0; 
unsigned char Lpwm1=0; 
unsigned char all=0, a=0,b=0,c=0,d=0,e=0,f=0; 
#define CIR 1 
#define LINE 2 
long jiaodu=0,juli=0; 
int time=0; 
unsigned char sec=0; 
 
 
 
void port_init(void) 
{ 
 PORTA = 0x00; 
 DDRA  = 0x00; 
 PORTB = 0x00; 
 DDRB  = 0x00; 
 PORTC = 0x00; //m103 output only 
 DDRC  = 0x00; 
 PORTD = 0x00; 
 DDRD  = 0x00; 
 PORTE = 0x00; 
 DDRE  = 0x00; 
 PORTF = 0x00; 
 DDRF  = 0x00; 
 PORTG = 0x00; 
 DDRG  = 0x00; 
} 
 
//TIMER2 initialize - prescale:64 
// WGM: Normal 
// desired value: 1mSec 
// actual value:  1.000mSec (0.0%) 
void timer2_init(void) 
{ 
 TCCR2 = 0x00; //stop 
 TCNT2 = 0x83; //setup 
 OCR2  = 0x7D; 
 TCCR2 = 0x03; //start 
} 
 
 
#pragma interrupt_handler timer0_ovf_isr:11 
void timer0_ovf_isr(void) 
{ 
 TCNT0 = 0x83; //reload counter value 
 if (time>500){sec++;time=0;} 
 time++; 
 ADCjohu=ADCL; 
 ADCjohu|=ADCH << 8; 
///////////////////////////////////////////////////////////////////平均值滤波  1毫秒采样滤波,10毫秒输出 
 if (lvbo==0) {lvbo_num=ADCjohu/2;lvbo++;return;} 
 if (lvbo<10) {lvbo_num+=ADCjohu/2;lvbo++;return;} 
 	else lvbo=0;/**/ 
////////////////////////////////////////////////////////////////////////////// 
 if ( (ADCjohuREF+6)) 
 	{ 
	 //ADCjohu-=511-10;//-511+31-3.27371     -10 
 	 xx=xx+ADCjohu-REF;// 
	} 
 ms=1; 
//ADCjohu==ADCL; 
//PORTD=ADCjohu; 
} 
 
//ADC initialize 
// Conversion time: 3uS 
void adc_init(void) 
{ 
 ADCSRA = 0x00; //disable adc 
 ADMUX = 0x01; //select adc input 0 
 ACSR  = 0x80; 
// ADCSRB = 0x00; 
 ADCSRA = 0xEf; 
} 
 
#pragma interrupt_handler adc_isr:22 
void adc_isr(void) 
{ 
 //conversion complete, read value (int) using... 
 // value=ADCL;            //Read 8 low bits first (important) 
 // value|=(int)ADCH << 8; //read 2 high bits and shift into top byte 
 //ADCjohu==ADCL; 
 //ADCjohu|=(int)ADCH << 8; 
 //PORTD=ADCjohu; 
 //adc=1; 
 PORTA=ADCL; 
 
} 
 
//call this routine to initialize all peripherals 
void init_devices(void) 
{ 
 //stop errant interrupts until set up 
 CLI(); //disable all interrupts 
 XDIV  = 0x00; //xtal divider 
 XMCRA = 0x00; //external memory 
 port_init(); 
 timer2_init();  
 
adc_init();uart1_init(); 
 
 MCUCR = 0x00; 
 EICRA = 0x00; //extended ext ints 
 EICRB = 0x00; //extended ext ints 
 EIMSK = 0x00; 
 TIMSK = 0x40; //timer interrupt sources 
 ETIMSK = 0x00; //extended timer interrupt sources 
 SEI(); //re-enable interrupts 
 //all peripherals are now initialized 
} 
//UART1 initialize 
// desired baud rate:9600 
// actual baud rate:9615 (0.2%) 
void uart1_init(void) 
{ 
 UCSR1B = 0x00; //disable while setting baud rate 
 UCSR1A = 0x00; 
 UCSR1C = 0x06; 
 UBRR1L = 0x33; //set baud rate lo 
 UBRR1H = 0x00; //set baud rate hi 
 UCSR1B = 0x98; 
} 
 
#pragma interrupt_handler uart1_rx_isr:31 
//cir+00100 
void uart1_rx_isr(void) 
{ 
 //uart has received a character in UDR 
 if (all==CIR) 
 	{ 
	   a=b; 
	   b=c; 
	   c=d; 
	   d=e; 
	   e=f; 
	   f=UDR1; 
	   if ((f<'0')||(f>'9'))return; 
	   if (a=='+')//0x2b 
	   	  {jiaodu=(b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48); 
		   all=0;return;} 
	   else if (a=='-')//0x2d 
	   	  {jiaodu=-((b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48)); 
		   all=0;return;} 
	   if (a!=0) {all=0;return;} 
	     //PORTF=jiaodu;//while(1); 
	} 
 else if (all==LINE) 
 	{ 
	   a=b; 
	   b=c; 
	   c=d; 
	   d=e; 
	   e=f; 
	   f=UDR1; 
	   if ((f<'0')||(f>'9'))return; 
	   if (a=='+') 
	   	  {juli=(b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48); 
		   all=0;return;} 
	   else if (a=='-') 
	   	  {juli=-((b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48)); 
		   all=0;return;} 
	   if (a!=0) {all=0;return;} 
	   //PORTF=a; 
	} 
 else   if (all==0)//(johu!=3)&&(johu!=5)  //5:7-bit    3:8-bit 
      { 
	   a=b; 
	   b=c; 
	   c=d; 
	   d=UDR1; 
	   if ((a=='c')&&(b=='i')&&(c=='r')) 
	     { 
		   all=CIR; 
		   a=0;b=0;c=0;d=0;e=0;f=0; 
		   return; }//RXlen=4;{johu=3;RXlen=3;return;} 
	   else if ((a=='l')&&(b=='i')&&(c=='n')&&(d=='e')) 
	     { 
		   all=LINE; 
		   a=0;b=0;c=0;d=0;e=0;f=0; 
		   return; }//RXlen=4;{johu=5;RXlen=3;return;} 
		   //////////////////////////////////////////////////////////////////// 
	 } 
} 
 
 
 
/*void number(unsigned char X,unsigned char Y,unsigned char LEN,int NUM,unsigned char BLACK) 
{ 
 char code[]={'0','1','2','3','4','5','6','7','8','9',}; 
 if (NUM<0) 
 	{LCD_write_String1(X,Y,"-");NUM=-NUM;} 
 else 
 	{LCD_write_String1(X,Y,"+");} 
X=X+6; 
 while(LEN--) 
 	 { 
	  LCD_write_String1(X+LEN*6,Y,& code[NUM%10]); 
	  NUM=NUM/10; 
	 }  
}*/ 
void number_uart1(long NUM,char huanhang) 
{long n=0; 
 			if (NUM<0){n=-NUM; while(!(UCSR1A&(1<>8)&0x07; 
 			 OCR1BL = pwm_duoji;  
			 /*OCR1BH = 0x03; 
 			 OCR1BL = 0xF0; 
 			 OCR1CH = 0x03; 
 			 OCR1CL = 0xF0; 
			 */ 
			PWM_ON 
			//SEI(); 
		  } 
		} 
		if (!(PINE & PREVSONG)) 
	     {delay_ms(500); 
		  if (!(PINE & PREVSONG)) 
		  { 
			 //CLI(); 
		     if(pwm_duoji>200) pwm_duoji--;//0.5ms 
 			 OCR1BH = (pwm_duoji>>8)&0x07; 
 			 OCR1BL = pwm_duoji;  
			 /*OCR1BH = 0x03; 
 			 OCR1BL = 0xF0; 
 			 OCR1CH = 0x03; 
 			 OCR1CL = 0xF0; 
			 */ 
			PWM_ON 
			//SEI(); 
		  } 
		} 
 
} 
  while(1) 
  { //(unsigned char)jiaodu; 
  	PORTF=juli; 
  	if (!(PINE & STOP))jiaodu=100;//PWM_ON//PORTD=~PORTD; 
    if (ms==1) 
   	   { //if (jiaodu!=n_jiaodu)//test 
	   	 if (jiaodu!=0) 
	      {//PORTF=jiaodu; 
		    //if (jiaodu<0-20)//test  旋转 
			if (ADCjohu0+20)//test 
			else if (ADCjohu>jiaodu+20) 
	   	 	 {DDRB |=(R_pwm0 | L_pwm1); 
			  DDRB &=~(R_pwm0 | L_pwm1); 
			  PORTB =0xf0; 
			  OCR1AL = 0; 
 		  	  OCR1BL = 0; 
 		  	  OCR1CL = pwm_pingjun; 
 		  	  OCR0=pwm_pingjun; 
			  PWM_ON 
			 } 
		    else PWM_OFF 
		   n_jiaodu=jiaodu; 
		  } 
		 if (juli!=0)  
		 if (sec!=n_sec) 
		    { 
			 if (juli>0) 
	   	 	  {DDRB |=(R_pwm1 | L_pwm1); 
			   DDRB &=~(R_pwm1 | L_pwm1); 
			   OCR1AL = 0; 
 		  	   OCR1BL = pwm_pingjun+Rpwm0; 
 		  	   OCR1CL = 0; 
 		  	   OCR0=pwm_pingjun; 
			   PWM_ON 
			   juli--; 
			  } 
			 else if (juli<0) 
	   	 	  {DDRB |=(R_pwm0 | L_pwm0); 
			   DDRB &=~(R_pwm0 | L_pwm0); 
			   OCR1AL = pwm_pingjun; 
 		  	   OCR1BL = 0; 
 		  	   OCR1CL = pwm_pingjun; 
 		  	   OCR0=0; 
			   PWM_ON 
			   juli++; 
			  }/**/ 
			 else PWM_OFF 
			n_sec=sec; 
			}	 
 
	   	//number(0,2,5,xx,1); 
		//number(0,4,5,ADCjohu,1); 
		if (!(PINE & PREVSONG))xx=0; 
		if (!(PINE & NEXTSONG)) 
	    	{ 
			//CLI(); 
			number_uart1(xx,0); 
			number_uart1(ADCjohu,1); 
			delay_ms(20); 
			//SEI(); 
			} 
   	    ms=0; 
       } 
  } 
 
}