Has anyone used a PWM to generate a 20 to 25 MHz clock on one of the GPIO pins? how would I be able to get a symmetric clock?
20-25MHz clock on a gpio pin
(10 posts) (3 voices)-
Posted 4 years ago #
-
specifically: would this work? would this give 18MHz? unfortunately I dont have access to an oscilloscope :(
<code> pinMode(14,PWM); timer.setPrescaleFactor(1); timer.setOverflow(4); pwmWrite(14, 2); </code>
Posted 4 years ago # -
Yes I believe that is correct.
Posted 4 years ago # -
newyorkbrass - you could make it easy to measure the actual cycle time by using scale factors to get big values for prescaler and overflow, and scale the pwm value to match it. Then you can time the signal by flashing an LED and using a watch, no oscilloscope needed.
So
- prescaler is (1 << 16)
- overflow (4 << 10)
- pwm(14, (2 << 10))
would be 2^(16+10) times slower = 67,108,864 times slower.
If the unshifted values are correct for 18MHz, the cycle time for these shifted values should be about 3.7 seconds, and if the values are wrong, the error will be about 1/4 of that, or about 0.9 seconds which should be plainly visible.I think you'll find the values you've chosen are too big for a symmetric PWM signal.
When the PWM signal is symmetric, it takes two PWM-counter cycles for each signal cycle. The counter counts up, then down to make a single PWM pulse. So the prescale value will need to be halved (which it can't be, it is one), or the overflow value and PWM value will need to be halved.Posted 4 years ago # -
found a better solution:
I found how to output the HSE clock as a peripheral on pin 6 (GPIOA 8):
I combined some stuff from the "official" libraries and the ones in libmaple:
#define RCC_MCO_HSE ((unsigned char)0x06)//typedef volatile unsigned char vu8;
/* CFGR register byte 4 (Bits[31:24]) base address */#define CFGR_BYTE4_ADDRESS ((unsigned int)0x40021007)
void RCC_MCOConfig(volatile unsigned char RCC_MCO)
{
/* Check the parameters *//* Perform Byte access to MCO[2:0] bits to select the MCO source */
*(volatile unsigned char *) CFGR_BYTE4_ADDRESS = RCC_MCO;
}then in the init:
rcc_clk_enable(RCC_GPIOA);
gpio_set_mode( GPIOA, 8, GPIO_OUTPUT_PP );
RCC_MCOConfig(RCC_MCO_HSE);
This should work .... :)
Posted 4 years ago # -
newyorkbrass - not sure how that gets you a symmetric 18MHz, or 20 to 25MHz square wave.
On a Maple, the crystal is 8MHz.What are you actually trying to achieve?
Posted 4 years ago # -
I want to connect two PixArt Wii cameras to the maple and it needs a 16-25MHz clock. Doing this would allow direct interfacing of the maple to the camera. The The HSE is 8MHz but according to documentation I understand I can use a PLL to generate that frequency. Just dont want to break anything else in the system on the way :)
Posted 4 years ago # -
I read the docs again:
The microcontroller clock output (MCO) capability allows the clock to be output onto the
external MCO pin. The configuration registers of the corresponding GPIO port must be
programmed in alternate function mode. One of 8 clock signals can be selected as the MCO
clock.
● SYSCLK
● HSI
● HSE
● PLL clock divided by 2 selected
● PLL2 clock selected
● PLL3 clock divided by 2 selected
● XT1 external 3-25 MHz oscillator clock selected (for Ethernet)
● PLL3 clock selected (for Ethernet)
The selected clock to output onto MCO must not exceed 50 MHz (the maximum I/O speed).So I guess your right except i am using the wrong MCO out, I should be using: "PLL3 clock selected (for Ethernet)" that will output 25MHz
Posted 4 years ago # -
OOOPS. connectivity line
This is the correct one:Clock-out capability
The microcontroller clock output (MCO) capability allows the clock to be output onto the
external MCO pin. The configuration registers of the corresponding GPIO port must be
programmed in alternate function mode. One of 4 clock signals can be selected as the MCO
clock.
● SYSCLK
● HSI
● HSE
● PLL clock divided by 2Posted 4 years ago # -
newyorkbrass - I think all of those clock outputs suffer the same problem. If it works first time, great, if it doesn't, it will be difficult to debug (without an oscilloscope or some way of measuring frequency or cycle time).
I think these things are clear:
1. A timer can generate either the 18MHz or 24MHz frequency needed
2. at either frequency it can be equal mark space ratio
3. at 18MHz, it can generate symmetric PWM
4. by simply shifting the prescaler, overflow and pwm values, it is practical to make the timing of the wave form so clear to human perception that it can be measured and verified with a watch, and then speeded up to the frequency needed.Posted 4 years ago #
Reply
You must log in to post.