Guest User

Untitled

a guest
Jun 2nd, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. // a potentiometer will control the position of a servo motor
  2. // 2 buttons will control a DC motor connected to an H-bridge
  3. //using pic 16f886
  4.  
  5. #include <htc.h>
  6.  
  7. // CONFIG1
  8. #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
  9. #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  10. #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
  11. #pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
  12. #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
  13. #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
  14. #pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
  15. #pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
  16. #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
  17. #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
  18.  
  19. // CONFIG2
  20. #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
  21. #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
  22.  
  23. signed short PW;
  24. signed short Period = 20000;
  25. signed short OffTime;
  26.  
  27. void main(void)
  28. {
  29. TRISA = 0xFF; //PORTA pins are inputs
  30.  
  31. TRISB = 0xEF; //PORTB pins are inputs except RB4
  32.  
  33. TRISC = 0xFD; //PORTC pins are inputs except pin 1
  34.  
  35. ADCON1 = 0X00; //0b00000000
  36.  
  37. ANSEL = 0xFF; //0b11111111
  38.  
  39. ADCON0 = 0x4D; //0b01001101
  40.  
  41. GO_DONE = 1;
  42.  
  43. while (GO_DONE == 1)
  44. {}
  45.  
  46. PW = 1000 + 1000/250 * ADRESH;
  47.  
  48. OffTime = Period - PW;
  49.  
  50. RB4 = 1;
  51. RC1 = 1;
  52. while(PW >= 0)
  53.  
  54. {PW = PW - 12;}
  55.  
  56. RB4 = 0;
  57. RC1 = 0;
  58.  
  59. while (OffTime >=0)
  60. {
  61. OffTime = OffTime - 12;
  62.  
  63. }
  64. //end servo control
  65.  
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment