?? pidmotor.lst
字號:
C51 COMPILER V8.08 PIDMOTOR 06/30/2007 15:58:44 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE PIDMOTOR
OBJECT MODULE PLACED IN PIDmotor.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE PIDmotor.c BROWSE DEBUG OBJECTEXTEND
line level source
1 /*------------------------------------------------------------------*-
2
3 PID_Motor.c (v1.01)
4
5 ------------------------------------------------------------------
6
7 Small library for PID control of a DC motor.
8 For C515c microcontroller.
9
10 The set point (required speed) is read via a potentiometer
11 and on-chip ADC.
12
13 The current speed is read via an optical encoder. The pulses
14 from the encoder are counted using T0.
15
16 The new speed is set by PWM using the on-chip capture-compare
17 unit (Timer 2).
18
19
20 COPYRIGHT
21 ---------
22
23 This code is from the book:
24
25 PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont
26 [Pearson Education, 2001; ISBN: 0-201-33138-1].
27
28 This code is copyright (c) 2001 by Michael J. Pont.
29
30 See book for copyright details and other information.
31
32 -*------------------------------------------------------------------*/
33
34 #include "Main.h"
35 #include "Port.h"
36
37 #include "PIDMotor.h"
38 #include "PC_O_in.h"
39
40
41 // ------ Public constants -----------------------------------------
42
43 extern const char code CHAR_MAP_G[10];
44
45 // ------ Private function prototypes ------------------------------
46
47 static tByte PID_MOTOR_Get_Required_Speed(void);
48 static tByte PID_MOTOR_Read_Current_Speed(void);
49 static void PID_MOTOR_Set_New_PWM_Output(const tByte);
50
51 // ------ Private constants ----------------------------------------
52
53 #define PULSE_HIGH (0)
54 #define PULSE_LOW (1)
55
C51 COMPILER V8.08 PIDMOTOR 06/30/2007 15:58:44 PAGE 2
56 // PID parameters
57 #define PID_PROPORTIONAL (5)
58 #define PID_INTEGRAL (50)
59 #define PID_DIFFERENTIAL (50)
60
61 // ------ Private variables ----------------------------------------
62
63 // Used for demo purposes only
64 tWord Ticks = 0;
65
66 // Stores the latest count value
67 static tByte Pulse_count_G;
68
69 // Data to be copied to the serial port
70 static char PID_MOTOR_data_G[50] = {" "};
71
72 // Measured speed, required speed and controller output variables
73 static tByte Speed_measured_G = 45;
74 static tByte Speed_required_G = 50;
75 static tByte Controller_output_G = 128;
76
77 static int Old_error_G = 0;
78 static int Sum_G = 0;
79
80
81 /*------------------------------------------------------------------*-
82
83 PID_MOTOR_Init()
84
85 Prepare for UD motor control.
86
87 -*------------------------------------------------------------------*/
88 void PID_MOTOR_Init(void)
89 {
90 1 // -----------------------------------------------------------
91 1 // Set up the initial data to be sent to the PC via RS-232
92 1 // -----------------------------------------------------------
93 1 char* pScreen_Data = "Cur Des PWM \n";
94 1
95 1 tByte c;
96 1
97 1 for (c = 0; c < 30; c++)
98 1 {
99 2 PID_MOTOR_data_G[c] = pScreen_Data[c];
100 2 }
101 1
102 1 // -----------------------------------------------------------
103 1 // Set up the A-D converter
104 1 // (used to measure the 'set point' (the desired motor speed)
105 1 // -----------------------------------------------------------
106 1
107 1 // Select internally-triggered single conversion
108 1 // Reading from P6.0 (single channel)
109 1 ADCON0 = 0xC0; // Mask bits 0 - 5 to 0
110 1
111 1 // Select appropriate prescalar ratio: see manual for details
112 1 ADCON1 = 0x80; // Make bit 7 = 1 : Prescaler ratio=8
113 1
114 1 // -----------------------------------------------------------
115 1 // Set up the PWM output (Cap Com) unit - T2
116 1 // (used to set the desired motor speed)
117 1 // -----------------------------------------------------------
C51 COMPILER V8.08 PIDMOTOR 06/30/2007 15:58:44 PAGE 3
118 1
119 1 // ---------- T2 Mode ---------------------------
120 1 // Mode 1 = Timerfunction
121 1
122 1 // Prescaler: Fcpu/6
123 1
124 1 // ---------- T2 reload mode selection ----------
125 1 // Mode 0 = auto-reload upon timer overflow
126 1 // Preset the timer register with autoreload value ! 0xFF00;
127 1 TL2 = 0x00;
128 1 TH2 = 0xFF;
129 1
130 1 // ---------- T2 general compare mode ----------
131 1 // Mode 0 for all channels
132 1 T2CON |= 0x11;
133 1
134 1 // ---------- T2 general interrupts ------------
135 1 // Timer 2 overflow interrupt is disabled
136 1 ET2=0;
137 1 // Timer 2 external reload interrupt is disabled
138 1 EXEN2=0;
139 1
140 1 // ---------- Compare/capture Channel 0 ---------
141 1 // Disabled??
142 1 // Set Compare Register CRC on: 0xFF00;
143 1 CRCL = 0x00;
144 1 CRCH = 0xFF;
145 1
146 1 // CC0/ext3 interrupt is disabled
147 1 EX3=0;
148 1
149 1 // ---------- Compare/capture Channel 1 ---------
150 1 // Compare enabled
151 1 // Set Compare Register CC1 on: 0xFF80;
152 1 CCL1 = 0x80;
153 1 CCH1 = 0xFF;
154 1
155 1 // CC1/ext4 interrupt is disabled
156 1 EX4=0;
157 1
158 1 // ---------- Compare/capture Channel 2 ---------
159 1 // Disabled
160 1 // Set Compare Register CC2 on: 0x0000;
161 1 CCL2 = 0x00;
162 1 CCH2 = 0x00;
163 1 // CC2/ext5 interrupt is disabled
164 1 EX5=0;
165 1
166 1 // ---------- Compare/capture Channel 3 ---------
167 1 // Disabled
168 1 // Set Compare Register CC3 on: 0x0000;
169 1 CCL3 = 0x00;
170 1 CCH3 = 0x00;
171 1
172 1 // CC3/ext6 interrupt is disabled
173 1 EX6=0;
174 1
175 1 // Set all above mentioned modes for channel 0-3
176 1 CCEN = 0x08;
177 1
178 1 // -----------------------------------------------------------
179 1 // Count pulses on Pin 3.5 [software only]
C51 COMPILER V8.08 PIDMOTOR 06/30/2007 15:58:44 PAGE 4
180 1 // (used to measure the current motor speed)
181 1 // -----------------------------------------------------------
182 1 Pulse_count_pin = 1;
183 1 Pulse_count_G = 0;
184 1 }
185
186 /*------------------------------------------------------------------*-
187
188 PID_MOTOR_Control_Motor()
189
190 The main motor control function.
191
192 -*------------------------------------------------------------------*/
193 void PID_MOTOR_Control_Motor(void)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -