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

📄 main._c

📁 数字舵机(1) 数字脉宽检测 数字脉宽检测
💻 _C
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////
//////////////////////////////   61493为近似180度

#include <iom64v.h>
#include <macros.h>

#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;

        unsigned int temp1=0, temp2=0;  
        unsigned char count1=0;//0表示一个捕获周期完成,1:准备捕获,2:第一次捕获,3:第二次捕获 

unsigned char shang_johu=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 ( (ADCjohu<REF-6) || (ADCjohu>REF+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 = 0x20; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//UART1 initialize
// desired baud rate:19200
// actual baud rate:19200 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UBRR1L = 0x23; //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<<UDRE1))); UDR1='-';}
			else  {n=NUM;while(!(UCSR1A&(1<<UDRE1))); UDR1='+';}	
		     while(!(UCSR1A&(1<<UDRE1)));
		     UDR1=(n/10000)%10+48;
		     while(!(UCSR1A&(1<<UDRE1)));
		     UDR1=(n/1000)%10+48;
			 while(!(UCSR1A&(1<<UDRE1)));
		     UDR1=(n/100)%10+48;
			 while(!(UCSR1A&(1<<UDRE1)));
		     UDR1=(n/10)%10+48;
			 while(!(UCSR1A&(1<<UDRE1)));
		     UDR1=n%10+48;
			if (huanhang)
			 {
			  while(!(UCSR1A&(1<<UDRE1)));
		      UDR1=0x0d;
			  while(!(UCSR1A&(1<<UDRE1)));
		      UDR1=0x0a;
			 }
			 else
			 {
			  while(!(UCSR1A&(1<<UDRE1)));
		      UDR1=0x20;
			  while(!(UCSR1A&(1<<UDRE1)));
		      UDR1=0x20;
			 } 
}



//TIMER0 initialize - prescale:64
// WGM: PWM Fast
// desired value: 1KHz
// actual value:  0.488KHz (-104.8%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT0 = 0x01; //set count
 OCR0  = 0x0f;
 TCCR0 = 0x6C; //start timer
}

//TIMER1 initialize - prescale:64
// WGM: 5) PWM 8bit fast, TOP=0x00FF
// desired value: 1KHz
// actual value:  0.488KHz (104.8%)
/*void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFF; //setup
 TCNT1L = 0x01;
 OCR1AH = 0x00;
 OCR1AL = 0x0f;
 OCR1BH = 0x00;
 OCR1BL = 0x0f;
 OCR1CH = 0x00;
 OCR1CL = 0x0f;
 ICR1H  = 0x00;
 ICR1L  = 0xFF;
 TCCR1A = 0xA9;
 TCCR1B = 0x0B; //start Timer
}*/
//TIMER1 initialize - prescale:8
// WGM: 9) PWM phz freq correct, TOP=OCRnA
// desired value: 40mSec
// actual value: 40.000mSec (0.0%)
/*void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xB1; //setup
 TCNT1L = 0xE0;
 OCR1AH = 0x4E;
 OCR1AL = 0x20;
 OCR1BH = 0x4E;

⌨️ 快捷键说明

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