?? main.lst
字號:
C51 COMPILER V7.50 MAIN 11/10/2010 15:11:26 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: D:\Keil\C51\BIN\C51.EXE main.c BROWSE DEFINE(XTAL=19660800) DEBUG OBJECTEXTEND
line level source
1 #include <stdio.h>
2 #include <intrins.h>
3 #include "STC12C5202AD.H"
4 #include "sio.h"
5
6 typedef struct PID
7 {
8
9 int p; // 比例常數 Proportional Const
10 int i; // 積分常數 Integral Const
11 int d; // 微分常數 Derivative Const
12
13 int position;
14 int hisPosition;
15 int lastPosition[3];
16 } PID;
17
18 static PID idata pid;
19 static int GAIN;
20
21 #define MIN9MS (XTAL/2L/256L*9L/1000L*9L/10L) //9ms脈寬*90%
22 #define MAX9MS (XTAL/2L/256L*9L/1000L*11L/10L) //9ms脈寬*110%
23
24 #define MIN45MS (XTAL/2L/256L*45L/10000L*9L/10L) //4.5ms脈寬
25 #define MAX45MS (XTAL/2L/256L*45L/10000L*11L/10L)
26
27 #define MIN225MS (XTAL/2L/256L*225L/100000L*9L/10L) //2.25ms脈寬
28 #define MAX225MS (XTAL/2L/256L*225L/100000L*11L/10L)
29
30 #define MIN056MS (XTAL/2L/256L*56L/100000L*6L/10L) //0.56ms脈寬*60%,下限
31 #define MAX056MS (XTAL/2L/256L*56L/100000L*14L/10L)
32
33 #define MIN168MS (XTAL/2L/256L*168L/100000L*6L/10L) //1.68ms脈寬*60%
34 #define MAX168MS (XTAL/2L/256L*168L/100000L*14L/10L)
35
36 sfr ISP_CUNTR = 0xE7;
37
38 sbit LED1 = P3^0;
39 sbit LED2 = P3^1;
40
41 sbit AD_LED1 = P2^0;
42 sbit AD_LED2 = P2^1;
43 sbit AD_LED3 = P2^7;
44
45 sbit IR_FRONT = P3^2;
46 sbit IR_LEFT = P3^3;
47 sbit IR_RIGHT = P2^6;
48 sbit IR_BACK = P3^7;
49
50 sbit IR_OUT = P3^5;
51
52 sbit MOTO_IN_B1 = P2^5;
53 sbit MOTO_IN_B2 = P2^4;
54
55 sbit MOTO_IN_A1 = P2^3;
C51 COMPILER V7.50 MAIN 11/10/2010 15:11:26 PAGE 2
56 sbit MOTO_IN_A2 = P2^2;
57
58 static unsigned char idata ad_datas[8]; //8路光電管采樣電壓
59 static unsigned char idata ad_datas_check[8]; //8路光電管采樣電壓校驗值,軌道的白色背景的采樣值
60
61 bit power_stat;
62 static unsigned char car_stat; //小車狀態:0,停止;1,前進;2,后退;3,左轉;4,右轉;5,尋線模式;ff,自控避障模式
63 static unsigned int now;
64
65 static unsigned char code led_mod_table[3][20] = {
66 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
67 {1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},
68 {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0}
69 };
70 unsigned char idata led_mod = 0;
71 static unsigned char idata led_tick = 0;
72 static unsigned char idata led_ptr = 0;
73
74
75 static bit test;
76
77 static unsigned char tick = 0;
78 static unsigned int pwm38k = 0;
79 static unsigned char check;
80
81 static unsigned char pwm_moto = 0;
82 static unsigned char pwm_moto_left = 0;
83 static unsigned char pwm_moto_right = 0;
84 static bit moto_left_forward = 1;
85 static bit moto_right_forward = 1;
86
87
88 #define IR_SINGAL_DELAY 11 //接收管輸出延遲載波數量
89 #define TEST_PERIOD 1620 //評估周期,這個不同的接收管差別很大.
90 #define IR_SINGAL_PERIOD 76 //持續發射紅外線載波數量
91 #define IR_VALID_THROLD 70 //判斷是否前方有障礙的閥值
92
93 /*
94 #define IR_SINGAL_DELAY 1 //接收管輸出延遲載波數量
95 #define TEST_PERIOD 200 //評估周期,這個不同的接收管差別很大.
96 #define IR_SINGAL_PERIOD 10 //持續發射紅外線載波數量
97 #define IR_VALID_THROLD 8 //判斷是否前方有障礙的閥值
98 */
99 static unsigned char idata front_signal = 0;
100 static unsigned char idata back_signal = 0;
101 static unsigned char idata left_signal = 0;
102 static unsigned char idata right_signal = 0;
103
104 static bit front_obj = 0, back_obj = 0, left_obj = 0, right_obj = 0;
105
106 /*
107 * PCA中斷計數,根據位置判斷信號區域和定義,位置0表示初始,1代表引導碼信號,2表示引導碼間隔,
108 * 3表示第一個bit的信號,4表示第一個bit的間隔,以次類推...
109 * 更具體見對應的紅外線協議.
110 */
111 static unsigned int idata pca_tick;
112 static unsigned char idata pca_int_count;
113 static unsigned char data pca_int_total; /* 根據引導頭確定總長度 */
114 static unsigned int idata period; /* 紅外信號占或空周期計數 */
115 static unsigned char idata data_buf[6]; /* 紅外線協議數據緩沖區 */
116 static unsigned int idata ccap1; //PCA0上一次的的計數
117 static unsigned char idata frame_dog; //紅外幀看門狗,限定時間未接收完成清除工作
C51 COMPILER V7.50 MAIN 11/10/2010 15:11:26 PAGE 3
118
119 static void delay_ms(unsigned int v) {
120 1 unsigned int wait = pca_tick + v / 7 + 1;
121 1 while (wait != pca_tick) {
122 2 PCON |= 0x01;
123 2 }
124 1 }
125
126 static void delay_100us(unsigned char v) {
127 1 unsigned char wait = tick + v * 2; //每tick約50us
128 1 while (wait != tick) {
129 2 ;//PCON |= 0x01;
130 2 }
131 1 }
132
133 void pid_init(PID *pid)
134 {
135 1 pid->position = 0;
136 1 pid->hisPosition = 0;
137 1 pid->lastPosition[0] = 0;
138 1 pid->lastPosition[1] = 0;
139 1 pid->lastPosition[2] = 0;
140 1 pid->p = 2;
141 1 pid->i = 0;
142 1 pid->d = 8;
143 1 }
144
145 void read_sensors(unsigned char sensors[]) { //讀光電傳感器的采樣值
146 1 unsigned char i;
147 1
148 1 for (i = 0; i < 8; i++) { //LED熄滅狀態測量一次電壓
149 2 ADC_CONTR = 0xE0 | i; //設置AD通道, 快速AD采樣.
150 2 delay_100us(5); //延遲等待電壓穩定
151 2 ADC_RES = 0;
152 2 ADC_CONTR = 0xE8 | i;; //啟動AD
153 2 while (!(ADC_CONTR & 0x10)) {
154 3
155 3 }
156 2 ADC_CONTR &= 0xE7;
157 2 sensors[i] = ADC_RES;
158 2 }
159 1
160 1 AD_LED1 = 0;
161 1 AD_LED2 = 0;
162 1 AD_LED3 = 0;
163 1
164 1 for (i = 0; i < 8; i++) { //LED打開再測量一次電壓,用于消除環境光的差異.
165 2 ADC_CONTR = 0xE0 | i; //設置AD通道, 快速AD采樣.
166 2
167 2 delay_100us(5); //延遲等待電壓穩定
168 2
169 2 ADC_RES = 0;
170 2 ADC_CONTR = 0xE8 | i;; //啟動AD
171 2 while (!(ADC_CONTR & 0x10)) {
172 3
173 3 }
174 2 ADC_CONTR &= 0xE7;
175 2
176 2 if (ADC_RES > sensors[i])
177 2 sensors[i] = ADC_RES - sensors[i];
178 2 else
179 2 sensors[i] = ADC_RES;
C51 COMPILER V7.50 MAIN 11/10/2010 15:11:26 PAGE 4
180 2 }
181 1 AD_LED1 = 1;
182 1 AD_LED2 = 1;
183 1 AD_LED3 = 1;
184 1 }
185
186 static void read_sensors_check() { //從flash中讀取光電傳感器的校驗值,結構是8byte的值+1字節的crc
187 1 unsigned char i, crc = 0;
188 1 IAP_ADDRH = 0;
189 1 IAP_CONTR = 0x80; //允許ISP/IAP操作
190 1 IAP_CMD = 0x01; //讀flash
191 1
192 1 for (i = 0; i < 8; i++) {
193 2 IAP_ADDRL = i;
194 2 IAP_TRIG = 0x5A;
195 2 IAP_TRIG = 0xA5; //啟動IAP操作
196 2 _nop_();
197 2 ad_datas_check[i] = IAP_DATA;
198 2 crc += IAP_DATA;
199 2 //com_putchar(IAP_DATA);
200 2 }
201 1 IAP_ADDRL = 8;
202 1 IAP_TRIG = 0x5A;
203 1 IAP_TRIG = 0xA5; //啟動IAP操作
204 1 _nop_();
205 1
206 1 if (crc != IAP_DATA) { //crc驗證錯誤,flash數據失效
207 2 for (i = 0; i < 8; i++) {
208 3 ad_datas_check[i] = 0xff;
209 3 }
210 2 }
211 1
212 1 IAP_CONTR = 0; //禁止IAP,防止誤操作
213 1 IAP_CMD = 0;
214 1 IAP_TRIG = 0;
215 1 IAP_ADDRH = 0xff;
216 1 IAP_ADDRL = 0xff;
217 1 }
218
219 static void write_sensors_check() { //將光電傳感器的校驗值寫入flash,結構是8byte的值+1字節的crc
220 1 unsigned char i, crc = 0;
221 1 IAP_ADDRH = 0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -