The best Videos on the www i`v ever see for how to learn programming a AVR . Respect and verry thanks how to getting started with all the diverence of Programms like WINAvr or Atmel Studio too. I learn alot thank you for doing videos like this !
Thanks. It may be set to zero by default, but this is really just a good practice to make sure that it is really set at zero. When the program becomes larger and more complex, this practice may save you time. For example, if the DDRB commands get used in its own function, there may be other functions or places that set this bit to 1, and finding that error would be time consuming.
@somebody301 Good tip. Another good investment is a logic analyzer. I actually have one of those and it's great to see if serial communication is actually happening. They usually come with many leads to probe multiple legs. I may get into that in the uart tutorial. A good project may even to create one from scratch and show the result on the LCD. Hmm!!
while (1) { if (bit_is_clear(PINB,1)) { PORTB ^= 0x05; // pin0 and pin2 toggle } } Is this correct? The code works fine for me. In this case, we don't need to use the variable pressed.
I am having trouble understanding how the 2nd if() statement comes into play.I realize that its used to prevent continuous toggling by making sure the button was released first before toggling.But if i were to follow the code systematically im having a hard time visualizing how this is done. So if i were to start the program and the button has not been pressed, it will fall into the else{} where var "Pressed" will be assigned to 0. But if we now press the button, it will now satisfy the first if() condition and thus fall into that bracket . it will now check the condition for the 2nd if() statement which should be true since var "Pressed" was assigned to 0 on the else{} earlier. Therefore now the toggling of the 2 leds occur. This is the part where im having trouble with as I don’t see how the button needs to be released ,since as soon as we press the button both if() conditions are satisfied and the toggling should occur immediately as opposed to waiting for the button to be released before toggling. But the code does seem to work as ive tried it, but i just cant grasp how the waiting for release is done.
Hi Patrick, are you using 2 or 4 pin button, what the value of the capacitor are you using? and you can actually use true and false if you include the header stdbool.h which is part of the standard C library.
noob question here. Can i use any micro controller with the programming language that you are using? (with a different programmer) and if i'm right the micro controller is enough of a resistor to not short the circuit right? since you connected the button to ground.
Thank you so much.. Amazing explanation Would you please add numbers to the names of the videos in the playlist so the one can remember where did he arrived
Anajonesr, I have a question. By default the DDRB of all the pins will be 0bit right? Then why do we have to explicitly set the Pin1(switch) to 0bit when it is already 0bit? BTW your videos are great and anybody who has not seen it is not blessed.
what would happen if you had put a delay in the prob meaning if the switch is pressed wait 10ms then switch/toggle. would that also solve the problem? back in the day we had used assembly language that seemed to work that was only 8bit and much smaller micro controller . thanks in advance
Delays will work and many folks do this but I like to keep delays out of the code. It's best to treat programming in microcontrollers like state machines where the clock cycles are there if you need it.
I have made a program for a CLICK project just like you but the problem is not about debounce in my case.After uploading this program the led keeps blinking very fast can you point out the problem is in this program?? #include #include int b=2; int main(void) { DDRA=0x00; DDRC=0xFF; while (1) { int c=b/2; int a = PINA; _delay_ms(10); int a1 = PINA; _delay_ms(10); if(a!=a1){ b+=1; } if(c%2==0){ PORTC=0xFF; } else if(c%2==1){ PORTC=0x00; } } } in this program i have made PORTC to be output for the LED. I have used ATMEL STUDIO`s GCC C but that should not matter.
If no pin is set high or low, it will default to low (0). However, it is important to set the pins that you are using to your desired level even though there is a default state. This is so you understand their intended state and you are not putting the pin in a tri-state (an input state that is not pulled high or low) condition where interference can cause undesirable results.
Amazing tutorials sir! I have a question. I tried writing my code like this: if (bit_is_clear(PINB, 1) && (Pressed == 0)) {...} but it didn't toggle when I pressed the button (1 LED keeps turning on and the other keeps flashing every time I pressed) I changed my code to be like yours and separate 2 conditions then it works as expected. Can you explain what is the problem with using && operator here? Thank you! Looking forward for your answer!
Thanks for the kind words. Looks like you may be missing a closing parenthesis. if (bit_is_clear(PINB, 1)*)* && (Pressed == 0)) Does it work by nesting them? That may answer your question.
Sir, I don't think I've missed any closing parenthesis. This is my conditional statement when it didn't work if ( bit_is_clear(PINB, 1) && (Pressed == 0) ) { PORTB ^= (1
Yes, you are right. Sorry about that. Separating the condition into a nested condition worked. Not sure the && doesn't work. I typically use that logic statement in my C# code, but I don't recollect using it in C.
You still want to set it even if it is already 0. It's a good practice because programs get longer and more complicated and you may not know what state that bit is all the time. I would suggest really looking at the program closely and test, test, test.
I'm pretty sure some of the code if wrong. it should be 1. Pressed_Confidence_Level ++; Released_Confidence_Level = 0; 2. Released_Confidence_Level = 0; if inside the if 3. if (Released_Confidence_Level > 500) needs a closing brace.
let me elaborate it with the program with comments #include #include int b=2; int main(void) { DDRA=0x00; //here i`ve set all the pins of PORTA as output DDRC=0xFF; /here i`ve set all the pins of PORTC as input while (1) { int c=b/2; // the value of b is going to be divided by tow for a further purpose int a = PINA; // first of all a is going to get a value _delay_ms(10); // in this delay the button will pressed or not pressed int a1 = PINA; //here i am getting the value to check if the button is pressed in the delay //in this part the if logic is going to work //and when i will take my finger off the button the b is going to increase with 1 value again //totally the increamention of b will be 2 // and with the (c=b/2) i am going to get a value of c increased 1 if(a!=a1){ b+=1; } //so on the first click c%2 will be 0 and an another click will make the value of c%2 to 1 // it is going to go on like this if(c%2==0){ PORTC=0xFF; // if the value of c%2 is 0 the LED will light up } else if(c%2==1){ PORTC=0x00; //if the value of c%2 is 1 the LED will turn off } } } AND I hope that the program should have worked just fine
Hi Nazat, I am unfortunately not able to investigate code for others, which is why I make tutorials. the best way to learn how to code embedded applications is to dig deep and trudge through. I am generally focused on making new tutorials and investigating others code would take time out of creating new tutorials.
The best Videos on the www i`v ever see for how to learn programming a AVR . Respect and verry thanks how to getting started with all the diverence of Programms like WINAvr or Atmel Studio too. I learn alot thank you for doing videos like this !
You're welcome!!
Thanks. It may be set to zero by default, but this is really just a good practice to make sure that it is really set at zero. When the program becomes larger and more complex, this practice may save you time. For example, if the DDRB commands get used in its own function, there may be other functions or places that set this bit to 1, and finding that error would be time consuming.
Thanks. You can't set a pin high or low if it is set as input. The state of an input pin in changed by some device outside of the chip.
Just ordered a sample. I will make a tutorial when the sample arrives.
@somebody301 the PC versions are actually less expensive than what I bought and have better features. The Owon that I purchased was around $400.
@somebody301 I'm using a cheap Owon oscilloscope. I hear that PC scopes are a great alternative.
@somebody301 I don't use a wave generator... yet!
@somebody301 Yeah, I wouldn't invest in anything until you see a need for it. Even an oscilloscope is handy but not an absolute necessity.
@somebody301 Good tip.
Another good investment is a logic analyzer. I actually have one of those and it's great to see if serial communication is actually happening. They usually come with many leads to probe multiple legs. I may get into that in the uart tutorial. A good project may even to create one from scratch and show the result on the LCD. Hmm!!
Ok.... I'm lost I can't find the project where you created the starting code file. So I'm lost on the following lines of code.
DDRB |= 1
Thanks.
Okay I have a quick question. Wouldn't a rectifier diode work better for dealing with the reverse voltage caused by a switch?
I have thought of the NI national instrument that might required me to buy their extra boards for inputs and output for data transfer..
while (1)
{
if (bit_is_clear(PINB,1))
{
PORTB ^= 0x05; // pin0 and pin2 toggle
}
}
Is this correct?
The code works fine for me. In this case, we don't need to use the variable pressed.
What is the size of the capacitor between VCC and GND (power to the uC)?
@josepaul2000 Canon Rebel T1i.
I am having trouble understanding how the 2nd if() statement comes
into play.I realize that its used to prevent continuous toggling by
making sure the button was released first before toggling.But if i
were to follow the code systematically im having a hard time visualizing
how this is done. So if i were to start the program and the button has not
been pressed, it will fall into the else{} where var "Pressed" will be assigned
to 0. But if we now press the button, it will now satisfy the first if() condition
and thus fall into that bracket . it will now check the condition for the 2nd if()
statement which should be true since var "Pressed" was assigned to 0 on the else{} earlier.
Therefore now the toggling of the 2 leds occur. This is the part where im having trouble with as I don’t see how the button needs to be released ,since as soon as we press the button both if() conditions are satisfied and the toggling should occur immediately as opposed to waiting for the button to be released before toggling.
But the code does seem to work as ive tried it, but i just cant grasp how the waiting for release is done.
Hi Patrick, are you using 2 or 4 pin button, what the value of the capacitor are you using? and you can actually use true and false if you include the header stdbool.h which is part of the standard C library.
I officially declare you the best :-D
also what do you recommend if I need to use video recognition any kits out there which works with atmel? thanks
It would be Nice if you can show how to use AT42QT1012 as touch button, becouse AT42QT1012 is made with the ability to toggle in touch mode
noob question here.
Can i use any micro controller with the programming language that you are using?
(with a different programmer) and if i'm right the micro controller is enough of a resistor to not short the circuit right? since you connected the button to ground.
Thank you so much.. Amazing explanation
Would you please add numbers to the names of the videos in the playlist so the one can remember where did he arrived
Bady Graphics that's a good idea. I will do that.
Done
A big thank you 🌷
I end up with one LED being dim and the green one switching off when I press the button.
how does capacitor remove switch debouncing ?
What camera are you using?
Anajonesr,
I have a question. By default the DDRB of all the pins will be 0bit right? Then why do we have to explicitly set the Pin1(switch) to 0bit when it is already 0bit? BTW your videos are great and anybody who has not seen it is not blessed.
Thank you anajonesr for the clarification. Your tutorial rocks....
U are awesome.U should have different color of wire.Upload a diagram
what would happen if you had put a delay in the prob meaning if the switch is pressed wait 10ms then switch/toggle. would that also solve the problem?
back in the day we had used assembly language that seemed to work that was only 8bit and much smaller micro controller . thanks in advance
Delays will work and many folks do this but I like to keep delays out of the code. It's best to treat programming in microcontrollers like state machines where the clock cycles are there if you need it.
I have made a program for a CLICK project just like you
but the problem is not about debounce in my case.After uploading this program the led keeps blinking very fast
can you point out the problem is in this program??
#include
#include
int b=2;
int main(void)
{
DDRA=0x00;
DDRC=0xFF;
while (1)
{
int c=b/2;
int a = PINA;
_delay_ms(10);
int a1 = PINA;
_delay_ms(10);
if(a!=a1){
b+=1;
}
if(c%2==0){
PORTC=0xFF;
}
else if(c%2==1){
PORTC=0x00;
}
}
}
in this program i have made PORTC to be output for the LED.
I have used ATMEL STUDIO`s GCC C but that should not matter.
sorry i have made a mistake in coments of the program
A was set as INPUT
and
C was set as OUTPUT
Hey..pattrick greetings...may I know why both pins pinb0 and pinb1 are initially zero
If no pin is set high or low, it will default to low (0). However, it is important to set the pins that you are using to your desired level even though there is a default state. This is so you understand their intended state and you are not putting the pin in a tri-state (an input state that is not pulled high or low) condition where interference can cause undesirable results.
Yeah,,I thought that should be that...thank you...got your confirmation...
Salutations@patrick
Amazing tutorials sir!
I have a question. I tried writing my code like this: if (bit_is_clear(PINB, 1) && (Pressed == 0)) {...}
but it didn't toggle when I pressed the button (1 LED keeps turning on and the other keeps flashing every time I pressed)
I changed my code to be like yours and separate 2 conditions then it works as expected.
Can you explain what is the problem with using && operator here?
Thank you! Looking forward for your answer!
Thanks for the kind words. Looks like you may be missing a closing parenthesis. if (bit_is_clear(PINB, 1)*)* && (Pressed == 0))
Does it work by nesting them? That may answer your question.
Sir, I don't think I've missed any closing parenthesis. This is my conditional statement when it didn't work
if ( bit_is_clear(PINB, 1) && (Pressed == 0) ) {
PORTB ^= (1
Yes, you are right. Sorry about that. Separating the condition into a nested condition worked. Not sure the && doesn't work. I typically use that logic statement in my C# code, but I don't recollect using it in C.
Thank you for your answers. I guess I'll just stick to the nested if statement for the moment ;)
You're welcome. Not sure how helpful I was, however. Let me know if you find a solution with the other option.
can u please tell how did u put pinb2 as input?? I didn't get d way u made pinb2 input using bitwise operation..
hope u'll solve this query..
You need to set the data direction register for that port and pin as input.
Patrick Hood-Daniel
ddrb&=~(1
You still want to set it even if it is already 0. It's a good practice because programs get longer and more complicated and you may not know what state that bit is all the time. I would suggest really looking at the program closely and test, test, test.
Patrick Hood-Daniel
thnxxxx.... I jst started my engineering... really loved d stuff u doing... keep posting tutorial lyk dis..
thanks so much for your effort and time ..........
My pleasure!
@anajonesr
In the statement -
PORTB |= 1
I'm pretty sure some of the code if wrong. it should be
1. Pressed_Confidence_Level ++;
Released_Confidence_Level = 0;
2. Released_Confidence_Level = 0; if inside the if
3. if (Released_Confidence_Level > 500) needs a closing brace.
Thanks a lot guru I understand now. I will follow as my guru says.
Thanks brother.
i just realzed that you are using the 2 pins button which is most logical, but keep the good work
best of the best
yes
Thanks you sir
A boolean doesn't use only 1 bit of memory ! Get it right :D. You can't allocate only 1 bit of memory.
I think u should start from the first video to understand @M.Wali M
let me elaborate it with the program with comments
#include
#include
int b=2;
int main(void)
{
DDRA=0x00; //here i`ve set all the pins of PORTA as output
DDRC=0xFF; /here i`ve set all the pins of PORTC as input
while (1)
{
int c=b/2; // the value of b is going to be divided by tow for a further purpose
int a = PINA; // first of all a is going to get a value
_delay_ms(10); // in this delay the button will pressed or not pressed
int a1 = PINA; //here i am getting the value to check if the button is pressed in the delay
//in this part the if logic is going to work
//and when i will take my finger off the button the b is going to increase with 1 value again
//totally the increamention of b will be 2
// and with the (c=b/2) i am going to get a value of c increased 1
if(a!=a1){
b+=1;
}
//so on the first click c%2 will be 0 and an another click will make the value of c%2 to 1
// it is going to go on like this
if(c%2==0){
PORTC=0xFF; // if the value of c%2 is 0 the LED will light up
}
else if(c%2==1){
PORTC=0x00; //if the value of c%2 is 1 the LED will turn off
}
}
}
AND I hope that the program should have worked just fine
Hi Nazat, I am unfortunately not able to investigate code for others, which is why I make tutorials. the best way to learn how to code embedded applications is to dig deep and trudge through. I am generally focused on making new tutorials and investigating others code would take time out of creating new tutorials.
okii
I would not consider myself a guru, but thanks anyway.
i dont understand nothing