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.
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
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.
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.
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.
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.
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 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.
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.
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.
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.