Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // a potentiometer will control the position of a servo motor
- // 2 buttons will control a DC motor connected to an H-bridge
- //using pic 16f886
- #include <htc.h>
- // CONFIG1
- #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
- #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
- #pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
- #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
- #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
- #pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
- #pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
- #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
- #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
- // CONFIG2
- #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
- #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
- signed short PW;
- signed short Period = 20000;
- signed short OffTime;
- void main(void)
- {
- TRISA = 0xFF; //PORTA pins are inputs
- TRISB = 0xEF; //PORTB pins are inputs except RB4
- TRISC = 0xFD; //PORTC pins are inputs except pin 1
- ADCON1 = 0X00; //0b00000000
- ANSEL = 0xFF; //0b11111111
- ADCON0 = 0x4D; //0b01001101
- GO_DONE = 1;
- while (GO_DONE == 1)
- {}
- PW = 1000 + 1000/250 * ADRESH;
- OffTime = Period - PW;
- RB4 = 1;
- RC1 = 1;
- while(PW >= 0)
- {PW = PW - 12;}
- RB4 = 0;
- RC1 = 0;
- while (OffTime >=0)
- {
- OffTime = OffTime - 12;
- }
- //end servo control
- }
Advertisement
Add Comment
Please, Sign In to add comment