英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 3775|回复: 1
打印 上一主题 下一主题

电机控制

[复制链接]
跳转到指定楼层
1#
发表于 2009-3-12 16:34:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
;**********************************************************************

list      p=16f877            ; list directive to define processor
#include <p16f877.inc>        ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_OFF & _LVP_ON & _DEBUG_OFF & _CPD_OFF
;**********************************************************************
;*
;* Define variable storage
;*
CBLOCK 0x20
ADC  ; PWM threshold is ADC result
LastSensor ; last read motor sensor data
DriveWord ; six bit motor drive data
ENDC
;**********************************************************************
;*
;* Define I/O
;*
#define OffMask  B'11010101'
#define DrivePort PORTC
#define DrivePortTris TRISC
#define SensorMask B'00000111'
#define SensorPort PORTE
#define DirectionBit PORTA,1
;**********************************************************************
org 0x000  ; startup vector
nop   ; required for ICD operation
clrf    PCLATH          ; ensure page bits are cleared
   goto    Initialize      ; go to beginning of program

ORG     0x004           ; interrupt vector location
retfie                  ; return from interrupt
;**********************************************************************
;*
;* Initialize I/O ports and peripherals
;*
Initialize
clrf DrivePort ; all drivers off
banksel TRISA
; setup I/O
clrf DrivePortTris ; set motor drivers as outputs
movlw B'00000011' ; A/D on RA0, Direction on RA1, Motor sensors on RE<2:0>
movwf TRISA  ;
; setup Timer0
movlw B'11010000' ; Timer0: Fosc, 1:2
movwf OPTION_REG
; Setup ADC (bank1)
movlw B'00001110' ; ADC left justified, AN0 only
movwf ADCON1
banksel ADCON0
; setup ADC (bank0)
movlw B'11000001' ; ADC clock from int RC, AN0, ADC on
movwf ADCON0
bsf ADCON0,GO ; start ADC
clrf LastSensor ; initialize last sensor reading
call  Commutate ; determine present motor positon
clrf ADC  ; start speed control threshold at zero until first ADC reading
;**********************************************************************
;*
;* Main control loop
;*
Loop
call ReadADC  ; get the speed control from the ADC
incfsz ADC,w  ; if ADC is 0xFF we're at full speed - skip timer add
goto PWM  ; add timer 0 to ADC for PWM
movf DriveWord,w ; force on condition
goto Drive  ; continue
PWM
movf ADC,w  ; restore ADC reading
addwf TMR0,w  ; add it to current timer0
movf DriveWord,w ; restore commutation drive data
btfss STATUS,C ; test if ADC + timer0 resulted in carry         ;不懂
andlw OffMask  ; no carry - supress high drivers                    ;不懂
Drive
movwf DrivePort ; enable motor drivers
call Commutate ; test for commutation change
goto Loop  ; repeat loop

ReadADC
;**********************************************************************
;*
;* If the ADC is ready then read the speed control potentiometer
;* and start the next reading
;*
btfsc ADCON0,NOT_DONE ; is ADC ready?
return   ; no - return
movf ADRESH,w ; get ADC result
bsf ADCON0,GO ; restart ADC
movwf ADC  ; save result in speed control threshold
return   ;
;**********************************************************************
;*
;* Read the sensor inputs and if a change is sensed then get the
;* corresponding drive word from the drive table
;*
Commutate
movlw SensorMask ; retain only the sensor bits
andwf SensorPort,w ; get sensor data
xorwf LastSensor,w ; test if motion sensed
btfsc STATUS,Z ; zero if no change
return   ; no change - back to the PWM loop

xorwf LastSensor,f ; replace last sensor data with current
btfss DirectionBit ; test direction bit
goto FwdCom  ; bit is zero - do forward commutation
    ; reverse commutation
movlw HIGH RevTable ; get MS byte of table
movwf PCLATH  ; prepare for computed GOTO
movlw LOW RevTable ; get LS byte of table
goto Com2
FwdCom    ; forward commutation
movlw HIGH FwdTable ; get MS byte of table
movwf PCLATH  ; prepare for computed GOTO
movlw LOW FwdTable ; get LS byte of table
Com2
addwf LastSensor,w ; add sensor offset
btfsc STATUS,C ; page change in table?
incf PCLATH,f ; yes - adjust MS byte
call GetDrive ; get drive word from table
movwf DriveWord ; save as current drive word
return
GetDrive
movwf PCL
;**********************************************************************
;*
;* The drive tables are built based on the following assumptions:
;* 1) There are six drivers in three pairs of two
;* 2) Each driver pair consists of a high side (+V to motor) and low side (motor to ground) drive
;* 3) A 1 in the drive word will turn the corresponding driver on
;* 4) The three driver pairs correspond to the three motor windings: A, B and C
;* 5) Winding A is driven by bits <1> and <0> where <1> is A's high side drive
;* 6) Winding B is driven by bits <3> and <2> where <3> is B's high side drive
;* 7) Winding C is driven by bits <5> and <4> where <5> is C's high side drive
;* 8) Three sensor bits consitute the address offset to the drive table
;* 9) A sensor bit transitions from a 0 to 1 at the moment that the corresponding
;*    winding's high side forward drive begins.
;* 10) Sensor bit <0> corresponds to winding A
;* 11) Sensor bit <1> corresponds to winding B
;* 12) Sensor bit <2> corresponds to winding C
;*
FwdTable
retlw B'00000000' ; invalid
retlw B'00010010' ; phase 6
retlw B'00001001' ; phase 4
retlw B'00011000' ; phase 5
retlw B'00100100' ; phase 2
retlw B'00000110' ; phase 1
retlw B'00100001' ; phase 3
retlw B'00000000' ; invalid
RevTable
retlw B'00000000' ; invalid
retlw B'00100001' ; phase /6
retlw B'00000110' ; phase /4
retlw B'00100100' ; phase /5
retlw B'00011000' ; phase /2
retlw B'00001001' ; phase /1
retlw B'00010010' ; phase /3
retlw B'00000000' ; invalid
END                       ; directive 'end of program'
我不懂ADC+TMR0是什么意思,还有就是为什么没有进位就与上屏蔽字?
2#
 楼主| 发表于 2009-3-12 16:34:59 | 只看该作者
AD是采集啥的?如果是采集电流的,那可能是一种电流对时间积分的简单办法,防止电流大了过热的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-11-24 06:50 , Processed in 0.054284 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表