1+ // copied from the mco_clock_output example in the ch32fun example
2+ // This allows the device to output one of four internal
3+ // clock signals to the PC8 pin. It cycles through no signal and
4+ // each of the four different clock signals. Each
5+ // signal is output for five seconds before moving on to the next.
6+ // The serial output displays the signals as they are selected.
7+
8+ // The different signals are:
9+ // 0) Nothing
10+ // 1) SYSCLK (48MHz)
11+ // 2) HSI (24MHz)
12+ // 3) HSE (depends on external XTAL)
13+ // 4) PLL clock output (48MHz)
14+
15+ #include "ch32fun.h"
16+ #include <stdio.h>
17+
18+ int main () {
19+ SystemInit ();
20+ Delay_Ms (100 );
21+ printf ("\n~ MCO Clock Test ~\n" );
22+
23+ funGpioInitAll ();
24+ funPinMode (PA8 , GPIO_CFGLR_OUT_50Mhz_AF_PP );
25+
26+ // uncomment this for the HSE clock source if you have an external crystal
27+ // // turn the HSE on
28+ // RCC->CTLR |= RCC_HSE_ON;
29+ // // Wait till HSE is ready
30+ // while(!(RCC->CTLR & RCC_HSERDY));
31+
32+ int count = 0 ;
33+ int regtemp ;
34+
35+ while (1 ) {
36+ regtemp = (RCC -> CFGR0 & ~RCC_CFGR0_MCO );
37+
38+ switch (count ) {
39+ case 0 :
40+ printf ("\r\nNo signal on MCO\r\n" );
41+ printf ("CFGR0 going from %08lX to %08lX\r\n" , RCC -> CFGR0 , regtemp );
42+ RCC -> CFGR0 = regtemp ;
43+ count ++ ;
44+ break ;
45+ case 1 :
46+ printf ("\r\nSYSCLK signal on MCO\r\n" );
47+ regtemp |= RCC_CFGR0_MCO_SYSCLK ;
48+ printf ("CFGR0 going from %08lX to %08lX\r\n" , RCC -> CFGR0 , regtemp );
49+ RCC -> CFGR0 = regtemp ;
50+ count ++ ;
51+ break ;
52+ case 2 :
53+ printf ("\r\nHSI signal on MCO\r\n" );
54+ regtemp |= RCC_CFGR0_MCO_HSI ;
55+ printf ("CFGR0 going from %08lX to %08lX\r\n" , RCC -> CFGR0 , regtemp );
56+ RCC -> CFGR0 = regtemp ;
57+ count ++ ;
58+ break ;
59+ case 3 :
60+ printf ("\r\nHSE signal on MCO\r\n" );
61+ regtemp |= RCC_CFGR0_MCO_HSE ;
62+ printf ("CFGR0 going from %08lX to %08lX\r\n" , RCC -> CFGR0 , regtemp );
63+ RCC -> CFGR0 = regtemp ;
64+ count ++ ;
65+ break ;
66+ case 4 :
67+ printf ("\r\nPLLCLK signal on MCO\r\n" );
68+ regtemp |= RCC_CFGR0_MCO_PLL ;
69+ printf ("CFGR0 going from %08lX to %08lX\r\n" , RCC -> CFGR0 , regtemp );
70+ RCC -> CFGR0 = regtemp ;
71+ count = 0 ;
72+ break ;
73+ }
74+ Delay_Ms (5000 );
75+ }
76+ }
0 commit comments