• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need help with c-code using a PIC18F4431 (PWM).

Status
Not open for further replies.

Ckramer

New Member
Hi, I need a little problem with my c-code. I'm using a PIC18F4431 to control 3 stepper motors(bi-polar). And using a Sega-pad to send 1 or 0 to the microcontroller.
I've connected only one 1 stepper motor atm, but having some difficulties with the program. I only get a response from one of the buttons on the pad.
Need help with direction etc. Does anyone know how to make i work??

Here is what I've done so far:

#include <18F4431.H>

//////////////////////////////////////////////////////////////////
//
// Demoprogram for Pulse Width Modulation (PWM).
//
// PIN_AN0 Direction forward (button)
// PIN_AN1 Direction backward (button)
// PIN_AN2 Direction right (button)
// PIN_AN3 Direction left (button)
// PIN_AN4 Direction down (button)
// PIN_AN5 Direction up (button)
// PIN_RB0,RB1,RB2,RB3,RD7,RD6 are connected to output

#fuses XT,NOBROWNOUT,NOMCLR,NOWDT,NOPROTECT,NOLVP,NOPUT
#use delay (clock=4150000)

/////////////////////////////////////////////////////////////////////
//
// Global variables.
//

// Mainprogram
void main(void) {

long duty=0;

// configurate CCP as PWM
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);

// configurate timer_2
setup_timer_2(T2_DIV_BY_1, 99, 1);

// Set startup value on duty

set_PWM1_duty(duty);
set_PWM2_duty(duty);
//set_PWM3_duty(duty);

// Pin_D7 is connected to Direction input on Stepper motor driver (PBD3517)
// Pin_A0 and Pin_A1 are inputs from the Sega cotrol-pad

while( TRUE ) {

if (input(PIN_A0)==1) {
output_bit( PIN_D7, 1);
duty = 100;
set_pwm1_duty(duty);
//motor forward!
}

if (input(PIN_A0)==0){
output_bit( PIN_D7, 1);
duty = 0;
set_pwm1_duty(duty);
//motor stop!
}

//--------------------------------------

if (input(PIN_A1==1)){
output_bit( PIN_D7, 0);
duty = 100;
set_pwm1_duty(duty);
//motor backward!
}

if (input(PIN_A1)==0){
output_bit( PIN_D7, 0);
duty = 0;
set_pwm1_duty(duty);
//motor stop!
}

}//end
}
 
Last edited:

GraveYard_Killer

New Member
what compiler are you using? can we see your schematics? how was the sega gamepad connected to pic?

you can instead use simple tact switches as of the moment and indicate it with LED light indicators to be able to know where to debug either on software side or the hardware side.
 

Ckramer

New Member
I'm using the CSS compiler.

Here is the pin layout on the Sega controlpad:

##+-----------> Power
##| +---------> Right
##| | +-------> Left
##| | | +-----> Down
##| | | | +---> Up
##| | | | |
___________
5 \ o o o o o / 1
##\ o o o o /
#9 `~~~~' 6
###| | | |
###| | | +----> Button 1
###| | +------> Select
###| +--------> Ground
###+----------> Button 2

Here is the schematics(made with ULTIboard):
 

Attachments

Status
Not open for further replies.

EE World Online Articles

Loading
Top