Continue to Site

Welcome to EDAboard.com

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

[SOLVED] Advice in Understanding C Code Program

Status
Not open for further replies.

decapitary

Banned
Joined
Jul 16, 2014
Messages
62
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
0
some1 help me whats going on here! specially first ten lines.I know about that interrupt registerys I just dont understand LOAD part!!!! and please tell me that does this code do



some part of the code was not copied.



Code:
#define LOAD1 0
#define LOAD2 1
int main(void)
{
    volatile int clap1;
	DDRB|=(1<<LOAD1);
	DDRB|=(1<<LOAD2);
	PORTD|=(1<<2);
MCUCR|=((1<<ISC00)|(1<<ISC01));    
GICR|=(1<<INT0);
    sei();
    clap=0;
	clapflag=0;
	
	while(1)
    {
	
	clapflag2=clapflag;
	
		
		while(clapflag2==1)
		{
			_delay_ms(4000);
			        
		
	clap1=clap;

	if(clap1==2)
	{
		if(PINB&(1<<LOAD1))
		PORTB&=~(1<<LOAD1);
		else
		PORTB|=(1<<LOAD1);
	}
	else if (clap1==3)
	{
		if(PINB&(1<<LOAD2))
		PORTB&=~(1<<LOAD2);
		else
		PORTB|=(1<<LOAD2);
	}
	else if (clap1==4)
	{
		PORTB=0x00;
	}

	clap=0;
	clapflag=0;
	clapflag2=0;
		}
		
		}
 

In

Code C - [expand]
1
DDRB|=(1<<LOAD2);

1 is shifted LOAD2 times that is 1 times to the left and the result is ORed with DDRB and assigned to DDRB. It DDRB was initially 0x00 then after this instruction DDRB.1 would be 1 and other bits 0.

In

Code C - [expand]
1
if(PINB&(1<<LOAD1))



1 is left shifted LOAD1 times that is 0 times that is no shifting, so, 1 will be 1 and PINB is ANDed with 1 and if true that is if bit 0 if PINB is 1 then if block executes.
 
In

Code C - [expand]
1
DDRB|=(1<<LOAD2);

1 is shifted LOAD2 times that is 1 times to the left and the result is ORed with DDRB and assigned to DDRB. It DDRB was initially 0x00 then after this instruction DDRB.1 would be 1 and other bits 0.

In

Code C - [expand]
1
if(PINB&(1<<LOAD1))



1 is left shifted LOAD1 times that is 0 times that is no shifting, so, 1 will be 1 and PINB is ANDed with 1 and if true that is if bit 0 if PINB is 1 then if block executes.

WHat is shifted where?!!!
x |=z means x=x|z
I dont understand << and DDRB |=(1<<LOAD2)
 

<< = Left shift and >> = Right shift. If DDRB was initially 0b00000000 and a (1 << LOAD2) is assigned to it then 1 << LOAD2 = 1 << 1 = 0x02 and so DDRB will be 0x02.

Let us assume DDRB was initially 0b00000000 and LOAD2 is 1

Then if

Code C - [expand]
1
DDRB |=(1<<LOAD2)

is executed

1<<LOAD2 = 1 << 1 = 1 * 2 = 2

DDRB = DDRB | 2

DDRB = 2
 
<< is the operator to shift bits to the left.

(1 << LOAD2) means shift a '1' bit to the left as many times as the value in LOAD2 holds.

In the code
Code:
if(PINB&(1<<LOAD1))
the result is true if PINB ANDed with the value created by shifting '1' LOAD1 bits to the left gives a non-zero result.

Brian.
 
can you tell me what is this code doing?! I want to make a clap switch
 

Nobody will be able to tell you without also seeing the schematic. We can't tell what the operations on the pin levels do without knowing what produces those levels and that means the external circuitry.

Brian.
 
Post the full code and I will explain how it works.

Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define LOAD1 0
#define LOAD2 1
volatile int clap;
volatile int clapflag;
volatile int clapflag2;

ISR(INT0_vect)
{
	
	clapflag++;
	clap++;
	
	
}



int main(void)
{
    volatile int clap1;
	DDRB|=(1<<LOAD1);
	DDRB|=(1<<LOAD2);
	PORTD|=(1<<2);
MCUCR|=((1<<ISC00)|(1<<ISC01));    
GICR|=(1<<INT0);
    sei();
    clap=0;
	clapflag=0;
	
	while(1)
    {
	
	clapflag2=clapflag;
	
		
		while(clapflag2==1)
		{
			_delay_ms(4000);
			        
		
	clap1=clap;

	if(clap1==2)
	{
		if(PINB&(1<<LOAD1))
		PORTB&=~(1<<LOAD1);
		else
		PORTB|=(1<<LOAD1);
	}
	else if (clap1==3)
	{
		if(PINB&(1<<LOAD2))
		PORTB&=~(1<<LOAD2);
		else
		PORTB|=(1<<LOAD2);
	}
	else if (clap1==4)
	{
		PORTB=0x00;
	}

	clap=0;
	clapflag=0;
	clapflag2=0;
		}
		
		}
		
				
}

here is full code. it suppose to turn on 2LED(on pin B0 & B1) from a pulse that 555 will send on INT0 when triggered by clap (condansor mic)

- - - Updated - - -

Its for 4clap(I think) I want to fully understand it and right it in Codevision for 2clap
 

Post the circuit which was requested earlier also.
 

ADVANCED CLAP SWITCH.jpg here is the circuit
 

That just about sums up why you should not trust designs you find on the internet!

Why on Earth would someone use an op-amp amplifer to drive a comparator to drive a darlington transistor to trigger a monostable to trigger an interrupt to light an LED??

Without doing any analysis, I would expect you could use the op-amp, a zener diode voltage clamp (for polarity and overvoltage protection) and connect it directly to the INT0 pin.

Brian.
 
That just about sums up why you should not trust designs you find on the internet!

Why on Earth would someone use an op-amp amplifer to drive a comparator to drive a darlington transistor to trigger a monostable to trigger an interrupt to light an LED??

Without doing any analysis, I would expect you could use the op-amp, a zener diode voltage clamp (for polarity and overvoltage protection) and connect it directly to the INT0 pin.

Brian.

I dont owe a voltage clamp ! I want to use 1000Gain op-amp with 3140 you say that connect it to INT0 pin?! would you mind to Post schematic in proeteus or ... ?!
 

I can't help with proteus, I do not have a copy or a computer to run it on at the moment.

The point I am making is that your objective is to provide a pulse on the INT0 pin when a sound is picked up on the microphone. To do that, all you need is enough amplification to raise the mic voltage up to logic level and a single Zener diode to prevent the op-amp output exceeding logic levels or going negative. The INT0 pin only needs a few uS of pulse so there is no point in using a 555 timer to generate it.

If your op-amp is running from a single 5V supply it wont work as shown in the schematic anyway, the output of the first stage will always be close to 0V. If you do use a single 5V amplifier in a suitable circuit you can probably eliminate the zener diode and connect the output pin directly to the ATMEGA8.

Brian.
 
I can't help with proteus, I do not have a copy or a computer to run it on at the moment.

The point I am making is that your objective is to provide a pulse on the INT0 pin when a sound is picked up on the microphone. To do that, all you need is enough amplification to raise the mic voltage up to logic level and a single Zener diode to prevent the op-amp output exceeding logic levels or going negative. The INT0 pin only needs a few uS of pulse so there is no point in using a 555 timer to generate it.

If your op-amp is running from a single 5V supply it wont work as shown in the schematic anyway, the output of the first stage will always be close to 0V. If you do use a single 5V amplifier in a suitable circuit you can probably eliminate the zener diode and connect the output pin directly to the ATMEGA8.

Brian.

that helped a lot thank you
 

I don't know what max voltage a electret mic gives out. Can you connect mic to ADC pin of AVR and use an LCD to display the max voltage out of the mic when a clap is done. I will write a code for you. I will design an amplifier running with 5V and which will amplify the clap voltage to logic high level for INT0 pin. Then all that needs is to use INT0 and Timerx interrupts to detect if one clap or two clap is done.
 
A typical electret with a clap a few metres away will give about 10mV. The amplifier gain has to be high enough to make that reach the INT0 threshold voltage. I wouldn't use the ADC, the clap will be too short to take an accurate enough reading. The amplifier can be set to high gain, it doesn't matter if it clips or is distorted, it is only the output voltage that matters so quality isn't important.

The 'gotcha' is that a correctly wired amplifier will have it's output sitting at about half VCC which is not good for ensuring a logic level change. There may be some benefit to capacitve coupling to the INT0 pin with a DC restoring diode and pull-down resistor after the capacitor. That should ensure the pin stays low until a signal hits it.

Brian.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top