Precise Delay Parameter Determination Method for STC Single-Chip Microcomputer

Source: Internet
Author: User
When using the STC single-chip microcomputer, do you encounter a problem: the delay is often difficult to grasp. Of course, you can use the latency function to generate waveforms and output them to the oscilloscope for observation to modify the latency parameters. However, there is no way to determine the latency parameters when there is no oscilloscope. I have created a method here to share it with you.

In this way, first set the timer time of the timer, enable the timer before calling the delay function, read the delay parameter from the timer interrupt function, and then output the delay parameter through the serial port. The Code is as follows:

// Program description: test the delay constant of the stc single-chip microcomputer, Serial Output // Author: mjx // Note: The program measurement accuracy is high, applicable to the MCU that does not fix the stc command # include "STC12C5A60S2. h "# include <intrins. h> int I, j, ii, jj; // ii, jj is used as the number of temporary records at the scheduled end of the timer. It is used to output void Uart1_Send (unsigned char Data) through the serial port after the delay ends) // send Data to the Controller {ES = 0; TI = 0; SBUF = Data; while (TI = 0); TI = 0; ES = 1 ;} // delay function void delay (int k) {for (I = 0; I <k; I ++) {for (j = 0; j <30000; j ++) {_ nop _ (); _ nop _ () ;}} void uart=init (void) // use timer 1 as the baud rate generator {PCON = 0x00; // SMOD = 0 SCON = 0x50; // 0101,0000 8-bit variable baud rate, no parity bit TMOD | = 0x20; //, sets the top time device 1 to 8-bit automatic reinstallation counter AUXR = 0x00; // set the timer 1 to work in 1 T mode, and the TH1 = 0xfd; // set the number of automatically reinstalls timer 1 TL1 = 0xfd; TR1 = 1; // enable timer 1 // ES = 1; // enable Serial Port 1 interrupt EA = 1; // enable total interrupt} void main () {TMOD = 0x01; // T0 timer TH0 = 0xdC; // 11.0592 MHz -- set different time for 10 ms. You can test the internal delay parameter TL0 = 0x00; ET0 = 1; EA = 1; uartdeskinit (); // initialize uart1, with a baud rate of 9600. Other Default values: TR0 = 1; // enable timed delay (100 ); // delayed uartmediasend (0x30 + jj/10000); uartmediasend (0x30 + (jj % 10000)/1000); uartmediasend (0x30 + (jj % 1000) /100); uartmediasend (0x30 + (jj % 100)/10); uartmediasend (0x30 + jj % 10); uartmediasend (0x2e ); uartmediasend (0x30 + ii/10000); uartmediasend (0x30 + (ii % 10000)/1000); uartmediasend (0x30 + (ii % 1000) /100); uartmediasend (0x30 + (ii % 100)/10); uartmediasend (0x30 + ii % 10); while (1 );} void Timer0 (void) interrupt 1 using 3 {ii = I; // I and j jj = j of the temporary delay function; // TR0 = 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.