forked from ypwhs/smartCar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
80x60串口输出赛道.c
420 lines (342 loc) · 10.6 KB
/
80x60串口输出赛道.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include "gpio.h"
#include "common.h"
#include "uart.h"
#include "dma.h"
#include "i2c.h"
#include "ov7725.h"
#include "oled_spi.h"
#include "ftm.h"
#include "lptmr.h"
#define offset 77
void turn(int angel){
angel += offset;
int pwm = (int)((angel/90.0 + 0.5) * 500); //90度是1.5ms
printf("turn:%d\r\n", pwm);
FTM_PWM_ChangeDuty(HW_FTM1, HW_FTM_CH0, pwm);
}
#define DRIVER_PWM_WIDTH 1000
void setSpeed(int spd);
void initDriver(){
printf("initPWM\r\n");
//for(int i=0;i<0;i++)
GPIO_QuickInit(HW_GPIOC, 0, kGPIO_Mode_OPP);
PCout(0)=1;
//使能INH
FTM_PWM_QuickInit(FTM1_CH0_PA12, kPWM_EdgeAligned, 50); //设置FTM,边沿对齐模式
turn(0);
//初始化舵机
FTM_PWM_QuickInit(FTM0_CH0_PC01, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH1_PC02, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH2_PC03, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH3_PC04, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
//初始化电机PWM输出
setSpeed(0);
}
void setSpeed(int spd){
if(spd>0){
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, 0);
}else
{
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, -spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, -spd);
}
}
/* 请将I2C.H中的 I2C_GPIO_SIM 改为 1 */
// 改变图像大小
//0: 80x60
//1: 160x120
//2: 240x180
#define IMAGE_SIZE 0
#if (IMAGE_SIZE == 0)
#define OV7620_W (80)
#define OV7620_H (60)
#elif (IMAGE_SIZE == 1)
#define OV7620_W (160)
#define OV7620_H (120)
#elif (IMAGE_SIZE == 2)
#define OV7620_W (240)
#define OV7620_H (180)
#else
#error "Image Size Not Support!"
#endif
// 图像内存池
uint8_t gCCD_RAM[(OV7620_H)*((OV7620_W/8)+1)]; //使用内部RAM
/* 行指针 */
uint8_t * gpHREF[OV7620_H+1];
/* 引脚定义 PCLK VSYNC HREF 接到同一个PORT上 */
#define BOARD_OV7620_PCLK_PORT HW_GPIOE
#define BOARD_OV7620_PCLK_PIN (8)
#define BOARD_OV7620_VSYNC_PORT HW_GPIOE
#define BOARD_OV7620_VSYNC_PIN (10)
#define BOARD_OV7620_HREF_PORT HW_GPIOE
#define BOARD_OV7620_HREF_PIN (9)
/*
摄像头数据引脚PTA8-PTA15 只能填入 0 8 16三个值
0 :PTA0-PTA7
8 :PTA8-PTA15
16:PTA16-PTA24
*/
#define BOARD_OV7620_DATA_OFFSET (0)
static void UserApp(uint32_t vcount);
//定义一帧结束后的用户函数
/* 状态机定义 */
typedef enum
{
TRANSFER_IN_PROCESS, //数据在处理
NEXT_FRAME, //下一帧数据
}OV7620_Status;
int SCCB_Init(uint32_t I2C_MAP)
{
int r;
uint32_t instance;
instance = I2C_QuickInit(I2C_MAP, 50*1000);
r = ov7725_probe(instance);
if(r)
{
return 1;
}
r = ov7725_set_image_size((ov7725_size)IMAGE_SIZE);
if(r)
{
printf("OV7725 set image error\r\n");
return 1;
}
return 0;
}
//行中断和场中断都使用PTE中断
void OV_ISR(uint32_t index)
{
static uint8_t status = TRANSFER_IN_PROCESS;
static uint32_t h_counter, v_counter;
// uint32_t i;
/* 行中断 */
if(index & (1 << BOARD_OV7620_HREF_PIN))
{
DMA_SetDestAddress(HW_DMA_CH2, (uint32_t)gpHREF[h_counter++]);
//i = DMA_GetMajorLoopCount(HW_DMA_CH2);
DMA_SetMajorLoopCounter(HW_DMA_CH2, (OV7620_W/8)+1);
DMA_EnableRequest(HW_DMA_CH2);
return;
}
/* 场中断 */
if(index & (1 << BOARD_OV7620_VSYNC_PIN))
{
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, false);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, false);
switch(status)
{
case TRANSFER_IN_PROCESS: //接受到一帧数据调用用户处理
UserApp(v_counter++);
//printf("i:%d %d\r\n", h_counter, i);
status = NEXT_FRAME;
h_counter = 0;
break;
case NEXT_FRAME: //等待下次传输
status = TRANSFER_IN_PROCESS;
break;
default:
break;
}
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, true);
PORTE->ISFR = 0xFFFFFFFF; //这里可以改PORTE
h_counter = 0;
return;
}
}
void initCamera(){
uint32_t i;
printf("OV7725 test\r\n");
//检测摄像头
if(SCCB_Init(I2C0_SCL_PB00_SDA_PB01))
{
printf("no ov7725device found!\r\n");
while(1);
}
printf("OV7620 setup complete\r\n");
//每行数据指针
for(i=0; i<OV7620_H+1; i++)
{
gpHREF[i] = (uint8_t*)&gCCD_RAM[i*OV7620_W/8];
}
DMA_InitTypeDef DMA_InitStruct1 = {0};
/* 场中断 行中断 像素中断 */
GPIO_QuickInit(BOARD_OV7620_PCLK_PORT, BOARD_OV7620_PCLK_PIN, kGPIO_Mode_IPD);
GPIO_QuickInit(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_Mode_IPD);
GPIO_QuickInit(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_Mode_IPD);
/* install callback */
GPIO_CallbackInstall(BOARD_OV7620_VSYNC_PORT, OV_ISR);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_PCLK_PORT, BOARD_OV7620_PCLK_PIN, kGPIO_DMA_RisingEdge, true);
/* 初始化数据端口 */
for(i=0;i<8;i++)
{
GPIO_QuickInit(HW_GPIOE, BOARD_OV7620_DATA_OFFSET+i, kGPIO_Mode_IFT);
}
//DMA配置
DMA_InitStruct1.chl = HW_DMA_CH2;
DMA_InitStruct1.chlTriggerSource = PORTE_DMAREQ; //这里可以改PORTE
DMA_InitStruct1.triggerSourceMode = kDMA_TriggerSource_Normal;
DMA_InitStruct1.minorLoopByteCnt = 1;
DMA_InitStruct1.majorLoopCnt = ((OV7620_W/8) +1);
DMA_InitStruct1.sAddr = (uint32_t)&PTE->PDIR + BOARD_OV7620_DATA_OFFSET/8; //这里可以改PTE
DMA_InitStruct1.sLastAddrAdj = 0;
DMA_InitStruct1.sAddrOffset = 0;
DMA_InitStruct1.sDataWidth = kDMA_DataWidthBit_8;
DMA_InitStruct1.sMod = kDMA_ModuloDisable;
DMA_InitStruct1.dAddr = (uint32_t)gpHREF[0];
DMA_InitStruct1.dLastAddrAdj = 0;
DMA_InitStruct1.dAddrOffset = 1;
DMA_InitStruct1.dDataWidth = kDMA_DataWidthBit_8;
DMA_InitStruct1.dMod = kDMA_ModuloDisable;
/* initialize DMA moudle */
DMA_Init(&DMA_InitStruct1);
}
// IMG
uint8_t gIMG[OV7620_W][OV7620_H]; //使用内部RAM
void findLine(){
int y;
int x;
for(y=0;y<OV7620_H-1;y++){
for(x=0;x<OV7620_W-1;x++){
gIMG[x][y] ^= gIMG[x+1][y];
}
gIMG[x][y]=0;
}
}
#define DELTA_MAX 3
int average;
int sum;
#define LOWSPEED 1500
#define PSPEED 30
int centers[OV7620_H] = {0};
void findCenter(){
int center = OV7620_W/2;
int lastcenter = OV7620_W/2;
int left, right;
int y;
average = 0;
sum=0;
int error = 0;
for(y=OV7620_H-2;y>0;y--){
for(left = lastcenter;left>0;left--)
if(gIMG[left][y])break;
//寻找左边缘
for(right = lastcenter;right<OV7620_W;right++)
if(gIMG[right][y])break;
//寻找右边缘
center = (left+right)/2;
int delta = lastcenter - center; //当前斜率
if(y != OV7620_H-2){
if(delta>DELTA_MAX | delta<-DELTA_MAX){
// for(int x=0;x<OV7620_W;x++)
// gIMG[x][y]=1;
//画一条截止线
error ++;
if(error>3)
break;
}else {
average += center;
centers[y]=center;
sum ++;
}
}
lastcenter = center;
// for(int x=0;x<OV7620_W;x++)
// gIMG[x][y]=0;
gIMG[center][y]=1;
}
for(;y>0;y--)centers[y]=0;
average /= sum;
// if(sum < 10){
// setSpeed(0);
// turn(0);
// DelayMs(500);
// }else {
// setSpeed(LOWSPEED+(30-abs(average-44))*PSPEED);
// turn((average-44)*1);
// }
//printf("\r\n");
}
bool printflag = false;
/* 接收完成一场后 用户处理函数 */
static void UserApp(uint32_t vcount)
{
for(int y=0;y<OV7620_H-1;y++)
for(int x=0;x<OV7620_W/8;x++)
for(int i=0; i<8; i++)
gIMG[x*8+i][y] = (gpHREF[y][x+1]>>(7-i))%2;
//将图片从OV7620_H*OV7620_W/8映射到OV7620_H*OV7620_W
//findLine();
//findCenter();
if(printflag){
printflag = false;
//打印出图像
printf("start\r\n");
for(int y=0;y<OV7620_H-1;y++){
for(int x=0;x<OV7620_W;x++){
// if(gIMG[x][y]){
// printf("%c%c\r\n",x,y);
// }
if(gIMG[x][y]){
printf("1");
}else{
printf("0");
}
}
printf("\r\n");
}
//打印出中心线
// for(int y=0;y<OV7620_H-1;y++){
// if(centers[y]!=0){
// printf("%c%c\r\n",centers[y],y);
// //DelayMs(4);
// }
// }
}
//打印到屏幕上
for(int y=0;y<8;y++){
LED_WrCmd(0xb0 + y); //0xb0+0~7表示页0~7
LED_WrCmd(0x00); //0x00+0~16表示将128列分成16组其地址在某组中的第几列
LED_WrCmd(0x10); //0x10+0~16表示将128列分成16组其地址所在第几组
for(int x=0;x<80;x++){
uint8_t data = 0;
for(int i=0;i<8 && y*8+i<OV7620_H ;i++)
data += gIMG[x][y*8+i]<<(i);
LED_WrDat(data);
}
}
char buf[20] = {0};
sprintf(buf, "a=%d ", average);
LED_P8x16Str(80, 0, buf);
}
//串口接收中断
void UART_RX_ISR(uint16_t byteRec){
//打印整个图像
if(byteRec == ' ')printflag = true;
}
int main(void)
{
DelayInit();
/* 打印串口及小灯 */
GPIO_QuickInit(HW_GPIOD, 10, kGPIO_Mode_OPP);
UART_QuickInit(UART0_RX_PB16_TX_PB17, 115200);
/* 注册中断回调函数 */
UART_CallbackRxInstall(HW_UART0, UART_RX_ISR);
/* 开启UART Rx中断 */
UART_ITDMAConfig(HW_UART0, kUART_IT_Rx, true);
initOLED();
initDriver();
initCamera();
while(1)
{
PDout(10) = !PDout(10);
DelayMs(500);
}
}