我有一个PIC16F628A,我有2个电机连接到PIC的引脚。引脚RB0和RB1连接到右电机。引脚RB2和RB3连接到左电机。我正在使用MPLAB作为.pbp到.hex的编译器。我想编写一个PIC BASIC程序(.pbp)来移动其中一个电机(例如:左一个)...我刚刚写了一个像这样的简单代码段。
TRISB= %11110000
PORTB.2=0
PORTB.3=0
MAINLOOP:
PORTB.2=1
PAUSE 50
PORTB.3=1
PAUSE 50
GOTO MAINLOOP
END
编译器正确编译它,在我成功刻录hex文件后,我放猪,然后我启动它,我什么也看不见......!我真的很喜欢这种编程语言,我只想问我是否需要编写任何其他代码段来运行我的电机?或者之前有没有人有这样的项目?
答案 0 :(得分:1)
list p=16F628A
include <P16F628A.INC>
cblock 0x20
COUNT1
COUNT2
endc
org 0x00
init movlw .50
movwf COUNT1
movwf COUNT2
;;;SET PWM FREQUENCY;;;
bank1 ;SELECT BANK 01
movlw D'128' ;SET PR2 TO 128 DECIMAL SO THE PWM PERIOD = 2064uS => PWM FREQUENCY = 484Hz
movwf PR2
bank0 ;SELECT BANK 00
clrf CCPR1L ;SET PWM STARTING DUTY CYCLE;;;
comf CCPR1L
movlw B'00001100' ;SET PWM MODE, BITS 5 AND 4 ARE THE TWO LSBs OF THE 10BIT DUTY CYCLE REGISTER (CCPR1L:CCP1CON<5:4>)
movwf CCP1CON ;SET PWM PIN TO OUTPUT MODE;;;
bank1 ;SELECT BANK 01
bcf TRISB, 3 ;SET RB3 AS OUTPUT, TO USE FOR PWM
bank0 ;SELECT BANK 00
movlw B'00000010' ;SET TIMER 2 PRESCALE VALUE;;;PRESCALE = 16 SO THE PWM PERIOD = 2064uS => PWM FREQUENCY = 484Hz
movwf T2CON
clrf TMR2 ;CLEAR TIMER 2 MODULE;;;
bsf T2CON, TMR2ON ;ENABLE TIMER 2 MODULE;;;
main call DELAY
goto main
DELAY
loop1 decfsz COUNT1,1
goto loop1
decfsz COUNT2,1
goto loop1
return
end