|
|
View previous topic :: View next topic |
Author |
Message |
slipknotcc
Joined: 13 Feb 2010 Posts: 8
|
Whats wrong with my H-bridge code? |
Posted: Sun Feb 21, 2010 1:59 am |
|
|
Hey guys,
What I'm trying to do is use 2 Switches (FwdSw, RevSw) to control the direction of my motor. When FwdSw is on and RevSw is off, it goes forward. When FwdSw is off and RevSw in on it reverses. When both FwdSw and RevSw are on or off together I want the Motor to stop.
The switches are wired with negative logic and the enable pin on my H-Bridge is enabled low.
I think there is a problem with my while loops because the program will work flawlessly for a second and then just fall apart and and work intermittently.
I'm using PCWH Compiler 4.101 on a PIC16f877A with a 20MHz clock
Thanks,
Cory
Code: |
#include <16f877a.h>
#device adc=8
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
//================================
#include "flex_lcd.c"
//================================
#define FwdPin Pin_B4 // Pin 37
#define RevPin Pin_B5 // Pin 38
#define FwdSw Pin_B1 // Pin 34
#define RevSw Pin_B2 // Pin 35
// PWM PIN is 17
//================================
int8 pwm;
//================================
void forward(void);
void reverse(void);
void stop(void);
//================================
void main()
{
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 254, 1);
setup_port_a(AN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(20);
lcd_init();
while(TRUE)
{
lcd_init();
while(!input(FwdSw))
{
forward();
}
lcd_init();
while(!input(RevSw))
{
reverse();
}
lcd_init();
while(!input(RevSw & FwdSw ))
//while(!input(RevSw & FwdSw | FwdSw & RevSw))
{
printf(lcd_putc,"STOP!! \n");
printf(lcd_putc,"Speed = %u ",pwm);
stop();
}
lcd_init();
while(input(RevSw & FwdSw ))
{
printf(lcd_putc,"STOP!! \n");
printf(lcd_putc,"Speed = %u ",pwm);
stop();
}
lcd_init();
}
}
void forward()
{
output_high(FwdPin);
output_low(RevPin);
pwm = read_adc();
set_pwm1_duty(pwm);
printf(lcd_putc,"FORWARD!! \n");
printf(lcd_putc,"Speed = %u ",pwm);
}
void reverse()
{
output_low(FwdPin);
output_high(RevPin);
pwm = read_adc();
set_pwm1_duty(pwm);
printf(lcd_putc,"REVERSE!! \n");
printf(lcd_putc,"Speed = %u ",pwm);
}
void stop()
{
for (pwm = 115; pwm<255; pwm += 20)
{
set_pwm1_duty(pwm);
}
output_high(FwdPin);
output_high(Revpin);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 21, 2010 1:44 pm |
|
|
Quote: |
#define FwdPin Pin_B4 // Pin 37
#define RevPin Pin_B5 // Pin 38
#define FwdSw Pin_B1 // Pin 34
#define RevSw Pin_B2 // Pin 35
|
It's the standard in C to put constants in upper case. This helps you
to tell the difference between a constant and a variable when you're
reading your code. Also, you should not change the case of the CCS
pin constants. They're all in upper case, as listed in the 16F877a.h file.
Most compilers are case sensitive. The CCS compiler is rare, in that
it's not case sensitive by default. So if you get into the bad habit of
changing the case, it will burn you if you ever move to a different
compiler. See this C style guide:
http://www.chris-lott.org/resources/cstyle/indhill-cstyle.pdf
It says:
Quote: |
11. Naming Conventions
#define constants should be in all CAPS.
|
Quote: |
while(!input(RevSw & FwdSw ))
|
The line above (and at least one other line) uses incorrect parameters
for the input() function. Download the CCS manual and look at the
description of the input() function:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
It says:
Quote: |
input( )
Syntax: value = input (pin)
Parameters: Pin to read.
|
It does not mention multiple pins, or combining pins with operators.
It only accepts one pin. You need to call the input() function for each
pin. Then, you can do operations on the results. |
|
|
|
|
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
|