اطلاعیه

Collapse
No announcement yet.

تابع delay دقیق

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    تابع delay دقیق

    سلام.کسی تابع delay دقیقی برای lpc1768 نداره.هرجور محاسبه میکنم یچیز دیگه در میاد.یک تابع استاندارد که طبق فرکانس کاری خودش رو تنظیم کنه. :redface:

    #2
    پاسخ : تابع delay دقیق

    نوشته اصلی توسط arvin2009
    سلام.کسی تابع delay دقیقی برای lpc1768 نداره.هرجور محاسبه میکنم یچیز دیگه در میاد.یک تابع استاندارد که طبق فرکانس کاری خودش رو تنظیم کنه. :redface:
    قبلا با چه کامپایلری با AVR کار میکردی؟

    دیدگاه


      #3
      پاسخ : تابع delay دقیق

      با کدویژن oo:
      اتفاقا رفتم تابع delay کدویژن رو پیدا کنم و از روی اون بنویسم پیداش تکردم.نمیدونم کجا تعریف شده

      دیدگاه


        #4
        پاسخ : تابع delay دقیق

        نوشته اصلی توسط arvin2009
        با کدویژن oo:
        اتفاقا رفتم تابع delay کدویژن رو پیدا کنم و از روی اون بنویسم پیداش تکردم.نمیدونم کجا تعریف شده
        از GCCها استفاده کن. مثل اتمل استودیو. همشو می تونی ببینی. تابع دلای ضریبی از کلاک هسته هستش.

        دیدگاه


          #5
          پاسخ : تابع delay دقیق

          خوب که یک تابع خوب داری بزار تا استفاده کنیم و نخوام باز برم برنامه نصب کنم :cry2:

          دیدگاه


            #6
            پاسخ : تابع delay دقیق

            نوشته اصلی توسط arvin2009
            خوب که یک تابع خوب داری بزار تا استفاده کنیم و نخوام باز برم برنامه نصب کنم :cry2:
            کجا برم برات پیدا کنم؟ :biggrin:
            برات هدر GCC رو میذارم. این فایل Delay.h


            کد:
            /* Copyright (c) 2002, Marek Michalkiewicz
              Copyright (c) 2004,2005,2007 Joerg Wunsch
              Copyright (c) 2007 Florin-Viorel Petrov
              All rights reserved.
            
              Redistribution and use in source and binary forms, with or without
              modification, are permitted provided that the following conditions are met:
            
              * Redistributions of source code must retain the above copyright
               notice, this list of conditions and the following disclaimer.
            
              * Redistributions in binary form must reproduce the above copyright
               notice, this list of conditions and the following disclaimer in
               the documentation and/or other materials provided with the
               distribution.
            
              * Neither the name of the copyright holders nor the names of
               contributors may be used to endorse or promote products derived
               from this software without specific prior written permission.
            
             THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
             AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
             ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
             LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
             CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
             SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
             INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
             CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
             ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
             POSSIBILITY OF SUCH DAMAGE. */
            
            /* $Id: delay.h.in 2189 2010-10-13 09:39:34Z aboyapati $ */
            
            #ifndef _UTIL_DELAY_H_
            #define _UTIL_DELAY_H_ 1
            
            #ifndef __HAS_DELAY_CYCLES
            #define __HAS_DELAY_CYCLES 1
            #endif
            
            #include <inttypes.h>
            #include <util/delay_basic.h>
            
            /** \file */
            /** \defgroup util_delay <util/delay.h>: Convenience functions for busy-wait delay loops
              \code
              #define F_CPU 1000000UL // 1 MHz
              //#define F_CPU 14.7456E6
              #include <util/delay.h>
              \endcode
            
              \note As an alternative method, it is possible to pass the
              F_CPU macro down to the compiler from the Makefile.
              Obviously, in that case, no \c \#define statement should be
              used.
            
              The functions in this header file are wrappers around the basic
              busy-wait functions from <util/delay_basic.h>. They are meant as
              convenience functions where actual time values can be specified
              rather than a number of cycles to wait for. The idea behind is
              that compile-time constant expressions will be eliminated by
              compiler optimization so floating-point expressions can be used
              to calculate the number of delay cycles needed based on the CPU
              frequency passed by the macro F_CPU.
            
              \note In order for these functions to work as intended, compiler
              optimizations <em>must</em> be enabled, and the delay time
              <em>must</em> be an expression that is a known constant at
              compile-time. If these requirements are not met, the resulting
              delay will be much longer (and basically unpredictable), and
              applications that otherwise do not use floating-point calculations
              will experience severe code bloat by the floating-point library
              routines linked into the application.
            
              The functions available allow the specification of microsecond, and
              millisecond delays directly, using the application-supplied macro
              F_CPU as the CPU clock frequency (in Hertz).
            
            */
            
            #if !defined(__DOXYGEN__)
            static inline void _delay_us(double __us) __attribute__((always_inline));
            static inline void _delay_ms(double __ms) __attribute__((always_inline));
            #endif
            
            #ifndef F_CPU
            /* prevent compiler error by supplying a default */
            # warning "F_CPU not defined for <util/delay.h>"
            # define F_CPU 1000000UL
            #endif
            
            #ifndef __OPTIMIZE__
            # warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed"
            #endif
            
            /**
              \ingroup util_delay
            
              Perform a delay of \c __ms milliseconds, using _delay_loop_2().
            
              The macro F_CPU is supposed to be defined to a
              constant defining the CPU clock frequency (in Hertz).
            
              The maximal possible delay is 262.14 ms / F_CPU in MHz.
            
              When the user request delay which exceed the maximum possible one,
              _delay_ms() provides a decreased resolution functionality. In this
              mode _delay_ms() will work with a resolution of 1/10 ms, providing
              delays up to 6.5535 seconds (independent from CPU frequency). The
              user will not be informed about decreased resolution.
            
              If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long)
              support, maximal possible delay is 4294967.295 ms/ F_CPU in MHz. For
              values greater than the maximal possible delay, overflows results in
              no delay i.e., 0ms.
            
              Conversion of __us into clock cycles may not always result in integer.
              By default, the clock cycles rounded up to next integer. This ensures that
              the user gets atleast __us microseconds of delay.
            
              Alternatively, user can define __DELAY_ROUND_DOWN__ and __DELAY_ROUND_CLOSEST__
              to round down and round to closest integer.
            
              Note: The new implementation of _delay_ms(double __ms) with 
              __builtin_avr_delay_cycles(unsigned long) support is not backward compatible. 
              User can define __DELAY_BACKWARD_COMPATIBLE__ to get a backward compatible delay
              although this will be deprecated in future.
            
             */
            void
            _delay_ms(double __ms)
            {
            	uint16_t __ticks;
            	double __tmp ; 
            #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && !defined(__DELAY_BACKWARD_COMPATIBLE__)
            	uint32_t __ticks_dc;
            	extern void __builtin_avr_delay_cycles(unsigned long);
            	__tmp = ((F_CPU) / 1e3) * __ms;
            
            	#if defined(__DELAY_ROUND_DOWN__)
            		__ticks_dc = (uint32_t)fabs(__tmp);
            
            	#elif defined(__DELAY_ROUND_CLOSEST__)
            		__ticks_dc = (uint32_t)(fabs(__tmp)+0.5);
            
            	#else
            		//round up by default
            		__ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
            	#endif
            
            	__builtin_avr_delay_cycles(__ticks_dc);
            
            #elif !__HAS_DELAY_CYCLES || (__HAS_DELAY_CYCLES && !defined(__OPTIMIZE__)) || defined (__DELAY_BACKWARD_COMPATIBLE__)
            	__tmp = ((F_CPU) / 4e3) * __ms;
            	if (__tmp < 1.0)
            		__ticks = 1;
            	else if (__tmp > 65535)
            	{
            		//	__ticks = requested delay in 1/10 ms
            		__ticks = (uint16_t) (__ms * 10.0);
            		while(__ticks)
            		{
            			// wait 1/10 ms
            			_delay_loop_2(((F_CPU) / 4e3) / 10);
            			__ticks --;
            		}
            		return;
            	}
            	else
            		__ticks = (uint16_t)__tmp;
            	_delay_loop_2(__ticks);
            #endif
            }
            
            /**
              \ingroup util_delay
            
              Perform a delay of \c __us microseconds, using _delay_loop_1().
            
              The macro F_CPU is supposed to be defined to a
              constant defining the CPU clock frequency (in Hertz).
            
              The maximal possible delay is 768 us / F_CPU in MHz.
            
              If the user requests a delay greater than the maximal possible one,
              _delay_us() will automatically call _delay_ms() instead. The user
              will not be informed about this case.
            
              If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long)
              support, maximal possible delay is 4294967.295 us/ F_CPU in MHz. For
              values greater than the maximal possible delay, overflow results in
              no delay i.e., 0us.
             
              Conversion of __us into clock cycles may not always result in integer.
              By default, the clock cycles rounded up to next integer. This ensures that
              the user gets atleast __us microseconds of delay.
            
              Alternatively, user can define __DELAY_ROUND_DOWN__ and __DELAY_ROUND_CLOSEST__
              to round down and round to closest integer.
             
              Note: The new implementation of _delay_us(double __us) with 
              __builtin_avr_delay_cycles(unsigned long) support is not backward compatible.
              User can define __DELAY_BACKWARD_COMPATIBLE__ to get a backward compatible delay
              although this will be deprecated in future.
            
             */
            void
            _delay_us(double __us)
            {
            	uint8_t __ticks;
            	double __tmp ; 
            #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && !defined(__DELAY_BACKWARD_COMPATIBLE__)
            	uint32_t __ticks_dc;
            	extern void __builtin_avr_delay_cycles(unsigned long);
            	__tmp = ((F_CPU) / 1e6) * __us;
            
            	#if defined(__DELAY_ROUND_DOWN__)
            		__ticks_dc = (uint32_t)fabs(__tmp);
            
            	#elif defined(__DELAY_ROUND_CLOSEST__)
            		__ticks_dc = (uint32_t)(fabs(__tmp)+0.5);
            
            	#else
            		//round up by default
            		__ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
            	#endif
            
            	__builtin_avr_delay_cycles(__ticks_dc);
            
            #elif !__HAS_DELAY_CYCLES || (__HAS_DELAY_CYCLES && !defined(__OPTIMIZE__)) || defined (__DELAY_BACKWARD_COMPATIBLE__)
            	__tmp = ((F_CPU) / 3e6) * __us;
            	if (__tmp < 1.0)
            		__ticks = 1;
            	else if (__tmp > 255)
            	{
            		_delay_ms(__us / 1000.0);
            		return;
            	}
            	else
            		__ticks = (uint8_t)__tmp;
            	_delay_loop_1(__ticks);
            #endif
            }
            
            
            #endif /* _UTIL_DELAY_H_ */
            اینم فایل delay_basic.h

            کد:
            /* Copyright (c) 2002, Marek Michalkiewicz
              Copyright (c) 2007 Joerg Wunsch
              All rights reserved.
            
              Redistribution and use in source and binary forms, with or without
              modification, are permitted provided that the following conditions are met:
            
              * Redistributions of source code must retain the above copyright
               notice, this list of conditions and the following disclaimer.
            
              * Redistributions in binary form must reproduce the above copyright
               notice, this list of conditions and the following disclaimer in
               the documentation and/or other materials provided with the
               distribution.
            
              * Neither the name of the copyright holders nor the names of
               contributors may be used to endorse or promote products derived
               from this software without specific prior written permission.
            
             THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
             AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
             ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
             LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
             CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
             SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
             INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
             CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
             ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
             POSSIBILITY OF SUCH DAMAGE. */
            
            /* $Id: delay_basic.h 2143 2010-06-08 21:19:51Z joerg_wunsch $ */
            
            #ifndef _UTIL_DELAY_BASIC_H_
            #define _UTIL_DELAY_BASIC_H_ 1
            
            #include <inttypes.h>
            
            #if !defined(__DOXYGEN__)
            static inline void _delay_loop_1(uint8_t __count) __attribute__((always_inline));
            static inline void _delay_loop_2(uint16_t __count) __attribute__((always_inline));
            #endif
            
            /** \file */
            /** \defgroup util_delay_basic <util/delay_basic.h>: Basic busy-wait delay loops
              \code
              #include <util/delay_basic.h>
              \endcode
            
              The functions in this header file implement simple delay loops
              that perform a busy-waiting. They are typically used to
              facilitate short delays in the program execution. They are
              implemented as count-down loops with a well-known CPU cycle
              count per loop iteration. As such, no other processing can
              occur simultaneously. It should be kept in mind that the
              functions described here do not disable interrupts.
            
              In general, for long delays, the use of hardware timers is
              much preferrable, as they free the CPU, and allow for
              concurrent processing of other events while the timer is
              running. However, in particular for very short delays, the
              overhead of setting up a hardware timer is too much compared
              to the overall delay time.
            
              Two inline functions are provided for the actual delay algorithms.
            
            */
            
            /** \ingroup util_delay_basic
            
              Delay loop using an 8-bit counter \c __count, so up to 256
              iterations are possible. (The value 256 would have to be passed
              as 0.) The loop executes three CPU cycles per iteration, not
              including the overhead the compiler needs to setup the counter
              register.
            
              Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
              can be achieved.
            */
            void
            _delay_loop_1(uint8_t __count)
            {
            	__asm__ volatile (
            		"1: dec %0" "\n\t"
            		"brne 1b"
            		: "=r" (__count)
            		: "0" (__count)
            	);
            }
            
            /** \ingroup util_delay_basic
            
              Delay loop using a 16-bit counter \c __count, so up to 65536
              iterations are possible. (The value 65536 would have to be
              passed as 0.) The loop executes four CPU cycles per iteration,
              not including the overhead the compiler requires to setup the
              counter register pair.
            
              Thus, at a CPU speed of 1 MHz, delays of up to about 262.1
              milliseconds can be achieved.
             */
            void
            _delay_loop_2(uint16_t __count)
            {
            	__asm__ volatile (
            		"1: sbiw %0,1" "\n\t"
            		"brne 1b"
            		: "=w" (__count)
            		: "0" (__count)
            	);
            }
            
            #endif /* _UTIL_DELAY_BASIC_H_ */

            دیدگاه


              #7
              پاسخ : تابع delay دقیق

              دستت درد نکنه.یک س.ل دیگه.اگه بخوام یک برنامه ای که نوشتم در اختیار کسان دیگه بزارم و بعضی از توابع که در فایلهای دیگه ذخیره شده رو مخفی کنم که قابل دسترس نباشه شدنی هست و برنامه در دست کاربر به درستی کامپایر میشه؟

              دیدگاه


                #8
                پاسخ : تابع delay دقیق

                این توابع برای کیل باید دستکاری بشن یا میشه ازش استفاده کرد؟کتابخانه استاندارد GCC یعنی کامل تر از کیل هست.اگه اینجوره برم هنوز اول کارم با اون کار کنم

                ***خیلی ارور میده.نظرتون در مورد این تابع چیه؟دقیقه
                کد:
                #ifndef _DELAY_INCLUDED_
                #define _DELAY_INCLUDED_
                //presentation variable
                unsigned long crystal=12000000; // for other crystal change 12000000
                unsigned long x;
                unsigned long y;
                
                //presentation subs ( we have three sub 1- delay_us 2- delay_ms 3- delay_s
                void delay_us(unsigned long a);
                void delay_ms(unsigned long b);
                void delay_s(unsigned long c);
                // sub delay_us 
                void delay_us(unsigned long a){
                do{
                unsigned long d=0;
                x=crystal/50000000;   //
                for (d=0;d<x;d++);   //start the VALUE off d is 0 , increase d , 
                //if d > x goto next line ,else (d<x) increase d
                y++;  // increase y 
                } while(y<a); //if y > a goto next line ,else (y<a) goto "do{"
                y=0;
                }
                //in mine program , you write "delay_s(4);" . in this Header "a" is 4 & do-while 
                //loop repeat 4 ---time = (x*5)/ crystal
                // sub delay_ms
                void delay_ms(unsigned long b){
                do{
                unsigned long e=0;
                x=crystal/5000;  
                for (e=0;e<x;e++);
                y++;
                } while(y<b);
                y=0;
                }
                // sub delay_s
                void delay_s(unsigned long c){
                do{
                unsigned long f=0;
                x=crystal/5;
                for (f=0;f<x;f++);
                y++;
                } while(y<c);
                y=0;
                }
                #endif?????

                دیدگاه


                  #9
                  پاسخ : تابع delay دقیق

                  در مورد عدم دسترسی بعضی از توابع در کیل کسی چیزی میدونه؟

                  دیدگاه


                    #10
                    پاسخ : تابع delay دقیق

                    سلام
                    ای بابا چرا اینقدر به خودتون سختی میدید برای یه delay
                    خوب از system tick استفاده کنید
                    فيلم آموزشي ميکروکنترلر LPC1768
                    فيلم آموزشي تکميلي و پيشرفته ميکروکنترلر LPC1768
                    فيلم آموزش کاربردی زبان سی در میکروکنترلر ARM
                    فیلم آموزش مقدماتی میکروکنترلر LPC1788 به زودی ...

                    دیدگاه


                      #11
                      پاسخ : تابع delay دقیق

                      با سلام
                      با استفاده از System Tick Timer به راحتی می شود توابع DELAY را نوشت به مثال زیر توجه کنید
                      یک فایل .Cدرست کن برای شرح توابع زیر(کافی است شرح توابع زیر را در .C کپی کنی) بعد یک فایل .Hدرست کن برای معرفی توابع (کافی است معرفی توابع زیر را در .H کپی کنی)بعد .H اول برنامه ات INCLUDE کن
                      موفق باشید.
                      /************************************************** **************************
                      SOURCE FILE FOR LPC17XX
                      ************************************************** **************************/
                      #include "delay.h"

                      extern uint32_t SystemCoreClock;
                      extern void SystemCoreClockUpdate (void);
                      void Delay_init(void)
                      {
                      SystemCoreClockUpdate();
                      SysTick->LOAD = (SystemCoreClock/1000000) - 1; /* each ~1 us */
                      SysTick->CTRL = 5;
                      return;
                      }

                      void Delay_ms (unsigned long ms)
                      {
                      uint32_t SysTickCnt;
                      ms*=1000;
                      for(SysTickCnt=0;SysTickCnt<ms;SysTickCnt++)
                      while(!(SysTick->CTRL&(1<<16)));
                      }

                      void Delay_us (unsigned long us)
                      {
                      uint32_t SysTickCnt;
                      for(SysTickCnt=0;SysTickCnt<us;SysTickCnt++)
                      while(!(SysTick->CTRL&(1<<16)));
                      }

                      /************************************************** **************************
                      End Of File
                      ************************************************** **************************/



                      /************************************************** **************************
                      Introduction to Functions
                      ************************************************** **************************/
                      #include "LPC177x_8x.H"
                      void Delay_init(void);
                      void Delay_ms (unsigned long ms);
                      void Delay_us (unsigned long us);
                      /************************************************** **************************
                      End Of File
                      ************************************************** **************************/

                      دیدگاه


                        #12
                        پاسخ : تابع delay دقیق

                        مرتضی جان متشکرم.خیلی کمک کردید.
                        کسی در مورد محدود کردن دسترسی بعضی از توابع حرفی نزد.فرض کنید میخوام یک پروژه کامل بزارم تو سایت فقط میخوام چند تا از تایع هاش دیده نشه و کسی نتونه ببینه.حالا میخوام ببینم امکانش هست یانه

                        دیدگاه


                          #13
                          پاسخ : تابع delay دقیق

                          نوشته اصلی توسط arvin2009
                          مرتضی جان متشکرم.خیلی کمک کردید.
                          کسی در مورد محدود کردن دسترسی بعضی از توابع حرفی نزد.فرض کنید میخوام یک پروژه کامل بزارم تو سایت فقط میخوام چند تا از تایع هاش دیده نشه و کسی نتونه ببینه.حالا میخوام ببینم امکانش هست یانه
                          سلام
                          البته این سوال ربطی به این موضوع این تایپیک نداره
                          ولی بله میشه
                          فيلم آموزشي ميکروکنترلر LPC1768
                          فيلم آموزشي تکميلي و پيشرفته ميکروکنترلر LPC1768
                          فيلم آموزش کاربردی زبان سی در میکروکنترلر ARM
                          فیلم آموزش مقدماتی میکروکنترلر LPC1788 به زودی ...

                          دیدگاه


                            #14
                            پاسخ : تابع delay دقیق

                            نوشته اصلی توسط mzarkoob
                            سلام
                            البته این سوال ربطی به این موضوع این تایپیک نداره
                            ولی بله میشه
                            مرسی مهدی حان حالا اگه بگی چه حوری ممنون میشم.
                            دو ماه بعد.....

                            دیدگاه

                            لطفا صبر کنید...
                            X