該設計是基于90c52單片機的電磁尋跡小車。搭好硬件后(包括兩個直流電機,一個萬向輪,傳感器(電感)及其支架 )。將小車放在通電的導線上傳感器與通電導線電磁感應產生電動勢,經過放大電路將微弱的信號放大后。再由單片機內部的ad讀取數據。分析數據在決定小車的方向.
具體的實現過程如下:
1.假設在小車的左右兩端各有1個電感并且經過放大電路后ad采集到的值分別為a和b。
2.電機的運轉快慢由定時器產生的pwm信號來控制,假設左右兩個電機的初始pwm為20,最高為100。
3.我們知道電勢的高低和傳感器離通電導線的距離有關,距離越遠得到的值越小,反之越高。最理想的狀態是小車一直在導線的中間走。也就是兩個電感的值相同。此時可以通過所得到的電感值算出小車的一個相對的位置 p=a-b/c 這只是一個最基本的式子具體還要根據自己實際所得的ad值來做調整。根據所得到的這個相對位置的值再來調整左邊或者右邊電機的pwm值。如果p>大于0,說明左邊的電感值大于右邊也就是說左邊的電感距離通電導線近,就應該左轉,反之右轉。
4.轉彎的方法第一可以保持一邊電機的pwm不變降低另一邊的pwm的值,第二可以保持一邊電機的pwm不變提高另一邊的pwm的值
本設計已經過測試確實可行由于時間倉促還有很多地方需要完善,見諒!
單片機源程序如下:
-
- #include "max7219.h"
- unsigned char dis_code[18]={0x7E,0x30,0x6D,0x79, // 0, 1, 2, 3
- 0x33,0x5B,0x5F,0x70,0x7F,0x7B,0x77,0x1f,0x4e,0x3d,0x4f,0x47,0x01,0x00};// 4, 5, 6, 7, 8, 9,a,b,c,d,e,f,-, ,
- void Write_Max7219_byte(unsigned char temp)
- {
- unsigned char i;
- for (i=0;i<8;i++)
- {
- MAX7219_CLK0; // CLK=0;
- if((temp>>7)&0x1)
- {
- MAX7219_DIN1;
- }
- else
- {
- MAX7219_DIN0;
- }
- //DIN=((temp>>7)&0x1);
- temp<<=1;
- MAX7219_CLK1;//CLK=1;
- }
- }
- void Write_Max7219(unsigned char address,unsigned char dat)
- {
- MAX7219_CS0;//MAX_LOAD=0;
- Write_Max7219_byte(address);
- Write_Max7219_byte(dat);
- MAX7219_CS1;//MAX_LOAD=1;
- }
- void clear(void)
- {
- Write_Max7219(1,0x00);
- Write_Max7219(2,0x00);
- Write_Max7219(3,0x00);
- Write_Max7219(4,0x00);
- Write_Max7219(5,0x00);
- Write_Max7219(6,0x00);
- Write_Max7219(7,0x00);
- Write_Max7219(8,0x00);
- }
- //void Max7219_port(void)
- //{
- //
- // GPIO_InitTypeDef GPIO_InitStructure;
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- // GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2 ;
- // GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
- // GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- // GPIO_Init(GPIOB,&GPIO_InitStructure);
- //// RCC->APB2ENR|=1<<2;
- ////
- //// GPIOA->CRL&=0X0F00FFFF;
- //// GPIOA->CRL|=0X30330000;//PA2 推挽輸出
- //// GPIOA->ODR|=1<<4; //PA2 輸出高
- //// GPIOA->ODR|=1<<5; //PA2 輸出高
- //// GPIOA->ODR|=1<<7; //PA2 輸出高
- ////
- //}
- void Init_Max7219(void)
- {
- Write_Max7219(SHUT_DOWN, 0x00); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0
- Write_Max7219(DECODE_MODE, 0x00); //Decode Mode Select D7~D0 1 B decode 0 No decode
- Write_Max7219(SCAN_LIMIT, 0x07); //SCAN LIMIT 0~7 0xX0~0xX7
- Write_Max7219(INTENSITY, 0x0A); //Set Intensity 0xX0~0xXf
- Write_Max7219(DISPLAY_TEST, 0x00); //Normal Operation XXXXXXX0 Display Test Mode
- Write_Max7219(SHUT_DOWN, 0x01); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0
- clear();
- }
- void TestLED(void)
- {
- Write_Max7219(1,dis_code[0]);
- Write_Max7219(2,dis_code[1]);
- Write_Max7219(3,dis_code[2]);
- Write_Max7219(4,dis_code[3]);
- Write_Max7219(5,dis_code[4]);
- Write_Max7219(6,dis_code[5]);
- Write_Max7219(7,dis_code[6]);
- Write_Max7219(8,dis_code[7]);
- }
- void LED(unsigned char position ,unsigned char Value)
- {
-
- Write_Max7219(position,dis_code[Value]);
- }
- void display(int num,int num1 )
- {
- char temp;
- temp = num/1000;
- LED(8 ,temp);
- temp = num%1000/100;
- LED(7 ,temp);
- temp = num%100/10;
- LED(6 ,temp);
- temp = num%10;
- LED(5 ,temp);
- temp = num1/1000;
- LED(4 ,temp);
- temp = num1%1000/100;
- LED(3 ,temp);
- temp = num1%100/10;
- LED(2 ,temp);
- temp = num1%10;
- LED(1 ,temp);
- }
復制代碼
所有資料51hei提供下載:
基于90c52單片機的電磁小車.rar
(127.24 KB, 下載次數: 14)
2018-12-20 16:41 上傳
點擊文件名下載附件
尋跡小車 下載積分: 黑幣 -5
|