Control de Un Motor DC
Control de Un Motor DC
Control de Un Motor DC
Dc motor control with PIC18F4550 and L293D CCS C code
2
3 #include <18F4550.h>
4 #device ADC = 10
5 #fuses NOMCLR INTRC_IO
6 #use delay(clock = 8000000)
7 #use fast_io(B)
8 #use fast_io(C)
9
10 unsigned int16 i ;
11 void main(){
12 setup_oscillator(OSC_8MHZ); // Set internal oscillator to 8MHz
13 output_b(0); // PORTB initial state
14 set_tris_b(7); // Configure RB0, RB1 & RB2 as inputs
15 output_c(0); // PORTC initial state
16 set_tris_c(0); // Configure PORTC pins as outputs
17 output_d(0); // PORTD initial state
18 set_tris_d(0); // Configure PORTD pins as outputs
19 setup_adc(ADC_CLOCK_DIV_8); // Set ADC conversion time to 8Tosc
20 setup_adc_ports(AN0); // Configure AN0 as analog input
21 set_adc_channel(0); // Select channel AN0
22 setup_timer_2(T2_DIV_BY_16, 255, 1); // Set PWM frequency to 500Hz
23 delay_ms(100); // Wait 100ms
24 while(TRUE){
25 i = read_adc(); // Read from AN0 and store in i
26 if(input(PIN_B3) == 1) // If direction 1 is selected
27 set_pwm1_duty(i); // Set pwm1 duty cycle
28 if(input(PIN_B4) == 1) // If direction 2 is selected
29 set_pwm2_duty(i); // Set pwm2 duty cycle
30 delay_ms(10); // Wait 10ms
31 if(input(PIN_B0) == 0){ // If RB0 button pressed
32 if(input(PIN_B3) == 0){ // If direction 1 not already selected
33 output_b(0); // Both LEDs OFF
34 setup_ccp1(CCP_OFF); // CCP1 OFF
35 setup_ccp2(CCP_OFF); // CCP2 OFF
36 output_c(0); // PORTC pins low
37 delay_ms(100); // Wait 100ms
38 setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
39 output_high(PIN_B3); // RB3 LED ON
40 }}
41 if(input(PIN_B1) == 0){ // If RB1 button pressed
42 if(input(PIN_B4) == 0){ // If direction 2 not already selected
43 output_b(0); // Both LEDs OFF
44 setup_ccp1(CCP_OFF); // CCP1 OFF
45 setup_ccp2(CCP_OFF); // CCP2 OFF
46 output_c(0); // PORTC pins low
47 delay_ms(100); // Wait 100ms
48 setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM
49 output_high(PIN_B4); // RB4 LED ON
50 }}
51 if(input(PIN_B2) == 0){ // If RB2 button pressed
52 setup_ccp1(CCP_OFF); // CCP1 OFF
53 setup_ccp2(CCP_OFF); // CCP2 OFF
54 output_c(0); // PORTC pins low
55 output_b(0);} // Both LEDs OFF
56 }
57 }