understanding the code

Thread Starter

RG23

Joined Dec 6, 2010
304
C:
#defineF_CPU1000000UL  //definition for util/delay.h


#include<avr/io.h>

#include<avr/wdt.h>

#include<avr/interrupt.h>

#include<util/delay.h>

#include<stdbool.h>




#defineRELAYPB1  // Define RELAY ext output pin on PB1



#defineRELAY_OFF  PORTB|=(1<<RELAY)  //Define Relay off



#defineRELAY_ON  PORTB&=~(1<<RELAY)  //Define Relay on





unsignedcharrelay_change_state_level_Twist  =100;  //level for light on (NC-relay coil power off) for twist lock photo cell

unsignedcharrelay_change_state_level_btn  =100;  //level for light on (NC-relay coil power off) for btn photocell



unsignedcharrelay_change_state_level  =100;  //level for light on (NC-relay coil power off)

unsignedint  timer_period=58594;    // Timer period 58594 = 60s

unsignedint  timer_count=0;  // Variable increased each timer period


unsignedchartimer_count_max=1;  //max time for counting //3 min

unsignedcharadc_level=0;  //current ADC level





bool  twistlock  =true;  //twistlock - true btn photocell -false



bool  adc_level_on=false;    //temp for on

bool  adc_level_on_prev=false;    //previous temp for on


/*

bool  adc_level_off;//temp for off

*/







/************************************************************************/

/* Timer initialization   */

/************************************************************************/


voidtimer0_init()

{

  //TCCR0B |= (0<<CS01) | (1<<CS00);  // no pre-scaler

  TCCR0B|=(1<<CS02)|(0<<CS01)|(1<<CS00);  // pre-scaler 1024

  TIMSK0|=(1<<OCIE0A);  // enable timer comp interrupt

  TCCR0A|=(0<<WGM01)|(0<<WGM00);

  TCCR0B|=(0<<WGM03)|(1<<WGM02);



  OCR0A=timer_period;

}




/************************************************************************/

/* ADC setup  */

/************************************************************************/



voidadc_setup(void)

{

  // Set the ADC input to PB2/ADC2

  ADMUX  |=(1<<MUX1)|(0<<MUX0);

  ADCSRA|=(1<<ADPS1)|(1<<ADPS0)|(1<<ADEN);

}



/************************************************************************/

/*  ADC read function  */

/************************************************************************/

unsignedcharadc_read(void)

{

  // Start the conversion

  ADCSRA|=(1<<ADSC);



  // Wait for it to finish

  while(ADCSRA&(1<<ADSC));



  returnADCL;



}





/************************************************************************/

/* main function  */

/************************************************************************/


intmain(void)

{



  /************************************************************************/

  /* Local variable  */

  /************************************************************************/






  /************************************************************************/

  /*  Clock setup  */

  /************************************************************************/



  // 1Mhz clock setup



  CCP  =0Xd8;

  CLKMSR=0b00;

  CCP  =0Xd8;

  CLKPSR=0x3;






  /************************************************************************/

  /* Watchdog Timer setup  */

  /************************************************************************/

  WDTO_1S;  //WDT time 1c


  /************************************************************************/

  /* Ports Initialization  */

  /************************************************************************/




  DDRB|=(1<<RELAY);    // Set output direction on RELAY

  DDRB|=(0<<PB0);  // Set input direction on tpidata



  PUEB|=(1<<PUEB0);  //Enable PullUp port b0






  /************************************************************************/

  /* Variable Initialization  */

  /************************************************************************/



  timer_count=0;



  /************************************************************************/

  /* Global Initialization  */

  /************************************************************************/

  cli();  //Disable all interrupts

  timer0_init();  // initialize timer

  adc_setup();

  sei();  //enable interrupts



  /************************************************************************/

  /* Main cycle  */

  /************************************************************************/

  while(1)

  {

  wdt_reset();



  twistlock=PB0;    //read twist lock - btn pin state



  if(twistlock)    // if twistlock (high)

  {

  relay_change_state_level=relay_change_state_level_Twist;  // set light level for twistlock

  }

  else

  {

  relay_change_state_level=relay_change_state_level_btn;  //or set light level for BTN photocell

  }









  if(timer_count<timer_count_max)

  {

  adc_level=adc_read();


  adc_level_on_prev=adc_level_on;





  if(adc_level<=relay_change_state_level)

  {

  adc_level_on=true;

  adc_level_on_prev=adc_level_on;

  timer_count=timer_count_max;

  wdt_reset();



  }

  else

  {

  adc_level_on=false;

  wdt_reset();



  }







  if(adc_level_on_prev!=adc_level_on)

  {

  timer_count=0;

  }



  }

  else

  {

  if(adc_level_on)

  {

  RELAY_ON;



  timer_count=0;

  }

  else

  {

  RELAY_OFF;



  timer_count=0;

  }

  }



  }

}


/************************************************************************/

/* Timer 0 interrupt vector  */

/************************************************************************/


ISR(TIM0_COMPA_vect)

{


  timer_count++;


}


I need help in understanding the code above and make modifications to change the timer setttings.

Thanks
 
Last edited by a moderator:

MaxHeadRoom

Joined Jul 18, 2013
28,686
It is easier to read if you used the CODE tags.
Max.
Code:
#defineF_CPU1000000UL //definition for util/delay.h


#include<avr/io.h>
#include<avr/wdt.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#include<stdbool.h>

#defineRELAYPB1 // Define RELAY ext output pin on PB1

#defineRELAY_OFF PORTB|=(1<<RELAY) //Define Relay off
 

djsfantasi

Joined Apr 11, 2010
9,163
There seems to be something with the code tags, as a problem appears in both preceding posts. Spaces seem to have disappeared. Could this have anything to to with recent changes, @jrap?
 

Thread Starter

RG23

Joined Dec 6, 2010
304
right now the output goes from low to high immediately whereas from high to low there is a delay of 1min.

I need to add delay of about 1min for low to high transition as well.

If anyone has an idea, let me know
thanks
 

theonewho

Joined Jul 9, 2015
17
Where did this code come from and why are there no spaces where there should be to make this valid code?

C:
#defineRELAYPB1  // Define RELAY ext output pin on PB1
#defineRELAY_OFF  PORTB|=(1<<RELAY)  //Define Relay off
#defineRELAY_ON  PORTB&=~(1<<RELAY)  //Define Relay on

unsignedcharrelay_change_state_level_Twist  =100;  //level for light on (NC-relay coil power off) for twist lock photo cell
unsignedcharrelay_change_state_level_btn  =100;  //level for light on (NC-relay coil power off) for btn photocell
C:
unsignedcharadc_read(void)
{
  // Start the conversion
  ADCSRA |= (1 << ADSC);

  // Wait for it to finish
  while (ADCSRA & (1 << ADSC));

  returnADCL;

}
all those blank lines...
 

WBahn

Joined Mar 31, 2012
30,056
I need help in understanding the code above and make modifications to change the timer setttings.

Thanks
Instead of asking people to wade through 420 lines of improperly formatted code that is spottily commented, how about spending some time up front to make the code easily readable and then indicating where in the code the problem seems to be originating (as best as you can tell)?

C:
#define F_CPU 1000000UL  //definition for util/delay.h

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdbool.h>

#define RELAY PB1  // Define RELAY ext output pin on PB1
#define RELAY_OFF  PORTB|=(1<<RELAY)  //Define Relay off
#define RELAY_ON  PORTB&=~(1<<RELAY)  //Define Relay on

unsigned char relay_change_state_level_Twist  = 100;  //level for light on (NC-relay coil power off) for twist lock photo cell
unsigned char relay_change_state_level_btn  = 100;  //level for light on (NC-relay coil power off) for btn photocell
unsigned char relay_change_state_level  = 100;  //level for light on (NC-relay coil power off)
unsigned int  timer_period = 58594;    // Timer period 58594 = 60s
unsigned int  timer_count = 0;  // Variable increased each timer period
unsigned char timer_count_max = 1;  //max time for counting //3 min
unsigned char adc_level = 0;  //current ADC level

bool  twistlock  = true;  //twistlock - true btn photocell -false
bool  adc_level_on = false;    //temp for on
bool  adc_level_on_prev = false;    //previous temp for on

/*
bool  adc_level_off;//temp for off
*/

/************************************************************************/
/* Timer initialization   */
/************************************************************************/

voidtimer0_init()
{
  //TCCR0B |= (0<<CS01) | (1<<CS00);  // no pre-scaler
  TCCR0B |= (1<<CS02)|(0<<CS01)|(1<<CS00);  // pre-scaler 1024
  TIMSK0 |= (1<<OCIE0A);  // enable timer comp interrupt
  TCCR0A |= (0<<WGM01)|(0<<WGM00);
  TCCR0B |= (0<<WGM03)|(1<<WGM02);
  OCR0A = timer_period;
}
See how much more readable that is?

You are much more likely to get someone to look through your code if you make some effort up front to make it easier for them to do so.
 
Top