Vous êtes sur la page 1sur 4

//uncomment the next line to target PIC16F690

//#define TARGET_PIC16F690
// MAP IO pins to signals
// Note: Forward and reverse are notional directions and
depend on how the
// motor is wired and how they are mounted. Change the
definitions below
// to match reality!
////// LEFT MOTOR
#define LMOT_FORWARD PORTC.F5
#define LMOT_REVERSE PORTC.F4
#define LMOT_ENABLE PORTC.F3
/// RIGHT MOTOR
#define RMOT_FORWARD PORTC.F0
#define RMOT_REVERSE PORTC.F2
#define RMOT_ENABLE PORTC.F1
// Useful macros
#define BIT(n) (1 <<(n))
void adcOff() // Turn off ADC
{
#if defined (TARGET_PIC16F690)
ANSEL = ANSELH = 0;
#else
ANSELA = ANSELB = ANSELC = 0;
#endif
}
void init()
{ //Initialize the system
TRISA = TRISB = 0xFF; // All of PORTA & PORTB are
inputs
PORTC = 0x00; //Make sure to turn off the
port first!
TRISC = 0x00; // All of PORTC is output
adcOff();
#if !defined(TARGET_PIC16F690)
// Initialize PWM generator. See text for explanation
PR2 = 255; // Maximum range for duty
PWM2CON = BIT(7) | BIT(6); // Bit7 is PWMxEN, Bit6 isPWMxOE
PWM4CON = BIT(7) | BIT(6);
T2CON.TMR2ON = 1;
PWM2DCH = PWM4DCH = 0;
#endif
}
//---------------- Motor Control functions
void setLeftVelocity(int velocity)
{
int direction, speed;
//Get direction and speed
if (velocity >= 0) {
direction = 0;
speed = velocity;
}
else {
direction = 1;
speed = -velocity;
}
if (speed > 255)
speed = 255;
// Set the direction
if (direction == 0) {
LMOT_REVERSE = 0;
LMOT_FORWARD = 1;
}
else {
LMOT_REVERSE = 1;
LMOT_FORWARD = 0;
}
//set the speed
// PIC16F690 does not have two PWM channels
// so it is either ON or OFF.
// So any speed >127 would be on, or else off
//
// PIC16F1507 has a smoother control
#if defined(TARGET_PIC16F690)
if (speed > 127)
LMOT_ENABLE = 1;
else
LMOT_ENABLE = 0;
#else
//Left Motor enable is connected to PWM channel 2
PWM2DCH = speed;
#endif
}
// Do the same for right motor
void setRightVelocity(int velocity)
{
int direction, speed;
//Get direction and speed
if (velocity >= 0) {
direction = 0;
speed = velocity;
}
else {
direction = 1;
speed = -velocity;
}
if (speed > 255)
speed = 255;
// Set the direction
if (direction == 0) {
RMOT_REVERSE = 0;
RMOT_FORWARD = 1;
}
else {
RMOT_REVERSE = 1;
RMOT_FORWARD = 0;
}
//set the speed
// PIC16F690 does not have two PWM channels
// so it is either ON or OFF.
// So any speed >127 would be on, or else off
//
// PIC16F1507 has a smoother control
#if defined(TARGET_PIC16F690)
if (speed > 127)
RMOT_ENABLE = 1;
else
RMOT_ENABLE = 0;
#else
//Right Motor enable is connected to PWM channel 4
PWM4DCH = speed;
#endif
}
#define DELAY_TIME 10
// Test function
void test()
{
int speed;
// Gradually increase the left motor velocity from 0 255
for (speed = 0; speed < 256; speed++) {
setLeftVelocity(speed);
delay_ms(DELAY_TIME);
}
// Go back to zero
for (speed = 255; speed >= 0; speed--) {
setLeftVelocity(speed);
delay_ms(DELAY_TIME);
}
//repeat in opposite irection
for (speed = 0; speed < 256; speed++) {
setLeftVelocity(-speed);
delay_ms(DELAY_TIME);
}
// Go back to zero
for (speed = 255; speed >= 0; speed--) {
setLeftVelocity(-speed);
delay_ms(DELAY_TIME);
}
// Repeat for right motor
for (speed = 0; speed < 256; speed++) {
setRightVelocity(speed);
delay_ms(DELAY_TIME);
}
// Go back to zero
for (speed = 255; speed >= 0; speed--) {
setRightVelocity(speed);
delay_ms(DELAY_TIME);
}
//repeat in opposite irection
for (speed = 0; speed < 256; speed++) {
setRightVelocity(-speed);
delay_ms(DELAY_TIME);
}
// Go back to zero
for (speed = 255; speed >= 0; speed--) {
setRightVelocity(-speed);
delay_ms(DELAY_TIME);
}
}
main()
{
init();
while (1) {
test(); // Keep running the test
}
}

Vous aimerez peut-être aussi