標(biāo)題:
注釋很詳細(xì)的單片機(jī)模塊化智能循跡小車代碼
[打印本頁]
作者:
小啊誠
時(shí)間:
2021-10-7 15:57
標(biāo)題:
注釋很詳細(xì)的單片機(jī)模塊化智能循跡小車代碼
/****************************************************************************
*File name: main.c
*Description:在第一個(gè)十字路口打開閘道
timer0:
timer1:
timer2: uart2/4
timer3: uart3
timer4: 1ms 時(shí)鐘滴答
****************************************************************************/
/*引入頭文件*/
#include <STC8.H>
#include "car_control.h"
#include "pwm_pca.h"
#include "delay.h"
#include "ir.h"
#include "hc-sr04.h"
#include "uart.h"
#include "timer.h"
#include "disp.h"
#include "user_string.h"
#include "intrins.h"
#include "color.h"
#define KEY4 P54
signed char temperature = 0; // 溫度
signed char humidity = 0; // 濕度
signed long encode_count_chA = 0; // 通道A編碼器計(jì)數(shù)值
signed long encode_count_chB = 0; // 通道B編碼器計(jì)數(shù)值
unsigned char at_com_type = 0; // AT指令讀數(shù)據(jù)狀態(tài)
extern unsigned short R_data,G_data,B_data;
/****************************************************************************
* 名 稱:main
* 功 能:程序入口
* 入口參數(shù):無
* 出口參數(shù):無
* 說 明:無
* 調(diào)用方法:無
****************************************************************************/
void main()
{
unsigned char t_10s = 10;
unsigned int dat=0;
unsigned char pre_tick = 0;
xdata unsigned char u4_txBuf[20];
P1M0 = 0xC0;
P1M1 = 0x0;
// P54口設(shè)為輸入口
P5M0 &= ~0x10;
P5M1 |= 0x10;
uart4_init();
uart3_init();
uart2_init();
EA = 1; /* 開總中斷 */
car_init(); /* 小車初始化 */
HC_SR04_Init(); /*超聲波初始化*/
delay_ms(100);
MOTOR_GO_EN;
delay_ms(10);
// disp_number(temperature);
disp_number(distance); // 數(shù)碼管顯示距離
uart4_tx_idle = 10;
uart4_send_array("AT+EON\r\n", sizeof("AT+EON\r\n")); // 控制指令不返回OK
delay_ms(10);
uart4_tx_idle = 10;
Timer4Init();
tick = 0;
//Color_Init();//開啟顏色檢測
// 使能串口2/3接收狀態(tài)
uart2_restart_rx();
uart3_restart_rx();
while(1)
{
if (pre_tick != tick)
{ // 每1ms刷新一次數(shù)碼管
pre_tick = tick;
disp_isr_call();
if (carWorking.delay != 0) // 特殊任務(wù)計(jì)時(shí)
carWorking.delay--;
if (trafficLight_opevmv.tick != 0) // 攝像頭計(jì)時(shí)
trafficLight_opevmv.tick--;
else
trafficLight_opevmv.light = 0;
}
if (tick >= 100)
{
tick = 0;
/*//disp_number(G_data);
u4_txBuf[0] = 'E';
u4_txBuf[1] = 'S';
u4_txBuf[2] = 'P';
u4_txBuf[3] = 1;
u4_txBuf[4] = 9;
u4_txBuf[5] = 6;
u4_txBuf[6] = R_data >> 8;
u4_txBuf[7] = R_data;
u4_txBuf[8] = G_data >> 8;
u4_txBuf[9] = G_data;
u4_txBuf[10] = B_data >> 8;
u4_txBuf[11] = B_data;
u4_txBuf[12] = 0xF8 + u4_txBuf[6] + u4_txBuf[7] + u4_txBuf[8] + u4_txBuf[9] + u4_txBuf[10] + u4_txBuf[11];
u4_txBuf[13] = 'E';
u4_txBuf[14] = 'N';
u4_txBuf[15] = 'D';
uart4_send_array(u4_txBuf, 16);*/
/*if (R_data >= G_data){
disp_number(R_data - G_data);
}
else{
disp_number(G_data - R_data);
disp_number_1(10);
}*/
dat = HC_SR04_Read();//讀取超聲波距離 每100ms測量一次
if(dat != -1)//距離采集成功
{
distance = dat;
disp_number(distance);
}
t_10s--;
if (t_10s == 0)
{
t_10s = 100;
uart4_send_array("AT+TEMPERATURE?\r\n", 17);
// 重新開始接收數(shù)據(jù)
uart4_restart_rx();
at_com_type = 1; // AT指令讀數(shù)據(jù)類型 1溫度 2濕度 3編碼器計(jì)數(shù)值 4速度 5電池電量
}
}
if (uart4_rx_idle == 3)
{ // 驅(qū)動(dòng)板接收數(shù)據(jù)解析
unsigned char rx_len = 100 - uart4_buf.r_free;
if (rx_len > 6)
{
// if (uart4_rx_buf[0] == '+')
{
unsigned char st;
switch (at_com_type)
{
case 1: // 溫度
{
st = user_array_seek(uart4_rx_buf, rx_len, "+TEMPERATURE:", 13);
if (st != 0xFF)
{
signed long buf;
if (user_str_DECnumStr2num(uart4_rx_buf + st + 15, &buf) != 0)
{
temperature = buf;
// disp_number(temperature);
}
}
break;
}
case 2: // 濕度
{
st = user_array_seek(uart4_rx_buf, rx_len, "+HUMIDITY:", 10);
if (st != 0xFF)
{
signed long buf;
if (user_str_DECnumStr2num(uart4_rx_buf + st + 12, &buf) != 0)
{
humidity = buf;
//disp_number(humidity);
}
}
break;
}
case 3: // 編碼器
{
st = user_array_seek(uart4_rx_buf, rx_len, "+MTENCODER:", 11);
if (st != 0xFF)
{
signed long buf;
register unsigned char len = user_str_DECnumStr2num(uart4_rx_buf + st + 13, &buf);
if (len != 0)
{ // <channel A>
encode_count_chA = buf;
st = len + st + 1;
len = user_str_DECnumStr2num(uart4_rx_buf + st + 13, &buf);
if (len != 0)
{ // <channel B>
encode_count_chB = buf;
}
}
}
break;
}
case 4: // 速度
{ // 需用戶自行完善
st = user_array_seek(uart4_rx_buf, rx_len, "+MTRPM:", 6);
if (st != 0xFF)
{ // <channel A>,<channel B> 轉(zhuǎn)/分鐘
signed long buf;
register unsigned char len = user_str_DECnumStr2num(uart4_rx_buf + st + 8, &buf);
if (len != 0)
{ // <channel A>
}
}
break;
}
case 5: // 電池電量
{ // 需用戶自行完善
st = user_array_seek(uart4_rx_buf, rx_len, "+BATTERY:", 8);
if (st != 0xFF)
{ // <電壓>,<電流>,<剩余電量>,<節(jié)數(shù)>,<已消耗電量>
signed long buf;
register unsigned char len = user_str_DECnumStr2num(uart4_rx_buf + st + 10, &buf);
if (len != 0)
{ // <電壓>
}
}
break;
}
case 6: // 報(bào)警狀態(tài)
{ // 需用戶自行編寫
break;
}
}
at_com_type = 0;
}
}
if (rx_len != 0)
{ // 重新開始接收數(shù)據(jù)
uart4_restart_rx();
}
}
else if (uart4_rx_idle >= 50)
{
uart4_rx_idle = 5;
if (at_com_type != 0)
{
at_com_type = 0;
}
}
if((distance<300) && //距離小于300mm 如果有路障重新規(guī)劃路線,則建議將此部分屏蔽掉
(carWorking.type == 0)) // 非特殊任務(wù)時(shí),才執(zhí)行
{
MOTOR_GO_STOP;//停車
}
else
{
car_FollowLine();//循跡
}
if (uart3_rx_idle == 5)
{ // lora通信測試
unsigned char len = 100 - uart3_buf.r_free;
if ((len >= 6) && (uart3_rx_buf[4] + 6 <= len))
{ // 紅綠燈命令解析,用戶可根據(jù)實(shí)際情況進(jìn)行修改
if (uart3_rx_buf[5] == 'r')
disp_number_1(1);
else if (uart3_rx_buf[5] == 'g')
disp_number_1(0);
else if (uart3_rx_buf[5] == 'y')
disp_number_1(2);
disp_number_2(0);
disp_number_3(uart3_rx_buf[7] / 10);
disp_number_4(uart3_rx_buf[7] % 10);
uart3_restart_rx();
}
}
if (uart2_rx_idle == 5)
{ // 攝像頭通信測試
unsigned char len = 100 - uart2_buf.r_free;
if (len > 17)
{ // 攝像頭檢測到紅綠燈時(shí),發(fā)送以下字符串
// "traffic light is red"
// "traffic light is green"
// "traffic light is yellow"
if (user_str_cmp(uart2_rx_buf, "traffic light is ") >= 17)
{ //
if (uart2_rx_buf[17] == 'r')
{ // "red"
trafficLight_opevmv.light = 'r';
//disp_number_1(1);
}
else if (uart2_rx_buf[17] == 'g')
{ // "green"
trafficLight_opevmv.light = 'g';
//disp_number_1(3);
}
else if (uart2_rx_buf[17] == 'y')
{ // "yellow"
trafficLight_opevmv.light = 'y';
//disp_number_1(2);
}
trafficLight_opevmv.tick = 250;
}
uart2_restart_rx();
}
else
uart2_restart_rx();
}
}
}
復(fù)制代碼
Keil代碼工程:
小車綜合任務(wù)-源碼.rar
(157.88 KB, 下載次數(shù): 16)
2021-10-7 15:56 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1