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: