Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
justinjohney
PostPosted: Jun 18, 2012 - 01:59 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

im a beginer to avr
what is wrong with this code
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int adc_result=0;
int ch=0;

int main (void)
{
//set portC as input and enable the pull up resistor
DDRC=0x00;
PORTC=0xFF;

//set up port D 0-5  as output& C 6,7 as input
DDRD=0x3F;

// Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);


// Set ADC to Free-Running Mode
ADCSRB &=~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)) ; 
 
// Enable ADC
ADCSRA |= (1 << ADEN); 
// Enable ADC Interrupt
//ADCSRA |= (1 << ADIE); 
   
// Enable Global Interrupts
 //sei(); 


 
   
// Loop Forever
    while(1)

       {
      // Set ADC reference to AREF
      // Left adjust ADC result to allow easy 10 bit reading
      //select adc channel 1
      ADMUX=0x00;

         for(ch=0;ch<3;ch++)
          {
             // Start A2D Conversions
            ADCSRA |= (1 << ADSC);
      
            //Read the adc result
            adc_result=ADCW;
                _delay_ms(10);
         
            if(adc_result>970)
            {
               if(ch==0)
               {
                  PORTD1=1;
                  _delay_ms(50);

                     if(adc_result>970)
                        {
                           PORTB4=1;
                        }
               }

               else if(ch==1)
               {

                  PORTD2=1;
                  _delay_ms(50);
                     if(adc_result>970)
                        {
                           PORTB4=1;
                        }
               }
               else(ch==2)
               {
                  PORTD2=1;
                  _delay_ms(50);
                     if(adc_result>970)
                        {
                           PORTB4=1;
                        }


            }
            ADMUX++;
         }

   }
   
}



 







 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 02:25 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 26115
Location: Wisconsin USA

Quote:

what is wrong with this code

What AVR model are you working with?
What do you expect to happen?
What is happening?
How are you testing?
What problem(s) are you encountering?

The entire logic of the program is strange. The value is checked for 970--and then again checked. How can it change?

A conversion is started and the result taken--without waiting for the conversion to complete.

In free-running mode, you can easily be grabbing the result from the "previous" channel.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 18, 2012 - 02:34 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Code:
             // Start A2D Conversions
            ADCSRA |= (1 << ADSC);
     
            //Read the adc result
            adc_result=ADCW;

Did we perhaps forget to do something in the middle here? Contrary to popular belief the result in ADCW will not be available just a few cycles after you set the ADSC bit. Try putting a:
Code:
while(ADCSRA & (1<<ADSC)));

between those lines to sit and wait while ADSC remains high - it will return to 0 and hence end the while() loop when the conversion is complete.
Code:
                  PORTD1=1;

I'd be interested to see the definition of "PORTD1". True, if you use something like Peter Danegger's sbit.h then something like this might be valid syntax but I see no such #include of the necessary .h file.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 02:42 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 26115
Location: Wisconsin USA

Quote:

Code:
PORTD1=1;

I'd be interested to see the definition of "PORTD1".

lol -- I missed that in my first read-through. Wouldn't you get something like a "not an lvalue" error?


Quote:
Error: C:\AtmelC\TestProgs\M88t.c(15): the expression must be a modifiable lvalue
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 18, 2012 - 05:45 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

clawson wrote:

Code:
                  PORTD1=1;

I'd be interested to see the definition of "PORTD1".

You will find that in .inc files for inclusion in an assembly source though it is equated as a bit number exactly the same as PD1 is. Definitely a non-working piece of code here.

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 18, 2012 - 05:49 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

You will find that in .inc files for inclusion in an assembly source

Two pints of whatever you are smoking. How are .inc files involved in the use of avr-gcc?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 18, 2012 - 07:09 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

clawson wrote:
Quote:

You will find that in .inc files for inclusion in an assembly source

Two pints of whatever you are smoking. How are .inc files involved in the use of avr-gcc?

They're not, but since you asked, I pointed out where they "are" defined. Wink

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 18, 2012 - 09:14 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

OT:
Quote:
Two pints of whatever you are smoking.

LOL! A really crap day can always be saved by something like this.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 10:24 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 26115
Location: Wisconsin USA

Quote:

OT:
Quote:
Two pints of whatever you are smoking.

LOL! A really crap day can always be saved by something like this.


Try "I'll have what she's having", from "When Harry Met Sally".
http://www.youtube.com/watch?v=F-bsf2x-aeE
Start at about 1:30.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 18, 2012 - 10:30 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

Quote:
Try "I'll have what she's having", from "When Harry Met Sally".

Seen that a few times, yup.

But this... http://www.youtube.com/watch?v=qJP8_6wKNso (round about 30 seconds). Absolutely priceless!
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
larryvc
PostPosted: Jun 18, 2012 - 10:49 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

I just want to know how you two got soooo OT from Johan's previous post. Laughing

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 11:15 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 26115
Location: Wisconsin USA

It was >>Cliff's fault<< for mixing his metaphors.

No, wait--the root cause was >>you<< -- inserting [the low-level programming technique that shall not be mentioned] into a high-brow C thread.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 05:32 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

theusch wrote

Code:
What AVR model are you working with?

AVR Studio 4, GCC compiler
Code:
What do you expect to happen?

actually i want to convert three analog signal into digtal simultaneously(with max speed) and compare it with threashold and if any one it is greater than the threashold then a led must glow corresponding to that signal.Moreover if the condition stable for a particular time then there must be get an o/p on a port pin at a particular pin.
Code:

What problem(s) are you encountering?


build error,i know the code is not completed



i think now you got it. can you help me?[/code]
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 19, 2012 - 06:58 AM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

Did you read clawson's post two posts ater your first post?

http://www.avrfreaks.net/index.php?name ... 434#966434

What does PORTD1=1; do?

Have you written any code to just blink an LED yet?

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 19, 2012 - 08:47 AM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

justinjohney wrote:
theusch wrote
Quote:
What AVR model are you working with?

AVR Studio 4, GCC compiler

Thanks for telling us the tool-chain, but what does this have to do with the AVR model (=the chip on which your program is intended to run)?

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 19, 2012 - 09:21 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

Quote:
actually i want to convert three analog signal into digtal simultaneously(with max speed) and compare it with threashold and if any one it is greater than the threashold then a led must glow corresponding to that signal.

That rang a bell. So I digged through earlier threads by the OP, and:

http://www.avrfreaks.net/index.php?name ... 571#962571

http://www.avrfreaks.net/index.php?name ... 310#964310

Hey Justin! Please stay in one thread with your work on these ADC-and-threshold questions.

MODERATOR: Perhaps the other two threads should be locked? [done - clawson]
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Jun 19, 2012 - 12:45 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

I asked if the signals were AC or DC. You said the signals were 50Hz AC. I assume you want to read the peak or the average amplitude of these signals, which will require either some digital signal processing to determine the peak or average of a group of samples, or a diode and an RC low pass filter to 'catch' the signal for 20ms so the a/d converter can read it. I felt sort of miffed that you didn't mention that you had read my suggestion and understood it.

_________________
Imagecraft compiler user


Last edited by bobgardner on Jun 19, 2012 - 10:11 PM; edited 1 time in total
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 01:26 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

atmega168
the following code shows error in avr studio, avr gcc compiler.
Code:
#include <avr/io.h>
#include <stdio.h> 
#include <avr/interrupt.h>
#include <util/delay.h>

//system clock @16MHz
#define F_CPU 16000000


//Global variable
int adc_result[2]={0,0,0};
uint8_t ch=0;


/* PORT INIT */
void port_init (void)
{
//set portB.1 as output
DDRB |=(1<<PB1);
PORTB&=~(1<<PB1);

//set portC0,1,2, as input and enable the pull up resistor
DDRC &=~(1<<PC0)|~(1<<PC1)|~(1<<PC2);
PORTC |=(1<<PC0)|(1<<PC1)|(1<<PC2);

//set up port D 0-2  as output& D6,7 as input with pull up resistor
DDRD &=~(1<<PC0)|~(1<<PC1)|~(1<<PC2);
DDRD &=~(1<<PD6)|~(1<<PD7);
PORTD |=(1<<PD6)|(1<<PD6);

}


void init_adc(void)
{
// Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);


// Set ADC to Free-Running Mode
//ADCSRB &=~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)) ;

//select adc channel 0
//ADMUX=ch;

// Enable ADC Interrupt
ADCSRA |= (1 << ADIE); 

// Enable ADC
ADCSRA |= (1 << ADEN); 


// Enable Global Interrupts
sei();

//Start first conversion
ADCSRA |= (1<<ADSC);
}



/* Interrupt service routines */

ISR(ADC_vect)
{

//delay  for discarding first conversion result
//_delay_ms(10);
//Read the adc result
   switch(ch)
   {
      case 0:
         {
         
         adc_result[0]=ADCW;
         
         if(adc_result[0]>862)
            {

               do
               {
               PORTD|=(1<<PD0);//glow the led corresponding to signal 1
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               adc_result[0]=ADCW;
                  if(adc_result[0]>887)
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB0);//  o/p drive if the state at adc0 exist for a particular time      
                     }while (adc_result[0]<970)
                  }   
               }while(adc_result[0]<862)

            }

         else if (adc_result[0]<754)
            {
            PORTD|=(1<<PD3);//glow the led corresponding to UV indication
            }
         else
            {
               if(ch<3)
               {

                  ch++;
                  ADMUX=ch;
               }
               else
               {
                  ch=0;
                  ADMUX=ch;
               }
            }
         break;
         }

      case 1:
         {
         adc_result[1]=ADCW;
      
         if(adc_result[1]>862)
            {

               do
               {
               PORTD|=(1<<PD1);//glow the led corresponding to signal 2
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               adc_result[0]=ADCW;
                  if(adc_result[0]>887)
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB0);//  o/p drive if the state at adc1 exist for a particular time      
                     }while (adc_result[1]<862)
                  }
               }while(adc_result[1]<862)

            }
         else if(adc_result[1]<754)
            {
            PORTD|=(1<<PD3);//glow the led corresponding to UV indication
            }
         else
            {
               if(ch<2)
               {
                  ch++;
                  ADMUX=ch;
               }
               else
               {
                  ch=0;
                  ADMUX=ch;
               }
            }
         break;
         }

      case 2:
         {
         adc_result[2]=ADCW;
         //Start next  conversion
         ADCSRA |= (1<<ADSC);
         if(adc_result[2]>862)
            {

               do
               {
               PORTD|=(1<<PD2);//glow the led corresponding to signal 3
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               adc_result[0]=ADCW;
               if(adc_result[2]>887)
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB0);//  o/p drive if the state at adc2 exist for a particular time      
                     }while (adc_result[2]<862)
                  }
               }while(adc_result[2]<862)

            }
         else if(adc_result[2]<754)
            {
            PORTD|=(1<<PD3);//glow the led corresponding to UV indication
            }
         else
            {
               if(ch<3)
               {
                  ch++;
                  ADMUX=ch;
               }
               else
               {
                  ch=0;
                  ADMUX=ch;
               }
            }
         break;
         }
   }
}


void main()
{

   port_init();

   

// Loop Forever
    while(1)

       {
      ADMUX=ch;
      init_adc();
      
      }         
}

 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 19, 2012 - 01:28 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Saying what the error is would help as then we wouldn't have to compile it ourselves (I'm not in a position to do so in my current location).

BTW you do know that warnings/errors include both the filename and the line number (test.c:109 is line 109 of test.c) so you should be able to identify the exact line where the error is located.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 19, 2012 - 01:40 PM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

Code:
int adc_result[3]={0,0,0};

instead of
Code:
int adc_result[2]={0,0,0};


Code:
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2));

instead of
Code:
DDRC &=~(1<<PC0)|~(1<<PC1)|~(1<<PC2);

(replace on all occurances).


Code:
// Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

Are you sure, that you are running on 16 MHz and not on the internal 1 MHz ( 8 MHz / 8 ) as this is default?

Put all stuff from your ISR outside since you do it in polling-mode and all this stuff including the delays should never be done inside an ISR.

I suggest you to read the tutorial on bit-manipulation

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 01:50 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

tnanx Mr.mtaschl
i changed the code and compile but again it shows error

B
Quote:
uild started 19.6.2012 at 18:17:29
avr-gcc -mmcu=atmega168 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT protector_circuit.o -MF dep/protector_circuit.o.d -c ../protector_circuit.c
In file included from ../protector_circuit.c:4:0:
c:\program files\atmel\avr tools\avr toolchain\bin\../lib/gcc/avr/4.5.1/../../../../avr/include/util/delay.h:89:3: warning: #warning "F_CPU not defined for <util/delay.h>"
../protector_circuit.c:7:0: warning: "F_CPU" redefined
c:\program files\atmel\avr tools\avr toolchain\bin\../lib/gcc/avr/4.5.1/../../../../avr/include/util/delay.h:90:0: note: this is the location of the previous definition
../protector_circuit.c: In function '__vector_21':
../protector_circuit.c:95:7: error: expected ';' before '}' token
../protector_circuit.c:98:5: error: expected expression before '}' token
../protector_circuit.c:100:4: error: expected 'while' before 'else'
../protector_circuit.c:104:4: error: expected '}' before 'else'
../protector_circuit.c:143:7: error: expected ';' before '}' token
../protector_circuit.c:146:5: error: expected expression before '}' token
../protector_circuit.c:147:4: error: expected 'while' before 'else'
../protector_circuit.c:151:4: error: expected '}' before 'else'
../protector_circuit.c:190:7: error: expected ';' before '}' token
../protector_circuit.c:193:5: error: expected expression before '}' token
../protector_circuit.c:194:4: error: expected 'while' before 'else'
../protector_circuit.c:198:4: error: expected '}' before 'else'
make: *** [protector_circuit.o] Error 1
Build failed with 12 errors and 2 warnings...
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 19, 2012 - 01:54 PM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

do - while constructs need to be terminated with a semikolon.
Code:
do {
   // code
} while (/* condition */);

They are all missing in your ISR.

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 01:59 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

thanx. problem solved
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 02:01 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

can i use mutiple adc along with analog comparator with inturrupts
in a project?
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 19, 2012 - 02:03 PM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

You can, but your concept is a polling-concept. IMO forget about interrupts and continue or rethink and start from the scratch again.

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 02:05 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

if an interrupt occur then then the control will go to ISR_()of that paricular interrupt.
after the process in the ISR , where to go the control?
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 19, 2012 - 02:09 PM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

It will return to the instruction next to that one that has been executed before the ISR.

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 19, 2012 - 10:18 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Get it working without interrupts. A conversion takes about 120usec. Write a subroutine that inits the a/d converter. Do not use free running mode. Write a subroutine that reads the given channel of the a/d converter by settting the channel in the ADMUX without stomping on the top 2 bits that set the ref, the poll the ADSC bit until it goes low. This only takes about 120usec. Read the result and return. Store the result.
Perhaps we can gain some insight to how to best solve your design problem if you explain why you require simultaneous reading of the 3 channels. This feature is used in sonar (search for simultaneous sampling converter on chip mfgr sites) where the time arrival of the returns must be grabbed on all inputs with the same usec timing relation. Using an AVR you read channel 0 then 1 then 2 120usec apart. Good enough?

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 20, 2012 - 05:31 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i make my avr clock into 16MHz
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 20, 2012 - 05:42 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Use a 16 MHz crystal.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 20, 2012 - 06:41 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i simulate my adc using atmel studio4 gcc compiler?
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 20, 2012 - 07:50 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i compare between two analog voltages using adc, for ensure that there must be a constant vge differece two between 2 signal. if the vge difference between the signal exceeds then an led must glow.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 20, 2012 - 08:30 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

! or ~ which is meant for NOT operation in avr studio 4, gcc compiler
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 20, 2012 - 08:41 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

That depends what kind of NOT operation you want. Both are supported by the avr-gcc compiler, and are applicable in different scenarios.

Open your C textbook and read about it.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 20, 2012 - 08:48 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

code shows error again and i cant predict the behaviour


Code:



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

//system clock @1MHz
#define F_CPU 1000000


//Global variable
int adc_result[3]={0,0,0};//for storing the adc out put
uint8_t ch=0;


/* PORT INIT */
void port_init (void)
{
//set portB.1 as output drive for relay
DDRB |=(1<<PB1);
PORTB&=~(1<<PB1);

//set portC0-4 as input and enable the pull up resistor
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));// ADC i/p R,B,Y & Gnd, neutral
PORTC |=((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));

//set up port D 0-3  as output
DDRD &=~((1<<PD0)|(1<<PD1)|(1<<PD2)|(1<<PD3));//o/p led for R,B,Y and GLP
}


void init_adc(void)
{
// Set ADC prescaler to 8 - 125KHz sample rate @ 1MHz
ADCSRA |= ((1 << ADPS1) | (1 << ADPS0));

// Enable ADC Interrupt
ADCSRA |= (1 << ADIE); 

// Enable ADC
ADCSRA |= (1 << ADEN); 

// Enable Global Interrupts
sei();

ADCSRA |= (1<<ADSC);

}




/* Interrupt service routine for Analog comparator*/
ISR(ANA_COMP)
{

}





/* Interrupt service routines */

ISR(ADC_vect)
{

//delay  for discarding first conversion result
//_delay_ms(10);
//Read the adc result
   switch(ch)
   {
      case 0:
         {
               
         adc_result[0]=ADCW;
         
         if(adc_result[0]>823) //checking for vge >265V
            {

               do
               {
               PORTD|=(1<<PD0);//glow the led corresponding to signal 1
               _delay_ms(3);
               //Start next  conversion with same channel
               ADCSRA |= (1<<ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1<<ADIF)));
               adc_result[0]=ADCW;
                  if(adc_result[0]>837) //checking for vge >270V
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     //wait for coversion to be completed
                     while (!(ADCSRA & (1<<ADIF)));
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB1);//  o/p drive ON if the state at adc0 exist for a particular time      
                     }while (adc_result[0]<823);//do until the vge reduced to 265V
                  }   
               }while(adc_result[0]>823);
               PORTD&=~(1<<PD0);//off the led corresponding to signal 1
               PORTB&=~(1<<PB1);// o/p drive off for ON the relay

            }

         else if (adc_result[0]<713)//checking for vge <230V
            {
               do
               {

                  PORTD|=(1<<PD3);//glow the led corresponding to UV indication
                  ADCSRA |= (1<<ADSC);
                  //wait for coversion to be completed
                  while (!(ADCSRA & (1<<ADIF)));
                  adc_result[0]=ADCW;
               }while(adc_result[0]<713);//do until vge >230V
               PORTD&=~(1<<PD3);//off the led corresponding to UV indication
            }
         else
            {
               if(ch<3)
               {

                  ch++;//change adc channel
                  ADMUX=ch;
               }   
            }
         break;
         }

      case 1:
         {
         adc_result[1]=ADCW;
      
         if(adc_result[1]>823)//checking for vge >265V
            {

               do
               {
               PORTD|=(1<<PD1);//glow the led corresponding to signal 2
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1<<ADIF)));
               adc_result[0]=ADCW;
                  if(adc_result[0]>837) //checking for vge >270V
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     //wait for coversion to be completed
                     while (!(ADCSRA & (1<<ADIF)));
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB1);//  o/p drive if the state at adc1 exist for a particular time      
                     }while (adc_result[1]>862);
                  }
               }while(adc_result[1]>823);
               PORTD|=(1<<PD1);//off the led corresponding to signal 2
               PORTB&=~(1<<PB1);// o/p drive off for ON the relay
            }
         else if(adc_result[1]<713)//checking for vge <230V
            {
               do   
                  {

                  PORTD|=(1<<PD3);//glow the led corresponding to UV indication
                  ADCSRA |= (1<<ADSC);
                  //wait for coversion to be completed
                  while (!(ADCSRA & (1<<ADIF)));
                  adc_result[0]=ADCW;
                  }while(adc_result[0]<713);//do until vge >230V
                  PORTD&=~(1<<PD3);//off the led corresponding to UV indication
            }
         else
            {
               if(ch<3)
               {
                  ch++;
                  ADMUX=ch;
               }
            }
         break;
         }

      case 2:
         {
         adc_result[2]=ADCW;
         //Start next  conversion
         ADCSRA |= (1<<ADSC);
         if(adc_result[2]>862)//checking for vge >265V
            {

               do
               {
               PORTD|=(1<<PD2);//glow the led corresponding to signal 3
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1<<ADIF)));
               adc_result[0]=ADCW;
               if(adc_result[2]>837) //checking for vge >270V
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     //wait for coversion to be completed
                     while (!(ADCSRA & (1<<ADIF)));
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB1);//  o/p drive if the state at adc2 exist for a particular time      
                     }while (adc_result[2]<862);
                  }
               }while(adc_result[2]<862);
               PORTD|=(1<<PD2);//glow the led corresponding to signal 3
               PORTB&=~(1<<PB1);// o/p drive off for ON the relay

            }
         else if(adc_result[2]<713)//checking for vge <230V
            {
               do   
                  {

                  PORTD|=(1<<PD3);//glow the led corresponding to UV indication
                  ADCSRA |= (1<<ADSC);
                  //wait for coversion to be completed
                  while (!(ADCSRA & (1<<ADIF)));
                  adc_result[0]=ADCW;
                  }while(adc_result[0]<713);
                  PORTD&=~(1<<PD3);//off the led corresponding to UV indication
            }
         else
            {
               if(ch<3)
               {
                  ch++;
                  ADMUX=ch;
               }   
            }
         break;
         }


// GLP
      case 3:
         {
            adc_result[3]=ADCW;
            ADMUX=0x04;//select adc 4
            //Start next  conversion
            ADCSRA |= (1<<ADSC);
            //wait for coversion to be completed
            while (!(ADCSRA & (1<<ADIF)));
            adc_result[4]=ADCW;
               if((adc_result[3]-adc_result[4])>31)
               {
                  PORTD|=(1<<PD3);//ON the led corresponding to GLP indication
                  do
                  {
                     ADMUX=0x03;//select adc 3
                     //Start next  conversion
                     ADCSRA |= (1<<ADSC);
                     //wait for coversion to be completed
                     while (!(ADCSRA & (1<<ADIF)));
                     adc_result[3]=ADCW;
                     ADMUX=0x04;//select adc 4
                     //Start next  conversion
                     ADCSRA |= (1<<ADSC);
                     //wait for coversion to be completed
                     while (!(ADCSRA & (1<<ADIF)));
                     adc_result[4]=ADCW;
                        if((adc_result[3]-adc_result[4])>38)
                        {
                           PORTB|=(1<<PB1);// o/p drive
                        }

                  }while((adc_result[3]-adc_result[4])>31);

                  PORTD&=~(1<<PD3);//OFF the led corresponding to GLP indication
                  PORTB&=~(1<<PB1);// off the  o/p drive
               }   

         }

 



}


int main(void)
{
   
   
   port_init();
   //discarding the first adc result
   ADCSRA |= (1<<ADSC);
   //wait for coversion to be completed
   while (!(ADCSRA & (1<<ADIF)));
   ADMUX=ch;

// Loop Forever
    while(1)

       {
      init_adc();
      }         
}

 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 20, 2012 - 10:28 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Oh how I love cross-posts! OP also sent this to me in a PM. This was my reply there:


I built your code and got:
Code:
../test.c:53: warning: 'ANA_COMP' appears to be a misspelled signal handler
../test.c: In function '__vector_14':
../test.c:291: warning: 'main' is normally a non-static function
../test.c:308: error: expected declaration or statement at end of input

The first will depend on which AVR you are building for but on the one I tried (mega16) the name for that ISR vector is ANA_COMP_vect so I guess you missed the "_vect" on the end?

The warning about main() being non-static is almost certainly because you have unbalanced parentheses above so the compiler thinks main() is being defined within the function above. This would also explain the final error.

In part you could do yourself a huge favour by learning to indent your code properly. Then it will become immediately obvious when the number of { does not match the number of } for a particular function as the indentation either won't get back to the left margin or it will with an additional }.

There is a utility in WinAVR called indent.exe that you can use to reformat your code. Just use "indent file.c" and it will update file.c to have correct indentation.

When I run it on your file I even get:
Code:
E:\avr>indent test.c
indent: test.c:309: Error:Unexpected end of file

which once again shows that the use of {} is wrong. What it wrote in the process (I used indent -nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -nprs -npsl -sai -saf -saw -ncs -nsc -sob -nfca -cp33 -ss -ts8 test.c in fact) is:
Code:
#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>

//system clock @1MHz
#define F_CPU 1000000

//Global variable
int adc_result[3] = { 0, 0, 0 };   //for storing the adc out put
uint8_t ch = 0;

/* PORT INIT */
void port_init(void)
{
//set portB.1 as output drive for relay
   DDRB |= (1 << PB1);
   PORTB &= ~(1 << PB1);

//set portC0-4 as input and enable the pull up resistor
   DDRC &= ~((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3) | (1 << PC4));   // ADC i/p R,B,Y & Gnd, neutral
   PORTC |=
       ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3) | (1 << PC4));

//set up port D 0-3  as output
   DDRD &= ~((1 << PD0) | (1 << PD1) | (1 << PD2) | (1 << PD3));   //o/p led for R,B,Y and GLP
}

void init_adc(void)
{
// Set ADC prescaler to 8 - 125KHz sample rate @ 1MHz
   ADCSRA |= ((1 << ADPS1) | (1 << ADPS0));

// Enable ADC Interrupt
   ADCSRA |= (1 << ADIE);

// Enable ADC
   ADCSRA |= (1 << ADEN);

// Enable Global Interrupts
   sei();

   ADCSRA |= (1 << ADSC);

}

/* Interrupt service routine for Analog comparator*/
ISR(ANA_COMP)
{

}

/* Interrupt service routines */

ISR(ADC_vect)
{

//delay  for discarding first conversion result
//_delay_ms(10);
//Read the adc result
   switch (ch) {
   case 0:
      {

         adc_result[0] = ADCW;

         if (adc_result[0] > 823)   //checking for vge >265V
         {

            do {
               PORTD |= (1 << PD0);   //glow the led corresponding to signal 1
               _delay_ms(3);
               //Start next  conversion with same channel
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[0] = ADCW;
               if (adc_result[0] > 837)   //checking for vge >270V
               {
                  do {
                     ADCSRA |= (1 << ADSC);
                     //wait for coversion to be completed
                     while (!
                            (ADCSRA &
                        (1 << ADIF))) ;
                     adc_result[0] = ADCW;
                     PORTB |= (1 << PB1);   //  o/p drive ON if the state at adc0 exist for a particular time     
                  }
                  while (adc_result[0] < 823);   //do until the vge reduced to 265V
               }
            }
            while (adc_result[0] > 823);
            PORTD &= ~(1 << PD0);   //off the led corresponding to signal 1
            PORTB &= ~(1 << PB1);   // o/p drive off for ON the relay

         }

         else if (adc_result[0] < 713)   //checking for vge <230V
         {
            do {

               PORTD |= (1 << PD3);   //glow the led corresponding to UV indication
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[0] = ADCW;
            }
            while (adc_result[0] < 713);   //do until vge >230V
            PORTD &= ~(1 << PD3);   //off the led corresponding to UV indication
         } else {
            if (ch < 3) {

               ch++;   //change adc channel
               ADMUX = ch;
            }
         }
         break;
      }

   case 1:
      {
         adc_result[1] = ADCW;

         if (adc_result[1] > 823)   //checking for vge >265V
         {

            do {
               PORTD |= (1 << PD1);   //glow the led corresponding to signal 2
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[0] = ADCW;
               if (adc_result[0] > 837)   //checking for vge >270V
               {
                  do {
                     ADCSRA |= (1 << ADSC);
                     //wait for coversion to be completed
                     while (!
                            (ADCSRA &
                        (1 << ADIF))) ;
                     adc_result[0] = ADCW;
                     PORTB |= (1 << PB1);   //  o/p drive if the state at adc1 exist for a particular time     
                  }
                  while (adc_result[1] > 862);
               }
            }
            while (adc_result[1] > 823);
            PORTD |= (1 << PD1);   //off the led corresponding to signal 2
            PORTB &= ~(1 << PB1);   // o/p drive off for ON the relay
         } else if (adc_result[1] < 713)   //checking for vge <230V
         {
            do {

               PORTD |= (1 << PD3);   //glow the led corresponding to UV indication
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[0] = ADCW;
            }
            while (adc_result[0] < 713);   //do until vge >230V
            PORTD &= ~(1 << PD3);   //off the led corresponding to UV indication
         } else {
            if (ch < 3) {
               ch++;
               ADMUX = ch;
            }
         }
         break;
      }

   case 2:
      {
         adc_result[2] = ADCW;
         //Start next  conversion
         ADCSRA |= (1 << ADSC);
         if (adc_result[2] > 862)   //checking for vge >265V
         {

            do {
               PORTD |= (1 << PD2);   //glow the led corresponding to signal 3
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[0] = ADCW;
               if (adc_result[2] > 837)   //checking for vge >270V
               {
                  do {
                     ADCSRA |= (1 << ADSC);
                     //wait for coversion to be completed
                     while (!
                            (ADCSRA &
                        (1 << ADIF))) ;
                     adc_result[0] = ADCW;
                     PORTB |= (1 << PB1);   //  o/p drive if the state at adc2 exist for a particular time     
                  }
                  while (adc_result[2] < 862);
               }
            }
            while (adc_result[2] < 862);
            PORTD |= (1 << PD2);   //glow the led corresponding to signal 3
            PORTB &= ~(1 << PB1);   // o/p drive off for ON the relay

         } else if (adc_result[2] < 713)   //checking for vge <230V
         {
            do {

               PORTD |= (1 << PD3);   //glow the led corresponding to UV indication
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[0] = ADCW;
            }
            while (adc_result[0] < 713);
            PORTD &= ~(1 << PD3);   //off the led corresponding to UV indication
         } else {
            if (ch < 3) {
               ch++;
               ADMUX = ch;
            }
         }
         break;
      }

// GLP
   case 3:
      {
         adc_result[3] = ADCW;
         ADMUX = 0x04;   //select adc 4
         //Start next  conversion
         ADCSRA |= (1 << ADSC);
         //wait for coversion to be completed
         while (!(ADCSRA & (1 << ADIF))) ;
         adc_result[4] = ADCW;
         if ((adc_result[3] - adc_result[4]) > 31) {
            PORTD |= (1 << PD3);   //ON the led corresponding to GLP indication
            do {
               ADMUX = 0x03;   //select adc 3
               //Start next  conversion
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[3] = ADCW;
               ADMUX = 0x04;   //select adc 4
               //Start next  conversion
               ADCSRA |= (1 << ADSC);
               //wait for coversion to be completed
               while (!(ADCSRA & (1 << ADIF))) ;
               adc_result[4] = ADCW;
               if ((adc_result[3] - adc_result[4]) >
                   38) {
                  PORTB |= (1 << PB1);   // o/p drive
               }

            }
            while ((adc_result[3] - adc_result[4]) > 31);

            PORTD &= ~(1 << PD3);   //OFF the led corresponding to GLP indication
            PORTB &= ~(1 << PB1);   // off the  o/p drive
         }

      }

   }

   int main(void) {

      port_init();
      //discarding the first adc result
      ADCSRA |= (1 << ADSC);
      //wait for coversion to be completed
      while (!(ADCSRA & (1 << ADIF))) ;
      ADMUX = ch;

// Loop Forever
      while (1) {
         init_adc();
      }
   }

As you can see - indentation has not returned to the right margin by the end.

Can I just say that this program shows very poor design. For one thing you should not be doing everything in an ISR() but for another you should never write code so complex that you run the risk of losing where the indentation level is in the first place! If a function is that complex then break it into several smaller functions. For example you have a large switch() in there - make each case: a separately called function.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 20, 2012 - 12:59 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

OT:
Quote:

This was my reply there

How about as many of us a possible try to enforce a principle not to answer any questions in PMs if it could be discussed in open in a forum.

I have a standard answer that I just paste into a reply PM, which essentially says "In all friendliness: take it to the forums".
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 20, 2012 - 01:48 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

clawson
i did as you told. but it again shows 2 error

Code:
#include <avr/io.h>
#include <stdio.h> 
#include <avr/interrupt.h>
#include <util/delay.h>

#define F_CPU 1000000 //system clock @1MHz

//Global variable
int adc_result[5]={0,0,0,0,0};//for storing the adc out put
uint8_t ch=0;
uint8_t y=0;
uint8_t i=0;
int k=0;

/* PORT INIT */
void port_init (void)
{
DDRB |=(1<<PB1);//set portB.1 as output drive for relay
PORTB&=~(1<<PB1);//initially set 0 as o/p drive
DDRB |=(1<<PB0)//error occur

//set portC0-4 as i/p with pull up resistor
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));// ADC i/p R,B,Y & Gnd, neutral
PORTC |=((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));

//set up port D 0-4  as output
DDRD &=~((1<<PD0)|(1<<PD1)|(1<<PD2)|(1<<PD3)|(1<<PD4));//o/p led for R,B,Y and GLP
}

void init_adc(void)
{
ADCSRA |= ((1 << ADPS1) | (1 << ADPS0));// Set ADC prescaler to 8 - 125KHz sample rate @ 1MHz
ADCSRA |= (1 << ADEN);  // Enable ADC
ADCSRA |= (1<<ADSC);//start conversion
}

void init_glp(void)
{
   i=ch;
   ADMUX=ch;
   init_adc();
   adc_result[i]=ADCW;
}



int main(void)
{
   i=ch;
   port_init();
   ADMUX=ch;
   init_adc();
   ADCSRA |= (1<<ADSC);//discarding the first adc result
   
// Loop Forever
    while(1)
       {
         ch=0;
         init_adc();
         if(ch<3)
         {
            for(ch=0;ch<3;ch++)
               {   
                  ADMUX=ch;
                  init_adc();
                  adc_result[i]=ADCW;
                  if (adc_result[i]>795)//checking for 265V
                  {
                     PORTD=y;
                     _delay_ms(1);
                     init_adc();
                     adc_result[i]=ADCW;
                     if(adc_result[i]>810)//checking for 270V
                     {
                        do
                        {
                           PORTB|=(1<<PB1); //  o/p drive ON
                           init_adc();
                           _delay_ms(1);
                           adc_result[i]=ADCW;
                        }while(adc_result[i]>795);
                        PORTD=0x00;//off the indicator led
                        PORTB&=~(1<<PB1);// o/p drive off
                     }
                  }
                  else(adc_result[i]<690)//checking for 230V
                  {
                     do
                     {
                        PORTD|=(1<<PD3);// on the led corresponding to UV indication
                        init_adc();
                        adc_result[i]=ADCW;
                     }while(adc_result[i]<690);//checking for 230V
                  }
               y++;
               }
               y=0;
            }   
         else if((2<ch)&&(ch<5))
         {
            ch=3;
            init_glp();
            ch++;
            init_glp();
            k=(adc_result[4]-adc_result[3]);
            if(k>30)//checking for 230V
            {
               PORTD|=(1<<PD4);//ON the led corresponding to GLP indication
               _delay_ms(1);
               ch=3;
               init_glp();
               ch++;
               init_glp();
               k=(adc_result[4]-adc_result[3]);
               if(k>45)//checking for 15V difference
               {
                  do
                  {
                     PORTB|=(1<<PB1); //  o/p drive ON
                     ch=3;
                     init_glp();
                     ch++;
                     init_glp();
                     k=(adc_result[4]-adc_result[3]);
                  }while(k>30);//checking for 15V difference
                  PORTB&=~(1<<PB1);// o/p drive off
                  PORTD&=~(1<<PD4);//OFF the led corresponding to GLP indication
               }
            
            }
         }
         else
         {
            PORTB|=(1<<PB0);
         }
      }
}
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 20, 2012 - 02:25 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

Quote:

i did as you told. but it again shows 2 error

WHAT errors?!

Please copy the error messages verbatim from your build output window, and paste them here.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 20, 2012 - 03:05 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

rm -rf technic_protector.o technic_protector.elf dep/* technic_protector.hex technic_protector.eep technic_protector.lss technic_protector.map
rm: cannot lstat `dep/*': Invalid argument
make: [clean] Error 1 (ignored)
Build succeeded with 0 Warnings...
avr-gcc -mmcu=atmega168 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT technic_protector.o -MF dep/technic_protector.o.d -c ../technic_protector.c
In file included from ../technic_protector.c:14:0:
c:\program files\atmel\avr tools\avr toolchain\bin\../lib/gcc/avr/4.5.1/../../../../avr/include/util/delay.h:89:3: warning: #warning "F_CPU not defined for <util/delay.h>"
../technic_protector.c:16:0: warning: "F_CPU" redefined
c:\program files\atmel\avr tools\avr toolchain\bin\../lib/gcc/avr/4.5.1/../../../../avr/include/util/delay.h:90:0: note: this is the location of the previous definition
../technic_protector.c: In function 'port_init':
../technic_protector.c:33:1: error: called object '1' is not a function
../technic_protector.c: In function 'main':
../technic_protector.c:96:11: warning: statement with no effect
../technic_protector.c:97:7: error: expected ';' before '{' token
make: *** [technic_protector.o] Error 1
Build failed with 2 errors and 3 warnings...
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 20, 2012 - 03:10 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

I find this almost amusing. So I took your code and built it and got the same errors you see. So I double clicked the first error to go to the source line involved and it says:
Code:
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));// ADC i/p R,B,Y & Gnd, neutral

To be honest I cannot see anything wrong with that line so I do the natural thing and start to look back to a previous line where the actual error may be. I don't have to go very far before I get to:
Code:
DDRB |=(1<<PB0)//error occur

//set portC0-4 as i/p with pull up resistor
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));// ADC i/p R,B,Y & Gnd, neutral

The comment even says "// error occur". Do you not see why? That line has no terminating semi-colon (just because it ends in a comment does not make the semi-colon statement terminator optional!)

After fixing that one the error that remains is:
Code:
                  else(adc_result[i]<690)//checking for 230V
                  {

I wonder IF you are missing some keyword in that line? Wink

Having fixed those two lines the code compiles without error - of course there's no guarantee that it actually does what you hope when it runs!

You need to learn to do what I just did - when you get an error (or a warning - don't just ignore those) you need to take the line number given in the message and go to that line in the source (most IDE let you double-click on the message to get there). Then study the line that's been highlighted. Either it has an obvious error (like your second error here missing "if") or if the line looks OK the chances are the problem has actually occurred on a previous line (which can sometimes even mean in a #include'd .h file) and you have to look back through the code for anything that looks suspicious - like your missing semi-colon.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 21, 2012 - 08:47 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

So, you have...

1) A definition of F_CPU that is in the wrong place, or possibly redundant

and

2) Likely a missing semicolon somewhere above line 97 in protector.c
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 21, 2012 - 08:59 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i simulate this code with analog signal.
I want to know about the behaviour of the code?


Code:
#include <avr/io.h>
#include <stdio.h> 
#include <avr/interrupt.h>
#include <util/delay.h>

//# define F_CPU 1000000UL //system clock @1MHz

//Global variable
int adc_result[5]={0,0,0,0,0};//for storing the adc out put
uint8_t ch=0;
uint8_t y=0;
uint8_t i=0;
int k=0;

/* PORT INIT */
void port_init (void)
{
DDRB |=(1<<PB1);//set portB.1 as output drive for relay
PORTB&=~(1<<PB1);//initially set 0 as o/p drive
DDRB |=(1<<PB0);//error occur

//set portC0-4 as i/p with pull up resistor
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));// ADC i/p R,B,Y & Gnd, neutral
PORTC |=((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));

//set up port D 0-4  as output
DDRD &=~((1<<PD0)|(1<<PD1)|(1<<PD2)|(1<<PD3)|(1<<PD4));//o/p led for R,B,Y and GLP
}

void init_adc(void)
{
ADCSRA |= ((1 << ADPS1) | (1 << ADPS0));// Set ADC prescaler to 8 - 125KHz sample rate @ 1MHz
ADCSRA |= (1 << ADEN);  // Enable ADC
ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
ADCSRA |= (1<<ADSC);//start conversion
while (!(ADCSRA & (1<<ADIF)));//wait for coversion to be completed

}

void init_glp(void)
{
   i=ch;
   ADMUX=ch;
   init_adc();
   adc_result[i]=ADCW;
}



int main(void)
{
   i=ch;
   port_init();
   ADMUX=ch;
   init_adc();
   ADCSRA |= (1<<ADSC);//discarding the first adc result
   
// Loop Forever
    while(1)
       {
         ch=0;
         init_adc();
         if(ch<3)
         {
            for(ch=0;ch<3;ch++)
               {
               {   
                  ADMUX=ch;
                  init_adc();
                  adc_result[i]=ADCW;
                  if (adc_result[i]>795)//checking for 265V
                  {
                     PORTD=y;//OV indication led on
                     _delay_ms(1);
                     init_adc();
                     adc_result[i]=ADCW;
                        while(adc_result[i]>810)//checking for 270V
                        {
                           do
                           {
                              PORTB|=(1<<PB1); //  o/p drive ON
                              init_adc();
                              _delay_ms(1);
                              adc_result[i]=ADCW;
                           }while(adc_result[i]>795);
                           PORTD=0x00;//off the OV indicator led
                           PORTB&=~(1<<PB1);// o/p drive off
                        }
                  }
                  else if(adc_result[i]<690)//checking for 230V
                     {
                        do
                        {
                           PORTD|=(1<<PD3);// on the led corresponding to UV indication
                           init_adc();
                           adc_result[i]=ADCW;
                        }while(adc_result[i]<690);//checking for 230V
                     }
                  y++;
               }
            }
            y=0;
         }   
         else if((2<ch)&&(ch<5))
         {
            ch=3;
            init_glp();
            ch++;
            init_glp();
            k=(adc_result[4]-adc_result[3]);
            if(k>30)//checking for 230V
            {
               PORTD|=(1<<PD4);//ON the led corresponding to GLP indication
               _delay_ms(1);
               ch=3;
               init_glp();
               ch++;
               init_glp();
               k=(adc_result[4]-adc_result[3]);
               if(k>45)//checking for 15V difference
               {
                  do
                  {
                     PORTB|=(1<<PB1); //  o/p drive ON
                     ch=3;
                     init_glp();
                     ch++;
                     init_glp();
                     k=(adc_result[4]-adc_result[3]);
                  }while(k>30);//checking for 15V difference
                  PORTB&=~(1<<PB1);// o/p drive off
                  PORTD&=~(1<<PD4);//OFF the led corresponding to GLP indication
               }
            
            }
         }
         else
         {
            PORTB|=(1<<PB0);
         }
      }
}
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 21, 2012 - 09:16 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i make a stimuly file for above code?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 21, 2012 - 10:17 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

how can i make a stimuly file for above code?

What did the user manual tell you?

(oh and just to be clear AS4.19 has the most advanced stimuli functionality ever produced by Atmel. AS5 and AS6 have not yet got the same functionality so you must use Studio 4.19 if you want to be able to write stimuli scripts).

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 21, 2012 - 12:33 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i write write stimuli scripts?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 21, 2012 - 12:36 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

how can i write write stimuli scripts?

By following the guidance in the manual. It's what all other programmers trying to use the feature do too. Or did you want someone to read the manual out to you or something? This is the example shown in the manual - it even shows them faking values in to the ADC registers:
Code:
The example also shows use of logging and break directives.
 
// Initial delay
#100
// Set up logging ADC and ADCSRA to file adc.log
$log ADCL
$log ADCH
$log ADCSRA
$startlog adc.log
// start of repeat loop
$repeat 100
// Assuming TCNT1 is running, use as data for ADC
ADCL = *TCNT1L
ADCH = *TCNT1H
// Set ADIF flag in ADCSRA, this will trigger ADCC interrupt
ADCSRA |= 0x10
#30
$endrep
// Stop logging (close log file)
$stoplog
// break program execution
$break

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 21, 2012 - 12:43 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

Cliff! It seems it is time for following the new policy and just walk away.

Justin! Over several threads you have repeated more or less the same question. You have been given directions as to what to study, but we have seen no or little response or reaction to those advice. Instead a similar question is just posed again.

While it may not be so, it makes you come through as either lazy or spoiled or possibly both. In the past, such a situation might have led to "flaming" or harsch language. There is a new policy in place here at AVRfreaks which means that in such a situation there should be no flaming or harsh words, but rather that one should just walk away and not comment on the question(s) at all.

To be blunt: We are not here to spoon-feed you a solution. We are here because we are looking for interesting discussions, because we are genuinely interested in seeing other people learn how to solve AVR-related problems, or we might be here and answer just to rub our egos. Nothing in your threads fulfils any of those goals for anyone of us.

I will now walk away.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Jun 21, 2012 - 12:49 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

First thing that I saw was lots of calls to init_adc(). Seems like the a/d should just be initialized once at the top of the program. Then it looks like the actual reading of the a/d is sort of distributed all over with lots of redundancy. The concept of 'subroutine' is applicable here. Why not have one function called readadchan(n) that selects admux channel n, inits conversion, polls the adsc bit till it goes low, then returns the value of the a/d? Then you could even have another function called readadchannels() that calls readadchan(n) in a loop. Then your main loop would be something like a while(1) loop with a call to readadchans() and processadvalues(). That would be easier to format and indent because it would be shorter and simpler. Unlike this message.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 21, 2012 - 02:02 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

thanx Mr bobgardner for your answer and patience.

here is the my updated code
is it look to be a standared code? can we predicts its nature

Code:
# define F_CPU 1000000UL //system clock @1MHz


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


//Global variable
uint16_t adc_result[5]={0,0,0,0,0};//for storing the adc out put
uint8_t ch=0;
uint8_t y=0;
uint8_t i=0;
int k=0;

/* PORT INIT */
void port_init (void)
{
DDRB |=(1<<PB1);//set portB.1 as output drive for relay
PORTB&=~(1<<PB1);//initially set 0 as o/p drive
DDRB |=(1<<PB0);//error occur

//set portC0-4 as i/p with pull up resistor
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));// ADC i/p R,B,Y & Gnd, neutral
PORTC |=((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));

//set up port D 0-4  as output
DDRD &=~((1<<PD0)|(1<<PD1)|(1<<PD2)|(1<<PD3)|(1<<PD4));//o/p led for OV,UV and GLP
}

void init_adc(void)
{
   ADCSRA |= ((1 << ADPS1) | (1 << ADPS0));// Set ADC prescaler to 8 - 125KHz sample rate @ 1MHz
   ADCSRA |= (1 << ADEN);  // Enable ADC
}

void read_adc(void)
{
   _delay_ms(1);
   ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
   ADCSRA |= (1<<ADSC);//start conversion
   while (!(ADCSRA & (1<<ADIF)));//wait for coversion to be completed
   adc_result[i]=ADCW;
}

void init_glp(void)
{
   i=ch;
   ADMUX=ch;
   read_adc();
   adc_result[i]=ADCW;
}
void glp(void)
{
   init_glp();
   ch++;
   init_glp();
   k=(adc_result[4]-adc_result[3]);
}



int main(void)
{
   i=ch;
   port_init();
   ADMUX=ch;
   init_adc();
   read_adc();
   
// Loop Forever
    while(1)
       {
         ch=0;
         if(ch<3)
         {
            for(ch=0;ch<3;ch++)
               {
                  ADMUX=ch;
                  read_adc();
                  if (adc_result[i]>792)//checking for 255V
                  {
                     PORTD|=(1<<PD0);//OV indication led on
                     _delay_ms(1);
                     read_adc();
                        while(adc_result[i]>823)//checking for 265V
                        {
                           do
                           {
                              PORTB|=(1<<PB1); //  o/p drive ON
                              read_adc();
                           
                           }while(adc_result[i]>792);//Should be in the state off until voltage reaches under 255V
                           PORTD=0x00;//off the OV indicator led
                           PORTB&=~(1<<PB1);// o/p drive off
                        }
                  }
                  else if(adc_result[i]<621)//checking for 200V
                  {
                     
                           PORTD|=(1<<PD3);// on the led corresponding to UV indication
                           read_adc();
                           while(adc_result[i]<495)// checking for 160V
                           {
                              do
                              {
                                 PORTB|=(1<<PB1); //  o/p drive ON
                                 read_adc();
                              }while(adc_result[i]<543);   _delay_ms(1);
                              PORTB&=~(1<<PB1);// o/p drive off
                           }
                        PORTD&=~(1<<PD3);//Off the UV indicator
                  }
                  i++;
               }
               ch++;
         }
         
//GLP SECTION
            
         else if((2<ch)&&(ch<5))
         {
            {
               ch=3;
               glp();
               if(k>22)//checking for 7V
               {
                  PORTD|=(1<<PD4);//ON the led corresponding to GLP indication
                  _delay_ms(1);
                  ch=3;
                  glp();
                  if(k>22)//checking for 9V difference
                  {
                     do
                     {
                        PORTB|=(1<<PB1); //  o/p drive ON
                        ch=3;
                        glp();
                     }while(k>3);//Should be in the state off until voltage reaches under 7V
                     PORTB&=~(1<<PB1);// o/p drive off
                     PORTD&=~(1<<PD4);//OFF the led corresponding to GLP indication
                  }
               }
            }
            ch=0;
         }
         else
         {
            PORTB|=(1<<PB0);
         }
      }
}


________________________________________
Be keep patience to those who needs it.

Justin
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 21, 2012 - 02:07 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Change this:
Code:
   ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
   ADCSRA |= (1<<ADSC);//start conversion
   while (!(ADCSRA & (1<<ADIF)));//wait for coversion to be completed
   adc_result[i]=ADCW;

For one thing you don't have to use ADIE to be able to use ADIF but if you do use ADIF then because it goes to 1 at the end you have to reset it. Using ADSC to determine when the conversion completes is a better idea as it automatically returns to 0.
to this
Code:
   ADCSRA |= (1<<ADSC);//start conversion
   while ((ADCSRA & (1<<ADSC)));//wait for coversion to be completed
   adc_result[i]=ADCW;

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 21, 2012 - 03:46 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

I recommend that programs should be tested and debugged without the use of interrupts from the a/d and uarts. Using interrupts means you have to turn them on and off and write the interrupt handlers correctly and name the interrupt vectors correctly, and its just generally error prone for newer programmers. Interrupts are used to get better performance as the program gets bigger and slower. This program isnt there yet.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 22, 2012 - 11:20 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

thanx Mr.bobgardner
i update my code as
Code:

# define F_CPU 1000000UL //system clock @1MHz
#define THRES_255V   792
#define   THRES_265V   823
#define   THRES_200V   621
#define   THRES_160V   495
#define   THRES_175V   543
#define   THRES_9V   28
#define   THRES_7V   22


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


//Global variable
uint16_t adc_result[5]={0,0,0,0,0};//for storing the adc out put
uint8_t ch=0;
uint8_t y=0;
uint8_t i=0;
int k=0;

/* PORT INIT */
void port_init (void)
{
DDRB |=(1<<PB1);//set portB.1 as output drive for relay
PORTB&=~(1<<PB1);//initially set 0 as o/p drive
DDRB |=(1<<PB0);//error occur pin

//set portC0-4 as i/p with pull up resistor
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));// ADC i/p R,B,Y & Gnd, neutral
PORTC |=((1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC4));

//set up port D 0-4  as output
DDRD &=~((1<<PD0)|(1<<PD1)|(1<<PD2)|(1<<PD3)|(1<<PD4));//o/p led for OV,UV and GLP
}

void init_adc(void)
{
   ADCSRA |= ((1 << ADPS1) | (1 << ADPS0));// Set ADC prescaler to 8 - 125KHz sample rate @ 1MHz
   ADCSRA |= (1 << ADEN);  // Enable ADC
}

void read_adc(void)
{
   i=ch;
   _delay_ms(1);
    ADCSRA |= (1<<ADSC);//start conversion
   while ((ADCSRA & (1<<ADSC)));//wait for coversion to be completed
      adc_result[i]=ADCW;
}

void init_glp(void)
{
   ADMUX=ch;
   read_adc();
}
void glp(void)
{
   init_glp();
   ch++;
   init_glp();
   k=(adc_result[4]-adc_result[3]);
}



int main(void)
{
   i=0;
   i=ch;
   port_init();
   ADMUX=ch;
   init_adc();
   read_adc();
   
// Loop Forever
    while(1)
       {

         PORTD&=~(1<<PD0);//OV indication led off

         if(ch<3)
         {
            for(ch=0;ch<3;ch++)
               {
                  ADMUX=ch;
                  read_adc();
                  if (adc_result[i]>THRES_255V)//checking for 255V
                  {
                     PORTD|=(1<<PD0);//OV indication led on
                     _delay_ms(1);
                     read_adc();
                        while(adc_result[i]>THRES_265V)//checking for 265V
                        {
                           do
                           {
                              PORTB|=(1<<PB1); //  o/p drive ON
                              read_adc();
                           
                           }while(adc_result[i]>THRES_255V);//Should be in the state off until voltage reaches under 255V
                           PORTD=0x00;//off the OV indicator led
                           PORTB&=~(1<<PB1);// o/p drive off
                        }
                  }
                  else if(adc_result[i]<THRES_200V)//checking for 200V
                  {
                     
                           PORTD|=(1<<PD3);// on the led corresponding to UV indication
                           read_adc();
                           while(adc_result[i]<THRES_160V)// checking for 160V
                           {
                              do
                              {
                                 PORTB|=(1<<PB1); //  o/p drive ON
                                 read_adc();
                              }while(adc_result[i]<THRES_175V);
                              PORTB&=~(1<<PB1);// o/p drive off
                           }
                        PORTD&=~(1<<PD3);//Off the UV indicator
                  }
               }
            ch++;
         }
         
//GLP SECTION
            
         else if((2<ch)&&(ch<5))
         {
            {   
               ch=3;
               glp();
               if(k>THRES_7V)
               {
                  PORTD|=(1<<PD4);//ON the led corresponding to GLP indication
                  _delay_ms(1);
                  ch=3;
                  glp();
                  if(k>THRES_9V)
                  {
                     do
                     {
                        PORTB|=(1<<PB1); //  o/p drive ON
                        ch=3;
                        glp();
                     }while(k>THRES_7V);
                     PORTB&=~(1<<PB1);// o/p drive off
                     PORTD&=~(1<<PD4);//OFF the led corresponding to GLP indication
                  }
               }
               else
               {
                  PORTD&=~(1<<PD4);//off the led corresponding to GLP indication
               }

            }
            ch=0;
         }
         else
         {
            PORTB|=(1<<PB0);// Error in microcontroller programming
            ch=0;
         }
      }
}

 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 22, 2012 - 11:22 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

# define F_CPU 1000000UL
is 1 MHz is the default system clock?
is i need to add any extra oscillator in my ciruit for setting system clock?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 22, 2012 - 11:46 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

# define F_CPU 1000000UL
is 1 MHz is the default system clock?

It is. So if you haven't changed fuses that is likely the speed the AVR is running at. The reason you set that #define in the code is simply so that _delay_ms() and _delay_us() can have a good go at working out how many cycles to run for to delay for the given number of milli or micro seconds.

I cannot see anything else in your code that makes use of F_CPU.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 23, 2012 - 07:12 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India



Last edited by justinjohney on Jun 23, 2012 - 08:18 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 23, 2012 - 07:33 AM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

how can i...? how can i...? I am not trying to be rude or callous, but, how can we make you understand that there is a point that you will have to sit down and figure out some things on your own like everybody else here has done or is doing. Most of us here have a lot of patience and will go to extremes at times to help someone understand whatever it is they don't understand. There is a limit to that patience, and when we see that there is no initiative on the part of the person we are helping, then that patience ends.

Start to take the initiative and look things up on your own by using google, bing, baidu or whatever search engine you have access to.

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 23, 2012 - 03:30 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

I'd just like to see Justin write a small program that reads one a/d channel and prints the result in a loop. You can test it with a pot from 5V to gnd. Should see smooth change from 0x000 to 0x3ff. The next program could read 3 channels and print them out. At least give us a spec of what a glp is and what the R G and Y represent. Its like a puzzle. If you need design help, explain the requirements. If you want program debugging help, listen to the suggestions that the programmers post. If you need programming help, show us your whack at the read-an-a/d-channel-and-print-it-out program.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 25, 2012 - 11:40 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

is a direct 5v ac can be supplied to adc pin of atmega series?is it need to be rectified?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 25, 2012 - 11:50 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Is that 5V AC with a 2.5V DC offset (so the entire wave is in the positive domain or is it centred at 0V so it +2.5V and -2.5V at the peaks? If the latter you are going to find your AVR does not like negative voltages (be ready with a butterfly net to catch the smoke cloud!). If the signal needs to be offset I think our sparky friends would advise the use of an op-amp.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 25, 2012 - 12:52 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

there is a problem occur in my design. the resistive divider does not ensure an isolation.i dont want to use transformer(for saving placeand cost).
can i use optocoupler? is optocoupler is able to give a voltage fluctuation?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 25, 2012 - 01:10 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

is optocoupler is able to give a voltage fluctuation?

No.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 25, 2012 - 01:43 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

if so i have a doubt Mr.clawson
if the input of the optocoupler vaies between 180-260v then the light emitting from the diode varies which limit the o/p voltage. is it true?
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 25, 2012 - 06:13 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

Well, 160-260 volts seem very dangerous.

Perhaps your initial idea of a transformer was a good one (if you want AC sampled with some realism: how can you do with optocouplers, which are very nonlinear if voltage signs change -they vary between -260 and 260 volts, do not they ?)

Perhaps it would be wise to try first testing some code , first trying a channel, with a continuous low voltage supply: when your code will work, addding 2 other channels and trying to be as close as possible to your final data processing (and the idea of what you want to do) would be very useful to you, and keep you alive.

Therefore Mr Bobgardner gave you valuable (perhaps even life saving) advices three posts before.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 27, 2012 - 06:17 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

can i use o/p of full bridge rectifier as input to adc in atmega168
 
 View user's profile Send private message  
Reply with quote Back to top
snigelen
PostPosted: Jun 27, 2012 - 07:15 AM
Posting Freak


Joined: Jan 08, 2009
Posts: 1199
Location: Lund, Sweden

Yes, as long as the voltage is in valid range, which usually means within -0.5V -- Vcc+0.5V (those are absolute maximum ratings).
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 27, 2012 - 09:08 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

thanx snigelen
actually i want to sense the line voltage using resister
drop method and there is no isolation.i dont want to use transformer because i need a small device.
 
 View user's profile Send private message  
Reply with quote Back to top
snigelen
PostPosted: Jun 27, 2012 - 10:10 AM
Posting Freak


Joined: Jan 08, 2009
Posts: 1199
Location: Lund, Sweden

I wouldn't mess with unisolated line voltage or recommend anyone to do it. It can kill people or burn down the house if something goes wrong.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 27, 2012 - 11:52 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

How about a small transformer? There are some the size of a walnut at Skycraft.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 27, 2012 - 12:06 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

i think the bridge give isolation. actually in my circuit ,i drop the 230 V to less than 1V and given to bridge.and output(unregulated dc) of that bridge to input pin of adc.
actually i'm worriying about the unregulated dc?is this unregulated dc consider as a analog signal?
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 27, 2012 - 12:26 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

A bridge (rectifier) does not give isolation, it just rectifies the two waves of the mains (and, if you hold [with damp hands]a card connected with the mains, you might be killed or shocked).

A transfo does give isolation, without the non linearities of an optocoupler (if your resistive voltage divider divides down to ca 1v, you are likely in a very non linear region of its internal IR LEDs; perhaps your optocoupler has a name, and even an application note google can find).
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 27, 2012 - 12:33 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

im not going to use optocoupler. what about a transistor bias for taking the linearity?
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 27, 2012 - 01:04 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

Can your design be drawed (use kicad, inkscape, gimp, tuxpaint or {scan|put into your CDreader} a pencil and paper schema and post it) It is very difficult to figure out what you want to do with the mains and it seems dangerous?

Did you test your adc conversion routines, and did they work (this is less dangerous than playing with the mains!) ?
Did you code your data processing, and test it with values you know a solution for?
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 27, 2012 - 01:13 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

no, im not test it.
the only thing confuse me is is a un regulated dc (o/p of bridge)act as a input to adc.are you try it?
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 27, 2012 - 01:44 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

I did not try anything, because

a) if your hardware (I cannot figure out what it is) is connected to the mains, it can be dangerous and I do not want to commit suicide.

b) if ***your*** unregulated dc goes above the supply of the avr (or becomes negative, with poor wiring), it would kill the avr.... and it would be annoying.

FYI I usually test with a voltmeter http://de.wikipedia.org/wiki/Voltmeter unregulated dc s I buy, before using them....

And your software works so well it does not need being tested?
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 30, 2012 - 12:37 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

is their any need for external capacitor(oscillator) for using 1MHz system clock?
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 30, 2012 - 01:39 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Maybe on the aref pin depending on how much ripple in the power supply.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 04, 2012 - 12:52 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

Can i give regulated bridge o/p as Vcc to atmega series?
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jul 04, 2012 - 12:59 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

a) What is the value of your regulated bridge output?
b) Is it really regulated (post a photo or the schematics if you do not know)?
c) Is it above the value you want to measure (I dare to remember it was your initial topic) and within the admissible range of your atmega (you may have 200 volt regulated)?
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 04, 2012 - 01:38 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

no, its from 7805 fixed regulator. for Vcc
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jul 04, 2012 - 02:24 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Lots of smart guys interested in your project. Time to draw up a nice clear block diagram, maybe a short description of what it is supposed to do. You'll get about a dozen messages full of good improvements and suggestions about 4 hours after the picture appears.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
indianajones11
PostPosted: Jul 04, 2012 - 05:05 PM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3628
Location: San Diego, Ca

Rolling Eyes Justin, you never told people what was wrong with the ADC code. I assume you fixed it Rolling Eyes since you've moved on to another topic. You don't know what you're doing/talking about with high voltage mains and you need to leave that alone. WHY would you want to do an adc of the boring 50-60 Hz mains voltage anyway ?!

Since you will do it anyway...you BETTER use a transformer ...period ! Confused

_________________
1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 05, 2012 - 07:16 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i insert my pdf schematic here.
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jul 05, 2012 - 07:24 AM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

The IMG button allows you to insert an image.
The option "add an attachment" allows you to add a file .
It becomes activated when you preview before posting.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jul 05, 2012 - 08:08 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

Quote:
The option "add an attachment" allows you to add a file .
It becomes activated when you preview before posting.


Or if you start out writing your post by clicking the "new reply" button rather than using the "Quick Reply" area.


Last edited by JohanEkdahl on Jul 05, 2012 - 08:26 AM; edited 1 time in total
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 05, 2012 - 08:08 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

this is my schematics.
components values are not shown here
comment on it.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 05, 2012 - 01:51 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

actually i'm using millivolt sensing. thats why i discarded the transformer. moreover i need a small one.
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jul 05, 2012 - 02:14 PM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

justinjohney wrote:
comment on it

All bypass caps missing.
U2 need caps on input and output (I can't read the type, refer to the datasheet, typically >>10uF)
For U3: Read AVR042 Hardware Design Considerations

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jul 05, 2012 - 03:20 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Its not much clearer to me what it does yet. I was sort of hoping for the paragraph description too. There are several flavors of three phase... wye, delta. I see yours has a neutral, so I guess it possible to measure amplitude to neutral, but the bridge rectifiers dont work if the 'ac legs' and the 'minus dc' have the same ground. Every bridge rectifier I have ever seen is driven from a transformer secondary where one leg of the ac is going up and ther other leg is going down symmetrically. And another question... why measure the amplitude of each leg, if they are all the same? If they have different amplitudes, I need some more explanation.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 06, 2012 - 08:22 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how much current needed for atmega series for VCC at 5V?i think its about 360mA
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jul 06, 2012 - 10:30 AM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

Well, there is an issue with your supplies:
you seem to use an 78**l**05, with a to92package (at most 100 mA); you should instead use a 7805 with a TO3 package (up to 1 A).
You should bewaare, when you read the datasheet, that the input (0.3 uF) and output (0.1 uF)capacitors are meant (avoiding high frequency oscillations) for continuous unregulated input: for input coming from a bridge, one add a 100-200uf, to filter the main "residuals" (both 100-200 uF and 0.3uf are useful, because electrolytic capacitors do not behave well at high frquencies).


To be sure your atmega is not starving, you might use a simple version of the soft you are developping (ex : only one aDC channel tested) and test everything is OK at starting time, and say, 1 hour after (the regulator has to dissipate (15*1.4 - 5)*0.4 = 11 watts, messeems : when it grows too hot, it stops functioning without furtehr damages, leading to oscillations).
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 06, 2012 - 01:39 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

you are write TO 220 package is needed for regulator. and by pass capacitor are needed.
but for how much current will drawn by atmega168?
is it around 300mA?
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 06, 2012 - 01:44 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

@ BOBGARDNER
actually the dc - and ac legs are not connected, they are isolated.
Quote:
why measure the amplitude of each leg, if they are all the same? If they have different amplitudes, I need some more explanation.
actually the 4 legs are RBY and earth.i take some samples and compare it thats only.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jul 06, 2012 - 02:23 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

AVR should only be using several 10s of ma.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
dbrion0606
PostPosted: Jul 06, 2012 - 02:48 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

Quote:
"you are write TO 220 package is needed for regulator. and by pass capacitor are needed.
but for how much current will drawn by atmega168?
is it around 300mA?"


No I wrote TO92 package would lead to a maximal current of 100 mA; the TO92 package is in the pdf you posted, and is consistent with the regulators name...

Now, let us look at what will be eaten :

* there is a LEd , LED2 -the first one..- which will eat ca 2 mA (resistor value known).
there are 3 LEDS, 3,4,5 whose resistor value is unknown : let us assume 10 mA each...
There remains the ATMega168:
according to http://www.atmel.com/Images/doc2545.pdf, page 304 (out of 377), it eats up to 12 mA at 8Mhz, with settings.
That sums up to about 45 mA (if nothing is forgotten) ... far enough from 100 mA to be reliable.

Whence do you write atmega168 draws 300 mA

Quote:
i think its about 360mA


? (in this hypothesis, you cannot consistently use the regulator in a TO92 package, a TO220 or TO3 package regulator would be needed, though more expensive and it will likely need an heathsink!).

If you google search for 7805 datasheets, tests circuits are only given with a continuous unnoisy input, leading to low bypass value, useful at high frequencies. It needs, too, a filtering capacitor to filter the non continuous, low frequency part of the rectified supply....
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jul 06, 2012 - 04:12 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Wikipedia says the 3 phase colors in India are Red Blue and Yellow, so I think you are building an overvoltage and undervoltage checker. I'd get 3 little transformers. They can be small because they are not providing any power. Use a comparator to detect the zero crossing of the reference phase, then you can take a voltage reading on that phase 3.333ms later, then the next sample at the peak of the next phase 6.666ms later. I'd scale the voltage so the peak voltage out of the secondary is about 4V. This will let you read a little over voltage.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 12, 2012 - 09:36 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

if i'm give a regulated bridge output to ADC pin ,is it convert that voltage into a digit.(ie, between 0 and 1024)
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jul 12, 2012 - 01:22 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

justinjohney wrote:
if i'm give a regulated bridge output to ADC pin ,is it convert that voltage into a digit.(ie, between 0 and 1024)


This has a lot of translation problems. My translator infers this as:
"If I supply the AC mains rectified (not regulated) and it is 370 volts, and I apply that to an AVR pin that can only handle 5V, will it convert the voltage into a 10 bit binary number than can be represented in decimal as 5 ascii digits?"

My answer would be no. You obviously need to attenuate or 'divide down' the rectified voltage. There is still a problem with what 'ground reference' to use to measure the mains. You can measure to neutral if you have one. When half a dozen experienced engineers recommend using an isolation transformer, they aren't typing just to exercise their fingers.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 13, 2012 - 05:50 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

yes i,m using an isolation transformer and give it to adc pin below 1V. (millivolt sensing)
then what happens
Code:
if i'm give a regulated bridge output to ADC pin ,is it convert that voltage into a digit.(ie, between 0 and 1024)
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jul 13, 2012 - 01:21 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

OK, your input signal is a +-1V peak sine wave. The a/d input on the AVR can't handle the negative part of the input, so you need some 'signal conditioning' like an opamp to bias it to the middle of the a/d range. If 5V is on AVCC, a 2.5V bias is good. A 10 bit binary number that can be printed in decimal as 0 to 1023 is not a digit in the way I use the term. It is 4 decimal digits.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 14, 2012 - 07:13 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

no it is from full bridge output.ie,0-1v
my doubt is that is i need to use capacitor for regulation to give the signal into adc pin?
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jul 15, 2012 - 01:39 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

I've never heard of a capacitor being used as a regulator. Maybe a dc blocking function, or a low pass function. Might I inquire once again about where the two ac inputs to the bridge rectifier are connected? Neutral and a phase? Or line to line? I'm under the impression that you dont just connect a fullwave bridge across the line to line voltage on a three phase system. I dont think you can get full wave recitification from neutral and one phase either. Someone else will say that's ok or not ok. I always use a fw bridge on a transformer out. Has magnetic isolation.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 16, 2012 - 06:50 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

Code:
230v L1___/\/\/\__|__/\/\/\____|---|-->(<1v)toADC pin
            R1    |    R2      |   |       
                  /            |FWR|
                  \R3          |   |
                  /            |   |
Neutarl___________|____________|   |-->to gnd
                               |---|
 

similar for L2 and L3
my question is that is there need a capacitor(.1uf/.01uf)at the o/p of bridge?
i also try to use this bridge o/p signal of L1-3 for checking phase sequence checking.
you already comment on my topic regarding phase sequence detection.i didn't get a proper ckt till now for phase sequence checking.

http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=975621#975621
L1-


Last edited by justinjohney on Jul 16, 2012 - 10:40 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jul 16, 2012 - 09:38 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden

Justin!

If you surround your figure with CODE tags it will be readable.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jul 16, 2012 - 10:40 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

@JohanEkdahl
thanx
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jul 16, 2012 - 10:46 AM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

What does FWR mean?
If it is a diode, then you/(your soft , if it exists and has been tested) will be only able to analyse the positive half of the signal;

if it is a bridge rectifier, you / (eventually your soft) can detect overvoltages on both polarities, at least if they are not high enough to burn the AVR... (perhaps some protection diode between the ADC input and the +5v might be useful to avoid such a drama).
In both cases, no capacities are needed.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 17, 2012 - 05:29 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

FWR-full wave rectifier
actually my requrement is to make a phase sequence detector which need to be work in coporated with my 3 phase protection circuit(OV and UV).
which is discussed in
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=121987
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jul 17, 2012 - 10:33 AM
Posting Freak


Joined: Jan 07, 2012
Posts: 1316
Location: North of France

Well, I already that you require a phase detector and that you were told to detect a rising edge near zero.

But how can you detect rising edges near zero with a full wave rectifier?

Note this is a question.
Another question might be How can one get help with providing incomplete, time varying information?
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 17, 2012 - 10:52 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

@dbrion0606
Quote:
But how can you detect rising edges near zero with a full wave rectifier?

ya .thats a problem need to be solved.
Quote:

Another question might be How can one get help with providing incomplete, time varying information?

but i cant understand what you mean?

is anyone have a circuit diagram for phase sequence detection?
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 20, 2012 - 06:16 AM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

how can i check wheather a i/p port pin is high or low?
Code:

       uint8_t p=9;

   do
   {
    _delay_ms(10);
      while(PINB&&(1<<PB7))
      {
         p=1;//phase sequence OK
      }
      
   }while(p=9);

it doesnt come out from the loop?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jul 20, 2012 - 09:46 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

it doesnt come out from the loop?


Look in your C manual for the difference between '&&' and '&' Wink

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
sternst
PostPosted: Jul 20, 2012 - 09:49 AM
Raving lunatic


Joined: Jul 23, 2001
Posts: 2472
Location: Osnabrueck, Germany

clawson wrote:
Quote:

it doesnt come out from the loop?


Look in your C manual for the difference between '&&' and '&' Wink
And for the difference between '=' and '=='. Wink

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jul 20, 2012 - 12:34 PM
Hangaround


Joined: Jun 04, 2012
Posts: 304
Location: Mumbai,India

Code:
if((PINB&0x80)==0x80)
      {
         p=1;//phase sequence OK
      }   

problem fixed
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits