twenglish1
February 27th, 2009, 06:47 PM
here is a little project i am working on, and i am having some problems, i am new to C programming and i am trying to custom make my own control board for a paintball gun, right now i am starting out simple, just with a programmable firing speed, i am using an attiny2313 avr microcontroller, when i program it(the code compiles, but it needs fixed) all it does it turn the fire_indicator led on, so can anyone help me??
here is my code:
#include <avr\io.h>
#include <util\delay.h>
#define TRIGGER_PORT PORTB
#define TRIGGER_BIT PB0
#define TRIGGER_PIN PINB
#define PROGRAM_BUTTON_PORT PORTB
#define PROGRAM_BUTTON_BIT PB1
#define PROGRAM_BUTTON_PIN PINB
#define FIRE_INDICATOR_ON PORTD |= (1<<PD2)
#define FIRE_INDICATOR_OFF PORTD &= (~(1<<PD2))
#define PROGRAM_MODE_ON PORTD |= (1<<PD1)
#define PROGRAM_MODE_OFF PORTD &= (~(1<<PD1))
#define DEBOUNCE_BUTTONS _delay_ms(24)
#define RATE_OF_FIRE _delay_ms(x)
#define ACTIVATION_TIME _delay_ms(1000*2)
int x = 100;
int TRIGGER_PULL()
{
if (bit_is_clear(TRIGGER_PIN, TRIGGER_BIT))
{
DEBOUNCE_BUTTONS;
if (bit_is_clear(TRIGGER_PIN, TRIGGER_BIT))
{
return 1;
}
else
{
return 0;
}
}
}
int INIT_IO()
{
/* PORTD SETUP */
DDRD |= (1<<PD2);
DDRD |= (1<<PD1);
/* PORTB SETUP */
DDRD &= (~(1<<PB0));
DDRD &= (~(1<<PB1));
/* ENABLE BUTTON PIN PULLUP */
PORTB |= (1<<PB0);
PORTB |= (1<<PB1);
}
int FIRE()
{
FIRE_INDICATOR_ON;
RATE_OF_FIRE;
FIRE_INDICATOR_OFF;
RATE_OF_FIRE;
FIRE_MODE();
}
int PROGRAM_BUTTON_PRESS()
{
if (bit_is_clear(PROGRAM_BUTTON_PIN, PROGRAM_BUTTON_BIT))
{
DEBOUNCE_BUTTONS;
if (bit_is_clear(PROGRAM_BUTTON_PIN, PROGRAM_BUTTON_BIT))
{
return 1;
}
else
{
return 0;
}
}
}
int PROGRAM_MODE()
{
/* TELL PROGRAM MODE IS ACTIVE */
PROGRAM_MODE_ON;
_delay_ms(500);
PROGRAM_MODE_OFF;
if (TRIGGER_PULL())
{
x = 0;
while(1)
{
if (TRIGGER_PULL())
{
_delay_ms(100);
if (TRIGGER_PULL())
{
x = x + 10;
}
else
{
/* TELL PROGRAM MODE IS INACTIVE */
PROGRAM_MODE_ON;
_delay_ms(500);
PROGRAM_MODE_OFF;
FIRE_MODE();
}
}
}
}
}
int FIRE_MODE()
{
while(1)
{
if (TRIGGER_PULL())
{
FIRE();
}
if (PROGRAM_BUTTON_PRESS())
{
_delay_ms(500);
if (PROGRAM_BUTTON_PRESS())
{
PROGRAM_MODE();
}
}
}
}
int main()
{
INIT_IO();
FIRE_MODE();
}
here is my code:
#include <avr\io.h>
#include <util\delay.h>
#define TRIGGER_PORT PORTB
#define TRIGGER_BIT PB0
#define TRIGGER_PIN PINB
#define PROGRAM_BUTTON_PORT PORTB
#define PROGRAM_BUTTON_BIT PB1
#define PROGRAM_BUTTON_PIN PINB
#define FIRE_INDICATOR_ON PORTD |= (1<<PD2)
#define FIRE_INDICATOR_OFF PORTD &= (~(1<<PD2))
#define PROGRAM_MODE_ON PORTD |= (1<<PD1)
#define PROGRAM_MODE_OFF PORTD &= (~(1<<PD1))
#define DEBOUNCE_BUTTONS _delay_ms(24)
#define RATE_OF_FIRE _delay_ms(x)
#define ACTIVATION_TIME _delay_ms(1000*2)
int x = 100;
int TRIGGER_PULL()
{
if (bit_is_clear(TRIGGER_PIN, TRIGGER_BIT))
{
DEBOUNCE_BUTTONS;
if (bit_is_clear(TRIGGER_PIN, TRIGGER_BIT))
{
return 1;
}
else
{
return 0;
}
}
}
int INIT_IO()
{
/* PORTD SETUP */
DDRD |= (1<<PD2);
DDRD |= (1<<PD1);
/* PORTB SETUP */
DDRD &= (~(1<<PB0));
DDRD &= (~(1<<PB1));
/* ENABLE BUTTON PIN PULLUP */
PORTB |= (1<<PB0);
PORTB |= (1<<PB1);
}
int FIRE()
{
FIRE_INDICATOR_ON;
RATE_OF_FIRE;
FIRE_INDICATOR_OFF;
RATE_OF_FIRE;
FIRE_MODE();
}
int PROGRAM_BUTTON_PRESS()
{
if (bit_is_clear(PROGRAM_BUTTON_PIN, PROGRAM_BUTTON_BIT))
{
DEBOUNCE_BUTTONS;
if (bit_is_clear(PROGRAM_BUTTON_PIN, PROGRAM_BUTTON_BIT))
{
return 1;
}
else
{
return 0;
}
}
}
int PROGRAM_MODE()
{
/* TELL PROGRAM MODE IS ACTIVE */
PROGRAM_MODE_ON;
_delay_ms(500);
PROGRAM_MODE_OFF;
if (TRIGGER_PULL())
{
x = 0;
while(1)
{
if (TRIGGER_PULL())
{
_delay_ms(100);
if (TRIGGER_PULL())
{
x = x + 10;
}
else
{
/* TELL PROGRAM MODE IS INACTIVE */
PROGRAM_MODE_ON;
_delay_ms(500);
PROGRAM_MODE_OFF;
FIRE_MODE();
}
}
}
}
}
int FIRE_MODE()
{
while(1)
{
if (TRIGGER_PULL())
{
FIRE();
}
if (PROGRAM_BUTTON_PRESS())
{
_delay_ms(500);
if (PROGRAM_BUTTON_PRESS())
{
PROGRAM_MODE();
}
}
}
}
int main()
{
INIT_IO();
FIRE_MODE();
}