efficient delay function

WBahn

Joined Mar 31, 2012
32,883
sorry but I don't see anything wrong in asking about project. what I asked is part of the hardware design, not program code for project. I asked question about project after doing some research work.

I don't like to defend myself again and again. truth is that I know the basics of c programming and I am just trying to improve programming skill's
One thing you might try is the Euler Project: https://projecteuler.net

This is a collection of over 600 math problems ranging from very simple to extremely difficult. The intent is that as you solve the simpler ones you gain insight and experience at problem solving that will help you solve slightly harder ones. Since the programs needed to solve a given problem tend to be very short, it will give you a lot of practice at applying and improving programming skills to real problems and be able to verify that your solutions are correct.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Since the programs needed to solve a given problem tend to be very short, it will give you a lot of practice at applying and improving programming skills to real problems and be able to verify that your solutions are correct.
Exactly I am doing this. I am solving problem's by writing c program's on my PC.

I am looking for job in embedded domain. There are so many companies they want's only for experience person and There is only few startup companies they give chance for fresher When ever I goes to interview they ask some question on embedded c programming. they ask about project , the communication protocol's like UART, I2C ISP, CAN. That's why I want to write more embedded program. I have been worked on raspberry and 8266 in past day's I have been written some program previously eg. (LED, LCD, DC Motor, Switch, Push button sensor's). I just want to make good in embedded c programming.

I know it's a good to having deep understanding on c programming but I think I should take care of time. I don't want to be unemployed person for long time. I think if I have only knowledge of c programming but not knowledge in embedded c then I think It will be difficult for me to get a job in embedded domain
 

MrChips

Joined Oct 2, 2009
34,829
When do you end a statement with semi-colon and when you do not?

When can you call a function with no parameters such as delayUS( ) and when must you have parameters?

What is the difference between C programming and embedded programming?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
When do you end a statement with semi-colon and when you do not?
C:
#include <REG51.h>
#define  Data_Port_Pins  P1
if(Switch_1 == 0)
/* function decleartion*/
bit check_Switch_1(void)
{

  }
When I write header file, when i define something, and write control statement like if statment then I don't use semi-colon

C:
sbit  Switch_1 = P2^4;
sbit  Switch_2 = P3^0;
sbit  Switch_3 = P3^3;
sbit  Switch_4 = P3^7;
  unsigned y;
bit check_Switch_1(void); /* prototype function decleartion*/
When I need to set pin, when i need to declear variable , when i need to declear prototype function then I use semi-colon

When can you call a function with no parameters such as delayUS( ) and when must you have parameters?
When I need fixed delay then I will call delay function with no parameter. For example if I want blink led only for 1 second then I will make

separate delay function with no parameter with no return type and then I will call that function form main function.
C:
int main(void)
{

    delay();
}
void delay(void)
{
   unsigned int i;
   for (i = 0; i < 1000; i++)
    {
      }
}
When I need different delay then I will call delay function with parameter. For Example If I want to blink Led for different time like 1s, 2s, 3s then I will call delay function with parameters
C:
int main(void)
{
   delay(1);
    delay(2);
}
void delay(unsigned int wait)
{
   unsigned int i;
   for (i = 0; i < wait; i++)
    {
      }
}
What is the difference between C programming and embedded programming?
Answer can be too long but I am explaining little bit

I think main difference is that we write c program on PC and program run on PC we see the result on the PC' screen where as we write embedded c program for micro-controller. program run on micro-controller.

In embedded programming we need header file for your micro-controller. Embedded C uses most of the syntax of standard C, You can also use same [main() function, variable , datatype declaration, if, else if ,switch case statement , while loop, for loop, functions, arrays pointer strings, structures , etc] like we used in standard C programming.

But the embedded c program is different then normal c program

Note : program example is just hand made. They are not complete
 
Last edited:

Ian Rogers

Joined Dec 12, 2012
1,136
Parth... Let me explain... You are using my code as example without knowing what its doing..

As all the other members are telling you... If you need accurate delays then use interrupts ( beyond your scope).

The delays I make are for me...

C:
void delayUs( int x)
   {
    while(x--);
   }
This code WILL NOT give a 1uS delay on a 12Hhz 8051.. I tried to tell you that..
I calculated that it will delay 12uS as the while() takes time the decrement of an int takes some time.

If you include x>>=3; the delay is more likely more accurate.. This was called 1000 times from delayMs()..

It was to create nearly!!!!! 1mS... It was never intended to be accurate..
 

miniwinwm

Joined Feb 2, 2018
68
A real embedded programmer uses a DMM, ICE, LED(s), Logic Analyzer, O'scope AND his/her brain to debug his/her programs. The first 5 tools are somewhat optional. The last one is required, non-negotiable.
Nah, if you use 3 or more TLA's in a sentence, you're management, and your brain can put its feet up. :)
 
Top