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

📄 步进电机.c

📁 我写的 四线 步进电机控制 用M16主控
💻 C
字号:
#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define  uint unsigned int
#define RS_CLR PORTA&=~BIT(PA7)
#define RS_SET PORTA|=BIT(PA7)
#define RW_CLR PORTA&=~BIT(PA6)
#define RW_SET PORTA|=BIT(PA6)
#define EN_CLR PORTA&=~BIT(PA5)
#define EN_SET PORTA|=BIT(PA5)
#define MIN 0xf3cb        
#define MAX 0xffff    
uint timer=MIN;
//uchar djz[]={0xf7,0xfb,0xfd,0xfe};  //4相4拍        
//uchar djf[]={0xfe,0xfd,0xfb,0xf7};
uchar djz[]={0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe,0xf6};          
uchar djf[]={0xf6,0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7};   //4相8拍         
uchar flag=1;
uchar count=0;
uchar display[]={"0123456789"}; 
/*//延时函数
void delay_us(uint n)
{if (n == 0) 
        return ; 
 while (--n);
}
//延时函数
void delay_ms(uchar i) 
{uchar a, b; 
for (a = 0; a < i; a++) 
 for (b = 1; b<255; b++);  
}*/
//延时程序:  
/*----------------------------------------------------------------------- 
延时函数 
系统时钟:8M 
-----------------------------------------------------------------------*/ 
void delay_1us(void)                 //1us延时函数 
  { 
   asm("nop"); 
  } 

void delay_nus(unsigned int n)       //N us延时函数 
  { 
   unsigned int i=0; 
   for (i=0;i<n;i++) 
   delay_1us(); 
  } 
   
void delay_1ms(void)                 //1ms延时函数 
  { 
   unsigned int i; 
   for (i=0;i<1140;i++); 
  } 
   
void delay_nms(unsigned int n)       //N ms延时函数 
  { 
   unsigned int i=0; 
   for (i=0;i<n;i++) 
   delay_1ms(); 
  } 

//命令写入
void LCD_write_com(uchar com) 
{delay_nms(5);
 RS_CLR; 
 RW_CLR; 
 EN_SET; 
 PORTC = com; 
 delay_nus(5); 
 EN_CLR;
}
//数据写入
void LCD_write_data(uchar data) 
{delay_nms(5);
RS_SET;
 RW_CLR; 
 EN_SET; 
 PORTC = data;
 delay_nus(5);
 EN_CLR;
}
//单字符写入
void LCD_write_char(uchar x,uchar y,uchar data) 
{
if(y==0) {LCD_write_com(0x80 + x);}    
 else  {LCD_write_com(0xC0 + x);}      
 LCD_write_data(data); 
}
//显示屏清空显示
void LCD_clear(void) 
 { LCD_write_com(0x01); 
 delay_nms(5);}
 void LCD_write_str(uchar x,uchar y,uchar *s)   
{if(y==0)
 {while (*s)
  {if(x<16)
  {LCD_write_com(0x80 + x);
  LCD_write_data(*s);  
  s++;
  x++;}
  else {LCD_write_com(0xc0 + x-16);
        LCD_write_data(*s);
		s++;
		x++;
		}
        
  
 }
}
 else {LCD_write_com(0xC0 + x);
      while (*s) 
      {LCD_write_data(*s);  
      s++;}
      }
}

//n位整数显示
void LCD_write_number(uchar x,uchar y, long number)
{uchar num[32]={""},i,j,k;
if(number==0)                   //number 0 时候
LCD_write_char(x,y,'0');
else
{for(i=0;number>0;i++)
{
num[i]=display[number%10];//每位以字符表示
number/=10; 
}
for(j=0;j<i/2;j++){k=num[j];num[j]=num[i-1-j];num[i-1-j]=k;}
 LCD_write_str(x,y,num);
}
}
/*显示屏初始化函数*/
void LCD_init(void) 
{ 
 DDRC=0xFF;    /*方向输出*/
 PORTC=0xFF;    /*电平设置*/
 DDRA|=BIT(PA5)|BIT(PA6)|BIT(PA7); 
 LCD_write_com(0x3c);    /*显示模式设置*/  
 LCD_write_com(0x01);    //显示清屏 
 LCD_write_com(0x06);    //显示光标移动设置
 LCD_write_com(0x0C);    //显示开及光标设置
}
/*********************************************/

void LCD_display(uint i,uint j)
{LCD_write_number(3,0,i);
LCD_write_number(3,1,j);
}
/****************键盘扫描部分*****************/
//D是键盘口 低行 高列  D0-D3 行 D4--D7列
uchar key_scan()
{
    uchar n=0;
    DDRD=0xf0;PORTD=0x0f;
	if(PIND!=0x0f){;}                
    else  return 0; 
	delay_nms(1);   //消抖延时                  
	 if((PIND&0x01)==0){n +=10;}
     else if((PIND&0X02)==0){n +=20;}
     else if((PIND&0X04)==0){n +=30;}
     else if((PIND&0X08)==0){n +=40;}   //行扫描
	 else  return 0;	
     DDRD=0x0f;PORTD=0xf0;
	 delay_nms(1);
	 if((PIND&0X10)==0){n +=1;}
     else if((PIND&0X20)==0){n +=2;}
     else if((PIND&0X40)==0){n +=3;}
     else if((PIND&0X80)==0){n +=4;}   //列扫描
	 else   return 0;
    while(PIND!=0Xf0)  ;             //检测按键弹起
	 return n;                         //返回键值   0时 无按下 
}
void key_manage(uchar n){                            
 switch(n){ case 11: SEI();break;                     //开
            case 12: CLI();PORTB=0xff;break;          //关
			case 13: if(timer<MAX){timer+=50;}break; //加速
			case 14: if(timer>MIN){timer-=50;}break;  //减速
			case 21: flag=1;break;                  //正转
			case 22: flag=0;break;                  //反转
              default:break;}
			  }
			  

#pragma interrupt_handler Time1_ovf_isr:9
void Time1_ovf_isr()
{
TCNT1=timer;
if(flag){
PORTB=djz[count];}
else PORTB=djf[count];
count++;
if(count>7)
count=0;
}

void timer1_init(void)
{
 TCCR1B =0x00; //stop
 TCNT1=MIN; 
TCCR1A = 0x00;
TCCR1B = 0x04; //256分频  
}
/**************************************************/
void init_devices(void)              //系统初始化                
{ CLI();
LCD_init();
timer1_init();
TIMSK = 0x04;
DDRB=0Xff;
PORTB=0xff;
SEI(); //开全局中断SREG的I位 头文件支持macros.h
 }
/***********************************************/


 void main()
{init_devices();
 while(1){ key_manage(key_scan());
          }
}

⌨️ 快捷键说明

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