久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 13109|回復(fù): 14
收起左側(cè)

MSP430f5529小車源碼

  [復(fù)制鏈接]
ID:138313 發(fā)表于 2020-10-11 17:49 | 顯示全部樓層 |閱讀模式
https://www.bilibili.com/video/BV1Jz4y1o7Lv/

單片機源程序如下:
  1. //******************************************************************************
  2. //   Author:wwj
  3. //   Built with IAR V7.0
  4. //   Gui Lin
  5. //   IDE IAR430
  6. /*截止當(dāng)前:
  7. 功能:
  8. 本程序從平衡車程序移植出來,仍有平衡車的影子,但不影響運行。
  9. 1.硬件IIC讀取6050,并在OLED上顯示
  10. 2.中斷測速
  11. 3.pwm電機驅(qū)動
  12. 4.循跡(由于每個人裝循跡模塊的位置都會有差異,需要自己調(diào)整傳感器)
  13. 5.按鍵檢測
  14. 6.藍牙控制
  15. 7....ADD ING

  16. 勿拿本代碼做交易!!!
  17. 勿拿本代碼做交易!!!
  18. 勿拿本代碼做交易!!!

  19. 本來只想放出來演示,突然發(fā)現(xiàn)有人從中謀取利益,于是決定共享源碼!!!
  20. 各位朋友加油,未來的科技看你們!!!

  21. */

  22. //******************************************************************************

  23. #include <msp430.h>
  24. #include <stdint.h>
  25. #include <stdbool.h>
  26. //#include "oledfont.h"
  27. //#include "msp430f5529.h"
  28. #include "MPU6050.h"
  29. #include "uart.h"
  30. #include "exter_interr.h"
  31. #include "pwm.h"
  32. #include "time.h"
  33. #include "hardw_i2c_oled.h"
  34. #include "mathfun.h"
  35. #include "xunji.h"

  36. void OledDisApp(void);

  37. void delay(unsigned int z)
  38. {
  39.   unsigned int x,y;
  40.   for(x=z;x>0;x--)
  41.     for(y=5000;y>0;y--);
  42. }


  43. //******************************************************************************
  44. // Device Initialization *******************************************************
  45. //******************************************************************************

  46. void initClockTo16MHz()
  47. {
  48.     UCSCTL3 |= SELREF_2;                      // Set DCO FLL reference = REFO
  49.     UCSCTL4 |= SELA_2;                        // Set ACLK = REFO
  50.     __bis_SR_register(SCG0);                  // Disable the FLL control loop
  51.     UCSCTL0 = 0x0000;                         // Set lowest possible DCOx, MODx
  52.     UCSCTL1 = DCORSEL_5;                      // Select DCO range 16MHz operation
  53.     UCSCTL2 = FLLD_0 + 487;                   // Set DCO Multiplier for 16MHz
  54.                                               // (N + 1) * FLLRef = Fdco
  55.                                               // (487 + 1) * 32768 = 16MHz
  56.                                               // Set FLL Div = fDCOCLK
  57.     __bic_SR_register(SCG0);                  // Enable the FLL control loop

  58.     // Worst-case settling time for the DCO when the DCO range bits have been
  59.     // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
  60.     // UG for optimization.
  61.     // 32 x 32 x 16 MHz / 32,768 Hz = 500000 = MCLK cycles for DCO to settle
  62.     __delay_cycles(500000);//
  63.     // Loop until XT1,XT2 & DCO fault flag is cleared
  64.     do
  65.     {
  66.         UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags
  67.         SFRIFG1 &= ~OFIFG;                          // Clear fault flags
  68.     }while (SFRIFG1&OFIFG);                         // Test oscillator fault flag
  69. }
  70. uint16_t setVCoreUp(uint8_t level){
  71.     uint32_t PMMRIE_backup, SVSMHCTL_backup, SVSMLCTL_backup;

  72.     //The code flow for increasing the Vcore has been altered to work around
  73.     //the erratum FLASH37.
  74.     //Please refer to the Errata sheet to know if a specific device is affected
  75.     //DO NOT ALTER THIS FUNCTION

  76.     //Open PMM registers for write access
  77.     PMMCTL0_H = 0xA5;

  78.     //Disable dedicated Interrupts
  79.     //Backup all registers
  80.     PMMRIE_backup = PMMRIE;
  81.     PMMRIE &= ~(SVMHVLRPE | SVSHPE | SVMLVLRPE |
  82.                 SVSLPE | SVMHVLRIE | SVMHIE |
  83.                 SVSMHDLYIE | SVMLVLRIE | SVMLIE |
  84.                 SVSMLDLYIE
  85.                 );
  86.     SVSMHCTL_backup = SVSMHCTL;
  87.     SVSMLCTL_backup = SVSMLCTL;

  88.     //Clear flags
  89.     PMMIFG = 0;

  90.     //Set SVM highside to new level and check if a VCore increase is possible
  91.     SVSMHCTL = SVMHE | SVSHE | (SVSMHRRL0 * level);

  92.     //Wait until SVM highside is settled
  93.     while((PMMIFG & SVSMHDLYIFG) == 0)
  94.     {
  95.         ;
  96.     }

  97.     //Clear flag
  98.     PMMIFG &= ~SVSMHDLYIFG;

  99.     //Check if a VCore increase is possible
  100.     if((PMMIFG & SVMHIFG) == SVMHIFG)
  101.     {
  102.         //-> Vcc is too low for a Vcore increase
  103.         //recover the previous settings
  104.         PMMIFG &= ~SVSMHDLYIFG;
  105.         SVSMHCTL = SVSMHCTL_backup;

  106.         //Wait until SVM highside is settled
  107.         while((PMMIFG & SVSMHDLYIFG) == 0)
  108.         {
  109.             ;
  110.         }

  111.         //Clear all Flags
  112.         PMMIFG &= ~(SVMHVLRIFG | SVMHIFG | SVSMHDLYIFG |
  113.                      SVMLVLRIFG | SVMLIFG |
  114.                      SVSMLDLYIFG
  115.                      );

  116.         //Restore PMM interrupt enable register
  117.         PMMRIE = PMMRIE_backup;
  118.         //Lock PMM registers for write access
  119.         PMMCTL0_H = 0x00;
  120.         //return: voltage not set
  121.         return false;
  122.     }

  123.     //Set also SVS highside to new level
  124.     //Vcc is high enough for a Vcore increase
  125.     SVSMHCTL |= (SVSHRVL0 * level);

  126.     //Wait until SVM highside is settled
  127.     while((PMMIFG & SVSMHDLYIFG) == 0)
  128.     {
  129.         ;
  130.     }

  131.     //Clear flag
  132.     PMMIFG &= ~SVSMHDLYIFG;

  133.     //Set VCore to new level
  134.     PMMCTL0_L = PMMCOREV0 * level;

  135.     //Set SVM, SVS low side to new level
  136.     SVSMLCTL = SVMLE | (SVSMLRRL0 * level) |
  137.                SVSLE | (SVSLRVL0 * level);

  138.     //Wait until SVM, SVS low side is settled
  139.     while((PMMIFG & SVSMLDLYIFG) == 0)
  140.     {
  141.         ;
  142.     }

  143.     //Clear flag
  144.     PMMIFG &= ~SVSMLDLYIFG;
  145.     //SVS, SVM core and high side are now set to protect for the new core level

  146.     //Restore Low side settings
  147.     //Clear all other bits _except_ level settings
  148.     SVSMLCTL &= (SVSLRVL0 + SVSLRVL1 + SVSMLRRL0 +
  149.                  SVSMLRRL1 + SVSMLRRL2
  150.                  );

  151.     //Clear level settings in the backup register,keep all other bits
  152.     SVSMLCTL_backup &=
  153.         ~(SVSLRVL0 + SVSLRVL1 + SVSMLRRL0 + SVSMLRRL1 + SVSMLRRL2);

  154.     //Restore low-side SVS monitor settings
  155.     SVSMLCTL |= SVSMLCTL_backup;

  156.     //Restore High side settings
  157.     //Clear all other bits except level settings
  158.     SVSMHCTL &= (SVSHRVL0 + SVSHRVL1 +
  159.                  SVSMHRRL0 + SVSMHRRL1 +
  160.                  SVSMHRRL2
  161.                  );

  162.     //Clear level settings in the backup register,keep all other bits
  163.     SVSMHCTL_backup &=
  164.         ~(SVSHRVL0 + SVSHRVL1 + SVSMHRRL0 + SVSMHRRL1 + SVSMHRRL2);

  165.     //Restore backup
  166.     SVSMHCTL |= SVSMHCTL_backup;

  167.     //Wait until high side, low side settled
  168.     while(((PMMIFG & SVSMLDLYIFG) == 0) &&
  169.           ((PMMIFG & SVSMHDLYIFG) == 0))
  170.     {
  171.         ;
  172.     }

  173.     //Clear all Flags
  174.     PMMIFG &= ~(SVMHVLRIFG | SVMHIFG | SVSMHDLYIFG |
  175.                 SVMLVLRIFG | SVMLIFG | SVSMLDLYIFG
  176.                 );

  177.     //Restore PMM interrupt enable register
  178.     PMMRIE = PMMRIE_backup;

  179.     //Lock PMM registers for write access
  180.     PMMCTL0_H = 0x00;

  181.     return true;
  182. }
  183. bool increaseVCoreToLevel2()
  184. {
  185.     uint8_t level = 2;
  186.     uint8_t actlevel;
  187.     bool status = true;

  188.     //Set Mask for Max. level
  189.     level &= PMMCOREV_3;

  190.     //Get actual VCore
  191.     actlevel = PMMCTL0 & PMMCOREV_3;

  192.     //step by step increase or decrease
  193.     while((level != actlevel) && (status == true))
  194.     {
  195.         if(level > actlevel)
  196.         {
  197.             status = setVCoreUp(++actlevel);
  198.         }
  199.     }

  200.     return (status);
  201. }

  202. void initGPIO()
  203. {
  204.     //I2C Pins
  205.     P3SEL |= BIT0 + BIT1;                     // P3.0,1 option select
  206.    
  207.     P1DIR |=BIT0;
  208.     P4DIR |=BIT7;
  209.     //按鍵輸入
  210.     P2DIR &=~BIT1;
  211.     P1DIR &=~BIT1;
  212.    
  213.     P2REN |= BIT1;//使能上下拉
  214.     P2OUT |= BIT1;//上拉
  215.    
  216.     P1REN |= BIT1;//使能上下拉
  217.     P1OUT |= BIT1;//上拉
  218. }

  219. void initI2C()
  220. {
  221.   
  222.   
  223.     UCB0CTL1 |= UCSWRST;                      // Enable SW reset(復(fù)位使能)
  224.     UCB0CTL0 =  UCMST + UCMODE_3 + UCSYNC;     //  Master, I2C,synchronous mode(同步模式)
  225.     UCB0CTL1 = UCSSEL_2 + UCSWRST;            // Use SMCLK, keep SW reset
  226.     UCB0BR0 = 160;                            // fSCL = SMCLK/160 = ~100kHz
  227.     UCB0BR1 = 0;
  228.     UCB0CTL0 &=~UCSLA10;                      //7位地址模式
  229.     UCB0I2CSA = SLAVE_ADDR>>1; //SlaveAddress>>1;//SLAVE_ADDR>>1;                //  
  230.     UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation
  231.    
  232.     UCB0IFG &=~UCTXIFG;
  233.    
  234. }

  235. int num;
  236. int main(void) {

  237.     WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer
  238.     increaseVCoreToLevel2();
  239.     initClockTo16MHz();                        //配置系統(tǒng)時鐘為16Mhz
  240.     //delay(10);
  241.     initGPIO();
  242.     initI2C();
  243.     //delay(10);
  244.     OLED_Init();
  245.     //delay(10);
  246.     UART_Init('n',8,1);
  247.     InitMPU6050();
  248.     //delay(10);
  249.     exterPin();//外部引腳中斷
  250.     //MotorSpeedDetectionInit();   
  251.     PWMInit();
  252.     TB6612INOUT();
  253.     time0InterInit();
  254.     xunioinit();//尋跡引腳初始化
  255.     delay(10);
  256.     //OLED_ShowChar(0,0,'A',16);
  257.     //OLED_ShowChar(16,0,'B',16);
  258.     //OLED_ShowChar(16*2,0,'C',16);
  259.    
  260.     _EINT();//開啟總中斷
  261.     //曾經(jīng)由于沒開總中斷,調(diào)了一下午...
  262.     while(1)
  263.     {
  264.       //按鍵檢測
  265.       if((P2IN & BIT1) == 0)
  266.       {
  267.         delay(3);
  268.         if((P2IN & BIT1) == 0)
  269.         {
  270.           P1OUT ^= BIT0;
  271.         }
  272.         
  273.       //PWM_Motor(-20,-20);
  274.       }
  275.       while(!(P2IN&BIT1));
  276.       
  277.       xunjiing();//尋跡
  278.      
  279.       
  280.   if(count_time >=20)
  281.   {
  282.     count_time =0;
  283.    // P1OUT ^= BIT0;              //形成閃燈效果
  284.    Angle = Mpu6050AccelAngle(ACCEL_XOUT,ACCEL_ZOUT);
  285.    Angle_dot =  Mpu6050GyroAngle(GYRO_YOUT);
  286.   
  287.   // pwm_calculate();//PWM計算輸出
  288.      
  289.    
  290.   }
  291.         

  292.   
  293. #if 1
  294.   if(count_time2 >=10)
  295.   {
  296.       count_time2 =0;
  297.       OledDisApp();
  298.    
  299.   }
  300. #endif
  301.   
  302. #if 0
  303.   if(Angle >10.0)
  304.   {
  305.    PWM_Motor(20,20);
  306.   }
  307.   else if(Angle < -10.0)
  308.   {
  309.   PWM_Motor(-20,-20);
  310.   }
  311.   else
  312.   {
  313.   Stop();
  314.   }
  315.   
  316. #endif
  317.   
  318.   
  319.   
  320.   
  321.   //printf ("Pwm = %d \r\n",Pwm);
  322.   
  323.    //
  324.     // OLED_Show_Number(0,6,abs(Angle),16);
  325.      //P1OUT ^=BIT0;
  326.       
  327.      //printf("angle = %1f , angle_dot = %2f \r\n",Angle,Angle_dot);
  328.      //printf("ML = %d , MR = %d \r\n",speed_mL,speed_mR);//打印編碼器
  329.       
  330.       
  331.      //PWM_Motor(20,-20);//測試電機,應(yīng)看到左輪后退,右輪前進
  332.       
  333.       //TA2CCR1 = abs(-100);//L
  334.       //TA1CCR1 = abs(256/2);//R
  335.       
  336.      P4OUT ^=BIT7;
  337.      
  338.      
  339.    // delay(10);
  340.     }
  341.    
  342. }
  343. void OledDisApp(void)
  344. {
  345. #if 1
  346. if(Angle < 0.0)
  347. {   
  348. //sprintf
  349. OLED_ShowChar(32, 2,  '-', 16);
  350. }
  351. else
  352. {
  353. OLED_ShowChar(32, 2, ' ', 16);
  354. }
  355. OLED_ShowNum(32+8, 2, abs(Angle), 3, 16);
  356. OLED_ShowNum(32+8, 5, speedcar/2, 3, 16);
  357. #endif
  358. //================Speed====================
  359. #if 0
  360. if(Speed < 0.0)
  361. {   
  362. //sprintf
  363. OLED_ShowChar(50, 2,  '-', 16);
  364. }
  365. else
  366. {
  367. OLED_ShowChar(50, 2, ' ', 16);
  368. }
  369. OLED_ShowNum(50+8, 2, abs(Speed), 3, 16);
  370. #endif

  371. //fillRect(124, 64/2, 2, -63, 1,1);

  372.   
  373.   }
復(fù)制代碼


回復(fù)

使用道具 舉報

ID:138313 發(fā)表于 2020-10-11 22:52 | 顯示全部樓層
使用FDC2214尋跡效果更佳
回復(fù)

使用道具 舉報

ID:828564 發(fā)表于 2020-10-12 01:07 | 顯示全部樓層
開發(fā)環(huán)境是ccs嗎?
回復(fù)

使用道具 舉報

ID:596548 發(fā)表于 2020-10-12 01:48 | 顯示全部樓層
大佬有接線圖嗎,I2C不是很看得懂,不會接線???
ball ball you。。。。。
回復(fù)

使用道具 舉報

ID:618513 發(fā)表于 2020-10-12 05:51 | 顯示全部樓層
樓主拿過來接上線就能跑哇
回復(fù)

使用道具 舉報

ID:755986 發(fā)表于 2020-10-12 17:20 | 顯示全部樓層
沐浴 發(fā)表于 2020-10-12 05:51
樓主拿過來接上線就能跑哇

大哥,求接線
回復(fù)

使用道具 舉報

ID:561575 發(fā)表于 2021-5-5 11:32 | 顯示全部樓層
這代碼不完整
回復(fù)

使用道具 舉報

ID:692345 發(fā)表于 2021-7-12 11:25 | 顯示全部樓層
頭文件能否分享
回復(fù)

使用道具 舉報

ID:954468 發(fā)表于 2021-7-18 23:30 | 顯示全部樓層
是用keil開發(fā)的嗎?
回復(fù)

使用道具 舉報

ID:954006 發(fā)表于 2021-7-20 09:32 | 顯示全部樓層
大佬可以分享一下頭文件嘛
回復(fù)

使用道具 舉報

ID:955079 發(fā)表于 2021-7-22 18:39 | 顯示全部樓層
這代碼少了個子函數(shù)啊,求完整的
回復(fù)

使用道具 舉報

ID:1039079 發(fā)表于 2022-7-15 11:10 | 顯示全部樓層
求完整的代碼和接線圖,謝謝
回復(fù)

使用道具 舉報

ID:1039079 發(fā)表于 2022-7-25 10:16 | 顯示全部樓層
這個咋接線啊,大佬
回復(fù)

使用道具 舉報

ID:1038977 發(fā)表于 2023-7-21 17:29 | 顯示全部樓層
頭文件里的.C文件可以分享嗎,否則不能用啊
回復(fù)

使用道具 舉報

ID:1088853 發(fā)表于 2023-7-24 11:29 | 顯示全部樓層
求完整代碼
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 久久y| 五月婷婷亚洲 | 老妇激情毛片免费 | 高清成人免费视频 | 亚洲人成在线播放 | 国产午夜精品福利 | 久久日本 | 欧美精品一区二区三区视频 | 中文字幕 在线观看 | 国产日韩久久 | 日韩成人精品一区二区三区 | 久久er精品 | 凹凸日日摸日日碰夜夜 | 亚洲入口 | 男人的天堂一级片 | 97伦理最新伦理 | 欧美精品一区二区三区在线 | 午夜久久av | 国产精品久久久久久久午夜 | 亚洲国产成人在线视频 | 91传媒在线观看 | 性生生活大片免费看视频 | 亚洲www| 久久精品久久久久久 | 97精品超碰一区二区三区 | 黑人一级黄色大片 | 99精品视频免费观看 | www.亚洲国产精品 | 精品视频一区二区 | 日韩一级免费电影 | 97av视频在线 | 国产精品一区视频 | 精品国产一区二区国模嫣然 | 69av片| 亚洲444eee在线观看 | 中文字幕一区二区三区精彩视频 | 国产在线观看不卡一区二区三区 | 久久久久久久一区 | 成人精品久久日伦片大全免费 | 蜜桃在线播放 | 午夜资源|