Use the internal MSI oscillator to provide 80MHz clock to the STM32L476RG microcontroller

Publisher:数字小巨人Latest update time:2018-08-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include

#include

 

// 1<=nus<=13107

void delay_us(uint16_t nus)

{

if ((RCC->APB1ENR2 & RCC_APB1ENR2_LPTIM2EN) == 0)

{

RCC->APB1ENR2 |= RCC_APB1ENR2_LPTIM2EN;

LPTIM2->CFGR = 4 << LPTIM_CFGR_PRESC_Pos; // 80MHz/16=5MHz

LPTIM2->CR = LPTIM_CR_ENABLE;

}

LPTIM2->ARR = nus * 5 - 1;

LPTIM2->CR |= LPTIM_CR_SNGSTRT;

while ((LPTIM2->ISR & LPTIM_ISR_ARRM) == 0);

LPTIM2->ICR = LPTIM_ICR_ARRMCF;

}

 

int fputc(int ch, FILE *fp)

{

if (fp == stdout)

{

if (ch == '\n')

{

while ((USART2->ISR & USART_ISR_TXE) == 0);

USART2->TDR = '\r';

}

while ((USART2->ISR & USART_ISR_TXE) == 0);

USART2->TDR = ch;

}

return ch;

}

 

void set_clock(void)

{

RCC->APB1ENR1 |= RCC_APB1ENR1_PWREN;

PWR->CR1 |= PWR_CR1_DBP; // Allow access to the backup area register

// Open LSE

if ((RCC->BDCR & RCC_BDCR_LSEON) == 0)

{

RCC->BDCR |= RCC_BDCR_LSEON;

while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0); // Wait for LSE to stabilize

}

RCC->CR |= RCC_CR_MSIPLLEN; //MSI uses LSE to calibrate MSI

//Configure PLL: MSI/M*N/R=4MHz/1*40/2=80MHz

RCC->PLLCFGR = RCC_PLLCFGR_PLLREN | (40 << RCC_PLLCFGR_PLLN_Pos) | RCC_PLLCFGR_PLLSRC_MSI;

RCC->CR |= RCC_CR_PLLON;

while ((RCC->CR & RCC_CR_PLLRDY) == 0); // Wait for PLL to stabilize

// According to reference manual 3.3.3 Read access latency, Table 11. Number of wait states according to CPU clock (HCLK) frequency

// The CPU clock must reach 64MHz or above, and LATENCY must be 4WS

// From PWR_CR1_VOS, we know that the current V_CORE is Range 1 (maximum clock frequency 80MHz)

FLASH->ACR |= FLASH_ACR_LATENCY_4WS; // 参阅: Increasing the CPU frequency

while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLASH_ACR_LATENCY_4WS);

// Select PLL as system clock

RCC->CFGR |= RCC_CFGR_SW;

while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS);

// At the beginning, MSI has not been calibrated, so a delay is required when using 115200 baud rate, otherwise the first four characters will be garbled

// Delay 5 bytes, 69.4us per byte, 347us in total

delay_us(347);

// When using 9600 baud rate, it takes 833.3us to send a character, which is much longer than this time, so no delay is needed

}

 

int main(void)

{

set_clock();

//LED light configuration

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;

GPIOA->MODE &= ~GPIO_MODER_MODE5_1;

GPIOA->BSRR = GPIO_BSRR_BS5; // LED turns on

//Configure serial port 2

RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;

GPIOA->AFR[0] = 7 << GPIO_AFRL_AFSEL2_Pos;

GPIOA->MODE &= ~GPIO_MODER_MODE2_0;

//GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED2;

USART2->BRR = 80000000 / 115200; // The dividend is the clock frequency, the divisor is the baud rate

USART2->CR1 = USART_CR1_UE | USART_CR1_TE;

printf("ABCDEFGH\n");

printf("PWR->CR1=0x%08x\n", PWR->CR1);

printf("RCC->BDCR=0x%08x\n", RCC->BDCR);

printf("RCC->CFGR=0x%08x\n", RCC->CFGR);

printf("RCC->CR=0x%08x\n", RCC->CR);

printf("RCC->PLLCFGR=0x%08x\n", RCC->PLLCFGR);

printf("USART2->BRR=%d\n", USART2->BRR);

GPIOA->BRR = GPIO_BRR_BR5; // LED off

while (1);

}

MSI晶振接到PLL上后会通过外部的LSE晶振进行自动校准。校准期间,不精准的MSI时钟频率被PLL放大之后就会显著地影响串口的时钟,导致串口字符乱码,因此时钟切换后串口发送第一个字符之前必须延时,等待MSI时钟校准。


There is a simpler way to solve the serial port garbled problem without delay. The microcontroller also has a 16MHz HSI clock. Switching the USART2 clock from the APB1 clock related to MSI to the HSI clock in the RCC->CCIPR register can completely solve the problem.


【Procedure 2】


#include

#include

 

int fputc(int ch, FILE *fp)

{

if (fp == stdout)

{

if (ch == '\n')

{

while ((USART2->ISR & USART_ISR_TXE) == 0);

USART2->TDR = '\r';

}

while ((USART2->ISR & USART_ISR_TXE) == 0);

USART2->TDR = ch;

}

return ch;

}

 

void set_clock(void)

{

RCC->APB1ENR1 |= RCC_APB1ENR1_PWREN;

PWR->CR1 |= PWR_CR1_DBP; // Allow access to the backup area register

// Open LSE

if ((RCC->BDCR & RCC_BDCR_LSEON) == 0)

{

RCC->BDCR |= RCC_BDCR_LSEON;

while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0); // Wait for LSE to stabilize

}

RCC->CR |= RCC_CR_MSIPLLEN; //MSI uses LSE to calibrate MSI

//Configure PLL: MSI/M*N/R=4MHz/1*40/2=80MHz

RCC->PLLCFGR = RCC_PLLCFGR_PLLREN | (40 << RCC_PLLCFGR_PLLN_Pos) | RCC_PLLCFGR_PLLSRC_MSI;

RCC->CR |= RCC_CR_PLLON;

while ((RCC->CR & RCC_CR_PLLRDY) == 0); // Wait for PLL to stabilize

// According to reference manual 3.3.3 Read access latency, Table 11. Number of wait states according to CPU clock (HCLK) frequency

// The CPU clock must reach 64MHz or above, and LATENCY must be 4WS

// From PWR_CR1_VOS, we know that the current V_CORE is Range 1 (maximum clock frequency 80MHz)

FLASH->ACR |= FLASH_ACR_LATENCY_4WS; // 参阅: Increasing the CPU frequency

while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLASH_ACR_LATENCY_4WS);

// Select PLL as system clock

RCC->CFGR |= RCC_CFGR_SW;

while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS);

}

 

int main(void)

{

set_clock();

//LED light configuration

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;

GPIOA->MODE &= ~GPIO_MODER_MODE5_1;

GPIOA->BSRR = GPIO_BSRR_BS5; // LED turns on

//Configure serial port 2

RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;

GPIOA->AFR[0] = 7 << GPIO_AFRL_AFSEL2_Pos;

GPIOA->MODE &= ~GPIO_MODER_MODE2_0;

//GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED2;

// Using HSI16 as the clock of USART2 can avoid garbled characters

RCC->CR |= RCC_CR_HSION;

while ((RCC->CR & RCC_CR_HSIRDY) == 0);

RCC->CCIPR = RCC_CCIPR_USART2SEL_1; // HSI16 as the clock of USART2

USART2->BRR = 16000000 / 115200; // The dividend is the clock frequency, the divisor is the baud rate

USART2->CR1 = USART_CR1_UE | USART_CR1_TE;

printf("ABCDEFGH\n");

printf("PWR->CR1=0x%08x\n", PWR->CR1);

printf("RCC->BDCR=0x%08x\n", RCC->BDCR);

printf("RCC->CFGR=0x%08x\n", RCC->CFGR);

printf("RCC->CR=0x%08x\n", RCC->CR);

printf("RCC->PLLCFGR=0x%08x\n", RCC->PLLCFGR);

printf("USART2->BRR=%d\n", USART2->BRR);

GPIOA->BRR = GPIO_BRR_BR5; // LED off

while (1);

}


Reference address:Use the internal MSI oscillator to provide 80MHz clock to the STM32L476RG microcontroller

Previous article:stm32l476 clock settings
Next article:STM3210X external clock configuration and frequency multiplication selection

Recommended ReadingLatest update time:2024-11-23 07:26

Clock signal of MSP430 microcontroller
  MSP430 has three clock signals: MCLK system main clock; SMCLK system sub-clock; ACLK auxiliary clock.      (1) MCLK system main clock. In addition to the CPU operation using this clock, peripheral modules can also use it. MCLK can select the clock signal generated by any oscillator and divide it by 1, 2, 4, or 8 as
[Microcontroller]
Machine cycle, instruction cycle, clock cycle, beat and crystal oscillator
Clock cycle The clock cycle is also called the oscillation cycle, which is defined as the inverse of the clock pulse (the clock cycle is the inverse of the crystal oscillator connected to the microcontroller. For example, the clock cycle of a 12M crystal oscillator is 1/12us). It is the most basic and smallest t
[Microcontroller]
System clock of s3c2440
The following content is from "ARM processor bare metal development practice - mechanism rather than strategy", taking the s3c2440 development board as an example The system clock is the heart of the entire circuit. Generally speaking, there are four main clocks related to the s3c2440 processor: Fin, FCLK, HCLK an
[Microcontroller]
System clock of s3c2440
Shiqiang Components e-commerce launched clock customization burning service
Following the launch of the EPSON programmable crystal oscillator frequency burning service, Shiqiang Components E-commerce has launched the Silicon Labs clock burning service, which supports multiple output formats and can be delivered within 2-3 days. The burning quality is 100% guaranteed, and it can more comprehen
[Semiconductor design/manufacturing]
Shiqiang Components e-commerce launched clock customization burning service
Driver for the real-time clock chip DS1302 based on single-chip microcomputer
This program realizes the rotation display of time and calendar.   Since it was debugged on January 5, 2011, the initial time is set to 11:59:58, and the calendar is 11-01-05                      // Title: Experiment with digital tube display clock                                                   //        
[Microcontroller]
51 MCU clock
1. The clock cycle is 12 times the machine cycle, and the machine cycle is the reciprocal of the crystal frequency.   2.51 MCU 1 machine cycle = 12 clock cycles, the frequency is 12MHZ, then one machine cycle is 1US. Specifically for the timer program, if you want to set it to 1MS, then each time the MCU adds one, it
[Microcontroller]
PIC microcontroller in ICD LCD display clock demonstration program
;******************************************************** ;* CLKTEST.ASM * ;***************************************************** ;* ICDDEMO teaching experiment board LCD display clock demonstration program* ;**************************************************** include ;*---- ; Define LCD data & control
[Microcontroller]
Testing high-speed clocks with low-cost instruments
       When you need to measure high-speed clock frequencies, you may choose an expensive benchtop instrument. In fact, you can test high-speed clocks using the digital capture capabilities of low-cost digital test instruments, plus some DSP software functions. The following article introduces the specific implementat
[Test Measurement]
Testing high-speed clocks with low-cost instruments
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号