Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC_Init not working in mikroc pro for AVR using Atmega8.

Status
Not open for further replies.

djc

Advanced Member level 1
Joined
Jan 27, 2013
Messages
402
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
India
Activity points
4,554
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.
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:

Hi,

In both codes I miss a loop.
Usually something like "while (1){"

***
You should learn how to use interrupts and interrupt driven tasks.
Every "blinking of LEDs" in your case consumes 100% processing power and the processor can't do anything else.

Additionally you should considser to add hysteresis to your thresholds to avoid unnecessary ON/OFF cycles.

Klaus
 

What kind of statements are these?

Code:
DDRB=(0<220)&&(ADC_Old<=300)){

Code:
DDRB=(0<0)&&(ADC_Old<=200)){

DDRx are Data Direction Registers.

You need to Configure your Relay pins as output pins and ADC input pin as input pin.
 

Hello all,
In both the codes there is "while(1)" loop. I didn't get why it has been pasted like this here. And there is no line as such "DDRB=(0<220)&&(ADC_Old<=300)){" it has been pasted wrongly, though i copy and pasted whole thing at once, some of the things are missing. Let me try once more,


Code C# - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <built_in.h>
//#include <delay.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);
                       }
           }
           }
           
           }
}






Code C# - [expand]
1
2
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);





Code C# - [expand]
1
2
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);





Code C# - [expand]
1
2
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);


Here port setting is done. Which is not showing at all. I don't understand why.


Code C# - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
                       }
           }
           }
           
           }

 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top