基于ATmega128编码器控制步进电机的平衡系统
#include "delay.h"
#if F_CPU == 7372800
void delay_nus(unsigned int time)
{
unsigned int i;
for(i=0;itime;i++)
{
NOP();
}
}
void delay_nms(unsigned int time)
{
unsigned int i,j;
for(i=0;itime;i++)
{
for(j=0;j1045;j++);
}
}
#elif F_CPU == 8000000
void delay_nus(unsigned int time)
{
unsigned int i;
for(i=0;itime;i++)
{
NOP();NOP();
}
}
void delay_nms(unsigned int time)
{
unsigned int i,j;
for(i=0;itime;i++)
{
for(j=0;j1178;j++);
}
}
#elif F_CPU == 11059200
void delay_nus(unsigned int time)
{
unsigned int i;
for(i=0;itime;i++)
{
NOP();NOP();NOP();NOP();NOP();
}
}
void delay_nms(unsigned int time)
{
unsigned int i,j;
for(i=0;itime;i++)
{
for(j=0;j1578;j++);
}
}
#elif F_CPU == 12000000
void delay_nus(unsigned int time)
{
unsigned int i;
for(i=0;itime;i++)
{
NOP();NOP();NOP();NOP();NOP();
}
}
void delay_nms(unsigned int time)
{
unsigned int i,j;
for(i=0;itime;i++)
{
for(j=0;j1713;j++);
}
}
#elif F_CPU == 16000000
void delay_us(unsigned int time)
{
unsigned int i;
for(i=0;itime;i++)
{
NOP();NOP();NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();
}
}
void delay_ms(unsigned int time)
{
unsigned int i,j;
for(i=0;itime;i++)
{
for(j=0;j2288;j++);
}
}
#endif
这里是dianji.h
#ifndef __DIANJI_H
#define __DIANJI_H
#define EN_1 PORTC|=(10)
#define EN_0 PORTC=~(10)
void one_circle(void);
void circle(unsigned int xcircle);
#endif
这里是dianji.c
#include"dianji.h"
#include"delay.h"
#includeiom128v.h>
/*步进电机旋转一周子函数*/
void one_circle(void)
{
unsigned int i;
for(i=0;i3200;i++)
{
EN_1;
delay_us(100);
EN_0;
delay_us(100);
}
}
/*步进电机根据编码器计算的数值旋转任意角度子函数*/
void circle(unsigned int xcircle)
{
unsigned int i,j;
for(j=0;jxcircle;j++)
{
for(i=0;i3;i++)
{
EN_1;
delay_us(100);
EN_0;
delay_us(100);
}
}
}
这里是caiji.h
#ifndef __CAIJI_H
#define __CAIJI_H
//宏定义595
#define SCK_0 PORTB=~(1PB4)
#define SCK_1 PORTB|=(1PB4)
#define LCK_0 PORTB=~(1PB5)
#define LCK_1 PORTB|=(1PB5)
#define SDI_0 PORTB=~(1PB6)
#define SDI_1 PORTB|=(1PB6)
#define DIR_0 PORTC=~(11)
#define DIR_1 PORTC|=(11)
void init_xianshi(void);
void send_595(unsigned char dat);
void jiaoduzhuanhuan(void);
#endif
这里是caiji.c
#include"caiji.h"
#include"dianji.h"
#includeiom128v.h>
#includemacros.h>
#includemath.h>
//一些变量的定义
int k=0;
unsigned int x=1,d=0,c=0,cir=0;
unsigned int a,aa,q,angle=0,p=0;
//595(数码管)显示数组
unsigned char led[]=
{
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
};
//外部中断0~3向量号码
#pragma interrupt_handler Zhongduan_0:2
#pragma interrupt_handler Zhongduan_1:3
#pragma interrupt_handler Zhongduan_2:4
//端口输入输出初始化
void init_xianshi(void)
{
DDRA=0XFF;
DDRB=0XFF;//595控制口
DDRC=0XFF;//正反转提示LED
PORTC=0XFF;//LED初始化(暗)
}
//595串入一字节并处发送一字节子函数
void send_595(unsigned char dat)
{
unsigned char i;
LCK_0;
SDI_1;
SCK_0;
//上面的三条语句为了初始化端口状态
for(i=0;i8;i++)
{
LCK_0;//时钟线拉低
if(dat0x80)
SDI_1;
else SDI_0;
dat=dat1;
delay_us(100);
LCK_1; //时钟线拉高将数据读入595的移位寄存器
delay_us(100);
}
SCK_1; //发送数据到并行端口
SCK_0;
}
void jiaoduzhuanhuan(void)
{
init_xianshi();
EIMSK=0X0F;
EICRA=0XAA;
while(1)
{
if(a!=aa) /*如果有角度变化就执行下面的程序*/
{
angle++;
if(!(PING0X04))
cir++;
while(cir)
{
if(!k)
{
delay_ms(800);
if(d=3)
one_circle();
d++;
}
}
if(!(PING0X02))
{
while(PING0X01)
{
DIR_0;
circle(1);
p++;
while(p==angle);
}
while(PING0X01);
if(q=-1)//正转
DIR_1;
circle(1);
if(q==1) //反转
DIR_1;
}
aa=a;
if(!x)
{
PORTC=0X00;
}
}
/*下面为数码管显示编码器当前计数的数值*/
PORTA=0X08;
send_595(0x00);
send_595(led[abs(k)%10]);
PORTA=0X01;
send_595(0x00);
send_595(led[(abs(k)%100)/10]);
PORTA=0X02;
send_595(0x00);
send_595(led[(abs(k)%1000)/100]);
PORTA=0X04;
send_595(0x00);
send_595(led[abs(k)/1000]);
SEI(); /*使能中断*/
}
}
- 基于ATmega128单片机的矿用磁力启动器控制系统设计(06-04)
- 一款宠物狗无线感知交互系统让人和动物的社交网络成真(06-03)
- ATmega128 流水灯的两种实现方法(基于AVR GCC)(11-23)
- Atmega128串口详解(11-23)
- 基于ATMEGA128单片机压力的测量(11-21)
- ATmega128型号标识说明(11-11)