djc
Advanced Member level 1
Hello all,
I have made little hardware. It has two relays. One is for controlling a cooler fan and another one is to control power to the electrical equipment like soldering iron and all. Power will be supplied by grid tied invert-er. I am reading ADC of solar panels. Once ADC of panel is above one threshold, relay controlling power to electrical equipment will be on. When ADC of panel will reach another upper threshold level, a cooler fan will be on. Now when ADC is decreasing, first of all, cooler fan will be Off, then if ADC still goes down further a mains relay will be off. And cycle goes on. Now my issue is, i wrote two programs. First one is as follows.
This code works on the hardware i have designed. I tested it on mikroc pro for AVR board too,by varying voltage on ADC channel 0 using pot. Now i decided to change the logic for program and wrote something like this with same configuration.
Now this is not working at all, whether on my designed hardware or on mikroc pro AVR board. Can anybody please guide me whats wrong with this logic. I have tried so many times. Created new project, changed computer, however result is same. There is "#include<built_in.h>" at first in both the codes. I don't know why it is not showing up there.
I have made little hardware. It has two relays. One is for controlling a cooler fan and another one is to control power to the electrical equipment like soldering iron and all. Power will be supplied by grid tied invert-er. I am reading ADC of solar panels. Once ADC of panel is above one threshold, relay controlling power to electrical equipment will be on. When ADC of panel will reach another upper threshold level, a cooler fan will be on. Now when ADC is decreasing, first of all, cooler fan will be Off, then if ADC still goes down further a mains relay will be off. And cycle goes on. Now my issue is, i wrote two programs. First one is as follows.
Code:
#include [COLOR="#000000"]<[/COLOR]built_in.h>
#define Mains_relay1 PORTD.B7
#define Mains_relay2 PORTB.B0
#define Cooler_relay1 PORTD.B2
#define Cooler_relay2 PORTD.B3
#define Load_Relay PORTB.B1
unsigned int ADC_Old=0,i=0;
void Blink_Led(unsigned int del ){
PORTD.B6 = 1;
Vdelay_ms(del);
PORTD.B6 = 0;
Vdelay_ms(del);
}
void main() {
DDRB=(0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (1<<DDB1) | (1<<DDB0);
PORTB=(0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
DDRC=(0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
PORTC=(0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (0<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
PORTD=(1<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
Mains_relay1 = 0;
Mains_relay2 = 0;
Cooler_relay1 = 0;
Cooler_relay2 = 0;
PORTD.B6 = 0;
PORTD.B5 = 0;
Delay_ms(500);
ADC_Init();
Delay_ms(2000);
while(1){
PORTD.B5 = 0;
Blink_Led(1000);
ADC_Old = ADC_Read(0);
Delay_ms(100);
while(ADC_Old<200){
ADC_Old = ADC_Read(0);
Delay_ms(100);
Load_Relay=1;
Blink_Led(1000);
Delay_ms(3000);
}
Load_Relay = 0;
if((ADC_Old>220) && (ADC_Old<=300)){
Delay_ms(100);
ADC_Old = ADC_Read(0);
if((ADC_Old>220) && (ADC_Old<=300)){
while((ADC_Old>220) && (ADC_Old<=300)){
ADC_Old = ADC_Read(0);
Delay_ms(100);
PORTD.B5 = 1;
Delay_us(100);
Mains_relay1 = 1;
Mains_relay2 = 1;
Cooler_relay1 = 0;
Cooler_relay2 = 0;
Blink_Led(500);
}
}
}
if((ADC_Old>300)){
Delay_ms(100);
ADC_Old = ADC_Read(0);
if((ADC_Old>300)){
while((ADC_Old>300)){
ADC_Old = ADC_Read(0);
Delay_ms(100);
PORTD.B5 = 1;
Delay_us(100);
Mains_relay1 = 1;
Mains_relay2 = 1;
Cooler_relay1 = 1;
Cooler_relay2 = 1;
Blink_Led(200);
}
}
}
if(ADC_Old<150){
Delay_ms(100);
ADC_Old = ADC_Read(0);
if(ADC_Old<150){
ADC_Old = ADC_Read(0);
Delay_ms(100);
PORTD.B5 = 0;
Delay_us(100);
Cooler_relay1 = 0;
Cooler_relay2 = 0;
Mains_relay1 = 0;
Mains_relay2 = 0;
}
}
}
}
This code works on the hardware i have designed. I tested it on mikroc pro for AVR board too,by varying voltage on ADC channel 0 using pot. Now i decided to change the logic for program and wrote something like this with same configuration.
Code:
#include[COLOR="#000000"] <[/COLOR]built_in.h>
#define Mains_relay1 PORTD.B7
#define Mains_relay2 PORTB.B0
#define Cooler_relay1 PORTD.B2
#define Cooler_relay2 PORTD.B3
#define Load_Relay PORTB.B1
unsigned int ADC_Old=0,i=0;
char step=1;
void Blink_Led(unsigned int del ){
PORTD.B6 = 1;
Vdelay_ms(del);
PORTD.B6 = 0;
Vdelay_ms(del);
}
void Blink_Led_1(unsigned int del ){
PORTD.B5 = 1;
Vdelay_ms(del);
PORTD.B5 = 0;
Vdelay_ms(del);
}
void main() {
DDRB=(0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (1<<DDB1) | (1<<DDB0);
PORTB=(0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
DDRC=(0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
PORTC=(0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (0<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
PORTD=(1<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
Mains_relay1 = 0;
Mains_relay2 = 0;
Cooler_relay1 = 0;
Cooler_relay2 = 0;
PORTD.B6 = 0;
PORTD.B5 = 0;
Delay_ms(500);
ADC_Init();
Delay_ms(2000);
while(1){
//PORTD.B5 = 0;
Blink_Led(4000);
Load_Relay=0;
Mains_relay1 = 0;
Mains_relay2 = 0;
Cooler_relay1 = 0;
Cooler_relay2 = 0;
ADC_Old = ADC_Read(0);
Delay_ms(100);
if(step==1){
if((ADC_Old>0)&&(ADC_Old<=200)){
while((ADC_Old>0) &&(ADC_Old<=200)){
Load_Relay = 0;
ADC_Old = ADC_Read(0);
Delay_ms(100);
if(ADC_Old>200){
step=2;
break;
}
Blink_Led(1000);
}
}
if(ADC_Old>200){step=2;}
}
if(step==2){
if((ADC_Old>200)&&(ADC_Old<=400)){
while((ADC_Old>200)&&(ADC_Old<=400)){
Load_Relay = 1;
Mains_relay1 = 0;
Mains_relay1 = 0;
ADC_Old = ADC_Read(0);
Delay_ms(100);
if(ADC_Old>400){
step=3;
break;
}
if(ADC_Old<200){
step=1;
break;
}
Blink_Led(500);
}
if(ADC_Old>400){step=3;}
if(ADC_Old<200){step=1;}
}
}
if(step==3){
if((ADC_Old>400)&&(ADC_Old<=770)){
while((ADC_Old>400)&&(ADC_Old<=770)){
Load_Relay = 0;
Mains_relay1 = 1;
Mains_relay1 = 1;
ADC_Old = ADC_Read(0);
Delay_ms(100);
if(ADC_Old>770){
step=4;
break;
}
if(ADC_Old<400){
step=2;
break;
}
Blink_Led(200);
}
if(ADC_Old>770){step=4;PORTD.B6=0;}
if(ADC_Old<400){step=2;}
}
}
if(step==4){
if(ADC_Old>770){
while(ADC_Old>770){
Load_Relay = 0;
Mains_relay1 = 1;
Mains_relay1 = 1;
Cooler_relay1 = 1;
Cooler_relay1 = 1;
ADC_Old = ADC_Read(0);
Delay_ms(100);
if(ADC_Old<770){
//Load_Relay = 1;
Cooler_relay1 = 0;
Cooler_relay1 = 0;
step=3;
PORTD.B5=0;
break;
}
Blink_Led_1(500);
}
}
}
}
}
Now this is not working at all, whether on my designed hardware or on mikroc pro AVR board. Can anybody please guide me whats wrong with this logic. I have tried so many times. Created new project, changed computer, however result is same. There is "#include<built_in.h>" at first in both the codes. I don't know why it is not showing up there.
Last edited: