-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwm.cpp
More file actions
92 lines (82 loc) · 3.56 KB
/
Copy pathpwm.cpp
File metadata and controls
92 lines (82 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* -------------------------------------------------------------------------- */
/* pwm.c */
/* CL21 */
/* */
/* -------------------------------------------------------------------------- */
/* 番号 更新履歴 日付 氏名 */
/* -------------------------------------------------------------------------- */
/* 000000 新規作成 2016/09/07 田中 翔吾 */
/* -------------------------------------------------------------------------- */
#include "pwm.h"
/* -------------------------------------------------------------------------- */
/* グローバル変数 */
/* -------------------------------------------------------------------------- */
SINT cycle = SRVO_CYCLE; /* PWM周期(us) */
SINT pulse = 0; /* パルス幅(us) */
/* -------------------------------------------------------------------------- */
/* 関数名 : AttachServo */
/* 機能名 : サーボモータ設定 */
/* 機能概要 : */
/* 引数 : volatile UCHR* : ddrx : ポート方向レジスタ(DDRx) */
/* : UCHR : bitno : ビット番号 */
/* 戻り値 : なし */
/* 作成日 : 2016/09/07 田中 翔吾 */
/* -------------------------------------------------------------------------- */
void AttachServo( volatile UCHR* ddrx, UCHR bitno )
{
High( *ddrx, bitno );
}
/* -------------------------------------------------------------------------- */
/* 関数名 : MoveServo */
/* 機能名 : サーボモータ動作 */
/* 機能概要 : サーボモータを指定角に動かす */
/* 引数 : SINT : degree : 角度(°) */
/* 戻り値 : なし */
/* 作成日 : 2016/09/07 田中 翔吾 */
/* -------------------------------------------------------------------------- */
void MoveServo( SINT degree )
{
/* 回転Duty比の算出 */
pulse = SRVO_RATIO; /* 1°あたりのパルス幅(us) */
pulse = pulse * degree + SRVO_MIN; /* 指定角度のパルス幅(us) */
SetParamPWM( SRVO_CYCLE/100, pulse/100 );
/* タイマ割込み設定 */
SetTimerInterrupt( 0.1 ); /* 100usごとに割込み */
}
/* -------------------------------------------------------------------------- */
/* 関数名 : SetParamPWM */
/* 機能名 : PWMパラメータ設定 */
/* 機能概要 : PWM周期とパルス幅(HIGHの時間)を設定する。 */
/* 引数 : SINT : cycle : PWM周期(us) */
/* : SINT : pulse : パルス幅(us) */
/* 戻り値 : なし */
/* 作成日 : 2016/09/07 田中 翔吾 */
/* -------------------------------------------------------------------------- */
void SetParamPWM( SINT cycle_us, SINT pulse_us )
{
cycle = cycle_us;
pulse = pulse_us;
}
/* -------------------------------------------------------------------------- */
/* 関数名 : PWM */
/* 機能名 : PWM処理 */
/* 機能概要 : PWMのHIGH/LOWの切り替えを行う。 */
/* 引数 : volatile UCHR* : portx : ポート出力レジスタ(PORTx) */
/* : UCHR : bitno : ビット番号 */
/* 戻り値 : なし */
/* 作成日 : 2016/09/07 田中 翔吾 */
/* -------------------------------------------------------------------------- */
void PWM( volatile UCHR* portx, UCHR bitno )
{
static int tcnt = 0;
if( tcnt % cycle == 0 ){
High( *portx, bitno );
tcnt = 0;
}else if( tcnt == pulse ){
Low( *portx, bitno );
}
tcnt++;
}
/* -------------------------------------------------------------------------- */
/* Copyright HAL College of Technology & Design */
/* -------------------------------------------------------------------------- */