Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of PwmOut_HelloWorld by
main.cpp
00001 #include "mbed.h" 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include <time.h> 00005 00006 PwmOut output(D2); 00007 Serial pc(USBTX, USBRX); 00008 00009 float random_number; 00010 00011 void Configure_System_Clocks(void); 00012 00013 int main() { 00014 00015 Configure_System_Clocks(); 00016 output.period_us(100); // 100 kHz Period PWM Output 00017 output.write(0.50); // 50% Duty Cycle 00018 00019 srand(time(NULL)); // Seed the rand function to intialise it 00020 00021 while(1){ 00022 //random_number = (float)rand()/(float)RAND_MAX; // Generate random number between 0 and 1 00023 random_number = 0.3; 00024 if(random_number <= 0.5f) 00025 { 00026 output.write(0.5); 00027 } 00028 else 00029 { 00030 output.write(0); 00031 } 00032 } 00033 } 00034 00035 void Configure_System_Clocks(void){ 00036 PWR->CR |= PWR_CR_VOS_1; //Voltage scale 2 00037 00038 RCC->APB1ENR |= RCC_APB1ENR_PWREN; //APB1 Clock power enable 00039 00040 RCC->CR |= RCC_CR_HSEON; 00041 while((RCC->CR & RCC_CR_HSERDY) == 0){}; //Wait for HSE Ready 00042 00043 //RCC->CFGR |= RCC_CFGR_MCO1PRE_0; 00044 RCC->CFGR |= RCC_CFGR_MCO1; //MCO1 OUTPUT = 0=,1=0x02 00045 00046 FLASH->ACR |= FLASH_ACR_PRFTEN; //Enable Prefetch Buffer 00047 FLASH->ACR |= FLASH_ACR_ICEN; //Instruction Cache Enable 00048 FLASH->ACR |= FLASH_ACR_DCEN; //Data Cache Enable 00049 FLASH->ACR |= FLASH_ACR_LATENCY_2WS; //Flash 2 Wait State 00050 00051 RCC->CFGR |= RCC_CFGR_HPRE_DIV1; //AHB1PRESCALER = HCLK = SYSCLK 1=0x00 00052 RCC->CFGR |= RCC_CFGR_PPRE1_DIV2; //APB1PRESCALER = HCLK/2 2=0x04 00053 RCC->CFGR |= RCC_CFGR_PPRE2_DIV1; //APB2PRESCALER = HCLK/1 00054 00055 RCC->CR &= ~RCC_CR_PLLON; 00056 00057 RCC->PLLCFGR = (4ul | // PLL_M = 4 00058 (84ul << 6) | // PLL_N = 84 00059 (0ul << 16) | // PLL_P = 2 00060 (RCC_PLLCFGR_PLLSRC_HSE) | // PLL_SRC = HSE 00061 (7ul << 24)); // PLL_Q = 7 00062 00063 RCC->CR |= RCC_CR_PLLON; // Enable PLL 00064 while((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP(); // Wait till PLL is ready 00065 00066 RCC->CFGR &= ~RCC_CFGR_SW; // Select PLL as system clock source 00067 RCC->CFGR |= RCC_CFGR_SW_PLL; 00068 while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); // Wait till PLL is system clock src 00069 //System clock should be setup for 84MHz, lets output this to MCO1 00070 00071 00072 //Setup PA8 MCO1 as GPIO Output 00073 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; //Enable GPIOA Clock 00074 GPIOA->MODER |= GPIO_MODER_MODER8_1; //GPIO_MODER_MODER8_1 = 2 = Alternate Function Pin 00075 GPIOA->OTYPER |= (0ul<<8); //Push Pull Output 00076 GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR8; //High Speed Output 00077 GPIOA->PUPDR |= (0ul << 2*8); //No Pullup Pulldown 00078 GPIOA->AFR[1] |= (0ul << 0); //AFRH register holds P8-P15 AF Config, 0x00 = AF0 for PORT8 Which from Datasheet Table 9 = MCO1 00079 }
Generated on Fri Jul 29 2022 01:58:56 by
1.7.2
