Thursday, September 5, 2013



C6/RESET

C5/ADC5
Change over relay
D0
Alternator volt sensing
C4/ADC4
Meters relay
D1
STmotor volt sensing
C3/ADC3
Start relay
D2/INT 0
Mains volt sensing
C2/ADC2
Stop relay
D3/INT 1

C1/ADC1

D4
Diesel float sensor
C0/ADC0
buzzer
VCC

GND

GND

AREF

B6

AVCC

B7

B5/SCK

D5
Mode ctrl
B4/MISO

D6

B3/MOSI

D7

B2

B0

B1


Mode ctrl 1
NC
Selection
1
NC
Auto
VCC
VCC
Semi auto
Meters relay
Uc2 power transistor
manual

program:

/*
 * msr_dg_control.c
 *
 * Created: 30-08-2013 19:06:29
 *  Author: elcot
 */ 


#include
#include
#include
#include

void Genset_Start();
void Genset_Stop(); // declaring code blocks
void Genset_Cranking();
void Genset_Stmotor(uint8_t crnktime);
void Genset_running();
void Buzz(uint8_t buzztime);
void Delay(uint8_t dtime);

bool gsrun = false;

int main(void)
{
DDRC = 0b111111; //set portc as output (meters and st motor)
DDRD = 0b000000; //set portd as input
MCUCR|=(1<
// checking main power by int0 pin high value
//Initialize INT0  Falling edge on INT0 triggers interrupt.
GICR|=(1<
//Enable INT0 interrupt
sei(); // Enable Global Interrupt
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

ISR(INT0_vect) //CPU Jumps here automatically when INT0 pin detect a falling edge
{
if (bit_is_set(PIND,0))
{
gsrun = true;// genset running
}
else Genset_Start();
}

void Buzz(uint8_t buzztime)
{
PORTC |= 1<
Delay(buzztime); //buzz for specified time
PORTC &= ~(1<
Delay(buzztime);
}

void Delay(uint8_t dtime)
{
for(uint8_t f3 =0;f3
{
_delay_ms(100);
}
}

void Genset_Start()
{
PORTC |= 1<
// set c4 high - to enable genset meters
Delay(10);
if (bit_is_set(PIND,1)) //checking Engine starter motor voltage
{
if ((bit_is_set(PIND,4))&&(bit_is_set(PIND,5))) //checking suffecient diesel level and op-mode
{
Buzz(4);
Genset_Cranking(); //normal starting
}
else
{
while (gsrun == false)
{
if(bit_is_set(PINB,2))
{
Buzz(4);
Genset_Cranking(); //forced start only by manual switching
}
}
}
}
else
{
// display no voltage from alternating dynamo
}

}


void Genset_Cranking()
{
Genset_Stmotor(15);
if(bit_is_set(PIND,1))
{
Genset_Stmotor(20);
}
if (bit_is_set(PIND,1))
{
Genset_Stmotor(25);
}
if(bit_is_set(PIND,1))
{
// display "Auto Start Fail"
//display "Use Ignition Key"
}
}


void Genset_Stmotor(uint8_t crnktime)
{
PORTC |= 1<
Delay(crnktime);
PORTC &= ~(1<
Delay(50);
}

void Genset_running()
{
}

void Genset_Stop()
{
PORTC |= 1<< PINC2;
Delay(50);
while (bit_is_clear(PINC,1))
{
}
PORTC &= ~ (1<< PINC2);
}


primary program:

/*
 * Primary_Program.c
 *
 * Created: 1/7/2013 10:38:59 PM
 *  Author: Administrator
 */ 

/*
#include
#include
#include
#include "MrLCD.h"

void Delay(uint8_t dtime);
void Genset_Idle();
void Genset_Start();
void Genset_Stop(); // declaring code blocks
void Genset_Running();
void Genset_Stopped();
void Genset_Startup_Check();
void Genset_Starting_Protocol(uint8_t ctrl);
void Genset_Cranking();
void Genset_Stmotor(uint8_t crnktime);
void Buzz(uint8_t buzztime);

int l_hour,l_min,diesel,eng_temp,alt_temp,RPM,op_volt,hour,min,sec,amblight;


int main(void)
{
InitializeMrLCD(); //initiating LCD Display
DDRC |= 1<
//setting pin c7,c6,c5,c4 as output
PORTC |= 1<
// power on status led (green)
Buzz(12); // buzz for 1.2s
//startup system check and display initial parameters
//check genset already running using the alt-dynamo red light

ADCSRA |= 1<
ADMUX |= 1<
ADCSRA |= 1<
ADCSRA |= 1<
sei(); // enable interrupts globally

ADCSRA |= 1<
// start adc conversion

MCUCR|=(1<
// checking main power by int0 pin high value
//Initialize INT0  Falling edge on INT0 triggers interrupt.
GICR|=(1<
//Enable INT0 interrupt 

Genset_Idle();
while (1)
{

}

}

ISR(INT0_vect)
{
Genset_Start(); //CPU Jumps here automatically when INT0 pin detect a falling edge
}


ISR(ADC_vect) // ADC routine ~ cpu jumps here when ADCSRA =1
{
uint8_t theLow = ADCL;
uint16_t theTenBitResult = ADCH<<8 div="" thelow="">

switch (ADMUX)
{
case 0xC0:
diesel=theTenBitResult;
ADMUX = 0xC1;
break;
case 0xC1:
eng_temp=theTenBitResult;
ADMUX = 0xC2;
break;
case 0xC2:
alt_temp=theTenBitResult;
ADMUX = 0xC3;
break;
case 0xC3:
op_volt=theTenBitResult;
ADMUX = 0xC4;
break;
case 0xC4:
amblight=theTenBitResult;
ADMUX = 0xC0;
break;
default:
//Default code
break;
}

void Delay(uint8_t dtime)
{
for(uint8_t f3 =0;f3
{
_delay_ms(100);
}
}

void Buzz(uint8_t buzztime)
{
PORTC |= 1<
Delay(buzztime); //buzz for specified time
PORTC &= ~(1<
Delay(buzztime);
}

void Genset_Start()
{
ADCSRA |= 1<
//Get ADC values once
DDRD |= 1<
PORTD |= 1<
// set d0 to high - to enable genset meters
DDRC &= ~(1<
Delay(1);
if (bit_is_set(PINC,1))
{
if (diesel<2 div="">
{
for (uint8_t f1=0;f1<3 div="" f1="">
{
PORTD ^= 1<
Buzz(6);
}
PORTC |= 1<
Genset_Starting_Protocol(3);
}
else
{
if (amblight > 40) Genset_Starting_Protocol(2);
else Genset_Starting_Protocol(1);
}
else
{
Send_A_StringToMrLCDWithLocation(1,1,"starter Motor");
Send_A_StringToMrLCDWithLocation(1,2,"Status -- Fail");
}

}


void Genset_Starting_Protocol(uint8_t ctrl)
{
if (ctrl == 1)
{
Genset_Cranking(); //normal start
}
if (ctrl == 2)
{
Delay(100); //delayed start after 10 sec
Genset_Cranking();
}
if (ctrl == 3)
{
while (bit_is_set(PINC,1))
{
if(bit_is_set(PINC,3))
{
Buzz(50);
Genset_Cranking(); //forced start only by manual switching
}
}
}
if (ctrl == 4)
{
Genset_Cranking(); //maintenance run
}

}

void Genset_Cranking()
{
Genset_Stmotor(15);
if(bit_is_set(PINC,1))
{
Genset_Stmotor(20);
}
if (bit_is_set(PINC,1))
{
Genset_Stmotor(25);
}
if(bit_is_set(PINC,1))
{
Send_A_StringToMrLCDWithLocation(1,1,"Auto Start Fail");
Send_A_StringToMrLCDWithLocation(1,1,"Use Ignition Key");
}
}


void Genset_Stmotor(uint8_t crnktime)
{
PORTD |= 1<
Delay(crnktime);
PORTD &= ~(1<
Delay(50);
}


void Genset_Running()
{
TCCR1B|=((1<
//Timer1 is used as 1 sec time base
//Timer Clock = 1/1024 of sys clock
//Mode = CTC (Clear Timer On Compare)
OCR1A=976; //Compare value=976
TIMSK|=(1<
//Output compare 1A interrupt enable
Send_A_StringToMrLCDWithLocation(1,1,"Time = 00:00:00");
}


ISR(TIMER1_COMPA_vect) //CPU Jumps here every 1 sec exactly!
{
rpm=count*60;
count=0;
++seconds;
if(seconds<10 div="">
{
Send_A_StringToMrLCDWithLocation(13,1,":0");
send_an_integertoMrLCDwithlocation(15,1,seconds,1);
}
else send_an_integertoMrLCDwithlocation(14,1,seconds,1);
if (seconds==60)
{
seconds=0;
++minute;
if (minute<10 div="">
{
Send_A_StringToMrLCDWithLocation(10,1,":0");
send_an_integertoMrLCDwithlocation(12,1,minute,1);
}
else send_an_integertoMrLCDwithlocation(11,1,minute,1);
}
if (minute==60)
{
++hour;
minute=0;
if (hour<10 div="">
{
Send_A_StringToMrLCDWithLocation(8,1,"0");
send_an_integertoMrLCDwithlocation(9,1,hour,1);
}
else send_an_integertoMrLCDwithlocation(8,1,hour,1);
}
}

void Genset_Stop()
{
DDRD |= 1<< PIND4;
PORTD |= 1<< PIND4;
Delay(50);
while (bit_is_clear(PINC,1))
{
}
PORTD &= ~ (1<< PIND4);
}

void Genset_Stopped()
{
}



*/