pic16f887 controlling pwm

Discussion in 'Embedded Systems and Microcontrollers' started by Tamim Neak, Mar 8, 2015.

  1. Tamim Neak

    Thread Starter New Member

    Feb 23, 2015
    13
    0
    hi all i have the following code
    Code (Text):
    1. const unsigned char sin_table[576]={
    2. 0, 6, 12, 18, 24, 30, 36, 42, 48, 53, 59, 64, 69, 74, 79, 84, 88, 93, 97, 100, 104, 107, 110, 113, 115, 118, 120, 121, 123, 124, 124, 125, 125, 125, 124, 124, 123, 121, 120, 118, 115, 113, 110, 107, 104, 100, 97, 93, 88, 84, 79, 74, 69, 64, 59, 53, 48, 42, 36, 30, 24, 18, 12, 6,
    3. //0, 6, 11, 17, 23, 28, 34, 39, 44, 50, 55, 60, 64, 69, 74, 78, 82, 86, 90, 93, 96, 99, 102, 105, 107, 109, 111, 113, 114, 115, 115, 116, 116, 116, 115, 115, 114, 113, 111, 109, 107, 105, 102, 99, 96, 93, 90, 86, 82, 78, 74, 69, 64, 60, 55, 50, 44, 39, 34, 28, 23, 17, 11, 6, //92.8%
    4.  
    5. 0, 5, 11, 16, 22, 27, 32, 37, 42, 47, 52, 57, 62, 66, 70, 75, 78, 82, 86, 89, 92, 95, 98, 100, 103, 105, 106, 108, 109, 110, 110, 111, 111, 111, 110, 110, 109, 108, 106, 105, 103, 100, 98, 95, 92, 89, 86, 82, 78, 75, 70, 66, 62, 57, 52, 47, 42, 37, 32, 27, 22, 16, 11, 5, //88.8%
    6.  
    7. 0, 5, 10, 16, 21, 26, 31, 36, 41, 45, 50, 54, 59, 63, 67, 71, 75, 79, 82, 85, 88, 91, 93, 96, 98, 100, 101, 103, 104, 105, 105, 106, 106, 106, 105, 105, 104, 103, 101, 100, 98, 96, 93, 91, 88, 85, 82, 79, 75, 71, 67, 63, 59, 54, 50, 45, 41, 36, 31, 26, 21, 16, 10, 5, //84.8%
    8.  
    9. 0, 5, 10, 15, 20, 25, 30, 34, 39, 44, 48, 52, 57, 61, 65, 68, 72, 76, 79, 82, 85, 87, 90, 92, 94, 96, 98, 99, 100, 101, 102, 102, 102, 102, 102, 101, 100, 99, 98, 96, 94, 92, 90, 87, 85, 82, 79, 76, 72, 68, 65, 61, 57, 52, 48, 44, 39, 34, 30, 25, 20, 15, 10, 5, //81.6%
    10.  
    11. 0, 5, 10, 14, 19, 24, 28, 33, 38, 42, 46, 50, 54, 58, 62, 66, 69, 73, 76, 79, 81, 84, 86, 89, 91, 92, 94, 95, 96, 97, 98, 98, 98, 98, 98, 97, 96, 95, 94, 92, 91, 89, 86, 84, 81, 79, 76, 73, 69, 66, 62, 58, 54, 50, 46, 42, 38, 33, 28, 24, 19, 14, 10, 5, //78.4%
    12.  
    13. 0, 5, 9, 14, 18, 23, 27, 32, 36, 40, 44, 48, 52, 56, 60, 63, 66, 70, 73, 76, 78, 81, 83, 85, 87, 89, 90, 91, 92, 93, 94, 94, 94, 94, 94, 93, 92, 91, 90, 89, 87, 85, 83, 81, 78, 76, 73, 70, 66, 63, 60, 56, 52, 48, 44, 40, 36, 32, 27, 23, 18, 14, 9, 5, //75.2%
    14.  
    15. 0, 4, 9, 13, 18, 22, 26, 31, 35, 39, 43, 47, 51, 54, 58, 61, 64, 67, 70, 73, 76, 78, 80, 82, 84, 86, 87, 88, 89, 90, 91, 91, 91, 91, 91, 90, 89, 88, 87, 86, 84, 82, 80, 78, 76, 73, 70, 67, 64, 61, 58, 54, 51, 47, 43, 39, 35, 31, 26, 22, 18, 13, 9, 4, //72.8%
    16.  
    17. 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 41, 45, 49, 52, 56, 59, 62, 65, 68, 71, 73, 75, 78, 80, 81, 83, 84, 85, 86, 87, 88, 88, 88, 88, 88, 87, 86, 85, 84, 83, 81, 80, 78, 75, 73, 71, 68, 65, 62, 59, 56, 52, 49, 45, 41, 38, 34, 30, 26, 21, 17, 13, 9, 4, //70.4%
    18.  
    19. 0, 4, 8, 12, 17, 21, 25, 29, 33, 36, 40, 44, 47, 51, 54, 57, 60, 63, 66, 68, 71, 73, 75, 77, 79, 80, 81, 82, 83, 84, 85, 85, 85, 85, 85, 84, 83, 82, 81, 80, 79, 77, 75, 73, 71, 68, 66, 63, 60, 57, 54, 51, 47, 44, 40, 36, 33, 29, 25, 21, 17, 12, 8, 4 //68%
    20.  
    21. };
    22. unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ, adder,FB_Step;
    23. unsigned int TBL_temp;
    24. unsigned char DUTY_CYCLE;
    25. unsigned int temp;
    26. unsigned int battery_voltage;
    27.  
    28. void interrupt(){
    29.  
    30.  
    31. if (TMR2IF_bit == 1){
    32. TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
    33. if (TBL_POINTER_NEW < TBL_POINTER_OLD){
    34. CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
    35. }
    36. TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10;
    37. DUTY_CYCLE = TBL_POINTER_SHIFT + adder;
    38. CCPR1L = sin_table[DUTY_CYCLE];
    39. TBL_POINTER_OLD = TBL_POINTER_NEW;
    40. TMR2IF_bit = 0;
    41.                 }
    42. }
    43. void Run_PWM(){
    44. PORTC = 0;
    45. PORTD = 0;
    46. SET_FREQ = 1024;
    47. TBL_POINTER_SHIFT = 0;
    48. TBL_POINTER_NEW = 0;
    49. TBL_POINTER_OLD = 0;
    50. DUTY_CYCLE = 0;
    51. adder = 0;
    52. FB_Step = 0;
    53. ECCPAS =0b11000000;       //autoshutodwm on INT pin high
    54. PR2 = 0b01111100; //124
    55. CCP1CON = 0b01001100; // duty lowest bits + PWM mode
    56. TMR2IF_bit = 0;
    57. T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
    58. while (TMR2IF_bit == 0);
    59. TMR2IF_bit = 0;     // Timer2 interrupt flag
    60. TRISC = 0;
    61. TMR2IE_bit = 1;      //interrupt when TMR2 matches PR2
    62. GIE_bit = 1;      // Global interrupt enabled
    63. PEIE_bit = 1;     // peripheral interrupt enabled
    64. PWM1CON    = PWM1CON +   0b10000000  ;     //Part of ECCPAS procedure
    65. }
    66.  
    67. void main() {
    68. ANSEL = 0x0F;
    69. ANSELH = 0x00;
    70. ADCON1 = 0x80;
    71. TRISA=0b00000001;
    72.  
    73. CM1CON0 = 0; //Disable Comparator
    74. CM2CON0 = 0;
    75.  
    76. TRISC = 0;
    77. TRISD = 0;
    78. TRISB = 1;
    79. PORTC = 0;
    80. PORTD = 0;
    81. PORTB = 0;
    82. ADC_Init();
    83.  
    84. battery_voltage = ADC_read(0);
    85. FB_Step = floor((battery_voltage - 750)/35);
    86.                     adder = FB_Step << 6;           //2^6 =64
    87.  
    88. while( PORTB.F0 == 0);
    89.   Run_PWM();
    90. while(1);
    91. }
    i am trying to control the spwm based on the battery voltage. The following portion of the code gets executed only once. i want it to execute at the end of each sin cycle so that the spwm is updated based on the value of battery voltage. any help is appreciated.
    Code (Text):
    1. battery_voltage = ADC_read(0);
    2. FB_Step = floor((battery_voltage - 750)/35);
    3.                     adder = FB_Step << 6;           //2^6 =64
     
  2. DNA Robotics

    Member

    Jun 13, 2014
    123
    26
    Put a copy of that code toward the end of your Run_PWM() function?
     
Loading...