📄 motor.s
字号:
.module motor.c
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator.1B4135EF7FF9405\桌面\智能小车案\code\motor.c
.dbfunc e motor_left_forward _motor_left_forward fV
; speed -> R16
.even
_motor_left_forward::
.dbline -1
.dbline 21
; /****************************************************************
; ** 文件名:motor.c 电机驱动函数
; ****************************************************************/
; #include "config.h"
; /*#define T0_EN TIMSK |= (1<<OCIE0)|(1<<TOIE0)
; #define T0_UEN TIMSK &=~ (1<<OCIE0)|(1<<TOIE0)
; #define T0_start TCCR0 |= (1<<WGM00)|(1<<WGM01)|(1<<COM01)|0x04;
; //0x04 0100B 代表256预分频
; #define T0_stop TCCR0 = 0x00
;
; #define T2_EN TIMSK |= (1<<OCIE2)|(1<<TOIE2)
; #define T2_UEN TIMSK &=~ (1<<OCIE2)|(1<<TOIE2)
; #define T2_start TCCR2 = (1<<WGM20)|(1<<WGM21)|(1<<COM21)|0x06
; #define T2_stop TCCR2 = 0x00
;
;
;
; /************************左电机动作*****************************/
; //左电机前进
; void motor_left_forward(uint8 speed)
; {
.dbline 22
; motol_uen1;
cbi 0x18,4
.dbline 23
; motol_en2;
sbi 0x18,5
.dbline 24
; if(speed!=0) //加入调速指令
tst R16
breq L2
.dbline 25
; {
.dbline 26
; OCR0=speed;
out 0x3c,R16
.dbline 27
; }
L2:
.dbline 28
; T0_EN;
in R24,0x39
ori R24,3
out 0x39,R24
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbsym r speed 16 c
.dbend
.dbfunc e motor_left_backward _motor_left_backward fV
; speed -> R16
.even
_motor_left_backward::
.dbline -1
.dbline 32
; }
; //左电机后退
; void motor_left_backward(uint8 speed)
; {
.dbline 33
; motol_en1;
sbi 0x18,4
.dbline 34
; motol_uen2;
cbi 0x18,5
.dbline 35
; if(speed!=0) //加入调速指令
tst R16
breq L5
.dbline 36
; {
.dbline 37
; OCR0=speed;
out 0x3c,R16
.dbline 38
; }
L5:
.dbline 39
; T0_EN;
in R24,0x39
ori R24,3
out 0x39,R24
.dbline -2
L4:
.dbline 0 ; func end
ret
.dbsym r speed 16 c
.dbend
.dbfunc e motor_left_speed_set _motor_left_speed_set fV
; speed -> R16
.even
_motor_left_speed_set::
.dbline -1
.dbline 43
.dbline 44
tst R16
breq L8
.dbline 45
.dbline 46
out 0x3c,R16
.dbline 47
L8:
.dbline -2
L7:
.dbline 0 ; func end
ret
.dbsym r speed 16 c
.dbend
.dbfunc e motor_left_stop _motor_left_stop fV
.even
_motor_left_stop::
.dbline -1
.dbline 51
; }
; //左电机速度设定
; void motor_left_speed_set(uint8 speed)
; {
; if(speed!=0)
; {
; OCR0=speed;
; }
; }
; //左电机滑行
; void motor_left_stop(void)
; {
.dbline 52
; motol_uen1;
cbi 0x18,4
.dbline 53
; motol_uen2;
cbi 0x18,5
.dbline 54
; T0_UEN;
in R24,0x39
andi R24,253
out 0x39,R24
.dbline -2
L10:
.dbline 0 ; func end
ret
.dbend
.dbfunc e motor_left_quick_stop _motor_left_quick_stop fV
.even
_motor_left_quick_stop::
.dbline -1
.dbline 58
; }
; //左电机急停
; void motor_left_quick_stop(void)
; {
.dbline 59
; motol_en1;
sbi 0x18,4
.dbline 60
; motol_en2;
sbi 0x18,5
.dbline 61
; T0_UEN;
in R24,0x39
andi R24,253
out 0x39,R24
.dbline -2
L11:
.dbline 0 ; func end
ret
.dbend
.dbfunc e motor_right_forward _motor_right_forward fV
; speed -> R16
.even
_motor_right_forward::
.dbline -1
.dbline 67
; }
;
; /*************************右电机动作*****************************/
; //右电机前进
; void motor_right_forward(uint8 speed)
; {
.dbline 68
; motor_en1;
sbi 0x18,1
.dbline 69
; motor_uen2;
cbi 0x18,2
.dbline 70
; if(speed!=0) //加入调速指令
tst R16
breq L13
.dbline 71
; {
.dbline 72
; OCR2=speed;
out 0x23,R16
L15:
.dbline 73
L16:
.dbline 73
in R2,0x22
sbrc R2,0
rjmp L15
.dbline 74
L13:
.dbline -2
L12:
.dbline 0 ; func end
ret
.dbsym r speed 16 c
.dbend
.dbfunc e motor_right_backward _motor_right_backward fV
; speed -> R16
.even
_motor_right_backward::
.dbline -1
.dbline 79
; while(ASSR&(1<<TCR2UB)==1) ; //异步操作需要等待 OCR2写入完毕
; }
; //T2_EN;
; }
; //右电机后退
; void motor_right_backward(uint8 speed)
; {
.dbline 80
; motor_uen1;
cbi 0x18,1
.dbline 81
; motor_en2;
sbi 0x18,2
.dbline 82
; if(speed!=0) //加入调速指令
tst R16
breq L19
.dbline 83
; {
.dbline 84
; OCR2=speed;
out 0x23,R16
L21:
.dbline 85
L22:
.dbline 85
in R2,0x22
sbrc R2,0
rjmp L21
.dbline 86
L19:
.dbline -2
L18:
.dbline 0 ; func end
ret
.dbsym r speed 16 c
.dbend
.dbfunc e motor_right_speed_set _motor_right_speed_set fV
; speed -> R16
.even
_motor_right_speed_set::
.dbline -1
.dbline 91
; while(ASSR&(1<<TCR2UB)==1) ; //异步操作需要等待 OCR2写入完毕
; }
; //T2_EN;
; }
; //右电机速度设定
; void motor_right_speed_set(uint8 speed)
; {
.dbline 92
; if(speed!=0)
tst R16
breq L25
.dbline 93
; {
.dbline 94
; OCR0=speed;
out 0x3c,R16
L27:
.dbline 95
L28:
.dbline 95
in R2,0x22
sbrc R2,0
rjmp L27
.dbline 96
L25:
.dbline -2
L24:
.dbline 0 ; func end
ret
.dbsym r speed 16 c
.dbend
.dbfunc e motor_right_stop _motor_right_stop fV
.even
_motor_right_stop::
.dbline -1
.dbline 100
; while(ASSR&(1<<TCR2UB)==1) ; //异步操作需要等待 OCR2写入完毕
; }
; }
; //右电机滑行
; void motor_right_stop(void)
; {
.dbline 101
; motor_uen1;
cbi 0x18,1
.dbline 102
; motor_uen2;
cbi 0x18,2
.dbline 103
; T2_UEN;
in R24,0x39
andi R24,127
out 0x39,R24
.dbline -2
L30:
.dbline 0 ; func end
ret
.dbend
.dbfunc e motor_right_quick_stop _motor_right_quick_stop fV
.even
_motor_right_quick_stop::
.dbline -1
.dbline 108
; }
;
; //右电机急停
; void motor_right_quick_stop(void)
; {
.dbline 109
; motor_en1;
sbi 0x18,1
.dbline 110
; motor_en2;
sbi 0x18,2
.dbline 111
; T2_UEN;
in R24,0x39
andi R24,127
out 0x39,R24
.dbline -2
L31:
.dbline 0 ; func end
ret
.dbend
.dbfunc e straight _straight fV
.even
_straight::
.dbline -1
.dbline 117
; }
;
; /**********************小车动作*****************************/
;
; void straight(void) //直行
; {motor_right_quick_stop();
.dbline 117
xcall _motor_right_quick_stop
.dbline 118
; motor_left_forward(0xfe);
ldi R16,254
xcall _motor_left_forward
.dbline -2
L32:
.dbline 0 ; func end
ret
.dbend
.dbfunc e turn_left _turn_left fV
.even
_turn_left::
.dbline -1
.dbline 124
; // motor_right_forward(0xfe);
;
; }
;
; void turn_left(void) //左转
; {
.dbline 125
; motor_left_forward(0xfe);
ldi R16,254
xcall _motor_left_forward
.dbline 126
; motor_right_forward(0x50);
ldi R16,80
xcall _motor_right_forward
.dbline -2
L33:
.dbline 0 ; func end
ret
.dbend
.dbfunc e turn_right _turn_right fV
.even
_turn_right::
.dbline -1
.dbline 130
; }
;
; void turn_right(void) //右转
; {
.dbline 131
; motor_left_forward(0xfe);
ldi R16,254
xcall _motor_left_forward
.dbline 132
; motor_right_backward(0x50);
ldi R16,80
xcall _motor_right_backward
.dbline -2
L34:
.dbline 0 ; func end
ret
.dbend
.dbfunc e straight_back _straight_back fV
.even
_straight_back::
.dbline -1
.dbline 136
; }
;
; void straight_back(void) //直回
; {motor_right_quick_stop();
.dbline 136
xcall _motor_right_quick_stop
.dbline 137
; motor_left_backward(0xfe);
ldi R16,254
xcall _motor_left_backward
.dbline -2
L35:
.dbline 0 ; func end
ret
.dbend
.dbfunc e stop _stop fV
.even
_stop::
.dbline -1
.dbline 142
; //motor_right_backward(0x77);
; }
;
; void stop(void) //急停
; {
.dbline 143
; motor_left_quick_stop();
xcall _motor_left_quick_stop
.dbline 144
; motor_right_quick_stop();
xcall _motor_right_quick_stop
.dbline -2
L36:
.dbline 0 ; func end
ret
.dbend
.dbfunc e mic_turn_right _mic_turn_right fV
.even
_mic_turn_right::
.dbline -1
.dbline 148
; }
;
; void mic_turn_right(void) //向右微调
; {
.dbline 149
; motor_left_forward(0xa0);
ldi R16,160
xcall _motor_left_forward
.dbline 150
; motor_right_backward(0x30);
ldi R16,48
xcall _motor_right_backward
.dbline -2
L37:
.dbline 0 ; func end
ret
.dbend
.dbfunc e mic_turn_left _mic_turn_left fV
.even
_mic_turn_left::
.dbline -1
.dbline 154
; }
;
; void mic_turn_left(void) //向左微调
; {
.dbline 155
; motor_left_forward(0xa0);
ldi R16,160
xcall _motor_left_forward
.dbline 156
; motor_right_forward(0x30);
ldi R16,48
xcall _motor_right_forward
.dbline -2
L38:
.dbline 0 ; func end
ret
.dbend
.dbfunc e flag_test _flag_test fV
.area func_lit(rom, con, rel)
L49:
.word `L48
.word `L44
.word `L45
.word `L47
.word `L45
.word `L45
.word `L44
.word `L44
.word `L44
.word `L45
.word `L44
.word `L45
.word `L46
.word `L45
.word `L44
.word `L43
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator.1B4135EF7FF9405\桌面\智能小车案\code\motor.c
; temp -> R20
.even
_flag_test::
xcall push_gset1
.dbline -1
.dbline 160
; }
;
; void flag_test(void) //测试小车状态
; {
.dbline 162
; uint8 temp;
; temp = PINA&0x0f; //PINA的低四位
in R20,0x19
andi R20,15
.dbline 163
; switch(temp)
clr R21
cpi R20,0
ldi R30,0
cpc R21,R30
brlt L41
ldi R24,15
ldi R25,0
cp R24,R20
cpc R25,R21
brlt L41
ldi R16,2
ldi R17,0
movw R18,R20
xcall empy16s
movw R30,R16
ldi R24,<L49
ldi R25,>L49
add R30,R24
adc R31,R25
lpm R0,Z+
lpm R1,Z
movw R30,R0
ijmp
X0:
.dbline 164
; {
L43:
.dbline 166
; case 0x0f:
; flag = 0; //未偏
clr R2
sts _flag,R2
.dbline 167
; break;
xjmp L41
L44:
.dbline 174
; case 0x01:
; case 0x06:
; case 0x07:
; case 0x08:
; case 0x0a:
; case 0x0e:
; flag = 1; //左偏
ldi R24,1
sts _flag,R24
.dbline 175
; break;
xjmp L41
L45:
.dbline 182
; case 0x02:
; case 0x04:
; case 0x05:
; case 0x09:
; case 0x0b:
; case 0x0d:
; flag = 2; //右偏
ldi R24,2
sts _flag,R24
.dbline 183
; break;
xjmp L41
L46:
.dbline 185
; case 0x0c:
; flag = 3; //前两出线
ldi R24,3
sts _flag,R24
.dbline 186
; break;
xjmp L41
L47:
.dbline 188
; case 0x03:
; flag = 4; //后两出线
ldi R24,4
sts _flag,R24
.dbline 189
; break;
xjmp L41
L48:
.dbline 191
ldi R24,5
sts _flag,R24
.dbline 192
.dbline 194
L41:
.dbline -2
L39:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r temp 20 c
.dbend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -