Advertisement
tmax

Untitled

Aug 19th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.27 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////
  2. ////                           EX_PWM.C                              ////
  3. ////                                                                 ////
  4. ////  This program will show how to use the built in PIC PWM.        ////
  5. ////  The program takes an analog input and uses the digital         ////
  6. ////  value to set the duty cycle.  The frequency is set by          ////
  7. ////  the user over the RS-232.                                      ////
  8. ////                                                                 ////
  9. ////  Configure the CCS prototype card as follows:                   ////
  10. ////      Connect a scope to pin C2                                  ////
  11. ////      Connect pin A0 to output of the POT                        ////
  12. ////                                                                 ////
  13. ////  Configure the CCS PCD 30F2010 development board as follows:    ////
  14. ////      Connect a scope to pin D0                                  ////
  15. ////                                                                 ////
  16. ////  Jumpers:                                                       ////
  17. ////     PCM,PCH    pin C7 to RS232 RX, pin C6 to RS232 TX           ////
  18. ////     PCD        none                                             ////
  19. ////                                                                 ////
  20. ////  This example will work with the PCM, PCH, and PCD compilers.   ////
  21. ////  The following conditional compilation lines are used to        ////
  22. ////  include a  valid device for each compiler.  Change the device, ////
  23. ////  clock and RS232 pins for your hardware if needed.              ////
  24. /////////////////////////////////////////////////////////////////////////
  25. ////        (C) Copyright 1996,2007 Custom Computer Services         ////
  26. //// This source code may only be used by licensed users of the CCS  ////
  27. //// C compiler.  This source code may only be distributed to other  ////
  28. //// licensed users of the CCS C compiler.  No other use,            ////
  29. //// reproduction or distribution is permitted without written       ////
  30. //// permission.  Derivative programs created using this software    ////
  31. //// in object code form are not restricted in any way.              ////
  32. /////////////////////////////////////////////////////////////////////////
  33.  
  34.  
  35. #if defined(__PCM__)
  36. #include <16F877.h>
  37. #fuses HS,NOWDT,NOPROTECT,NOLVP
  38. #use delay(clock=10000000)
  39. #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)
  40.  
  41. #elif defined(__PCH__)
  42. #include <18F452.h>
  43. #fuses HS,NOWDT,NOPROTECT,NOLVP
  44. #use delay(clock=10000000)
  45. #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)
  46.  
  47. #elif defined(__PCD__)
  48. #include <30F2010.h>
  49. #fuses HS,NOWDT
  50. #device ADC=10
  51. #use delay(clock=20000000)
  52. #use rs232(baud=9600, UART1A)
  53. #endif
  54.  
  55. #if !defined(__PCD__)
  56.  
  57. void main(void)
  58. {
  59.    
  60.    /* PWM EXAMPLE FOR 8-BIT PICS */
  61.    
  62.    char selection;
  63.    unsigned int8 value;
  64.  
  65.    printf("\r\nFrequency:\r\n");
  66.    printf("    1) 19.5 khz\r\n");
  67.    printf("    2) 4.9 khz\r\n");
  68.    printf("    3) 1.2 khz\r\n");
  69.  
  70.    do {
  71.      selection=getc();
  72.    } while((selection<'1')||(selection>'3'));
  73.  
  74.    setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
  75.  
  76.           //   The cycle time will be (1/clock)*4*t2div*(period+1)
  77.           //   In this program clock=10000000 and period=127 (below)
  78.           //   For the three possible selections the cycle time is:
  79.           //     (1/10000000)*4*1*128 =  51.2 us or 19.5 khz
  80.           //     (1/10000000)*4*4*128 = 204.8 us or 4.9 khz
  81.           //     (1/10000000)*4*16*128= 819.2 us or 1.2 khz
  82.  
  83.    switch(selection) {
  84.      case '1' : setup_timer_2(T2_DIV_BY_1, 127, 1);
  85.                 break;
  86.      case '2' : setup_timer_2(T2_DIV_BY_4, 127, 1);
  87.                 break;
  88.      case '3' : setup_timer_2(T2_DIV_BY_16, 127, 1);
  89.                 break;
  90.    }
  91.  
  92.   setup_port_a(ALL_ANALOG);
  93.   setup_adc(adc_clock_internal);
  94.   set_adc_channel( 0 );
  95.   printf("%c\r\n",selection);
  96.  
  97.   while( TRUE ) {
  98.     value=read_adc();
  99.  
  100.     printf("%2X\r",value);
  101.  
  102.     set_pwm1_duty(value);          // This sets the time the pulse is
  103.                                    // high each cycle.  We use the A/D
  104.                                    // input to make a easy demo.
  105.                                    // the high time will be:
  106.                                    //  if value is LONG INT:
  107.                                    //    value*(1/clock)*t2div
  108.                                    //  if value is INT:
  109. //    value*4*(1/clock)*t2div
  110.                                    // for example a value of 30 and t2div
  111.                                    // of 1 the high time is 12us
  112.                                    // WARNING:  A value too high or low will
  113.                                    //           prevent the output from
  114.                                    //           changing.
  115.   }
  116.  
  117. }
  118. #else
  119.  
  120. void main(void)
  121. {
  122.  
  123.    /* PWM EXAMPLE FOR 16-BIT PICS */
  124.    
  125.    char selection;
  126.    unsigned int16 value;
  127.  
  128.    printf("\r\nFrequency:\r\n");
  129.    printf("    1) 76.37 hz\r\n");
  130.    printf("    2) 9.55  hz\r\n");
  131.    printf("    3) 1.19  hz\r\n");
  132.    
  133.    do {
  134.      selection=getc();
  135.    } while((selection<'1')||(selection>'3'));
  136.  
  137.           //   The cycle time will be (1/clock)*4*t2div*(period+1)
  138.           //   In this program clock=20000000 and period=127 (below)
  139.           //   For the three possible selections the cycle time is:
  140.           //     (1/20000000)*4*1*65473  =  13.1 ms or 76.37 hz
  141.           //     (1/20000000)*4*8*65473  = 104.7 ms or 9.55  hz
  142.           //     (1/20000000)*4*64*65473 = 838.1 ms or 1.19  hz
  143.  
  144.    switch(selection) {
  145.      case '1' : setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 0xFFC0);
  146.                 break;
  147.      case '2' : setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8, 0xFFC0);
  148.                 break;
  149.      case '3' : setup_timer2(TMR_INTERNAL | TMR_DIV_BY_64, 0xFFC0);
  150.                 break;
  151.    }
  152.  
  153.   setup_adc_ports(ALL_ANALOG);
  154.   setup_adc(adc_clock_internal);
  155.   set_adc_channel(0);
  156.   printf("%c\r\n",selection);
  157.  
  158.   setup_compare(1, COMPARE_PWM | COMPARE_TIMER2);
  159.  
  160.   while(TRUE)
  161.   {
  162.     value=read_adc();
  163.  
  164.     printf("%LX\r",value);
  165.    
  166.     /* set the duty cycle using the adc reading.  the adc reading is in 10 bits,
  167.       the duty cylce is in 16 bits, multiplying by 64 brings adc up to 16 bits */
  168.     set_pwm_duty(1,value * (int16)64); // This sets the time the pulse is
  169.                                        // high each cycle.  We use the A/D
  170.                                        // input to make a easy demo.
  171.                                        // the high time will be:
  172.                                        // value is int16, clock = 20MHz, t2div = 1
  173.                                        // This will give period of 13.1 ms
  174.                                        // For example if value = 200, then
  175.                                        //  value*64*4*(1/clock)*t2div = 2.56 ms
  176.                                        // This will give an duty cycle of about 20 %
  177.                                        // WARNING:  A value too high or low will
  178.                                        //           prevent the output from
  179.                                        //           changing.
  180.                                    
  181.   }
  182.  
  183. }
  184. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement