Previous in Forum: Changing Circles to Points in AutoCAD LT 2010   Next in Forum: Windows XP Repair
Close
Close
Close
16 comments
Rate Comments: Nested
Power-User

Join Date: Nov 2010
Posts: 277

C Program For This Algorithm (Using Atmega16)

10/25/2011 3:13 AM

I have the AC mains (240 V, 50 Hz) connected to a zero-crossing detector (ZCD) circuit. The output of the ZCD triggers INT1 of Atmega16 and sets the Timer 1. After the timer times till the required value, I need a continuous high pulse at Port D.5.

The value of the timing is user defined, based on a number of keys the user presses. Though I know basic C language, I have never used Atmega16 and the header files and their functions aren't known to me. If anyone could help in the modification of the program in this link, I would appreciate it.

http://extremeelectronics.co.in/avr-projects/avr-project-remote-controlled-fan-regulator/

Some parts of the code would not be required and a few additions needed, I guess.

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Power-User

Join Date: Nov 2010
Posts: 277
#1

Re: C program for this Algorithm (if Im using Atmega16)

10/25/2011 4:55 AM

Another thing I thought I should add. Some functions which are probably defined in the header files I have not used before. I am assuming GetRemoteCmd , sei(), RemoteInit() are defined in some of the header files.

But what I don't understand is why wait() , ISR(INT1_vect) and ISR(TIMER_COMP_vect) are defined when they are not called in any part of the program.

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#2
In reply to #1

Re: C program for this Algorithm (if Im using Atmega16)

10/25/2011 6:21 AM

I don't know the compiler, but I'd bet that the ISR()s are interrupt service routines, invoked by INT1 and TIMER_COMP events.

I'd guess that wait() was used some time during testing, and the author forgot to remove it.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Power-User

Join Date: Nov 2010
Posts: 277
#4
In reply to #2

Re: C program for this Algorithm (if Im using Atmega16)

10/25/2011 2:37 PM

Yea, could you explain though why the ISRs (Interrupt Subroutines) don't seem to be called in any C program? I don't understand the logic of it. Clearly, they aren't like functions (though defined in a similar manner). When does it come to use then, when the code being exectued is actually the main() ?

Regarding the wait() function he doesn't use it, its possible he forgot about it.

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#5
In reply to #4

Re: C program for this Algorithm (if Im using Atmega16)

10/25/2011 6:02 PM

I suspect that either the compiler picks it [ISR()] up as a special case, or that one of the header files has it defined as a macro, that sets it up as an interrupt routine and installs the interrupt vector in the appropriate place, based on the argument.

Haven't programmed in C for a while, but I think that the header file/macro explanation is favourite.

As I said, wait() is probably a left-over.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Power-User

Join Date: Aug 2008
Location: Melbourne, Australia
Posts: 476
Good Answers: 32
#6
In reply to #1

Re: C program for this Algorithm (if Im using Atmega16)

10/25/2011 11:24 PM

The ISRs are called by the C support system. Notice that the ISRs are using global variables that are set by the mainline - that's the communications link between ISR and main.

The Wait() is a red herring.

__________________
johny451
Register to Reply
Power-User

Join Date: Nov 2010
Posts: 277
#7
In reply to #6

Re: C program for this Algorithm (if Im using Atmega16)

10/26/2011 1:08 AM

"set by the mainline"? Didn't understand.

Register to Reply
Power-User

Join Date: Aug 2008
Location: Melbourne, Australia
Posts: 476
Good Answers: 32
#8
In reply to #7

Re: C program for this Algorithm (if Im using Atmega16)

10/26/2011 1:34 AM

In main() there are several places that changes the value of variables.

For instance the up and down keys change 'speed':

//DOWN Key if(cmd==RC_DOWN) { if(speed>0) speed--; }

Then in one of the ISRs you can see the variable 'speed' being used:

ISR(INT1_vect) { if(!fan_on) { PORTD|=(1<<PD5); //High = TRIAC Off return; } if(speed==9) { PORTD&=(~(1<<PD5)); //low = TRIAC ON return; } PORTD|=(1<<PD5); //High = TRIAC Off OCR2=table[speed]; TCNT2=0x00; TCCR2|=((1<<CS22)|(1<<CS21)|(1<<CS20)); //Start Timer prescaler =1024 }

So the ISRs use values of variables that the mainline, main(), is manipulating.

Hope this helps.

Sorry, CR4 the editor mucks up all the formatting but you can see the code in the link you gave.

John

__________________
johny451
Register to Reply
Power-User

Join Date: Nov 2010
Posts: 277
#9
In reply to #8

Re: C program for this Algorithm (if Im using Atmega16)

10/26/2011 12:07 PM

Thank you. Despite the cr4 editor, your explaination was great :-)

Now that the wait() function doesn't seem to be called. I can't understand the use of the "table array" either.

Also, could you explain me the assignment:

PORTD&=(~(1<<PD5)); // Triac low (ON)

and PORTD|=(1<<PD5); // Triac high (OFF)

If I am not mistaken the statements the same as:

1. PORT D = PORT D AND (~(1<<PD5)); and

2. PORT D = PORT D OR (1<<PD5);

I understand | is for OR and & is for AND and the tilde is NOT. But I am not clear on why the bitwise left shift assignment is used. What's the logic of saying the TRIAC is ON (LOW) in the first statement and OFF (HIGH) in the second?

Register to Reply
Anonymous Poster #1
#11
In reply to #9

Re: C program for this Algorithm (if Im using Atmega16)

10/26/2011 2:38 PM

the table array has the timer counts for each speed setting. The larger the number, the longer before the triac turns on.

The triac control is on bit 5 of Port d. those statements selectively turn on (or off) bit 5 while leaving the other bits alone.

If you look at the diode in the triac (where the "out" signal is) you'll see that if out is low, the diode conducts, turning the triac on.

PORTD has more than 1 bit. the shift moves a single bit into the correct position in the register. Presumably PD5 is just a constant of 5.

as far as off and on, notice there is a return statement after each of the first two triac adjustments, so the program doesn't process the following ones.

Rufus

Register to Reply
Power-User

Join Date: Nov 2010
Posts: 277
#14
In reply to #11

Re: C program for this Algorithm (if Im using Atmega16)

10/27/2011 5:22 AM

"the table array has the timer counts for each speed setting."

But which part of the code tell that (that the table values are the timer counts for the speed setting)?

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#15
In reply to #14

Re: C program for this Algorithm (if Im using Atmega16)

10/27/2011 7:55 AM

The value of "speed" is set in main() according to the UP and DOWN inputs from the remote control.

In the zero-crossing ISR, the "speedth" element of the table is used to initialize the timer - see the line:

OCR2=table[speed];

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#12
In reply to #9

Re: C program for this Algorithm (if Im using Atmega16)

10/26/2011 6:58 PM

A bit (pun intended ) of clarification on the ands, ors, nots and shifts.

Remember that these are all bitwise operators.

Let's assume that PD5 = 5 (specifying bit 5 of port D).

(1<<PD5) means "shift 1 to the left 5 times", thus:

00000001 (no shifts)

00000010 (one shift)

00000100 (two shifts)

00001000 (three shifts)

00010000 (four shifts)

00100000 (five shifts)

If this number is ored with any eight-bit number, the resulting number will have the 25 bit set (with no other bits affected).

e.g. 00000000 or 00100000 = 00100000, 11111111 or 00100000 = 11111111

The ~ (not) operator does a bitwise inversion, changing 00100000 into 11011111. If this is anded with any eight-bit number, the resulting number will have the 25 bit clear (with no other bits affected).

e.g. 00000000 and 11011111 = 00000000, 11111111 and 11011111 = 11011111

I haven't looked closely at the circuit, but I think you'll find that outputting a low value (0) to bit 5 of port D turns the triac on, and outputting a high (1) turns it off. [Edit: think Rufus pointed this out].

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply Score 1 for Good Answer
Power-User

Join Date: Nov 2010
Posts: 277
#13
In reply to #12

Re: C program for this Algorithm (if Im using Atmega16)

10/27/2011 3:26 AM

Yea, I know those are bitwise operators. The logical not (like in the if loop) is by ! and the OR || and AND &&. But good you clarified.

Thanks for the explaination on the shifting

Register to Reply
Guru
Popular Science - Weaponology - New Member United Kingdom - Member - New Member

Join Date: May 2007
Location: Harlow England
Posts: 16512
Good Answers: 670
#3

Re: C Program For This Algorithm (Using Atmega16)

10/25/2011 7:15 AM

maybe... just maybe...and remember I just a cat.
The program sits in a wait loop and is interupted periodically by the timer interupt and thats where all the real program work gets done? (in the timer interrupt routine)
Del
(I shall scamper off now and peekout from behind the sofa).

__________________
health warning: These posts may contain traces of nut.
Register to Reply
Anonymous Poster #1
#10

Re: C Program For This Algorithm (Using Atmega16)

10/26/2011 2:21 PM

Hi Jay,

Wait() was probably a function used during development but not any more.

The ISR() macro declares an interrupt function entry point. Interrupts in most processors are handled differently (than regular functions/subroutines) upon entrance and exit because the main program has to be continued. after the interrupt returns, with all the registers back to what they were before the interrupt, so the interrupt processing can't have any side effects to interfere with what the main program was doing. It does this by saving the registers used during the interrupt, then putting those values back when it returns.

As far as the principle of operation, this program "chops" the sine wave, which is how you do a regulator. It blocks the first part of each half of the sine wave (after the zero crossing), then turns the triac on at different points, depending on how much power (portion of the half wave) you are passing each cycle.

If you look at the INT1_vect routine, this is what is called at the zero crossing interrupt. If the fan is off, it turns the triac off, and returns. If the fan is full on (speed=9) the triac is turned on and the routine returns. however, if it is being regulated, it starts with the triac off, and sets the timer. When the timer turns on, the triac turns on for the remainder of the half-wave. So the pair of timer routines do the regulation of the sine wave. You could just as well pump this through an incandescent bulb and it would act as a dimmer.

The main program is simply an endless loop, that probably hangs at the GetRemoteCmd call until a command is received (remember, the regulation of power continues, because it is going on in interrupts). Or it returns a command value that corresponds to no programmed action,so it just loops back around each pass.

It is a poorly documented project page as it give no "theory of operation". (As I have given above)

If you are using it with a 60 Hz sine wave, you will want to change the table of timers. (Multiplying each value by 5/6 should about do it.) It says the circuit was tested on 220v 50Hz A/C. (the highest count value should be equal to the number of timer ticks in a half-wave).

I don't do much work with power, but I'm not sure the evenly spaced timer values are the best way to chop the sine wave, but it's probably not critical.

Rufus

Register to Reply
Power-User

Join Date: Nov 2010
Posts: 277
#16

Re: C Program For This Algorithm (Using Atmega16)

10/29/2011 3:37 AM

Thanks guys, its clear now (most part I guess).

Register to Reply
Register to Reply 16 comments

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

Jay_ (7); JohnDG (4); Johny451 (2); user-deleted-1105 (1)

Previous in Forum: Changing Circles to Points in AutoCAD LT 2010   Next in Forum: Windows XP Repair

Advertisement