The microcontroller generates a 38KHZ square wave at the IO port

Publisher:和谐相处Latest update time:2016-12-02 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/************************************************

  This function will continuously generate a 38KHZ square wave at the P3^0 port of the microcontroller.

**********************************************/

#include //Includes special function register library

sbit P3_0=P3^0;

void main()

{

TMOD = 0x02; //TMOD is the timer mode control register, here timer 0 is selected, working in mode 2

TL0 = 0xF3; //Timer initial value

TH0 = 0xF3; //Timer initial value

EA = 1; // Enable general interrupt

ET0 = 1; // Enable CPU interrupt and T0 interrupt

TR0 = 1; //Start the timer

while(1);

}

void time0_int(void) interrupt 1 //Timer 0 interrupt service routine. Except for the function name, the other parts should be written in this format.

{

P3_0 = ~P3_0;

}

/*

******Calculation of initial value of timer************

If timer 0 is used, working in mode 1:

(2^8 - X)*oscillation period*12 = timing time

The crystal oscillator I use here is 12MHZ, that is, the oscillation period is 1/12MHZ = 1 microsecond.

To generate a 38KHZ square wave, that is, 1/38KHZ = 26 microseconds, the timer should be set to 13 microseconds to ensure that a square wave with a period of 26 microseconds can be generated at P3^0. The initial count value X = 253 (0xF3)

*/

/*

***************Timer Mode Selection*******************

The timer has four working modes. In principle, it can work in any mode, but working in mode 2 is more accurate and has a smaller error. This is because the timer in mode 2 has the feature of automatically reloading the initial value.

*/

/*

********Writing of interrupt service routine***********

void function name() interrupt interrupt number

{

}

Interrupt number

 0 External interrupt 0

 1 Timer 0

 2 External interrupt 1

 3 Timer 1

 4 Serial interrupt

Here we use timer 0, so the interrupt function is written as

void void time0_int(void) interrupt 1

{}

*/

/*

********Start and stop the timer**********

As long as TR0 = 1, timer 0 is started, and TR0 = 0, timer 0 is stopped.

*/


Keywords:MCU Reference address:The microcontroller generates a 38KHZ square wave at the IO port

Previous article:USB debugging for C8051F320
Next article:About C51 absolute addressing

Recommended ReadingLatest update time:2024-11-25 13:28

Working principle of independent buttons of PIC microcontroller human-machine interface module
  An independent button refers to a single button circuit composed of I/O port lines. Each independent button occupies an I/O port line separately. The working status of the button on each I/O port line will not affect the working status of other I/O port lines. That is, one button corresponds to a port input, and each
[Microcontroller]
Working principle of independent buttons of PIC microcontroller human-machine interface module
Taking 51 single chip microcomputer as an example, a simple serial communication protocol C program
/**************************************************  File description: Take 51 single-chip microcomputer as an example, a simple serial communication protocol C program,  the protocol starts with 0XAA, ends with 0XCC, 0X33, 0XC3, 0X3C, the data length is not fixed      **********************************************
[Microcontroller]
Summary of MCS-51 MCU instruction system
The MCS-51 single-chip microcomputer instruction system has a total of 111 instructions, which are divided into five categories according to their functions: (1) Data transmission class 28 (2) Arithmetic operations 24 (3) Logical operations 25 (4) Control transfer category 17 (5) Bit operation class 17 (1) Data tran
[Microcontroller]
AVR MCU Program Development and Design without Emulator
When developing single-chip microcomputer programs, many people rely on emulators. Once they leave the emulator, they feel at a loss. In fact, for FLASH memory single-chip microcomputers, programs can be developed quickly and conveniently without emulators. Specifically, you can start from the following aspects: 1.
[Microcontroller]
Design ideas and circuit design of external power-on delay reset circuit of PIC microcontroller
  1. Design ideas   When designing a power-on delay reset circuit, the rise time of the power supply voltage VDD and the start-up time of the oscillator must be considered. The rise time of the power supply voltage VDD is related to the power supply and the circuits in the entire microcontroller application system tha
[Microcontroller]
Design ideas and circuit design of external power-on delay reset circuit of PIC microcontroller
Building the "Chinese Core" Xinhai Technology's signal chain MCU makes smart terminals smarter
Using Chinese heart to create "Chinese core" | Xinhai Technology signal chain MCU makes smart terminals smarter On December 21, the 2021 Guangdong-Hong Kong-Macao Greater Bay Area Consumer Electronics Leaders Summit, hosted by Zhuhai Advanced Integrated Circuit Research Institute, was held at Zhuhai International Co
[Microcontroller]
Building the
Design of wireless power automatic meter reading system based on GSM and single chip microcomputer
0 Introduction At present, the collection of electricity data in my country is basically manual meter reading. This method requires meter readers to read the numbers of electricity meters from house to house, once a month or every two months. If no one is at home, the data cannot be read. Electricity bills made
[Microcontroller]
PIC microcontroller makes a simple table lookup program on ICD
include status equ 03h ;2004/4/28 rp0 equ 05h ; z equ 02h ;1-2-3-9-1 pcl equ 02h portd equ 08h trisd equ 88h mm equ 20h Count equ 22h Count1 equ 23h Count2 equ 24h org 10 start bsf status,rp0 clrf trisd bcf status,rp0 movlw 0 movwf mm main call table ;mm=0查表返回后mm=b'11111000' mov
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号