Hello dear sir,
i have written the C program in AVR studio for examples 1.4 "Flash one LED with delay". As being first time with embedded C programing i have few questions on the programing code in this example as given below:
1. What is meant by 65000 in the code?
2. Why we used that particular value in the code?
3. The LED flashes at around 10Hz, What value i have to change in the code in order to flash the LED at 1Hz? I have tried lots of other values but couldn't slow down the flashing speed of the LED i.e flashing frequency. How to code the value in the program for particular flashing frequency?
4. What the "1" in "while" means? Its described in the course but not very much cleared.
C programing help for AVR microcontroller
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: C programing help for AVR microcontroller
Hello
In answer to your questions
1) 65000 is the number used to assign how many program cycles the chip should wait before continuing to the next instruction.
2) That particular value is used because it roughly generates enough of a delay when running at 20MHz to allow for 1/20th of a second delay.
3) The variable is near its maximum at 65000 in fact the largest number you can store in a unsigned int is 65535. See the example program below for allowing the LED to flash at a rate of 1hz.
4) The while statement in C is a basic desicion statement that decides if the loop should run or not on each iteration of the loop. A static input of 1 will always equal true to the decision so the while loop will run forever unless reset etc.
Basic 1hz flasher example.
In answer to your questions
1) 65000 is the number used to assign how many program cycles the chip should wait before continuing to the next instruction.
2) That particular value is used because it roughly generates enough of a delay when running at 20MHz to allow for 1/20th of a second delay.
3) The variable is near its maximum at 65000 in fact the largest number you can store in a unsigned int is 65535. See the example program below for allowing the LED to flash at a rate of 1hz.
4) The while statement in C is a basic desicion statement that decides if the loop should run or not on each iteration of the loop. A static input of 1 will always equal true to the decision so the while loop will run forever unless reset etc.
Basic 1hz flasher example.
Code: Select all
#include <AVR\io.h>
void delay(void)
{
unsigned int i;
unsigned char j;
for (j=0; j<10; j++)
{
for ( i=0 ; i < 65000 ; i= i + 1 ) ;
}
}
void main(void)
{
DDRA=0x01;
while (1)
{
PORTA = 0x01;
delay();
PORTA = 0x00;
delay();
}
}
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: C programing help for AVR microcontroller
Dear sir Benj,
I am confusing a lot with the program codes which you have provided for flashing LED at 1Hz frequency. Please let me guide thoroughly step-by-step how does the program work. What does the "for" loop for variables i and j do? Why we are using nested "for" loop for these variables? Why we are using two different functions "delay" and "main"?
sincerely,
kpmatrix
I am confusing a lot with the program codes which you have provided for flashing LED at 1Hz frequency. Please let me guide thoroughly step-by-step how does the program work. What does the "for" loop for variables i and j do? Why we are using nested "for" loop for these variables? Why we are using two different functions "delay" and "main"?
sincerely,
kpmatrix
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: C programing help for AVR microcontroller
Hello
I am using a nested for loop because 1 of the delays from your previous program delayed for approx 1/20th of a second. Now I am calling that same delay 10 times to allow for a delay of 1/2 a second. I cannot simply increase the size of the original delay because the Unsigned INT variable size is 2 bytes and can hold a maximum value of 65535.
I have used a second function called delay so you do not have to rewrite the same piece of code twice. The main function is always called first and is the only function called by default. The C programming course should start teaching you about writing functions shortly.
I am using a nested for loop because 1 of the delays from your previous program delayed for approx 1/20th of a second. Now I am calling that same delay 10 times to allow for a delay of 1/2 a second. I cannot simply increase the size of the original delay because the Unsigned INT variable size is 2 bytes and can hold a maximum value of 65535.
I have used a second function called delay so you do not have to rewrite the same piece of code twice. The main function is always called first and is the only function called by default. The C programming course should start teaching you about writing functions shortly.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel