找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3117|回复: 0
收起左侧

stm32f051无刷电机的无传感器控制源码与原理图及说明资料下载

[复制链接]
ID:347063 发表于 2018-6-7 17:31 | 显示全部楼层 |阅读模式
基于stm32f051的无刷电机无传感器的控制。成本低,st原代码

电路原理图如下:
0.png 0.png

stm32单片机源程序如下:


  1. #include "stm32f0xx.h"
  2. #include "definitions.h"
  3. #include "motorcontrolsettings.h"


  4. unsigned char zcfound;
  5. unsigned char phase;
  6. unsigned char autostep;
  7. unsigned char risingdelay;
  8. unsigned char fallingdelay;
  9. unsigned char risingedge;
  10. unsigned char startstate;
  11. unsigned char run;
  12. unsigned char ontimesample;

  13. unsigned short commcounter;
  14. unsigned short step;
  15. unsigned short bemfsample;
  16. unsigned short dcbussample;
  17. unsigned short commcounter;
  18. unsigned short demagcounter;
  19. unsigned short zccounter;
  20. unsigned short commthreshold;
  21. unsigned short demagthreshold;
  22. unsigned short runningdc;
  23. unsigned short potvalue;
  24. unsigned short zcthreshold;
  25. unsigned short dutycyclehold;

  26. unsigned long holdcounter;
  27. unsigned long alignmentcounter;
  28. unsigned long rampspeed;
  29. unsigned long bemfchannel;



  30. void commutate(void);
  31. void commutate2(void);
  32. void motorstartinit(void);
  33. void delay( unsigned long time);
  34. void flashled(unsigned char flashes);
  35. void selftest(void);
  36. unsigned short readadc( unsigned char chnl);

  37. int main(void)
  38. {


  39. // clock chain setup
  40. RCC->CFGR = (4<<18); // set PLL X6 for 24MHZ
  41. RCC->CR = 0x00000083 + (1<<24); //turn PLL on
  42. RCC->CFGR = (4<<18) + 2; // select PLL as clock

  43. RCC->AHBENR = b19+b18+b17; // enable GPIOA, B, C input clock

  44. RCC->APB2ENR = b11+b9+b0; // enable tim1 and ADC input clocks

  45. RCC->APB1ENR = b29; // enable DAC input clock




  46. //**********************************************

  47. delay(5000000) ;  //let power supply settle


  48. selftest();








  49. // port A setup
  50. //GPIOA->MODER = 0x2A2AFFF0; // a5,a4,3,2,6,7 anlg, a8,9,10,12 AF
  51. GPIOA->MODER = 0x2A6AFFF0; // 11 pp, a5,a4,3,2,6,7 anlg, a8,9,10,12 AF
  52. GPIOA->AFRH  = 0x00020222; // pa8,9,10,12 AF2 for timer

  53. // port B setup
  54. // b22 PB11 as output
  55. GPIOB->MODER = 0xA800000F+b22; // b1,0 anlg, b15,14,13 AF
  56. GPIOB->AFRH = 0x22220000;  // b15,14,13,12 AF2 for timer

  57. // port C setup
  58. GPIOC->ODR = 0;
  59.   

  60. // ADC setup
  61. ADC1->CR= b31 ; // start ADC calibration
  62. while( ADC1->CR & b31);  // wait for calibration to complete
  63. while( ADC1->CR & b31);  // wait for calibration to complete
  64. ADC1->CR = 1; // enable ADC
  65. ADC1->CFGR1 = b16; // enable discontinuous mode
  66. ADC1->SMPR = 1;




  67. // comparator setup
  68. // (b25+b24) send comp2 output to tim1 OCref clear
  69. // b27 invert comp2 output
  70. // b16 enable comp2
  71. // b22+b20 select PA5 as inverting input for comp2

  72. // b0 enable comp1
  73. // (b6+b5) select PA0 comp1 inverting input
  74. // b8 send comp1 output to tim1 break
  75. // b11 to invert comp1 output

  76. COMP->CSR = 0;
  77. #ifdef currentlimitenable
  78. COMP->CSR = COMP->CSR + b27+b16+b22+b20+b25+b24;  // cy-by-cy active
  79. #endif
  80. #ifdef overcurrentenable
  81. COMP->CSR = COMP->CSR + b0+b6+b5+b11+b8; //  brk active
  82. #endif


  83. // tim1 setup
  84. TIM1->SMCR = b15+b4+b5+b6; // make ETR input active low
  85. TIM1->CR2= 0;
  86. TIM1->CCR1= 200;
  87. TIM1->CCR2= 200;
  88. TIM1->CCR3= 200;
  89. TIM1->CCR4= 1100;
  90. TIM1->ARR=1200;
  91. TIM1->CR1=0x0001;


  92. // note: b15 b7 and b7 are to enable ETR  based current limit
  93. TIM1->CCMR1= 0x6868 +b15 + b7;
  94. TIM1->CCMR2= 0x6868 +b7;
  95. // b4 for cc4 and b7 for brk interrupt
  96. //TIM1->DIER = b4+b7;  // enable cc4 interrupt
  97. TIM1->DIER = b4+b6;

  98. // DAC setup
  99. DAC->CR = 1; // enable DAC









  100. // set up interrupts
  101. NVIC_EnableIRQ(14);
  102. __enable_irq();

  103. // initialization
  104. run=0;
  105. motorstartinit();

  106. clearmark;






  107. while(1)
  108. { // backgroung loop

  109. if( (TIM1->BDTR & b15)==0) // overcurrent fault condition
  110. {
  111. run=0;
  112. while(1)
  113. {
  114. ledon;
  115. delay(1000000);
  116. ledoff;
  117. delay(1000000);
  118. if( (4095-potvalue)<100)
  119. {
  120. TIM1->BDTR= b15+b11+b10+b12+b13;  //  set MOE
  121. break;
  122. }


  123. } //end of LED flash loop


  124. } // end of if overcurrent




  125. //TIM1->BDTR= b15+b11+b10+b12+b13;  //  set MOE



  126. if( (4095-potvalue)>200) run=255;
  127. if( (4095-potvalue)<100) run=0;

  128. if(run) ledon;
  129. if(run==0) ledoff;



  130. runningdc = ((4095-potvalue)*1200)>>12;
  131. if(runningdc>1195) runningdc=1300;
  132. if(dutycyclehold && (runningdc>600)) runningdc=600;

  133. #ifdef ontimesampleenable
  134. if(runningdc>800) ontimesample=255;
  135. else ontimesample=0;
  136. #endif







  137. //if(COMP->CSR & b30) // comp2
  138. if(COMP->CSR & b14)  // comp1
  139. {
  140. setmark;
  141. }
  142. else
  143. {
  144. //clearmark;
  145. }





  146. //DAC->DHR12R1 = potvalue; // put out for diagnostic purposes




  147. } // end of backgroung loop





  148. }  // end of main









  149. void commutate(void)
  150. {

  151. switch(phase)
  152. {
  153. case 0: // phase AB
  154. TIM1->CCER = b3+b0  +b7+b4 +b8; //
  155. TIM1->CCMR1= 0x4868 +b15 + b7; // force B active low
  156. TIM1->CCMR2= 0x6868 +b7;
  157. break;
  158. case 1: // phase AC
  159. TIM1->CCER = b3+b0  +b4  +b8+b11; //
  160. TIM1->CCMR1= 0x6868 +b15 + b7;
  161. TIM1->CCMR2= 0x6848 +b7; // force C active low
  162. break;
  163. case 2: // phase BC
  164. TIM1->CCER = b4+b7  +b8+b11  +b0;
  165. TIM1->CCMR1= 0x6868 +b15 + b7;
  166. TIM1->CCMR2= 0x6848 +b7; // force C active low
  167. break;
  168. case 3: // phase BA
  169. TIM1->CCER = b4+b7 +b3+b0 +b8;
  170. TIM1->CCMR1= 0x6848 +b15 + b7; // force A active low
  171. TIM1->CCMR2= 0x6868 +b7;
  172. break;
  173. case 4: // phase CA
  174. TIM1->CCER = b8+b11 +b3+b0 +b4;
  175. TIM1->CCMR1= 0x6848 +b15 + b7; // force A active low
  176. TIM1->CCMR2= 0x6868 +b7;
  177. break;
  178. case 5: // phase CB
  179. TIM1->CCER = b8+b11 +b4+b7 +b0;
  180. TIM1->CCMR1= 0x4868 +b15 + b7; // force B active low
  181. TIM1->CCMR2= 0x6868 +b7;
  182. break;
  183. } // end of phase switch statement
  184. } // end of commutate function

  185. void commutate2(void)
  186. {

  187. //TIM1->CCER=b10+b8+b6+b4+b2+b0; // enable all 6
  188. switch(phase)
  189. {
  190. case 0: // phase AB
  191. // enable all 6 except AN
  192. // invert AN
  193. TIM1->CCER=b10+b8+b6+b4+b0  +b3;
  194. TIM1->CCMR1=0x4868+b15+b7; // B low, A PWM
  195. TIM1->CCMR2= 0x6858 +b7; // force C ref high (phc en low)
  196. break;
  197. case 1: // phase AC
  198. // enable all 6 except AN
  199. // invert AN
  200. TIM1->CCER=b10+b8+b6+b4+b0  +b3;
  201. TIM1->CCMR1=0x5868+b15+b7; // force B high and A PWM
  202. TIM1->CCMR2= 0x6848 +b7; // force C ref low
  203. break;
  204. case 2: // phase BC
  205. // enable all 6 except BN
  206. // invert BN
  207. TIM1->CCER=b10+b8+b4+b2+b0 +b7;
  208. TIM1->CCMR1=0x6858+b15+b7; // force B PWM and A high
  209. TIM1->CCMR2= 0x6848 +b7; // force C ref low
  210. break;
  211. case 3: // phase BA
  212. // enable all 6 except BN
  213. // invert BN
  214. TIM1->CCER=b10+b8+b4+b2+b0 +b7;
  215. TIM1->CCMR1=0x6848+b15+b7; // force B PWM and A ref low
  216. TIM1->CCMR2= 0x6858 +b7; // force C ref high
  217. break;
  218. case 4: // phase CA
  219. // enable all 6 except CN
  220. // invert CN
  221. TIM1->CCER=b8+b6+b4+b2+b0 +b11; // enable all 6 except CN
  222. TIM1->CCMR1=0x5848+b15+b7; // force B high and A ref low
  223. TIM1->CCMR2= 0x6868 +b7; // force C PWM
  224. break;
  225. case 5: // phase CB
  226. // enable all 6 except CN
  227. // invert CN
  228. TIM1->CCER=b8+b6+b4+b2+b0 +b11; // enable all 6 except CN
  229. TIM1->CCMR1=0x4858+b15+b7; // force B low and A high
  230. TIM1->CCMR2= 0x6868 +b7; // force C PWM
  231. break;
  232. } // end of phase switch statement
  233. } // end of commutate2 function


  234. // pwmisr runs just at the end of each PWM cycle
  235. void pwmisr(void)
  236. {

  237. //**********************************************************

  238. unsigned long long0;

  239. ADC1->CHSELR = bemfchannel; // set ADC MUX to proper bemf channel
  240. ADC1->CR = b2;  // start adc conversion
  241. // housekeeping increment of some timers while adc is converting
  242. zccounter++;  // housekeeping increment of some timers while adc is converting
  243. alignmentcounter++;
  244. holdcounter++;
  245. while((ADC1->CR & b2)==b2) ; // wait for conversion to complete
  246. bemfsample= ADC1->DR;

  247. //DAC->DHR12R1 = bemfsample; // put out for diagnostic purposes



  248. // autostep is true during ramping up
  249. if(autostep)
  250. {
  251. commcounter++;
  252.   if(commcounter>step)
  253.   {
  254.   commcounter=0;
  255.   phase++;
  256.   if(phase>5) phase=0;
  257.   commutate2();

  258.   }
  259. } // end of if(autostep)


  260. if(run==0) startstate=0;

  261. switch(startstate)
  262. {
  263. case 0:
  264. TIM1->CCER = 0;
  265. if(run)
  266. {
  267. motorstartinit();
  268. startstate=5;
  269. }
  270. break;

  271. case 5: // setup alignment
  272. TIM1->CCR1= alignmentdc;
  273. TIM1->CCR2= alignmentdc;
  274. TIM1->CCR3= alignmentdc;
  275. phase=0;
  276. commutate2();
  277. alignmentcounter=0;
  278. startstate=10;
  279. break;

  280. case 10: // timing out alignment
  281.   if(alignmentcounter>alignmenttime)
  282.   {
  283.   rampspeed=1;
  284.   commcounter=0;
  285.   autostep=255;
  286.   TIM1->CCR1= rampupdc;
  287.   TIM1->CCR2= rampupdc;
  288.   TIM1->CCR3= rampupdc;
  289.   startstate=20;
  290.   }
  291. break;

  292. case 20: // ramping up
  293. rampspeed = rampspeed+rampuprate; // accelerate
  294. long0 = 4000000000;
  295. long0 = long0/rampspeed; // step time = constant/speed
  296. if(long0>30000) long0=30000;
  297. step=long0;
  298.   if(step<=minstep)
  299.   {
  300.   holdcounter=0;
  301.   startstate=100;
  302.   }
  303. break;

  304. case 100: // wait for hold time, holding at speed to allow sync
  305. if(holdcounter>holdtime) startstate=110;
  306. break;

  307. case 110: // wait to get into phase 5
  308. if(phase==5) startstate=120;
  309. break;

  310. case 120: // wait for leading edge of phase 0 (commutation)
  311. if(phase==0)
  312. {
  313. demagcounter=0;
  314. demagthreshold = (step*demagallowance)>>8;
  315. startstate=130;
  316. }
  317. break;

  318. case 130: // wait out demag time (for current in phase to go to 0)
  319. demagcounter++;
  320.   if(demagcounter>demagthreshold)
  321.   {
  322.   startstate=140;
  323.   }
  324. break;

  325. case 140: // looking for zero crossing of bemf
  326. if( zcfound && (zccounter>maxstep) )
  327. {
  328. startstate=0;
  329. break;
  330. }


  331. if(risingedge)
  332. {
  333.   if((bemfsample>zcthreshold)||(zccounter>maxstep) )
  334.   {
  335.   if(zcfound) step = zccounter;
  336.   commthreshold = (step*risingdelay)>>8;
  337.   zccounter=0;
  338.   commcounter=0;
  339.   startstate=150;
  340.   risingedge=0;
  341.   zcfound=255;
  342.   autostep=0;
  343.   }
  344. }
  345. else
  346. {
  347.   if((bemfsample<zcthreshold)||(zccounter>maxstep) )
  348.   {
  349.   if(zcfound) step = zccounter;
  350.   commthreshold = (step*fallingdelay)>>8;
  351.   zccounter=0;
  352.   commcounter=0;
  353.   startstate=150;
  354.   risingedge = 255;
  355.   zcfound=255;
  356.   autostep=0;
  357.   }
  358. }
  359. break;

  360. case 150:  // wait out commutation delay (nominal 1/2 step time)
  361. TIM1->CCR1= runningdc;
  362. TIM1->CCR2= runningdc;
  363. TIM1->CCR3= runningdc;
  364. commcounter++;
  365.   if(commcounter>commthreshold)
  366.   {
  367.   phase++; // commutate
  368.   if(phase>5) phase=0;
  369. commutate2();
  370.   demagcounter=0;
  371.   demagthreshold = (step*demagallowance)>>8;
  372.   startstate=130;  // go back to wait out demag
  373.   if(dutycyclehold) dutycyclehold--;
  374.   }
  375. break;
  376. } // end of startstate state machine






  377. ADC1->CHSELR = b2; // set ADC MUX to pot channel (ain2)
  378. ADC1->CR = b2;  // start adc conversion

  379. // switch statement determines next bemf ADC input channel
  380. switch(phase)
  381. {
  382. case 0: // ab
  383. bemfchannel= 1<<8 ; // read phase c
  384. break;
  385. case 1: // ac
  386. bemfchannel= 1<<7 ; // read phase b
  387. break;
  388. case 2:  // bc
  389. bemfchannel= 1<<6 ; // read phase a
  390. break;
  391. case 3:  // ba
  392. bemfchannel= 1<<8 ; // read phase c
  393. break;
  394. case 4:  // ca
  395. bemfchannel= 1<<7 ; // read phase b
  396. break;
  397. case 5:  // cb
  398. bemfchannel= 1<<6 ; // read phase a
  399. break;
  400.   
  401. } // end of phase switch statement


  402. if(ontimesample)
  403. {
  404. GPIOC->MODER = 0x54000000; // C15,14,13 PP to turn on attenuator
  405. TIM1->CCR4 = 100; // move isr to halfway thry on time
  406. //zcthreshold = 620;  // for 12V bus
  407. long0 = dcbussample;
  408. long0 = long0 * 599;
  409. long0 = long0>>10;
  410. zcthreshold = long0;
  411. }
  412. else
  413. {
  414. GPIOC->MODER = 0; // turn attenuator off
  415. TIM1->CCR4 = 1100;
  416. zcthreshold = 200;  // threshold to about zero
  417. }

  418. while((ADC1->CR & b2)==b2) ; // wait for conversion to complete
  419. potvalue=  ADC1->DR;

  420. ADC1->CHSELR = b9; // set ADC MUX to dc bus channel (ain9)
  421. ADC1->CR = b2;  // start adc conversion
  422. while((ADC1->CR & b2)==b2) ; // wait for conversion to complete
  423. dcbussample =  ADC1->DR;

  424. DAC->DHR12R1 = dcbussample; // put out for diagnostic purposes


  425. //**************************************************************

  426. // clear interrupt
  427. TIM1->SR &= ~0x01F;
  428. NVIC_ClearPendingIRQ(14);
  429. }





  430. void  motorstartinit(void)
  431. {
  432. TIM1->CCER = 0;

  433. // b12 to enable brk input
  434. // b13 for break polarity
  435. // (b15+b11);  //  set MOE

  436. TIM1->BDTR= b15+b11+b10+b12+b13;  //  set MOE


  437. // ADC count used as zero crossing threshold
  438. // may need to be set higher/lower for higher/lower bemf motors
  439. // setting too low may cause noise sensitivity issues
  440. zcthreshold = 200 ;


  441. ontimesample=0;
  442. phase = 0;
  443. holdcounter=0;
  444. startstate=0;
  445. commcounter=0;
  446. step=3670; // unit is 50uS pwm periods
  447. autostep = 0;
  448. risingedge = 0;
  449. zcfound=0;
  450. alignmentcounter=0;
  451. rampspeed=1;
  452. commthreshold=0;

  453. // risingdelay and fallingdelay control the time delay waited
  454. // between zero crossing detection and commutation.  The nominal
  455. // value is 128 which means 128/256 or 1/2 of a step time, which
  456. // is the phase shift between line to neutral zero crossing
  457. // (what is actually detected) and line to line zero cross
  458. // (nominal correct commutation time)
  459. // adjustment will advance or delay commutation (phase advance)
  460. risingdelay=128;
  461. fallingdelay=128;

  462. // sets open loop motor voltage (speed) 1200 for 100% duty cycle
  463. runningdc = 600;

  464. dutycyclehold=100;

  465. } // end of motor start init function



  466. void delay( unsigned long time)  // 1000 is 200 usec
  467. {
  468. while(time>0) time--;
  469. }



  470. void selftest(void)
  471. {

  472. unsigned short word0;


  473. // port B setup
  474. GPIOB->ODR = 0;
  475. /*
  476. 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  477. pp pp pp pp in pp pp pp pp pp pp pp pp pp an an
  478. 01 01 01 01 00 01 01 01 01 01 01 01 01 01 11 11
  479.   5     5     1     5     5     5     5     F
  480. */
  481. GPIOB->MODER = 0x5515555F;


  482. //test for jumper from P6-2(pb10) to P6-1(pb11)and return from selftest if not
  483. GPIOB->ODR &= ~b10;
  484. delay(1000);
  485. if((GPIOB->IDR & b11) != 0) return;

  486. GPIOB->ODR |= b10;
  487. delay(1000);
  488. if((GPIOB->IDR & b11) != b11) return;

  489. GPIOB->ODR  &= ~b10;
  490. delay(1000);
  491. if((GPIOB->IDR & b11) != 0) return;

  492. GPIOB->ODR  |= b10;
  493. delay(1000);
  494. if((GPIOB->IDR & b11) != b11) return;

  495. // program is in test mode

  496. // port A setup
  497. GPIOA->ODR = 0;
  498. /*
  499. 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  500. in af af in pp pp pp pp an an an an an an an an
  501. 00 10 10 00 01 01 01 01 11 11 11 11 11 11 11 11
  502.   2     8     5     5     F     F     F     F
  503. */
  504. GPIOA->MODER = 0x2855FFFF;


  505. // comparator setup
  506. COMP->CSR = 0;
  507. #ifdef currentlimitenable
  508. COMP->CSR = COMP->CSR + b27+b16+b22+b20+b25+b24;  // cy-by-cy active
  509. #endif
  510. #ifdef overcurrentenable
  511. COMP->CSR = COMP->CSR + b0+b6+b5+b11+b8; //  brk active
  512. #endif

  513. // ADC setup
  514. ADC1->CR= b31 ; // start ADC calibration
  515. while( ADC1->CR & b31);  // wait for calibration to complete
  516. while( ADC1->CR & b31);  // wait for calibration to complete
  517. ADC1->CR = 1; // enable ADC
  518. ADC1->CFGR1 = b16; // enable discontinuous mode
  519. ADC1->SMPR = 1;


  520. // routine to manually test pot and LED
  521. while(1)
  522. {

  523. potvalue = 4095 - readadc(2);

  524. switch((potvalue>>9))
  525. {
  526. case 0:
  527. ledoff;
  528. break;
  529. case 1:
  530. ledon;
  531. break;
  532. case 2:
  533. ledoff;
  534. break;
  535. case 3:
  536. ledon;
  537. break;
  538. case 4:
  539. ledoff;
  540. break;
  541. case 5:
  542. ledon;
  543. break;
  544. case 6:
  545. ledoff;
  546. break;
  547. case 7:
  548. ledon;
  549. break;
  550. case 8:
  551. break;
  552. }  // end of pot test case statement

  553. //test for jumper from P6-2(pb10) to P6-1(pb11)and break out of loop if not
  554. GPIOB->ODR &= ~b10;
  555. delay(1000);
  556. if((GPIOB->IDR & b11) != 0) break;

  557. GPIOB->ODR |= b10;
  558. delay(1000);
  559. if((GPIOB->IDR & b11) != b11) break;

  560. GPIOB->ODR  &= ~b10;
  561. delay(1000);
  562. if((GPIOB->IDR & b11) != 0) break;

  563. GPIOB->ODR  |= b10;
  564. delay(1000);
  565. if((GPIOB->IDR & b11) != b11) break;

  566. } // end of pot test endless loop

  567. ledoff;
  568. delay(19999999);

  569. // now test DC bus sensing
  570. word0 = readadc(9); // read bemfa
  571. if(word0<1430) flashled(11);
  572. if(word0>1748) flashled(12);

  573. // now test bridge outputs and bemf sensing
  574. phaseadisable;
  575. phasebdisable;
  576. phasecdisable;

  577. phaseahigh;
  578. phaseblow;
  579. phaseclow;

  580. // enable A and set high.  disable B and C
  581. phaseaenable;
  582. delay(500);  // wait 100 usec for settling

  583. word0 = readadc(6); // read bemfa
  584. if(word0<2048) flashled(13);  // fault if pha not high

  585. word0 = readadc(7); // read bemfb
  586. if(word0<2048) flashled(14);  // fault if phb not high

  587. word0 = readadc(8); // read bemfc
  588. if(word0<2048) flashled(15);  // fault if phc not high

  589. // now enable B low
  590. phasebenable;
  591. delay(500);  // wait 100 usec for settling
  592. word0 = readadc(7); // read bemfb
  593. phasebdisable;
  594. if(word0>2048) flashled(21);  // fault if phb not low

  595. // now enable C low
  596. phasecenable;
  597. delay(500);  // wait 100 usec for settling
  598. word0 = readadc(8); // read bemfc
  599. phasecdisable;
  600. if(word0>2048) flashled(22);  // fault if phc not low

  601. phaseadisable;
  602. phasebenable;
  603. phasebhigh;

  604. // B is high, A and C disabled
  605. delay(500);  // wait 100 usec for settling
  606. word0 = readadc(6); // read bemfa
  607. if(word0<2048) flashled(23);  // fault if pha not high

  608. // disable verified for all three phases

  609. phaseadisable;
  610. phasebdisable;
  611. phasecdisable;

  612. // activate phase AB
  613. phaseahigh;
  614. phaseblow;
  615. phaseaenable;
  616. phasebenable;
  617. delay(500);  // wait 100 usec for settling
  618. word0 = readadc(6); // read bemfa
  619. if(word0<2048) flashled(24);  // fault if pha not high
  620. word0 = readadc(7); // read bemfb
  621. if(word0>2048) flashled(24);  // fault if phb not low

  622. // activate phase BA
  623. phasebhigh;
  624. phasealow;
  625. delay(500);  // wait 100 usec for settling
  626. word0 = readadc(6); // read bemfa
  627. if(word0>2048) flashled(25);  // fault if pha not low
  628. word0 = readadc(7); // read bemfb
  629. if(word0<2048) flashled(25);  // fault if phb not high

  630. // activate phase AC
  631. phasebdisable;
  632. phaseahigh;
  633. phaseclow;
  634. phasecenable;
  635. delay(500);  // wait 100 usec for settling
  636. word0 = readadc(6); // read bemfa
  637. if(word0<2048) flashled(31);  // fault if pha not high
  638. word0 = readadc(8); // read bemfc
  639. if(word0>2048) flashled(31);  // fault if phc not low

  640. // activate phase CA
  641. phasechigh;
  642. phasealow;
  643. delay(500);  // wait 100 usec for settling
  644. word0 = readadc(6); // read bemfa
  645. if(word0>2048) flashled(32);  // fault if pha not low
  646. word0 = readadc(8); // read bemfc
  647. if(word0<2048) flashled(32);  // fault if phc not high

  648. // activate phase BC
  649. phaseadisable;
  650. phasebenable;
  651. phasebhigh;
  652. phaseclow;
  653. delay(500);  // wait 100 usec for settling
  654. word0 = readadc(7); // read bemfb
  655. if(word0<2048) flashled(33);  // fault if phb not high
  656. word0 = readadc(8); // read bemfc
  657. if(word0>2048) flashled(33);  // fault if phc not low

  658. // activate phase CB
  659. phasechigh;
  660. phaseblow;
  661. delay(500);  // wait 100 usec for settling
  662. word0 = readadc(7); // read bemfb
  663. if(word0>2048) flashled(34);  // fault if phb not low
  664. word0 = readadc(8); // read bemfc
  665. if(word0<2048) flashled(34);  // fault if phc not high

  666. // all 6 phases have been tested (voltage sensing)

  667. phaseadisable;
  668. phasebdisable;
  669. phasecdisable;

  670. // activate phase AB  (should not trip)
  671. phaseahigh;
  672. phaseblow;
  673. phaseaenable;
  674. phasebenable;
  675. delay(500);  // wait 100 usec for settling
  676. if(COMP->CSR & b30) flashled(35); // comp2 at 2.24A threshold

  677. // now turn on C lower (should trip)
  678. phaseclow;
  679. phasecenable;
  680. delay(500);  // wait 100 usec for settling
  681. if((COMP->CSR & b30)==0) flashled(41); // comp2 at 2.24A threshold

  682. // overcurrent (higher threshold) should be low
  683. if((COMP->CSR & b14)) flashled(42); // comp1  high threshold


  684. phaseadisable;
  685. phasebdisable;
  686. phasecdisable;
  687. ledon;

  688. while(1)
  689. {
  690. word0=0;
  691. // break out of loop when jumper restored
  692. GPIOB->ODR &= ~b10;
  693. delay(1000);
  694. if((GPIOB->IDR & b11) != 0) word0=0;
  695. else word0++;
  696. GPIOB->ODR |= b10;
  697. delay(1000);
  698. if((GPIOB->IDR & b11) != b11) word0=0 ;
  699. else word0++;
  700. GPIOB->ODR  &= ~b10;
  701. delay(1000);
  702. if((GPIOB->IDR & b11) != 0) word0=0 ;
  703. ……………………

  704. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:

en.DM00072008.pdf

946.49 KB, 下载次数: 37, 下载积分: 黑币 -5

en.DM00073153.pdf

159.14 KB, 下载次数: 40, 下载积分: 黑币 -5

说明材料

en.steval-ihm043v1_bom.pdf

10.47 KB, 下载次数: 37, 下载积分: 黑币 -5

器件清单

en.steval-ihm043v1_fw.zip

448.24 KB, 下载次数: 66, 下载积分: 黑币 -5

代码

en.steval-ihm043v1_schematic.pdf

83.58 KB, 下载次数: 41, 下载积分: 黑币 -5

原理图

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表