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

📄 main.c

📁 avr的视频采集
💻 C
字号:
//////////////////////////////
//////////////////////////////   22967为近似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=254;//平均速度
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
#define CLS 3
long jiaodu=0,juli=0;
int time=0;
unsigned char sec=0;

#define video_x 32
#define video_y 48
unsigned int  v_num=0;
unsigned char v_temp[video_y][video_x],chang=0,hang=0,   adc1=0;;

unsigned char x_adc;
unsigned char  y_adc;

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


//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();
//adc_init();
uart1_init();

 MCUCR = 0x00;
 EICRA = 0x0A; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x03;
 //TIMSK = 0x40; //timer interrupt sources
 TIMSK = 0x00;
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
//UART1 initialize
// desired baud rate:19200
// actual baud rate:19231 (0.2%)
void uart1_init(void)
{
 UCSR1B = 0x00; //disable while setting baud rate
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UBRR1L = 0x40; //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;}
	   else if ((b=='c')&&(c=='l')&&(d=='s')) 
	     {
		  xx=0;
		  jiaodu=0;
		  juli=0;
		 }
		   ////////////////////////////////////////////////////////////////////
	 }
}



/*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;
			 } 
}

unsigned char shuliang=0;
unsigned char xuanze=0;
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
 //external interupt on INT0
 
x_adc=0;y_adc=0;
chang=1;
 shuliang=ms;
 ms=0;

}

#pragma interrupt_handler int1_isr:3
void int1_isr(void)
{//CLI();
 //external interupt on INT1
/*while(x_adc<video_x)
{ v_temp[y_adc*video_x  +  x_adc] =0;x_adc++;}*/
if(xuanze++>5){xuanze=0;x_adc=0;y_adc++;}

x_adc=video_x+1;
//v_num=(v_num/video_x+1)*video_x;
ms++;
 //hang=1;
 //SEI();
}

//TIMER0 initialize - prescale:8
// WGM: Normal
// desired value: 2uSec
// actual value:  2.000uSec (0.0%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT0 = 0xFB; //set count
 OCR0  = 0x05;
 TCCR0 = 0x02; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
 TCNT0 = 0xFB; //reload counter value
if (  (xuanze!=0) &&  (xuanze<2)  )//滤波均值
  v_temp[y_adc][x_adc]=v_temp[y_adc][x_adc]/4+ADC/4;
else /**/
  v_temp[y_adc][x_adc]=ADC>>1;
}
#define PWM_ON    DDRB|=   R_pwm0 | R_pwm1 | L_pwm0 | L_pwm1;
#define PWM_OFF   DDRB&= ~(R_pwm0 | R_pwm1 | L_pwm0 | L_pwm1);

main()
{int n;
unsigned char n_jiaodu=0,n_juli=0;
unsigned char n_sec=0;

unsigned int v_num_now=0;//x_now,y_now;
unsigned char xv,yv;

init_devices();
PORTD|=0x03;//外部中断
DDRD&=~03; 
//while(1);
DDRE|=0x38;//while(1);
/////////LCD
	/* DDRB = 0xBF;    //SI输入,SO,SCK,SS输出
	 SPCR = 0x50;  //setup SPI
	 SPSR = 0x01;  //setup SPI*/
/*while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';*/
delay_ms(3000);
number_uart1(1010,1);
			number_uart1(xx,0);
			number_uart1(ADCjohu,1);
delay_ms(3000);	
xx=0;
//LCD_Init();
//LCD_clear();
DDRE &= ~0x7c;//if (!(PORTE & STOP))  if (!(PORTE & VOLDOWN))  if (!(PORTE & VOLUP))  if (!(PORTE & NEXTSONG))  if (!(PORTE & PREVSONG))
PORTE |=0x7c;
DDRE=0x00;PORTE=0xff;
  while(1)
  { 	   	
			   
	//for(xv=0;xv<video_x;xv++)		   	  
  	ADMUX = 0x01;                           // A/D converter input pin number = 0
    ADCSRA = 0xc0;//|ADATE;                           // single A/D conversion, fCK/32, conversion now started 
    //for(y_adc=0;y_adc<video_y;y_adc++)
//while(PINE!=0xff)
 if (xuanze==0)
{
	for(x_adc=0;x_adc<video_x;x_adc++)
    {
        do
        {
		 //PORTE=~PORTE;
        } while(!(ADCSRA&(1<<ADIF)));// ADSC        // Wait for A/D conversion to finish
        /*count = 6;                          
        do                                   // Customize this loop to 66 cycles !!
        {
        } while(--count);*/                   // wait some cycles
        ADCSRA |= 0x40;                      // start new A/D conversion 
        //write_to_flash(ADC-0x1D5);          // read data, convert to 8 bit and store in flash
		if (  (xuanze!=0) &&  (xuanze<2)  )//滤波均值
		  v_temp[y_adc][x_adc]=v_temp[y_adc][x_adc]/4+ADC/4;
		else /**/
		  v_temp[y_adc][x_adc]=ADC>>1;
		  
    }
}
    ADCSRA = 0x00;                           // disable AD converter
	
  		/*if (adc1>0)
		   {//if (n_sec++>1000)
		   	   {
			   	v_temp[v_num++]=ADC;
			   	adc1=0;
			   	if (v_num>1023)v_num=0;
				n_sec=0;
			   }
			adc1=0;
		   }*/
		if  (!(PINE & NEXTSONG))//( v_num_now!=v_num )
		   {
		   	for(yv=0;yv<video_y;yv++)
			   {
			   	for(xv=0;xv<video_x;xv++)
				  { //SEI();
				   while(!(UCSR1A&(1<<UDRE1)));
		     	     UDR1=v_temp[yv][xv];//CLI();
				  }
				while(!(UCSR1A&(1<<UDRE1)));
		        UDR1=50;
			    while(!(UCSR1A&(1<<UDRE1)));
		        UDR1=2;  
			   }
			while(!(UCSR1A&(1<<UDRE1)));
		    UDR1=100;
			while(!(UCSR1A&(1<<UDRE1)));
		    UDR1=4;
		  }
		  if  (!(PINE & PREVSONG))
		  //if (chang==1)
		  {number_uart1(shuliang,0);
		   number_uart1(shuliang,0);
		   number_uart1(shuliang,1);
		   chang=0;
		   ms=0;
		  }
		  
		   
		/*if (!(PINE & NEXTSONG))
	    	{
			//CLI();
			number_uart1(xx,0);
			number_uart1(ADCjohu,1);
			delay_ms(20);
			//SEI();
			}
   	    ms=0;*/
       
  }

}

⌨️ 快捷键说明

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