ad locum , Let me talk about MSP430 Use of single chip microcomputer __delay_cycles The problem of delay .
IAR for MSP430 The compiler provides a compiler inline precise delay function ( Not really
function ) To provide users with accurate delay use , The prototype of this function is :
__intrinsic void __delay_cycles(unsigned long __cycles);
The inner function implements __cycles individual CPU Delay of cycle , But for the setting of this parameter , I want to state :
__cycles What we need to deliver is CPU The number of running cycles

The common usage on the Internet is :
#define CPU_CLOCK 8000000
#define delay_us(us) __delay_cycles(CPU_CLOCK/1000000*(us))
#define delay_ms(ms) __delay_cycles(CPU_CLOCK/1000*(ms))

stay CPU The master clock frequency is 8MHz when , It's really no problem , But this way of writing :
#define CPU_CLOCK 8000000
It's easy for people to think of , We can modify its value to realize the unification of different main frequency system parameters , Actually
This is not true ! For example, change it to #define CPU_CLOCK 32768 In order to realize the 32KHz The delay of the main frequency ...

Let's take a look at the calculation :
When the system master clock frequency CPU_CLOCK by 8MHz when :
frequency f = 8MHz = 8,000,000Hz
Machine cycle Tm = 1/f = 1/8MHz = 1/8us
in other words , One machine cycle (nop) The length of time is 1/8us, So the delay 1us namely 8*Tm, Same as above :
#define delay_us(us) __delay_cycles(8*(us))
#define delay_ms(ms) __delay_cycles(8000*(ms))

Follow the macro definition above , We put CPU_CLOCK Defined as 32768, that :
frequency f = 32KHz = 32,768Hz
Machine cycle Tm = 1/f = 1/32768Hz ~= 30.5us
As one can imagine ,CPU The shortest instruction execution cycle is 30.5us, At this time , Want to delay 1us, Is that possible? ?
therefore , Simply change the above definition to
#define CPU_CLOCK 32768
It's absolutely wrong .

Again , And some friends have realized 0.5us Time delay of , This is when f = 1MHz = 1000000Hz When they were
It's unrealistic , At this point, the machine cycle Tm = 1us. stay f = 8Mhz when , 4 One machine cycle is 0.5us fair .

therefore , In order to avoid causing wrong use or incorrect understanding , It's like defining a macro like this :
#if CPU_CLOCK == 8000000
#define delay_us(us) __delay_cycles(8*(us))
#define delay_ms(ms) __delay_cycles(8000*(ms))
#pragma error "CPU_CLOCK is defined implicitly!"

in addition :
  __delay_cycles It's not really a function , Just provide compiler inline expansion , This function does not
Variable parameters are not supported , Its parameters can only be constants .

Yes MSP430 Single chip microcomputer __delay_cycles Description and correction of precise delay

  1. [MSP430] Yes MSP430 Single chip microcomputer __delay_cycles Description and correction of precise time delay

    ad locum , Let me talk about MSP430 Use of single chip microcomputer __delay_cycles The problem of delay . IAR for MSP430 The compiler provides a compiler inline precise delay function ( Not really function ) To provide users with accurate delay use ...

  2. 51 Single chip microcomputer Keil C The simplicity of the delay program ( Crystal oscillator 12MHz, One machine cycle 1us.)

    One . 500ms Delay subroutine void delay500ms(void) { unsigned char i,j,k; ;i>;i--) ;j>;j--) ;k>;k--); } produce ...

  3. Keil C51 Several precise delay methods in programming

    1 Use timer / The counter realizes precise delay Single chip microcomputer system is often used 11.059 2 MHz.12 MHz or 6 MHz Crystal oscillator . The first one is easier to generate various standard baud rates , One machine cycle of the latter two is 1 μs and 2 μs, It's easy to be precise ...

  4. About 51 Precise delay and keil Simulation delay time

    from : Sometimes you need precise delays , such as 18B20 Temperature sensors are very strict with timing , It has to be accurate to microseconds One ...

  5. STC12C5A60S2 @ 22.0184Mhz Precise delay

    #include "STC12.h" // STC12C5A60S2 @ 22.0184Mhz Precise delay void delay_10us(unsigned char n) { un ...

  6. MSP430 There are two kinds of MCU SPI Bus implementation

    MSP430 On the single chip microcomputer SPI There are two ways to implement the bus : Hardware implementation and software implementation . They have different levels of abstraction , In hardware implementation mode, the programmer only needs to complete the register layer of bus protocol , That's a byte (char,8 Bit binary ) data , And the way of software implementation is next ...

  7. MSP430 Bit operation of single chip microcomputer

    MSP430 SCM belongs to RISC(Reduced Instruction Set Computer) Type processor . With the ordinary 51 Single chip complex instruction set is different . Ordinary 51 It needs at least 12 Clock cycles , and R ...

  8. MSP430 Single chip microcomputer input and output

    MSP430 The input and output lines of single chip microcomputer are mostly taken , Except for individual ports , Is basically 8 As a group , Different models MSP430 The ports are different , Just msp430F5438 for , Altogether 11 individual I/O port , Except for the eleventh P11 ...

  9. TI The company with MSP430 Single chip microcomputer

    Texas Instruments (Texas Instruments) company , abbreviation TI, Is the world's leading semiconductor company , Founded on 1930 year , At the beginning of its establishment, it was a geological exploration company that used seismic signal processing technology to explore crude oil ,1951 It changed its name to Texas Instruments ,1 ...

Random recommendation

  1. swift Code addition image

    let image_ElectricianBtn = UIImage(named: "ElectricianBtn") let vimage_ElectricianBtn = UI ...

  2. BZOJ 1029: [JSOI2007] Rush repairs to buildings Pile up + greedy

    1029: [JSOI2007] Rush repairs to buildings Description Xiao Gang is playing JSOI The one provided is called “ Rush repairs to buildings ” Computer games of : After a fierce battle ,T The tribe wiped out all z The invaders of the tribe . however T The tribe's base already has ...

  3. Dynamic layout LayoutInflater

    LayoutInflater Role is to layout Of xml The layout file is instantiated as View Class object .LayoutInflater Is similar to findViewById(), The difference is LayoutInflater yes ...

  4. idea2017.3 The latest cracking method

    IntelliJ IDEA2017.3 Activate   Reprinted to : Abreast of the times IDEA Activation mode Use the Internet ...

  5. 6-5 Moving boxes uva12657

    A more complicated question : It's kind of similar 6-1   But after the analysis, it's better than 6-1 Simple   Just follow the train of thought ! Learned two way linked list   Initialize first   link Is the key Analysis of the command   Can greatly simplify the code  : Reverse linked list does not need to reverse   Change ...

  6. Java - 18 Java Scanner class

    java.util.Scanner yes Java5 New features , We can go through Scanner Class to get user input . Here is the creation Scanner Basic syntax for objects : Scanner s = new Scanner ...

  7. ubuntu Next Sprak(IDE)wordcount Example

    One . Get into IDE Interface cd ~/Downloads/idea/bin Two . establish scala project Step 1: Import Spark-hadoop The corresponding package , Secondary selection “File”–> “Proj ...

  8. python2 and python3 There is also how to install and use pip

    linux Next without pip You need to install pip python2 install pip sudo apt install python-pip1 If it is python3, Is the following : sudo apt install pyt ...

  9. be based on SSM The single point of landing 04 JDBC_DRIVER=org.mariadb.jdbc.Driver JDBC_URL=jdbc:mariadb:// JD ...

  10. Windows Overview of exploit technology

    Windows Summary of exploit technology 1. Preface This article is my study summary of vulnerability exploitation technology , It's also self practice QAD (Questions.Answer.Discussions) A practice of . In this paper, through reading several articles of Daniel . Play ...