System clock and timer - PWM timer

Publisher:BlossomBeautyLatest update time:2016-08-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The clock control logic provides three clocks for the entire chip: FCLK is used for the CPU core; HCLK is used for AHB bus devices, such as the CPU core, memory controller, interrupt controller, DMA and USB host module; PCLK is used for devices on the APB bus, such as WATCHDOG, IIS, IIC, PWM, MMC, ADC, RTC, etc.

When the operating voltage of the S3C2440 CPU core is 1.2V, the main frequency can reach 300MHz; when the operating voltage is 1.3V, the main frequency can reach 400MHz. The development board is 12MHz, and the system clock needs to be increased through the PLL of the clock control logic.

S3C2440 has two PLLs: MPLL and UPLL. UPLL is dedicated to USB devices, and MPLL is used to set FCLK, HCLK, and PCLK.

System clock and timer - PWM timer - Cheng - Xuehai Fanzhou
                                                    Figure 1: MPLL startup process after power-on
The length of Lock Time is set by the register LOCKTIME. Generally, the default value is used.
Check the chip manual, the MPLL calculation formula is as follows:
 
System clock and timer - PWM timer - Cheng - Xuehai Fanzhou

 Where m=MDIV+8, p=PDIV+2, s=SDIV

CLKDIVN register: used to set the ratio of FCLK, HCLK, and PCLK. The register settings corresponding to various clock ratios are shown in the figure below:

System clock and timer - PWM timer - Cheng - Xuehai Fanzhou

 

System clock and timer - PWM timer - Cheng - Xuehai Fanzhou
 
 
1. PWM (Pulse Width Modulation) timer
S3C2440 has 5 16-bit timers. Timers 0, 1, 2, and 3 have PWM functions, that is, they all have a pin, and the timer can be used to control the periodic high and low level changes of the pin; timer 4 has no output pin. The timer clock source is PCLK. The internal structure is shown in the figure:
System clock and timer - PWM timer - Cheng - Xuehai Fanzhou
Timer 0 and 1 share the first prescaler, and timer 2, 3, and 4 share the second prescaler. The output of the prescaler enters the second-stage divider, which has five clock frequencies: 2, 4, 8, and 16. The prescaler is set by TCFG0, and the division value (2, 4, 8, 16) is set by TCFG1.
 
 Now we can conclude that starting a PWM function can be divided into the following steps:
1. Set TOUT0-TOUT3. This requires configuring the relevant external pins to TOUT mode. The external pins GPB and TOUT are multiplexed pins.
2. Set the output frequency of the timer and set the value of the prescaler through TCFG0 and the value of the divider through TCFG1.
3. The specific pulse width is completed by setting the two registers TCMPBn and TCNTBn. This step involves the workflow of the internal logic of the register. As follows:
Load TCMPBn and TCNTBn with initial values, and start the timer after setting the TCON register. At this time, load the values ​​of the two registers TCMPBn and TCNTBn into the internal registers TCMPn and TCNTn. At the working frequency of timer n, TCNTn starts to count down by one. When the value of TCNTn is equal to the value of TCMPn, TOUTn flips. When the value of TCNTn reaches 0, it flips again, and it repeats in this way, forming a pulse. And trigger the interrupt of timer n (if the interrupt is set).
 
4. Specific control is to be handed over to the TCON register. It has the following functions: (see the data sheet first)
System clock and timer - PWM timer - Cheng - Xuehai Fanzhou
 
 It can be seen that it has the following four functions:
1. Start/stop the timer. TCON[0]
2. Manual update. Used to manually update TCNTBn and TCMPBn. It should be noted that this bit must be cleared to zero when starting the timing, otherwise the timer cannot be started. TCON[1]
3. Output inversion. TOUTn non-inversion (0)/inversion (1) TCON[2]
4. Automatic loading. When the timer counts to 0, the values ​​of the TCMPBn and TCNTBn registers are automatically loaded into the internal registers TCMPn and TCNTn. The TCON[3]
program is analyzed as follows: (This program is from Professor Zhao Chunjiang)

 

#include "2440addr.h"

#define U32 unsigned int

typedef unsigned char BOOL;

#define TRUE       1  

#define FALSE     0

 

BOOL stop;

 

static void __irq Key3_ISR(void) /*Pause key, turn off the buzzer*/

{

       rSRCPND = rSRCPND | (0x1<<2); /*define EINT2*/

       rINTPND = rINTPND | (0x1<<2);

      

       rTCON &= ~0x8; /*Disable timer automatic reload, that is, turn off timer 0111*/

       stop = TRUE;

}

 

void __irq Key2_ISR(void) /*Restart key, turn on the buzzer*/

{

       rSRCPND = rSRCPND | 0x1; /*define EINT0*/

       rINTPND = rINTPND | 0x1;

      

       stop = FALSE;

}

 

 

void delay(int a)

{

       int k;

       for(k=0;k

              ;

}

 

void Main(void)

{

       int freq;

      

       rGPBCON = 0x155556; /*B0 is TOUT0, B5~B8 are output, for LED 0001 0101 0101 0101 0101 0110*/

       rGPBUP = 0x7ff; /*0111 1111 1111 turn off pull-up enable*/

       rGPFCON = 0xaaaa; /*Port F is EINT, give the button 1010 1010 1010 1010*/

       /*Some necessary configurations for buttons*/

       rSRCPND = 0x0f; /*Interrupt setting*/

       rINTMSK = ~0x0f;

       rINTPND =0x0f;

       rEXTINT0 = 0x2222; /*EINT0/EINT1 are both set to falling edge trigger*/

      

       freq = 2500;

      

       rTCFG0 &= 0xFFFF00;

       rTCFG0 |= 0x31;    /*prescal 是49 3*16+1=49 timer0 and timer1*/

       rTCFG1 &= ~0xF; /*Clear the lower four bits, divider value=1/2, because PCLK is 50MHz, so 50MHz/50/2=500kHz/

       rTCNTB0 = 5000; /*Timer count initial value*/

       rTCMPB0 = freq; /*Timer comparison value*/

       rTCON &= ~0x1F;       

       rTCON |= 0xf; /*Dead zone invalid, automatic loading, level inversion, manual update, timer on*/

       rTCON &= ~0x2 ; /*Manual update bit cleared, PWM starts working*/

      

       pISR_EINT0 = (U32)Key2_ISR;

       pISR_EINT2 = (U32)Key3_ISR;

 

       stop = FALSE;

      

       rGPBDAT = ~0x60; /*Two LEDs are on*/

      

       while(1)

       {

              /*Frequency increasing*/

              for ( ; freq<4950 ; )

              {

                     freq+=10;

                     rTCMPB0 = freq; /*Reassign value*/

                     delay(20000);

                    

                     while (stop == TRUE) /*Whether to pause*/

                     {

                            delay(1000);

                            if (stop == FALSE) /*Judge whether to restart*/

                            {

                                   rTCON &= ~0x1F;

                                   rTCON |= 0xf;

                                   rTCON &= ~0x2 ; /*Restore PWM function*/

                            }

                     }

                     /*4 LEDs turn on and off depending on the frequency. Number of lights on: 4-3-2-1*/

                     if(freq == 100)

                            rGPBDAT = ~0x560; /*0001 1110 0000 inverted 1110 0001 1111*/

                     if(freq == 1300)

                     rGPBDAT = ~0x160; /*1110 0000 inverted 0001 1111*/

                     if(freq == 2500)

                            rGPBDAT = ~0x60; /*0110 0000 is inverted to 1001 1111*/

                     if(freq == 3700)

                            rGPBDAT = ~0x20; /*0010 0000 is inverted to 1101 1111*/

                     if(freq == 4900)

                            rGPBDAT = ~0x0; /*0000 is inverted 11111111111*/

                           

              }

             

              /*Frequency decreasing*/

              for( ; freq>50 ; )

              {

                     freq-=10;

                     rTCMPB0 = freq;

                     delay(20000);

                     while (stop == TRUE)

                     {

                            delay(1000);

                            if (stop ==FALSE)

                            {

                                   rTCON &= ~0x1F;

                                   rTCON |= 0xf;

                                   rTCON &= ~0x2 ;

                            }

                     }

                     if(freq == 100)

                            rGPBDAT = ~0x560;

                     if(freq == 1300)

                            rGPBDAT = ~0x160;

                     if(freq == 2500)

                            rGPBDAT = ~0x60;

                     if(freq == 3700)

                            rGPBDAT = ~0x20;

                     if(freq == 4900)

                            rGPBDAT = ~0x0;

              }

       }    

}

 

The waveform of TOUT output is:
  
System clock and timer - PWM timer - Cheng - Xuehai Fanzhou
 The figure above is a rough waveform when the frequency rises. It can be seen that the high level is maintained for a longer and longer time in each cycle, and the low level time is getting shorter and shorter. The waveform when the frequency decreases is just the opposite.
Reference address:System clock and timer - PWM timer

Previous article:System clock and timer - watchdog timer
Next article:S3C2440 external interrupt key interrupt

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号