Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <htc.h>
  2.  
  3. // CONFIG
  4. #pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
  5. #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
  6. #pragma config PWRTE = ON // Power-Up Timer Enable bit (PWRT enabled)
  7. #pragma config MCLRE = ON // GP3/MCLR pin function select (GP3/MCLR pin function is MCLR)
  8. #pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
  9. #pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
  10. #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
  11.  
  12. #define _XTAL_FREQ 4000000
  13. #define servoPin GP2
  14. //#define pulseWidth int
  15.  
  16.  
  17. void servoPulse(pulseWidth) //Sends a single pulse to the servo
  18. {
  19. unsigned short Period = 20000; //total time of pulse in microseconds
  20. T1CON=0b00000001; //Configuration for Timer 1. Prescaler = 1
  21. //Send pulse for a duration of pulsewidth microseconds
  22. TMR1 = 65536 - (_XTAL_FREQ/(4*1000000))*pulseWidth;//preload timer1 with this
  23. TMR1IF = 0; //reset Timer 1 flag before proceedin
  24. servoPin = 1;
  25. while (!TMR1IF){} //When Timer1 hits 65536, this flag is set. Loop until then
  26.  
  27.  
  28. //Low voltage for the rest of the 20 ms
  29. unsigned short offTime = Period - pulseWidth;
  30. TMR1 = 65536 - (_XTAL_FREQ/(4*1000000))*offTime; //preload timer1 with this
  31. TMR1IF = 0; //reset Timer 1 flag before proceeding
  32. servoPin = 0;
  33. while (!TMR1IF){}
  34. }
  35.  
  36.  
  37. /*Main Program*/
  38. void main()
  39. {
  40. /* Setup - do this once */
  41. //ADCON1 0b00000000;
  42. ADCON0 = 0b00001101;
  43. ANSEL = 0b00001000; //Configures pin 3 of port A as Analog
  44. //GPIO3 = 1;
  45. unsigned char adNumber;
  46. unsigned short pulseWidth; //pulse width in microseconds
  47. /* Loop forever*/
  48. while(1)
  49. {
  50. GO_DONE = 1; /*Sets the GO_DONE bit in ADCON0 (bit 1)
  51. which starts the A to D conversion*/
  52. while (GO_DONE == 1); /*wait until A to D is complete, at which point
  53. GO_DONE will drop to 0*/
  54. adNumber = ADRESH; //reads the high register of the A to D result
  55. // into port C, which displays it on the 7seg
  56. pulseWidth = 1000 + (1000/250)*adNumber; //converts AtoD into a value from 1000 to 2000
  57.  
  58. servoPulse(pulseWidth);
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement