CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

DC motor control with PIC18F4431 and L293D

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
EimYeu



Joined: 21 Nov 2024
Posts: 1

View user's profile Send private message

DC motor control with PIC18F4431 and L293D
PostPosted: Thu Nov 21, 2024 4:56 am     Reply with quote

I am trying to control a DC motor using the PIC18F4431 microcontroller and the L293D motor driver. The CCP1 and CCP2 pins don’t work at all. Can somebody tell me why the code doesn’t work? Thank you.
Code:
#include <18F4431.h>
#device ADC = 10
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
#use fast_io(B)
#use fast_io(C)

unsigned int16 i;

void main() {
    setup_oscillator(OSC_8MHZ);
    output_b(0);
    set_tris_b(7);
    output_c(0);
    set_tris_c(0);
    output_d(0);
    set_tris_d(0);

    setup_adc(ADC_CLOCK_DIV_8);
    setup_adc_ports(sAN0);
    set_adc_channel(0);

    setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
    set_rtcc(0);

    delay_ms(100);

    while(TRUE) {
        i = read_adc();

        if(input(PIN_B3) == 1)
            set_pwm1_duty(i);
        if(input(PIN_B4) == 1)
            set_pwm2_duty(i);

        delay_ms(10);

        if(input(PIN_B0) == 0) {
            if(input(PIN_B3) == 0) {
                output_b(0);
                setup_ccp1(CCP_OFF);
                setup_ccp2(CCP_OFF);
                output_c(0);
                delay_ms(100);
                setup_ccp1(CCP_PWM);
                output_high(PIN_B3);
            }
        }

        if(input(PIN_B1) == 0) {
            if(input(PIN_B4) == 0) {
                output_b(0);
                setup_ccp1(CCP_OFF);
                setup_ccp2(CCP_OFF);
                output_c(0);
                delay_ms(100);
                setup_ccp2(CCP_PWM);
                output_high(PIN_B4);
            }
        }

        if(input(PIN_B2) == 0) {
            setup_ccp1(CCP_OFF);
            setup_ccp2(CCP_OFF);
            output_c(0);
            output_b(0);
        }
    }
}
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 21, 2024 7:07 am     Reply with quote

general comments...

1st, post compiler version. There may be a 'bug' in it.....

2nd, put comments at end of each line of code. While YOU know what's supposed to happen, others don't. 3 months, heck 3 days from now YOU might forget why you wrote a line of code.BTDT

3rd, break the program down into smaller sections. Overall your program does 'something' based upon condition of switches and ADC input. CONFIRM that these actually work as expected, THEN build from there.

4th, CONFIRM the hardware IS connected as you've coded ! Maybe you've swapped pins. Easy to do,IE: 10 and 11 instead of 9 and 10. Motor doesn't turn as no connection ??

5th, well should be done 1st.... code/compile/run a simple '1Hz LED' program to CONFIRM the PIC actually is connected and runs properly !.

6th, When editting the program, copy the old source,save as prg002, edit,test, then repeat the steps so you'll have pgm1,pgm2,pgm3,.....pgm66....
While this does take up harddrive space, it does allow you to 'go back' to a previous version,in effect 'erase' whatever NEW code that didn't work as expected.

hopefully someone will look and see what may be wrong with the code( if anything....)
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Nov 22, 2024 3:05 am     Reply with quote

How are you pulling pins B3 and B4 down?. If you use a button or switch for
this, the chip will short out and reset. You detect a low on these pins, and then
setup the PWM, and turn the pin on. If it is connected to ground, this
will have the PIC trying to pull high a pin that is held low. Result the PIC
will almost certainly reset.
The big problem though is you are not setting up the timer for the PWM.
The PWM uses timer2. This is not running, so there will be no PWM output.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group