📄 main.lst
字号:
C51 COMPILER V7.50 MAIN 05/16/2010 14:53:48 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 #define MIN9MS (XTAL/2L/256L*9L/1000L*9L/10L) //9ms脉宽*90%
7 #define MAX9MS (XTAL/2L/256L*9L/1000L*11L/10L) //9ms脉宽*110%
8
9 #define MIN45MS (XTAL/2L/256L*45L/10000L*9L/10L) //4.5ms脉宽
10 #define MAX45MS (XTAL/2L/256L*45L/10000L*11L/10L)
11
12 #define MIN225MS (XTAL/2L/256L*225L/100000L*9L/10L) //2.25ms脉宽
13 #define MAX225MS (XTAL/2L/256L*225L/100000L*11L/10L)
14
15 #define MIN056MS (XTAL/2L/256L*56L/100000L*6L/10L) //0.56ms脉宽*60%,下限
16 #define MAX056MS (XTAL/2L/256L*56L/100000L*14L/10L)
17
18 #define MIN168MS (XTAL/2L/256L*168L/100000L*6L/10L) //1.68ms脉宽*60%
19 #define MAX168MS (XTAL/2L/256L*168L/100000L*14L/10L)
20
21 sfr ISP_CUNTR = 0xE7;
22
23 sbit LED1 = P3^0;
24 sbit LED2 = P3^1;
25
26 sbit AD_LED1 = P2^0;
27 sbit AD_LED2 = P2^1;
28 sbit AD_LED3 = P2^7;
29
30 sbit IR_FRONT = P3^2;
31 sbit IR_LEFT = P3^3;
32 sbit IR_RIGHT = P2^6;
33 sbit IR_BACK = P3^7;
34
35 sbit IR_OUT = P3^5;
36
37 sbit MOTO_IN_B1 = P2^5;
38 sbit MOTO_IN_B2 = P2^4;
39
40 sbit MOTO_IN_A1 = P2^3;
41 sbit MOTO_IN_A2 = P2^2;
42
43 static unsigned char ad_datas[8]; //8路光电管采样电压
44
45 bit power_stat;
46 static unsigned char car_stat; //小车状态:0,停止;1,前进;2,后退;3,左转;4,右转;5,寻线模式;ff,自控避障模式
47 static unsigned int now;
48
49 static unsigned char code led_mod_table[3][20] = {
50 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
51 {1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},
52 {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0}
53 };
54 unsigned char idata led_mod = 0;
55 static unsigned char idata led_tick = 0;
C51 COMPILER V7.50 MAIN 05/16/2010 14:53:48 PAGE 2
56 static unsigned char idata led_ptr = 0;
57
58
59 static bit test;
60
61 static unsigned char tick = 0;
62 static unsigned char pwm_tick = 0;
63 static unsigned char pwm = 0;
64 static unsigned char pwm_left = 0;
65 static unsigned char pwm_right = 0;
66 static unsigned char check;
67
68 #define IR_SINGAL_DELAY 11 //接收管输出延迟载波数量
69 #define TEST_PERIOD 1620 //评估周期,这个不同的接收管差别很大.
70 #define IR_SINGAL_PERIOD 76 //持续发射红外线载波数量
71 #define IR_VALID_THROLD 70 //判断是否前方有障碍的阀值
72
73 /*
74 #define IR_SINGAL_DELAY 1 //接收管输出延迟载波数量
75 #define TEST_PERIOD 200 //评估周期,这个不同的接收管差别很大.
76 #define IR_SINGAL_PERIOD 10 //持续发射红外线载波数量
77 #define IR_VALID_THROLD 8 //判断是否前方有障碍的阀值
78 */
79 static unsigned char idata front_signal = 0;
80 static unsigned char idata back_signal = 0;
81 static unsigned char idata left_signal = 0;
82 static unsigned char idata right_signal = 0;
83
84 static bit front_obj = 0, back_obj = 0, left_obj = 0, right_obj = 0;
85
86 /*
87 * PCA中断计数,根据位置判断信号区域和定义,位置0表示初始,1代表引导码信号,2表示引导码间隔,
88 * 3表示第一个bit的信号,4表示第一个bit的间隔,以次类推...
89 * 更具体见对应的红外线协议.
90 */
91 static unsigned int idata pca_tick;
92 static unsigned char idata pca_int_count;
93 static unsigned char data pca_int_total; /* 根据引导头确定总长度 */
94 static unsigned int idata period; /* 红外信号占或空周期计数 */
95 static unsigned char idata data_buf[6]; /* 红外线协议数据缓冲区 */
96 static unsigned int idata ccap1; //PCA0上一次的的计数
97 static unsigned char idata frame_dog; //红外帧看门狗,限定时间未接收完成清除工作
98
99 static void delay_ms(unsigned int v) {
100 1 unsigned int wait = pca_tick + v / 7 + 1;
101 1 while (wait != pca_tick) {
102 2 PCON |= 0x01;
103 2 }
104 1 }
105
106 void time0_isr() interrupt 1
107 {
108 1 tick++;
109 1 if (tick >= pwm_tick)
110 1 {
111 2 tick = 0;
112 2
113 2 if (pwm == 0)
114 2 {
115 3 if (pwm_left > 0)
116 3 {
117 4 MOTO_IN_A1 = 0;
C51 COMPILER V7.50 MAIN 05/16/2010 14:53:48 PAGE 3
118 4 MOTO_IN_A2 = 1;
119 4 } else
120 3 {
121 4 MOTO_IN_A1 = 0;
122 4 MOTO_IN_A2 = 0;
123 4 }
124 3 if (pwm_right > 0)
125 3 {
126 4 MOTO_IN_B1 = 0;
127 4 MOTO_IN_B2 = 1;
128 4 } else
129 3 {
130 4 MOTO_IN_B1 = 0;
131 4 MOTO_IN_B2 = 0;
132 4 }
133 3 } else
134 2 {
135 3 if (pwm == pwm_left)
136 3 {
137 4 MOTO_IN_A1 = 0;
138 4 MOTO_IN_A2 = 0;
139 4 }
140 3 if (pwm == pwm_right)
141 3 {
142 4 MOTO_IN_B1 = 0;
143 4 MOTO_IN_B2 = 0;
144 4 }
145 3 }
146 2 pwm++;
147 2 if (pwm == 100)
148 2 pwm = 0;
149 2
150 2 }
151 1 }
152
153 void time0_initialize(void)
154 {
155 1 TMOD &= ~0x0F; /* clear timer 0 mode bits */
156 1 TMOD |= 0x02; /* put timer 0 into MODE 2 */
157 1 AUXR |= 0x80; // timer0工作在1T模式
158 1 TH0 = 0;
159 1 TL0 = 0x0;
160 1
161 1 // WAKE_CLKO = 0x01; // T0在P3.4上输出时钟.
162 1
163 1 PT0 = 1; /* 时钟0中断高优先级 */
164 1 TR0 = 1; //
165 1 ET0 = 1;
166 1 }
167
168 static void wakeup (void) interrupt 2
169 {
170 1
171 1 }
172
173
174 static void pca_isr (void) interrupt 6
175 {
176 1 unsigned char i, j;
177 1
178 1 if (CCF0) {
179 2 CCF0 = 0; //清PCA1中断标志
C51 COMPILER V7.50 MAIN 05/16/2010 14:53:48 PAGE 4
180 2 LED1 = IR_BACK;
181 2 LED2 = IR_BACK;
182 2 if (!pca_int_count) { //第一次收到信号
183 3 if (!IR_BACK) {
184 4 ccap1 = pca_tick * 256 + CCAP0H;
185 4 pca_int_count++;
186 4 }
187 3 } else { //已经收到一些信号
188 3 period = pca_tick * 256 + CCAP0H - ccap1;
189 3 ccap1 = pca_tick * 256 + CCAP0H;
190 3 //com_putchar(period / 256);
191 3 //com_putchar(period % 256);
192 3
193 3 if (pca_int_count == 1) {
194 4 if (period < MIN9MS || period > MAX9MS) { //9ms
195 5 pca_int_count = 0;
196 5 frame_dog = 0;
197 5 } else
198 4 pca_int_count++;
199 4 } else if (pca_int_count == 2) {
200 4 if (period > MIN225MS && period < MAX225MS) { //2.25ms
201 5 pca_int_total = 3;
202 5 pca_int_count++;
203 5 } else if (period > MIN45MS && period < MAX45MS) { //4.5ms
204 5 pca_int_total = 67;
205 5 pca_int_count++;
206 5 } else {
207 5 pca_int_count = 0;
208 5 frame_dog = 0;
209 5 }
210 4 } else {
211 4 if (IR_BACK) {
212 5 if (period > MIN056MS && period < MAX056MS) { //0.56ms
213 6 if (pca_int_count >= pca_int_total) { //帧接收完毕,下面进行有效性分析.
214 7 if (pca_int_total == 67) { //完整信号,含有引导信号,设备码8bit,设备反码8bit,命
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -