Advertisement
cr88192

Simple 2/3 Phase Inverter

Aug 25th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.56 KB | None | 0 0
  1. /*
  2.  * P1.0: L1  / L1A
  3.  * P1.1: L2  / L1B
  4.  * P1.2: L3  / L2A
  5.  * P1.3: Com / L2B
  6.  * P1.4: Speed Pot
  7.  * P1.5: Speed Duty
  8.  * P1.6: Voltage Sense
  9.  * P1.7: Voltage Ref
  10.  *
  11.  * P2.0: Voltage Control
  12.  * P2.1: Out1
  13.  * P2.2: Direction
  14.  * P2.3: Sin/Square
  15.  * P2.4: 3/2 Phase
  16.  *
  17.  * Clock: 32kHz.
  18.  * Phase_Div=0, Out=1kHz
  19.  * Phase_Div=16, Out=62.5Hz
  20.  * Phase_Div=1000, Out=1Hz
  21.  */
  22.  
  23. #include <msp430.h>
  24.  
  25. typedef unsigned char byte;
  26.  
  27. const byte sintab1_2[64]= {
  28.         128, 167, 184, 196, 206, 215, 223, 229,
  29.         235, 240, 244, 247, 250, 252, 254, 255,
  30.         255, 255, 254, 252, 250, 247, 244, 240,
  31.         235, 229, 223, 215, 206, 196, 184, 167,
  32.         128,  88,  71,  59,  49,  40,  32,  26,
  33.          20,  15,  11,   8,   5,   3,   1,   0,
  34.           0,   0,   1,   3,   5,   8,  11,  15,
  35.          20,  26,  32,  40,  49,  59,  71,  88
  36. };
  37.  
  38. short phase_cnt;
  39. short phase_div;
  40. short phase_pot;
  41. byte phase;
  42. byte phofs[4];
  43. byte phcnt[4];
  44. byte phduty[4];
  45. byte dutysc;
  46.  
  47. byte phcnt2[2];
  48. byte phduty2[2];
  49. byte dacm;
  50.  
  51. byte vcsense;
  52. byte vcref;
  53. byte vcphase;
  54.  
  55. byte (*func)(byte ph);
  56. void (*updatePhase)(byte ph);
  57. void (*updatePhaseB)(byte ph);
  58.  
  59.  
  60. byte fix_sin8(byte ph)
  61. {
  62.     return(sintab1_2[(byte)(ph>>2)]);
  63. }
  64.  
  65. byte fix_cos8(byte ph)
  66. {
  67.     return(sintab1_2[(byte)((ph+64)>>2)]);
  68. }
  69.  
  70. byte fix_sqr8(byte ph)
  71. {
  72.     return((byte)(((signed char)ph)>>7));
  73. }
  74.  
  75. #if 1
  76. void updatePhase1(byte ph)
  77. {
  78.     int ds;
  79.     ds=func(phase+phofs[ph]);
  80.     ds=(((ds-128)*dutysc)>>8)+128;
  81.     phduty[ph]=ds;
  82. }
  83.  
  84. void updatePhase1C(byte ph)
  85. {
  86.     phduty[ph]=128;
  87. }
  88.  
  89. void updatePhase2(byte ph)
  90. {
  91.     int ds;
  92.     ds=func(phase+phofs[ph]);
  93.     ds=(ds<<1)-256;
  94.     if(ph&1)ds=-ds;
  95.     if(ds<0)ds=0;
  96.     ds=(((unsigned int)ds)*dutysc)>>8;
  97.     phduty[ph]=ds;
  98. }
  99. #endif
  100.  
  101. void updateVreg(void)
  102. {
  103.     int i;
  104.     if(vcsense<vcref)
  105.     {
  106.         i=phduty2[0]-1;
  107.         if(i<16)i=16;
  108.         phduty2[0]=i;
  109.     }
  110.  
  111.     if(vcsense>vcref)
  112.     {
  113.         i=phduty2[0]+1;
  114.         if(i>240)i=240;
  115.         phduty2[0]=i;
  116.     }
  117. }
  118.  
  119. void updatePin1(byte ph)
  120. {
  121.     int i;
  122. //  i=phcnt[ph]+sintab[(byte)(phase+phofs[ph])];
  123.     i=phcnt[ph]+phduty[ph];
  124.     phcnt[ph]=i;
  125.     if(i&0x100)
  126.         { P1OUT|=1<<ph; }
  127.     else
  128.         { P1OUT&=~(1<<ph); }
  129. }
  130.  
  131. void updatePin2(byte ph)
  132. {
  133.     int i;
  134.     i=phcnt2[ph]+phduty2[ph];
  135.     phcnt2[ph]=i;
  136.     if(i&0x100)
  137.         { P2OUT|=1<<ph; }
  138.     else
  139.         { P2OUT&=~(1<<ph); }
  140. }
  141.  
  142. void clearPhases(void)
  143. {
  144.     P1OUT&=~15;
  145. }
  146.  
  147. byte st, lst, nph;
  148.  
  149. struct phaseMode_s {
  150.     byte phofs[4];
  151.     byte (*func)(byte ph);
  152.     void (*updatePhase)(byte ph);
  153.     void (*updatePhaseB)(byte ph);
  154.     byte nph;
  155. };
  156.  
  157. const struct phaseMode_s phaseMode[]={
  158. {   0, 85, 171, 0, fix_sin8,
  159.     updatePhase1, updatePhase1C, 3},
  160. {   171, 85, 0, 0, fix_sin8,
  161.     updatePhase1, updatePhase1C, 3},
  162. {   0, 85, 171, 0, fix_sqr8,
  163.     updatePhase1, updatePhase1C, 3},
  164. {   171, 85, 0, 0, fix_sqr8,
  165.     updatePhase1, updatePhase1C, 3},
  166. {   0, 0, 64, 64, fix_sin8,
  167.     updatePhase2, updatePhase2, 4},
  168. {   64, 64, 0,  0, fix_sin8,
  169.     updatePhase2, updatePhase2, 4}
  170. };
  171.  
  172. void setPhaseMode(byte mode)
  173. {
  174.     struct phaseMode_s *phm;
  175.  
  176.     st=mode;
  177.     lst=st;
  178.     phm=(struct phaseMode_s *)(&(phaseMode[st]));
  179.  
  180.     phofs[0]=phm->phofs[0];
  181.     phofs[1]=phm->phofs[1];
  182.     phofs[2]=phm->phofs[2];
  183.     phofs[3]=phm->phofs[3];
  184.     func=phm->func;
  185.     updatePhase=phm->updatePhase;
  186.     updatePhaseB=phm->updatePhaseB;
  187.     nph=phm->nph;
  188. }
  189.  
  190. void updateMainPhase(void)
  191. {
  192.     int i;
  193.  
  194.     phase=phase+8;
  195. //  phase+=8;
  196. //          phase+=16;
  197. //          phase+=4;
  198. //          phase++;
  199.     phase_cnt=phase_div;
  200.  
  201.     updatePhase(0);
  202.     updatePhase(1);
  203.     updatePhase(2);
  204.     updatePhaseB(3);
  205.  
  206.     i=P2IN;
  207.     st=(i>>2)&7;
  208.     if(st!=lst)
  209.     {
  210.         lst=st;
  211.         setPhaseMode(st);
  212.     }
  213. }
  214.  
  215. void updateMainPhase2(void)
  216. {
  217.     if(!(vcphase++))
  218.     {
  219.         updateVreg();
  220.     }
  221.  
  222.     if(phase_cnt<=0)
  223.     {
  224. #if 1
  225.         if(phase_div>1012)
  226.         {
  227.             clearPhases();
  228.             while(phase_div>1012)
  229.                 __bis_SR_register(CPUOFF + GIE);
  230.             return;
  231.         }
  232. #endif
  233.  
  234.         updateMainPhase();
  235.     }
  236. }
  237.  
  238. int main(void)
  239. {
  240.     BCSCTL1 = CALBC1_16MHZ;
  241.     DCOCTL = CALDCO_16MHZ;
  242.  
  243.     WDTCTL = WDT_MDLY_0_5;      //0.5ms at 1MHz (2kHz), 0.03125ms (32kHz) at 16MHz
  244.     IE1 |= WDTIE;
  245.  
  246.     P1DIR|=0x0F;
  247.     P2DIR|=0x03;
  248.     P1OUT=0;
  249.     P2OUT=0;
  250.  
  251.     P2REN|=0x1C;
  252.  
  253.     ADC10CTL0 = SREF_1 + ADC10SHT_2 + ADC10ON + ADC10IE + REF2_5V + REFON;
  254. //  ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE + REF2_5V + REFON;
  255.     ADC10CTL1 = INCH_4;
  256.     ADC10AE0 |= 0x10;
  257.  
  258.     phase_div=16;
  259.  
  260.     setPhaseMode(0);
  261.  
  262.     dutysc=255;
  263.     dacm=0;
  264.     ADC10CTL0 |= ENC + ADC10SC;
  265.  
  266.     __bis_SR_register(GIE);
  267.  
  268.     for(;;)
  269.     {
  270.         updatePin1(0);
  271.         updatePin1(1);
  272.         updatePin1(2);
  273.         updatePin1(3);
  274.  
  275.         updatePin2(0);
  276.         updatePin2(1);
  277.  
  278.         updateMainPhase2();
  279.     }
  280. }
  281.  
  282.  
  283. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  284. #pragma vector=ADC10_VECTOR
  285. __interrupt void ADC10_ISR(void)
  286. #elif defined(__GNUC__)
  287. void __attribute__ ((interrupt(ADC10_VECTOR))) ADC10_ISR (void)
  288. #else
  289. #error Compiler not supported!
  290. #endif
  291. {
  292.     unsigned long i;
  293.     __bic_SR_register_on_exit(CPUOFF);
  294.  
  295. #if 1
  296.     switch(dacm)
  297.     {
  298.     case 0:
  299.         phase_pot=ADC10MEM;
  300.         i=1023-phase_pot;
  301.         i=(i*i+512)>>10;
  302. //      i=(i*i*i)>>20;
  303.         phase_div=i;
  304.         dacm=1;
  305.         break;
  306.     case 1:
  307. //      dutysc=ADC10MEM>>2;
  308.         dacm=2;
  309.         break;
  310.     case 2:
  311.         vcsense=ADC10MEM>>2;
  312.         dacm=3;
  313.         break;
  314.     case 3:
  315.         vcref=ADC10MEM>>2;
  316.         dacm=0;
  317.         break;
  318.     default:
  319.         dacm=0;
  320.         break;
  321.     }
  322. #endif
  323.  
  324.     ADC10CTL1 = (ADC10CTL1&0x0FFF)|((4+dacm)<<12);
  325.     ADC10AE0 = 0x10<<dacm;
  326.     ADC10CTL0 |= ENC + ADC10SC;
  327. }
  328.  
  329. // Watchdog Timer interrupt service routine
  330. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  331. #pragma vector=WDT_VECTOR
  332. __interrupt void watchdog_timer(void)
  333. #elif defined(__GNUC__)
  334. void __attribute__ ((interrupt(WDT_VECTOR))) watchdog_timer (void)
  335. #else
  336. #error Compiler not supported!
  337. #endif
  338. {
  339.     phase_cnt--;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement