TRISC2 = 0 // configure RC2 pin as output (PWM signal output)ĬCP1CON = 0x0C // configure CCP1 module as PWM with single output & clear duty cycle 2 LSBs ADC clock derived from a dedicated internal oscillator,ĪDCON1 = 0x80 // right justified, VSS & VDD references NRBPU = 0 // clear RBPU bit (OPTION_REG.7) TRISD = 0 // configure all PORTD pins as outputs 1) MPLAB XC8 compiler: This is a free compiler available for Windows, Linux. Below is a list of the most common and links to download and more information. Many are commercial but they have evaluation version available for download. OSCCON = 0x70 // set internal oscillator to 8MHzĪNSELH = 0 // configure all PORTB pins as digital For AVR-C programming, there are many compilers available in the market. While (GO_nDONE = 1) // wait for conversion complete GO_nDONE = 1 // start an A/D conversion cycle
* This is a free software with NO WARRANTY. * DC Motor speed and direction of rotation control with PIC16F887 MCU. PWM frequency and resolution can be calculated using the functions below (Fosc = 8 MHz): PIC16F887 Timer2 module is configured to generate a PWM signal with frequency of 1.953 KHz and resolution of 10 bits (prescaler = 4 and preload value = 255). The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05. In this project the PIC16F887 microcontroller uses its internal oscillator 8 MHz, MCLR pin is configured as an input pin.ĭC Motor speed and direction control with PIC16F887 MCU and L293D code: To answer the question in comment, the compiler appears to be ANSI, with the sole documented exception (a huge one): 'Due to limited memory and no hardware implementation of a data stack, recursion is not supported and functions are not reentrant.' - MPLAB XC8 C Compiler User's Guide 5.2.1 I found the guide here. IN1 and IN2 pins are connected to RD0 and RD1 respectively (they can be reversed). The PIC16F887 generates a PWM signal on pin RC2 (#17) using CCP1 module (CCP: Capture/Compare/PWM), this pin is connected to EN1,2 pin of the L293D chip. IN1 and IN2 pins are the control pins where: IN1
The L293D driver has 2 VCCs: VCC1 is +5V and VCC2 is +12V (same as motor nominal voltage). If the button is pressed the motor will change its direction of rotation directly. The speed of the DC motor (both directions) is controlled with the 10k potentiometer which is connected to AN0 pin of the PIC16F887 (#2) and the direction of rotation is controlled with the push button which is connected to RB1 pin (#34). The circuit is supplied with two sources, one with voltage of 5V which supplies the PIC16F887 microcontroller and the L293D driver IC (VCC1), and the second source with voltage of 12V which supplies the L293D chip (VCC2) and therefore the DC motor.
(All grounded terminals are connected together)