|
希望大家一起學(xué)習(xí)學(xué)習(xí)
電路原理圖如下:
0.jpg (65.15 KB, 下載次數(shù): 51)
下載附件
2018-8-29 03:25 上傳
單片機(jī)源程序如下:
- #include <stdio.h>
- #include <intrins.h>
- #include "STC12C5202AD.H"
- #include "sio.h"
- typedef struct PID
- {
- int p; // 比例常數(shù) Proportional Const
- int i; // 積分常數(shù) Integral Const
- int d; // 微分常數(shù) Derivative Const
- int position;
- int hisPosition;
- int lastPosition[3];
- } PID;
- static PID idata pid;
- static int GAIN;
- #define MIN9MS (XTAL/2L/256L*9L/1000L*9L/10L) //9ms脈寬*90%
- #define MAX9MS (XTAL/2L/256L*9L/1000L*11L/10L) //9ms脈寬*110%
- #define MIN45MS (XTAL/2L/256L*45L/10000L*9L/10L) //4.5ms脈寬
- #define MAX45MS (XTAL/2L/256L*45L/10000L*11L/10L)
- #define MIN225MS (XTAL/2L/256L*225L/100000L*9L/10L) //2.25ms脈寬
- #define MAX225MS (XTAL/2L/256L*225L/100000L*11L/10L)
- #define MIN056MS (XTAL/2L/256L*56L/100000L*6L/10L) //0.56ms脈寬*60%,下限
- #define MAX056MS (XTAL/2L/256L*56L/100000L*14L/10L)
- #define MIN168MS (XTAL/2L/256L*168L/100000L*6L/10L) //1.68ms脈寬*60%
- #define MAX168MS (XTAL/2L/256L*168L/100000L*14L/10L)
- sfr ISP_CUNTR = 0xE7;
- sbit LED1 = P3^0;
- sbit LED2 = P3^1;
- sbit AD_LED1 = P2^0;
- sbit AD_LED2 = P2^1;
- sbit AD_LED3 = P2^7;
- sbit IR_FRONT = P3^2;
- sbit IR_LEFT = P3^3;
- sbit IR_RIGHT = P2^6;
- sbit IR_BACK = P3^7;
- sbit IR_OUT = P3^5;
- sbit MOTO_IN_B1 = P2^5;
- sbit MOTO_IN_B2 = P2^4;
- sbit MOTO_IN_A1 = P2^3;
- sbit MOTO_IN_A2 = P2^2;
- static unsigned char idata ad_datas[8]; //8路光電管采樣電壓
- static unsigned char idata ad_datas_check[8]; //8路光電管采樣電壓校驗(yàn)值,軌道的白色背景的采樣值
- bit power_stat;
- static unsigned char car_stat; //小車狀態(tài):0,停止;1,前進(jìn);2,后退;3,左轉(zhuǎn);4,右轉(zhuǎn);5,尋線模式;ff,自控避障模式
- static unsigned int now;
- static unsigned char code led_mod_table[3][20] = {
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},
- {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0}
- };
- unsigned char idata led_mod = 0;
- static unsigned char idata led_tick = 0;
- static unsigned char idata led_ptr = 0;
- static bit test;
- static unsigned char tick = 0;
- static unsigned int pwm38k = 0;
- static unsigned char check;
- static unsigned char pwm_moto = 0;
- static unsigned char pwm_moto_left = 0;
- static unsigned char pwm_moto_right = 0;
- static bit moto_left_forward = 1;
- static bit moto_right_forward = 1;
- #define IR_SINGAL_DELAY 11 //接收管輸出延遲載波數(shù)量
- #define TEST_PERIOD 1620 //評估周期,這個(gè)不同的接收管差別很大.
- #define IR_SINGAL_PERIOD 76 //持續(xù)發(fā)射紅外線載波數(shù)量
- #define IR_VALID_THROLD 70 //判斷是否前方有障礙的閥值
- /*
- #define IR_SINGAL_DELAY 1 //接收管輸出延遲載波數(shù)量
- #define TEST_PERIOD 200 //評估周期,這個(gè)不同的接收管差別很大.
- #define IR_SINGAL_PERIOD 10 //持續(xù)發(fā)射紅外線載波數(shù)量
- #define IR_VALID_THROLD 8 //判斷是否前方有障礙的閥值
- */
- static unsigned char idata front_signal = 0;
- static unsigned char idata back_signal = 0;
- static unsigned char idata left_signal = 0;
- static unsigned char idata right_signal = 0;
- static bit front_obj = 0, back_obj = 0, left_obj = 0, right_obj = 0;
- /*
- * PCA中斷計(jì)數(shù),根據(jù)位置判斷信號(hào)區(qū)域和定義,位置0表示初始,1代表引導(dǎo)碼信號(hào),2表示引導(dǎo)碼間隔,
- * 3表示第一個(gè)bit的信號(hào),4表示第一個(gè)bit的間隔,以次類推...
- * 更具體見對應(yīng)的紅外線協(xié)議.
- */
- static unsigned int idata pca_tick;
- static unsigned char idata pca_int_count;
- static unsigned char data pca_int_total; /* 根據(jù)引導(dǎo)頭確定總長度 */
- static unsigned int idata period; /* 紅外信號(hào)占或空周期計(jì)數(shù) */
- static unsigned char idata data_buf[6]; /* 紅外線協(xié)議數(shù)據(jù)緩沖區(qū) */
- static unsigned int idata ccap1; //PCA0上一次的的計(jì)數(shù)
- static unsigned char idata frame_dog; //紅外幀看門狗,限定時(shí)間未接收完成清除工作
- static void delay_ms(unsigned int v) {
- unsigned int wait = pca_tick + v / 7 + 1;
- while (wait != pca_tick) {
- PCON |= 0x01;
- }
- }
- static void delay_100us(unsigned char v) {
- unsigned char wait = tick + v * 2; //每tick約50us
- while (wait != tick) {
- ;//PCON |= 0x01;
- }
- }
- void pid_init(PID *pid)
- {
- pid->position = 0;
- pid->hisPosition = 0;
- pid->lastPosition[0] = 0;
- pid->lastPosition[1] = 0;
- pid->lastPosition[2] = 0;
- pid->p = 2;
- pid->i = 0;
- pid->d = 8;
- }
- void read_sensors(unsigned char sensors[]) { //讀光電傳感器的采樣值
- unsigned char i;
- for (i = 0; i < 8; i++) { //LED熄滅狀態(tài)測量一次電壓
- ADC_CONTR = 0xE0 | i; //設(shè)置AD通道, 快速AD采樣.
- delay_100us(5); //延遲等待電壓穩(wěn)定
- ADC_RES = 0;
- ADC_CONTR = 0xE8 | i;; //啟動(dòng)AD
- while (!(ADC_CONTR & 0x10)) {
- }
- ADC_CONTR &= 0xE7;
- sensors[i] = ADC_RES;
- }
- AD_LED1 = 0;
- AD_LED2 = 0;
- AD_LED3 = 0;
- for (i = 0; i < 8; i++) { //LED打開再測量一次電壓,用于消除環(huán)境光的差異.
- ADC_CONTR = 0xE0 | i; //設(shè)置AD通道, 快速AD采樣.
- delay_100us(5); //延遲等待電壓穩(wěn)定
- ADC_RES = 0;
- ADC_CONTR = 0xE8 | i;; //啟動(dòng)AD
- while (!(ADC_CONTR & 0x10)) {
- }
- ADC_CONTR &= 0xE7;
- if (ADC_RES > sensors[i])
- sensors[i] = ADC_RES - sensors[i];
- else
- sensors[i] = ADC_RES;
- }
- AD_LED1 = 1;
- AD_LED2 = 1;
- AD_LED3 = 1;
- }
- static void read_sensors_check() { //從flash中讀取光電傳感器的校驗(yàn)值,結(jié)構(gòu)是8byte的值+1字節(jié)的crc
- unsigned char i, crc = 0;
- IAP_ADDRH = 0;
- IAP_CONTR = 0x80; //允許ISP/IAP操作
- IAP_CMD = 0x01; //讀flash
- for (i = 0; i < 8; i++) {
- IAP_ADDRL = i;
- IAP_TRIG = 0x5A;
- IAP_TRIG = 0xA5; //啟動(dòng)IAP操作
- _nop_();
- ad_datas_check[i] = IAP_DATA;
- crc += IAP_DATA;
- //com_putchar(IAP_DATA);
- }
- IAP_ADDRL = 8;
- IAP_TRIG = 0x5A;
- IAP_TRIG = 0xA5; //啟動(dòng)IAP操作
- _nop_();
- if (crc != IAP_DATA) { //crc驗(yàn)證錯(cuò)誤,flash數(shù)據(jù)失效
- for (i = 0; i < 8; i++) {
- ad_datas_check[i] = 0xff;
- }
- }
- IAP_CONTR = 0; //禁止IAP,防止誤操作
- IAP_CMD = 0;
- IAP_TRIG = 0;
- IAP_ADDRH = 0xff;
- IAP_ADDRL = 0xff;
- }
- static void write_sensors_check() { //將光電傳感器的校驗(yàn)值寫入flash,結(jié)構(gòu)是8byte的值+1字節(jié)的crc
- unsigned char i, crc = 0;
- IAP_ADDRH = 0;
- IAP_CONTR = 0x80; //允許ISP/IAP操作
- read_sensors(ad_datas_check);
- IAP_ADDRL = 0;
- IAP_CMD = 0x03; //擦除flash
- IAP_TRIG = 0x5A;
- IAP_TRIG = 0xA5; //啟動(dòng)IAP操作
- _nop_();
- IAP_CMD = 0x10; //寫flash
- for (i = 0; i < 8; i++) {
- IAP_ADDRL = i;
- IAP_DATA = ad_datas_check[i];
- //com_putchar(IAP_DATA);
- crc += ad_datas_check[i];
- IAP_CMD = 0x02; //寫flash
- IAP_TRIG = 0x5A;
- IAP_TRIG = 0xA5; //啟動(dòng)IAP操作
- _nop_();
- }
- IAP_ADDRL = 8;
- IAP_DATA = crc;
- IAP_TRIG = 0x5A;
- IAP_TRIG = 0xA5; //啟動(dòng)IAP操作
- _nop_();
- IAP_CONTR = 0; //禁止IAP,防止誤操作
- IAP_CMD = 0;
- IAP_TRIG = 0;
- IAP_ADDRH = 0xff;
- IAP_ADDRL = 0xff;
- }
- void time0_isr() interrupt 1
- {
- tick++;
- pwm38k++;
- if (pwm38k == 0)
- {
- front_signal = 0;
- back_signal = 0;
- left_signal = 0;
- right_signal = 0;
- } else if (pwm38k == TEST_PERIOD)
- {
- //com_putchar(check);
- if (front_signal >= IR_VALID_THROLD)
- {
- front_obj = 1;
- } else
- front_obj = 0;
- if (back_signal >= IR_VALID_THROLD)
- {
- back_obj = 1;
- } else
- back_obj = 0;
- if (left_signal >= IR_VALID_THROLD)
- {
- left_obj = 1;
- } else
- left_obj = 0;
- if (right_signal >= IR_VALID_THROLD)
- {
- right_obj = 1;
- } else
- right_obj = 0;
- pwm38k = 0xFFFF;
- return;
- }
- if (pwm38k == 0)
- {
- IR_OUT = 1;
- test = 0;
- } else if (pwm38k == IR_SINGAL_PERIOD)
- {
- IR_OUT = 0;
- }
- // if (!test && !IR_FRONT) //調(diào)試接收管的延遲
- // {
- // test = 1;
- // com_putchar(tick);
- // }
- if (pwm38k >= IR_SINGAL_DELAY && pwm38k < IR_SINGAL_DELAY + IR_SINGAL_PERIOD)
- {
- if (!IR_FRONT)
- front_signal++;
- else
- {
- if (front_signal)
- front_signal--;
- }
- if (!IR_BACK)
- back_signal++;
- else
- {
- if (back_signal)
- back_signal--;
- }
- if (!IR_LEFT)
- left_signal++;
- else
- {
- if (left_signal)
- left_signal--;
- }
- if (!IR_RIGHT)
- right_signal++;
- else
- {
- if (right_signal)
- right_signal--;
- }
- }
- if (pwm_moto == 0)
- {
- if (pwm_moto_left > 0)
- {
- if (moto_left_forward)
- MOTO_IN_A2 = 1;
- else
- MOTO_IN_A1 = 1;
- } else
- {
- MOTO_IN_A1 = 0;
- MOTO_IN_A2 = 0;
- }
- if (pwm_moto_right > 0)
- {
- if (moto_right_forward)
- MOTO_IN_B2 = 1;
- else
- MOTO_IN_B1 = 1;
- } else
- {
- MOTO_IN_B1 = 0;
- MOTO_IN_B2 = 0;
- }
- } else
- {
- if (pwm_moto == pwm_moto_left)
- {
- MOTO_IN_A1 = 0;
- MOTO_IN_A2 = 0;
- }
- if (pwm_moto == pwm_moto_right)
- {
- MOTO_IN_B1 = 0;
- MOTO_IN_B2 = 0;
- }
- }
- pwm_moto++;
- }
- void time0_initialize(void)
- {
- TMOD &= ~0x0F; /* clear timer 0 mode bits */
- TMOD |= 0x02; /* put timer 0 into MODE 2 */
- AUXR |= 0x80; // timer0工作在1T模式
- TH0 = 256 - XTAL / 2L / 38400L; // 256 - XTAL/T1_12/f, f=輸出時(shí)鐘頻率
- TL0 = 0x0;
- WAKE_CLKO = 0x01; // T0在P3.4上輸出時(shí)鐘.
- PT0 = 1; /* 時(shí)鐘0中斷高優(yōu)先級 */
- TR0 = 1; //
- ET0 = 1;
- }
- static void wakeup (void) interrupt 2
- {
- }
- static void pca_isr (void) interrupt 6
- {
- unsigned char i, j;
- if (CCF0) {
- CCF0 = 0; //清PCA1中斷標(biāo)志
- LED1 = IR_BACK;
- if (!pca_int_count) { //第一次收到信號(hào)
- if (!IR_BACK) {
- ccap1 = pca_tick * 256 + CCAP0H;
- pca_int_count++;
- }
- } else { //已經(jīng)收到一些信號(hào)
- period = pca_tick * 256 + CCAP0H - ccap1;
- ccap1 = pca_tick * 256 + CCAP0H;
- //com_putchar(period / 256);
- //com_putchar(period % 256);
- if (pca_int_count == 1) {
- if (period < MIN9MS || period > MAX9MS) { //9ms
- pca_int_count = 0;
- frame_dog = 0;
- } else
- pca_int_count++;
- } else if (pca_int_count == 2) {
- if (period > MIN225MS && period < MAX225MS) { //2.25ms
- pca_int_total = 3;
- pca_int_count++;
- } else if (period > MIN45MS && period < MAX45MS) { //4.5ms
- pca_int_total = 67;
- pca_int_count++;
- } else {
- pca_int_count = 0;
- frame_dog = 0;
- }
- } else {
- if (IR_BACK) {
- if (period > MIN056MS && period < MAX056MS) { //0.56ms
- if (pca_int_count >= pca_int_total) { //幀接收完畢,下面進(jìn)行有效性分析.
- if (pca_int_total == 67) { //完整信號(hào),含有引導(dǎo)信號(hào),設(shè)備碼8bit,設(shè)備反碼8bit,命令字8bit,命令字反碼8bit
- if ((data_buf[0] ^ data_buf[1] == 0xff) && (data_buf[2] ^ data_buf[3] == 0xff)) {
- com_putchar(data_buf[0]);
- com_putchar(data_buf[2]);
- if (data_buf[0] == 0x40) {
- switch (data_buf[2]) {
- case 0x5F: //左
- car_stat = 3;
- break;
- case 0x5B: //右
- car_stat = 4;
- break;
- case 0x5A: //上
- car_stat = 1;
- break;
- case 0x5E: //下
- car_stat = 2;
- break;
- case 0x56: //菜單
- car_stat = 0;
- break;
- case 0x0: //數(shù)字0
- car_stat = 0xff;
- now = pca_tick;
- break;
- case 0x1: //數(shù)字1
- car_stat = 5;
- pid_init(&pid);
- GAIN = 150;
- now = pca_tick;
- break;
- case 0x3: //數(shù)字3
- write_sensors_check(); //校驗(yàn)光電傳感器
- break;
- case 0x1A: //+號(hào)
- pid.p++;
- break;
- case 0x1E: //-號(hào)
- if (pid.p)
- pid.p--;
- break;
- case 0x10: //靜音鍵
- GAIN++;
- break;
- case 0x1C: //屏顯
- if (GAIN)
- GAIN--;
- break;
- case 0x1B: //^
- pid.d++;
- break;
- case 0x1F: //向下
- if (pid.d)
- pid.d--;
- break;
- case 0x12: //POWER
- // power_stat = ~power_stat;
- break;
- default:
- break;
- }
- }
- }
- } else { //重復(fù)信號(hào),僅含有引導(dǎo)信號(hào)
- }
- pca_int_count = 0;
- frame_dog = 0;
- } else {
- pca_int_count++;
- }
- } else {
- pca_int_count = 0;
- frame_dog = 0;
- }
- } else {
- j = (pca_int_count - 3) / 2;
- i = j / 8;
- j = j % 8;
- if (period > MIN168MS && period < MAX168MS) { //1.68ms
- // com_putchar(0x01);
- data_buf[i] |= (0x01 << j);
- pca_int_count++;
- } else if (period > MIN056MS && period < MAX056MS) { //0.56ms
- // com_putchar(0x00);
- data_buf[i] &= ~(0x01 << j);
- pca_int_count++;
- } else {
- pca_int_count = 0;
- frame_dog = 0;
- }
- }
- }
- }
- }
- if (CF) { //PCA計(jì)數(shù)溢出中斷,19.6608MHZ晶體大約6.7ms溢出
- CF = 0;
- pca_tick++;
- if (led_tick++ >= 10) {
- led_tick = 0;
- if (led_mod_table[led_mod][led_ptr++]) {
- LED1 = 0;
- LED2 = 0;
- } else {
- if (left_obj || right_obj || front_obj || back_obj) {
- LED1 = 0;
- LED2 = 0;
- } else {
- LED1 = 1;
- LED2 = 1;
- }
- }
- led_ptr %= 20;
- }
- if (pca_int_count) {
- frame_dog++;
- if (frame_dog >= 15) { //100ms后重新開始分析新的紅外線數(shù)據(jù)包
- pca_int_count = 0;
- frame_dog = 0;
- }
- }
- }
- }
- int read_black_line() { //讀黑線位置.
- unsigned char i;
- int grayscale_min; //最黑的線
- char line_begin, line_end; //黑線對應(yīng)的傳感器的開始和結(jié)束位置.
- int tmp;
- read_sensors(ad_datas);
- /*
- line_begin = -1; line_end= -1;
- for (i = 0; i < 8; i++) {
- //com_putchar(ad_datas[i]);
- //com_putchar(ad_datas_check[i]);
- tmp = (ad_datas[i] * 100);
- tmp = tmp / ad_datas_check[i];
- ad_datas[i] = tmp;
- if (ad_datas[i] <= 40) //灰度<=40%認(rèn)為下面有部分黑線
- {
- if (line_begin == -1)
- line_begin = i;
- else
- line_end = i;
- }
- //com_putchar(ad_datas[i]);
- }
- if (line_begin == -1)
- return -1;
- if (line_end == -1)
- return line_begin * 10;
- return 10*line_begin + (line_end - line_begin) * 10 * ad_datas[line_begin] / (ad_datas[line_begin] + ad_datas[line_end]);
- */
- line_begin = 0;
- // ad_datas[0] = ad_datas[0] * 100 / ad_datas_check[0];
- grayscale_min = 500;
- for (i = 1; i < 8; i++) {
- //com_putchar(ad_datas[i]);
- //com_putchar(ad_datas_check[i]);
- tmp = (ad_datas[i] * 100);
- tmp = tmp / ad_datas_check[i];
- ad_datas[i] = tmp;
- if (ad_datas[i] <= grayscale_min)
- {
- grayscale_min = ad_datas[i];
- line_begin = i;
- }
- //com_putchar(ad_datas[i]);
- }
- if (grayscale_min < 50) //灰度<50%認(rèn)為是有效黑線
- {
- if (line_begin == 0)
- line_end = 1;
- else if (line_begin == 7)
- {
- line_begin = 6;
- line_end = 7;
- } else if (ad_datas[line_begin - 1] < ad_datas[line_begin + 1])
- {
- line_end = line_begin;
- line_begin = line_begin - 1;
- } else
- line_end = line_begin + 1;
- if (ad_datas[line_begin] == 0)
- ad_datas[line_begin] = 1;
- if (ad_datas[line_end] == 0)
- ad_datas[line_end] = 1;
- return 10*line_begin + (line_end - line_begin) * 10 * ad_datas[line_begin] / (ad_datas[line_begin] + ad_datas[line_end]);
- } else
- return -1;
- }
- void track_line(PID *pid) { //未使用PID算法的版本,四驅(qū)車用PID算法效果差些,需要改進(jìn),三輪車用PID效果還可以.
- int line_pos;
- line_pos = read_black_line();
- if (line_pos == -1)
- {
- return;
- }
- com_putchar(line_pos);
- if (line_pos > 45)
- {
- pwm_moto_left = 255;
- pwm_moto_right = 255;
- moto_left_forward = 0;
- moto_right_forward = 1;
- } else if (line_pos < 25) {
- pwm_moto_left = 255;
- pwm_moto_right = 255;
- moto_left_forward = 1;
- moto_right_forward = 0;
- } else
- {
- pwm_moto_left = GAIN;
- pwm_moto_right = GAIN;
- moto_left_forward = 1;
- moto_right_forward = 1;
- }
- }
- /*
- void track_line(PID *pid) { //使用PID算法的版本,三輪小車效果比較不錯(cuò).
- int p; //P增益
- int i; //I增益
- int d; //D增益
- int gain; //控制量
- pid->position = read_black_line();
- if (pid->position == -1)
- {
- // pwm_moto_left = 0;
- // pwm_moto_right = 0;
- // pid_init(pid);
- return;
- }
- com_putchar(pid->position);
- pid->position -= 30;
- pid->hisPosition += pid->position; //記錄偏差之和
- pid->lastPosition[2] = pid->lastPosition[1]; //
- pid->lastPosition[1] = pid->lastPosition[0]; //
- pid->lastPosition[0] = pid->position; //
- p = (pid->p) * (pid->position); //計(jì)算比例分量(P)=比例系數(shù)*本次位置差
- i = (pid->i) * (pid->hisPosition); //計(jì)算積分分量(I)=積分系數(shù)*偏差之和
- d = (pid->d) * ((pid->position) - (pid->lastPosition[2])); //計(jì)算微分分量(D)=微分系數(shù)*(本次位置差-前3次的位置差)
- // 由于采樣比較快,用本次位置-前3次位置才有足夠大的控制
- gain = p + i + d; //P分量和D分量相加,得到控制量
- // if (gain > GAIN)
- // gain = GAIN;
- // if (gain < -GAIN)
- // gain = -GAIN;
- if (gain > 30)
- {
- pwm_moto_left = GAIN + gain;
- pwm_moto_right = GAIN + gain;
- moto_left_forward = 0;
- moto_right_forward = 1;
- } else if (gain < -30) {
- pwm_moto_left = GAIN - gain;
- pwm_moto_right = GAIN - gain;
- moto_left_forward = 1;
- moto_right_forward = 0;
- } else
- {
- pwm_moto_left = GAIN - gain;
- pwm_moto_right = GAIN -gain;
- moto_left_forward = 1;
- moto_right_forward = 1;
- }
- }
- */
- void main (void)
- {
- unsigned int i;
- P2M0 = 0x3C; //P2.2~P2.5 強(qiáng)推挽輸出
- P3M0 = 0x30; //P3.4,P3.5強(qiáng)輸出.
- P1ASF = 0xff; //P1.7~P1.0用做AD
- i = 0;
- MOTO_IN_A1 = 0;
- MOTO_IN_A2 = 0;
- MOTO_IN_B1 = 0;
- MOTO_IN_B2 = 0;
-
- EA = 0;
- power_stat = 0;
- time0_initialize();
- // com_initialize (); /* initialize interrupt driven serial I/O */
- // com_baudrate (14400); /* setup for 14400 baud */
- /*
- CMOD = 0x01; // #00000001B,PCA空閑計(jì)數(shù),PCA計(jì)數(shù)源=Fosc/12,PCA溢出中斷(做一個(gè)定時(shí)器使用)
- CCON = 0x00; //PCA中斷標(biāo)志清0,PCA停止計(jì)數(shù)
- CL = 0x0;
- CH = 0x0;
- CCAPM1 = 0x31; //PCA1上升下降沿捕獲
- */
- CMOD = 0x03; // #00000011B,PCA空閑計(jì)數(shù),PCA計(jì)數(shù)源=fosc/2,PCA溢出中斷
- CCON = 0x00; //PCA中斷標(biāo)志清0,PCA停止計(jì)數(shù)
- CL = 0x0;
- CH = 0x0;
- CCAPM0 = 0x31; //PCA0上升下降沿捕獲
- /*
- CCAPM0 = 0x42; //PCA0工作模式:8位pwm
- PCA_PWM0 = 0x00;
- CCAP0L = 0x40; //25%占空比,可調(diào)節(jié)占空比來調(diào)節(jié)發(fā)射功率.低電平驅(qū)動(dòng).
- CCAP0H = 0x40;
- */
- // EPCA_LVD = 1; //允許PCA和低壓檢測中斷
- ELVD = 1;
- car_stat = 0x00;
- pca_tick = 0;
- pca_int_count = 0;
- frame_dog = 0;
- EA = 1; /* Enable Interrupts */
- CR = 1; //啟動(dòng)PCA計(jì)數(shù)
- now = pca_tick;
- delay_ms(200);
- read_sensors_check();
- while (1)
- {
- if (power_stat) {
- // auto_power_down(); //自動(dòng)關(guān)機(jī)
- }
- switch (car_stat) {
- case 0:
- /*
- MOTO_IN_A1 = 0;
- MOTO_IN_A2 = 0;
- MOTO_IN_B1 = 0;
- MOTO_IN_B2 = 0;
- */
- pwm_moto_left = 0;
- pwm_moto_right = 0;
- break;
- case 1:
- if (!front_obj) {
- /*
- MOTO_IN_A1 = 0;
- MOTO_IN_A2 = 1;
- MOTO_IN_B1 = 0;
- MOTO_IN_B2 = 1;
- */
- moto_left_forward = 1;
- moto_right_forward = 1;
- pwm_moto_left = 255;
- pwm_moto_right = 255;
- } else {
- /*
- MOTO_IN_A1 = 1;
- MOTO_IN_A2 = 0;
- MOTO_IN_B1 = 1;
- MOTO_IN_B2 = 0;
- delay(1000);
- */
- car_stat = 0;
- }
- break;
- case 2:
- if (!back_obj) {
- moto_left_forward = 0;
- moto_right_forward = 0;
- pwm_moto_left = 255;
- pwm_moto_right = 255;
- /*
- MOTO_IN_A1 = 1;
- MOTO_IN_A2 = 0;
- MOTO_IN_B1 = 1;
- MOTO_IN_B2 = 0;
- */
- } else {
- /*
- MOTO_IN_A1 = 0;
- MOTO_IN_A2 = 1;
- MOTO_IN_B1 = 0;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
0.png (37.6 KB, 下載次數(shù): 49)
下載附件
2018-8-29 03:26 上傳
全部資料51hei下載地址:
四驅(qū)車遙控避障尋線演示程序(GP41QS).zip
(291.78 KB, 下載次數(shù): 7)
2018-8-29 01:10 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|