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

📄 motor_master._c

📁 用ICC编写的程序包括SPI
💻 _C
字号:

#include <iom16v.h>
#include <macros.h>
//#include "Display.h"
#include "SPI_master.h"
#include "time_master.h"

extern unsigned char timeflag,dispflag;


extern unsigned char SPI_rx_buff[SIZE];  
extern unsigned char SPI_tx_buff[SIZE];  
extern unsigned char rx_wr_index,rx_rd_index,rx_counter,rx_buffer_overflow;  
extern unsigned char tx_wr_index,tx_rd_index,tx_counter,SPI_ok,SPI_datain,temp;

unsigned char pwm[2]={0x7f,0xff};
unsigned char qep_data1[3],qep_data2[3];
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}



//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer0_init();
 timer2_init();
 spi_init();

 
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x41; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//
void main(void)
{
 	 unsigned char i=0,t=0;
	 int j=0;
	 unsigned char SPI_flag=1;
	 unsigned char c;
	 unsigned char set_pwm=1;
	 
	 //unsigned char aa_flag;
	 
	 
	 init_devices();
 	 //insert your functional code here...
	 PORTB&=~BIT(SS);
	 putSPIchar(0xaa);
	 getSPIchar();
	 putSPIchar(0x00);
	 getSPIchar();
     putSPIchar(0x00);
	 getSPIchar();
	 PORTB|=BIT(SS);
		  
	 
	 
	  
	 while(1)
	 {
		while(timeflag)
		{
		  timeflag=0;
		  if(SPI_flag)
		  {
		  PORTB&=~BIT(SS);
		  putSPIchar(0xaa);
		  getSPIchar();
		  putSPIchar(0x01);
		  getSPIchar();
		  putSPIchar(0x00);
		  for(i=0,j=-1;i<10&j<3;i++)
		   {
		     if((c=getSPIchar())==0x55)
			  j=0;
			 putSPIchar(0x44);
			 if(j>=0)
			 qep_data1[j++]=c;
			}
		  getSPIchar();
	
		  putSPIchar(0x00);
		  getSPIchar();
		  
		  NOP();
		  putSPIchar(0xaa);
		  getSPIchar();
		  putSPIchar(0x02);
		  getSPIchar();
		  putSPIchar(0x00);
		  for(i=0,j=-1;i<10&j<3;i++)
		   {
		     if((c=getSPIchar())==0x56)
			  j=0;
			 putSPIchar(0x44);
			 if(j>=0)
			 qep_data2[j++]=c;
			}
		  getSPIchar();
		  NOP();
		  PORTB|=BIT(SS);
		  NOP();
		  }
		  if(set_pwm)
		  {
		  PORTB&=~BIT(SS);
	  	  putSPIchar(0x55);
	 	  getSPIchar();
	 	  putSPIchar(pwm[0]);
	 	  getSPIchar();
     	  putSPIchar(pwm[1]);
	 	  getSPIchar();
		  putSPIchar(0x00);
		  getSPIchar();
	 	  PORTB|=BIT(SS);
		  }
		}
	}
}

⌨️ 快捷键说明

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