#define uint unsigned int
#define uchar unsigned char
uchar num1 = 0
uint v_set = 40
xdata uint pcacap2
xdata uchar dutycycle
uint A=10//float D=0.04float C=5
//int xdata e0=0,e1=0,e2=0
float xdata PWM=0
bit isnewdata
bit DIRECTON
#define POSITIVE DIRECTON = 1
#define NEGATIVE DIRECTON = 0
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE
OSCICN= 0x83
}
void Port_IO_Init()
{
SFRPAGE = 0x0F
XBR0 = 0xF7
XBR2 = 0x40
}
void PCA_Init()
{
SFRPAGE=0x00
PCA0CPM0=0x02
PCA0CPM1=0x02
PCA0CPM2=0x21
PCA0CPL2=0x00
PCA0CPH2=0x00
PCA0MD=0x00
PCA0CN=0x40
EIE1|=0x08
}
void PWM_set(uchar low)
{//占空比设置,高电平占空比为(256-low)/256
SFRPAGE = PCA0_PAGE
if (DIRECTON == 1) {PCA0CPH0 = lowPCA0CPM1 = 0x02PCA0CPM0 = 0x42}
else {PCA0CPH1 = lowPCA0CPM0 = 0x02PCA0CPM1 = 0x42}
}
void PCA_ISR(void) interrupt 9 using 1 {
static xdata uint tmpcnt=0
static xdata uint PCA0CP2=0
xdata uint tmpcnt2
if(CCF2){
tmpcnt2 = PCA0CPH2
tmpcnt2 = tmpcnt2<漏铅带<8
PCA0CP2 = tmpcnt2 + PCA0CPL2
pcacap2 = PCA0CP2-tmpcnt
tmpcnt = PCA0CP2
CCF2=0
}
}
PID_SC(uint v)
{
int Uk_zint e
e = v - v_set
//e1 = v1 - v_set
/激尺/e2 = v2 - v_set
//v2 = v1
//v1 = v
Uk_z = A*e/*+ D*e0 + C*(e0 - 2*e1 + e2)*/返芦
//Uk0 = Uk1 + Uk_z
//Uk1 = Uk0
//PWM = PWM - 256
dutycycle = 0.0256 * Uk_z
}
void main()
{
//Oscillator_Init()
WDTCN = 0x07
WDTCN = 0xDE
WDTCN = 0xAD
Port_IO_Init()
EA=1
num1=0
isnewdata=0
PCA_Init()
POSITIVE
while(1){PID_SC(pcacap2)PWM_set(dutycycle)}
}
4相4拍步进电机,步进角为7.5步进电机驱动口连接在RA0-RA3
include<p16f877a.inc> 包含877A的头文件
__CONFIG _DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_ON&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC
芯片配置字,看门狗关,上电延时开,掉电检测关饥圆碧,低压编程关,加密,4M晶体HS振荡
#define step1 9h 转动第1步驱动信号(不同的电机,可以引出线顺序不一样)
#define step2 0ch 转动第2步驱动信号(本程序以“常州市丰源微特电记有限公司的35BY48S053”电机为基础)
#define step3 6h 转动第3步驱动信号
#define step4 3h 转动第4步驱动信号
countequ 20h
org 00h 复位入口
nop ICD所需要的烂举空指令
goto main 跳转到主程序
org 05h
****************************主程序***************************
main
banksel adcon1
movlw 07h
movwf adcon1设置所有A口为普通数字口
banksel trisa
clrf trisa 设置A口为输出
clrf status
clrf porta
movlw .12电机旋转一周需循环次数 360/(7.5*4)
movwf count
loop
movlw step1 第1步
movwf porta
call delay
movlw step2 第2步
movwf porta
call delay
movlw step3 第3步
movwf porta
call delay
movlw step4 第4步
movwf porta
call delay
decfsz count,1是否旋转完一周
goto loop 否,继腔隐续转动
clrf porta 是,停止转动
goto $-1停机
*******************************延时程序********************************
控制输出的频率
delay
movfw 0FH
movwf 21h
movlw 0ffh
movwf 22h
decfsz 22h,1
goto $-1
decfsz 21h,1
goto $-5
return
******************************源程序结束*******************************
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)