1. From the document, we can know that mini2440 has a total of 5 timers. As shown in the following figure:
Among them, the four registers of timer 0, 1, 2, and 3 have output pins, which are combined with the buzzer. Timer 4 has no output pin, so timer 4 is used exclusively for timing.
Timers 0 and 1 share an 8-bit frequency divider, and timers 2, 3, and 4 share an 8-bit frequency divider.
T = times * cycle = 1/fre(pwm) where: cycle = 1/f = 1/(50M/(prescaler+1)/(divider value).
About the configuration method of timer 4:
1. From the schematic diagram above, we can know that we need to input a PCLK first (the size is 50M, which has been set in the startup code).
Then it passes through an 8-bit divider, the value can be between (0 and 255). Then it passes through a clock divider (the value can be 2, 4, 8, 16)
So first we should configure the divider and divider value (the corresponding registers are TCFG0, TCFG1).
2. We need to configure
Setting times
3. Configuration:
First, we need to set the manual update bit and auto reload bit (TCON)
Then, we need to set up the timer interrupt. For the interrupt method, see the document of the interrupt summary.
Next, we need to set the start bit and clear the manual update bit.
In this way, we have completed setting a timer.
How to set timer 0:
1. First, we need to set the corresponding output pins (as can be seen from the schematic diagram).
2. Secondly, we need to set the divider value and divider value. (TCFG0, TCFG1).
3. Again, we need to configure the initial value and comparison value of the number of times, where the comparison value determines the reversal moment of the level.
4. Then, we need to set the manual update bit and auto reload bit (TCON)
Next, we need to configure the timer interrupt. For the interrupt method, see the relevant documentation for interrupt configuration.
Finally, we start our timer. We need to set the start bit and clear the manual update bit.
In this way, timer 0 is configured.
The timer related code is as follows:
/************************************************
NAME : PWM.C
DESC :
Revision: 2015.8.18 ver 0.0
************************************************/
#include "pwm.h"
#include "led.h"
#include "music.h"
#include "uart.h"
volatile unsigned int div=0;
volatile unsigned int div10ms=0;
volatile unsigned int t=0;
volatile unsigned int t1=0;
extern void Delay_MS( unsigned int time);
volatile int count = 0;
// Tone frequency table
unsigned Syllable_freq[30]={
262,294,330,349,392,440,494,50000000,0,0,
523,587,659,698,784,880,988,0,0,0,
1046,1175,1318,1397,1568,1760,1976,0,0,0
};
void delayns(volatile int ns) //delay 200ms
{
t=(int)ns*200;
while(1)
{
if(t==0)
{
break;
}
}
}
void delayns1(volatile int ns) //delay 1s
{
t1=(int)ns*1000;
while(1)
{
if(t==0)
{
break;
}
}
}
/***********************************************
Function name : timer4_irq
Description: Timer 4 interrupt service function
Input parameter : none
Return : none
Others : none
*************************************************/
static void __irq timer4_irq(void)
{
rSRCPND |= BIT_TIMER4;
rINTPND |= BIT_TIMER4;
t--;
t1--;
div++;
switch(div%2)
{
case 0:
timer10ms();
break;
case 1:
break;
default:
break;
}
}
/***********************************************
Function name : time4_init
Description: Timer 4 initialization
Input parameter : none
Return : none
Others : none
*************************************************/
void time4_init(unsigned int msec) //pass in 1
{
volatile unsigned temp;
temp=rTCFG0;
temp&=~(0xf<<8);
temp|=0xff<<8;
rTCFG0=temp;
temp=rTCFG1;
temp&=~(0xf<<20|0xf<<16);
temp|=0x2<<16;
rTCFG1=temp; //Use 1/8 frequency
msec=(msec==0)?1:msec; //Legal boundary value judgment
rTCNTB4 = (50000*msec)>>11; //Set the timing count buffer value
temp=rTCON;
temp&=~(0x7<<20); //initialization
temp|=1<<22|1<<21; // can automatically reload and redefine the value of TCNTB4
rTCON=temp;
rSRCPND|=BIT_TIMER4;
rINTPND|=BIT_TIMER4;
rINTMOD &=~BIT_TIMER4;
rINTMSK &=~BIT_TIMER4;
pISR_TIMER4 = (unsigned int)timer4_irq;
temp = rTCON;
temp &=~(0x7<<20);
temp|=1<<22|1<<20;
rTCON=temp;
}
/***********************************************
Function name : timer10ms
Description: Timer processing function
Input parameter : none
Return : none
Others : none
*************************************************/
void timer10ms()
{
(div10ms<=4)?(div10ms++):(div10ms=0); //0 1 2 3 4
switch(div10ms)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
break;
}
}
/***********************************************
Function name : timer0_irq
Description: Timer 0 interrupt service function
Input parameter : none
Return : none
Others : none
*************************************************/
static void __irq timer0_irq(void)
{
rSRCPND |= BIT_TIMER0;
rINTPND |= BIT_TIMER0;
}
/***********************************************
Function name : time0_stop
Description: Disable timer 0
Input parameter : none
Return : none
Others : none
*************************************************/
void time0_stop()
{
volatile int temp;
//1. Stop register
rTCON &= ~(0x1<<0);
//2.configure GPB0 to output and low level disable the pull up function
temp = rGPBCON;
temp &=~(0x3<<0);
temp |= (0x1<<0);
rGPBCON =temp;
rGPBDAT &=~(0x1<<0);
rGPBUP |=(0x1<<0);
}
/***********************************************
Function name : time0_init
Description: Timer 0 initialization
Input parameter : none
Return : none
Others : timer0 init
*************************************************/
void time0_init(unsigned int nes)
{
volatile unsigned temp;
temp=rGPBCON;
temp&=~(0x3);
temp|=0x2;
rGPBCON=temp;
temp=rGPBUP;
temp&=0;
rGPBUP=temp;
temp=rTCFG0;
temp&=~(0xf);
temp|=0xff;
rTCFG0=temp;
temp=rTCFG1;
temp&=~(0xf<<20|0xf);//initialization
temp|=0x2;
rTCFG1=temp; //Use 1/8 frequency
rTCNTB0 = (50000000/nes)>>11;
rTCMPB0 = (50000000/nes)>>13;
temp=rTCON;
temp&=~(1<<3|1<<2|1<<1|1);
temp|=1<<3|1<<1;
rTCON=temp;
rSRCPND|=BIT_TIMER0;
rINTMOD &=~BIT_TIMER0;
rINTMSK &=~BIT_TIMER0;
rINTPND|=BIT_TIMER0;
pISR_TIMER0 = (unsigned int)timer0_irq;
temp=rTCON;
temp&=~(0xf);
temp|=1<<3|1;
rTCON=temp;
}
/***********************************************
Function name : play_music
Description: Music playback function
Input parameter : none
Return : none
Others : none
*************************************************/
void play_music(void)
{
unsigned char ch;
ch=uart_getc();
while(1)
{
switch(ch)
{
case 'q':
{
time0_stop();
time0_init(262);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'w':
{
time0_stop();
time0_init(294);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'e':
{
time0_stop();
time0_init(330);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'r':
{
time0_stop();
time0_init(349);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 't':
{
time0_stop();
time0_init(392);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'y':
{
time0_stop();
time0_init(440);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'u':
{
time0_stop();
time0_init(494);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'a':
{
time0_stop();
time0_init(523);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 's':
{
time0_stop();
time0_init(587);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'd':
{
time0_stop();
time0_init(659);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'f':
{
time0_stop();
time0_init(698);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'g':
{
time0_stop();
time0_init(784);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'h':
{
time0_stop();
time0_init(880);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'j':
{
time0_stop();
time0_init(988);
delays(1);
time0_init(Syllable_freq[7]); // 1046,1175,1318,1397,1568,1760,1976
break;
}
case 'z':
{
time0_stop();
time0_init(1046);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'x':
{
time0_stop();
time0_init(1175);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'c':
{
time0_stop();
time0_init(1318);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'v':
{
time0_stop();
time0_init(1397);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'b':
{
time0_stop();
time0_init(1568);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'n':
{
time0_stop();
time0_init(1760);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
case 'm':
{
time0_stop();
time0_init(1976);
delays(1);
time0_init(Syllable_freq[7]);
break;
}
default:
break;
}
ch=uart_getc();
}
}
Previous article:Summary of mini2440LCD module
Next article:Analysis of the interrupt generation principle in the mini2440 startup code
Recommended ReadingLatest update time:2024-11-16 07:33
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Millimeter wave 5G costs dropped by 52%
- 28335 memory space
- Circuit Learning
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] Bluetooth Protocol Basics
- How to configure the appropriate inductor for switching power supply
- GD32e231 ADC acquisition
- A preliminary study of k210 - MixNo - graphical programming
- 【i.MX6ULL】Driver Development 6——Pinctrl subsystem and GPIO subsystem light up LED
- Audi A6 computer version internal structure analysis, please come and teach me
- Excellent Works of the National College Student Electronic Design Competition - Smart Car Album