The Easiest Way to Save and Share Code Snippets on the web

useless_toggleless

c

posted: Dec, 27th 2010 | jump to bottom

#include <avr/io.h>
#include <avr/interrupt.h>
 
void timer0_activate(unsigned short code)
{
  cli();
  TCCR0A=0; // Normal opearation
//  TCCR0B=5; // f devider 1024 (at least must be 1 in any of last 3 digits
  TCCR0B=4; // f devider 256  : about 3 mls for interrupt
  TIMSK0 = 1;
  sei();
}
 
// Vector to process timer interrupt
 
volatile uint8_t time=0;
 
ISR(TIMER0_OVF_vect)
{
  ++time;
  return;
}
 
// Time delay in mls ...
void ldelay(long tmm)
{ 
  uint8_t delay = tmm/3;
  uint8_t start = time;
 
  while(1)
  {
    volatile short ix;
    for(ix=0; ix != 100; ++ix);
  	uint8_t delta = time - start;
  	if(delta > delay){
    	  break;
	}
  }
}
 
void timeReset()
{
  time = 0;
}
 
int8_t isIntervalExpired(long interval)
{
  uint8_t delay = interval/3;
  uint8_t delta = time;
  if(delta > delay) {
    return 1;
  }
  return 0;
}
 
void turnMotor(int8_t direction)
{
  DDRB |= (1 << DDB1);
  DDRD |= (1 << DDD5);
  switch(direction)
  {
    case 1:
      PORTB |= (1 << PB1);
      PORTD &= ~(1 << PD5);
      break;
  case -1:
      PORTB &= ~(1 << PB1);
      PORTD |= (1 << PD5);
    break;
  case 0:
      PORTB |= (1 << PB1);
      PORTD |= (1 << PD5);
    break;
  default:
      PORTB &= ~(1 << PB1);
       PORTD &= ~(1 << PD5);
  break;
  }  
}
 
 
void turnRelay(uint8_t on)
{
  DDRB |= (1 << DDB2);
  DDRD |= (1 << DDD6);
  if(on){
    PORTB |= (1 << PB2);
    PORTD &= ~(1 << PD6);
  }else{
    PORTB |=(1 << PB2);
    PORTD |=(1 << PD6);
  }
}
 
void turnLed(uint8_t on)
{
  DDRD |= (1 << DDD1);
  if(on){
     PORTD |= (1 << PD1);
  }else{
     PORTD &= ~(1 << PD1);
  }
}
 
void activateInput()
{
  DDRD &= ~((1 << DDD7) | (1 << DDD4)| (1 << DDD3) | (1 << DDD2)); // signal direction IN
  PORTD |= (1 << PD7) | (1 << PD4)| (1 << PD3) | (1 << PD2); // pull up resistor activated
}
 
static int8_t m_IsLeverBack = 1;
int8_t  isLeverBack()
{
  if((PIND & (1 << PD7))==0) {
    m_IsLeverBack = 1;
  } else if ((PIND & (1 << PD4))==0) {
    m_IsLeverBack = 0;
  }
  return m_IsLeverBack;
}
 
 
static int8_t m_IsButtonOn=1;
int8_t isButtonOn()
{
 
  if((PIND & (1 << PD3))==0) {
     m_IsButtonOn = 1;
  } else if((PIND & (1 << PD2))==0) {
     m_IsButtonOn = 0;
  }
  return m_IsButtonOn;
}
 
int8_t GetButtonState()
{
  return isButtonOn();
}
 
int main(void)
{
  timer0_activate(0);
  activateInput();
  if(GetButtonState() != 1)
  {
    ldelay(5);
  }
  turnRelay(1);
  turnLed(0);
  turnMotor(0);
  int8_t pushCount=0;
  int8_t prevButtonState = 1;
  int8_t currButtonState;
  for(;;prevButtonState = currButtonState)
  {// loop to count button pushes
    currButtonState = GetButtonState();
    if(currButtonState == 0)
    {
        if(prevButtonState !=0) {
        ++pushCount;
        timeReset();
      }
    } else {
      continue;
    }
    if(isIntervalExpired(450)) {
      break;
    }
  }
 
  for(int8_t ix = 0; ix != pushCount; ++ix)
  { // working loop
    turnLed(1);
    turnMotor(1);
    while(GetButtonState() == 0);
    turnMotor(0);
    ldelay(20);
    turnMotor(-1);
    turnLed(0);
    while(!isLeverBack());
    turnMotor(0);
    ldelay(20);
  }
  turnRelay(0);
  ldelay(10);
  return 0; 
}
 
96 views