Setting PWM prescale
Moderators: TomKerekes, dynomotion
Setting PWM prescale
I am trying to use PWM to control laser intensity using a board provided by the laser manufacturer that accepts a PWM input with recommended frequency of 1-2 KHz. I thought I had it set up correctly, but the board is outputting the maximum voltage for even low PWM duty cycles. Could you verify that I am setting the PWM frequency correctly in the attached code? The lines I added are the "divide clock" and "square wave" ones. I'm measuring 0-3 V from the PWM, so that seems correct, but I'm not sure if I'm setting the frequency correctly.
Thanks!
Thanks!
Re: Setting PWM prescale
On second though, I'm not sure if I can change the frequency, because K2 seems to be using the other PWM pins, and I don't want to mess them up. I may try to use an Arduino to measure the PWM and convert it to 1 KhZ.
- TomKerekes
- Posts: 2676
- Joined: Mon Dec 04, 2017 1:49 am
Re: Setting PWM prescale
Hi amowry,
I think you posted the wrong file. I don't see "divide clock" anywhere.
But yes there only is one pre-scaler so if K2 is setting it (probably to a higher frequency) to drive motors then it shouldn't be changed.
I think you posted the wrong file. I don't see "divide clock" anywhere.
But yes there only is one pre-scaler so if K2 is setting it (probably to a higher frequency) to drive motors then it shouldn't be changed.
Regards,
Tom Kerekes
Dynomotion, Inc.
Tom Kerekes
Dynomotion, Inc.
Re: Setting PWM prescale
It looks like the correct file-- the fourth line from the bottom is this:
FPGA(IO_PWMS_PRESCALE) = 65; // divide clock
I think I'm doing it correctly, but yes, I think I'd better not mess with the frequency. I'll pursue other options.
Thanks!
FPGA(IO_PWMS_PRESCALE) = 65; // divide clock
I think I'm doing it correctly, but yes, I think I'd better not mess with the frequency. I'll pursue other options.
Thanks!
- TomKerekes
- Posts: 2676
- Joined: Mon Dec 04, 2017 1:49 am
Re: Setting PWM prescale
It occurs to me that it would make more sense to just communicate with a 3.3V Arduino by UART-- Are the only UART pins IO44 and IO45 as mentioned in the documentation? I wasn't sure if those were just an example. I believe those pins are already in use on mine, but could the J5 pins be used instead, for instance? Or maybe another method you would suggest to communicate with an Arduino to send a spindle speed?
Yesterday I tried sampling the PWM digitally with the Arduino and didn't have a lot of luck, maybe because the frequency is so high, and I also tried a simple RC filter to try to make an analog signal with slightly better results, but it seems like a noisy and unnecessarily complex method of communicating.
Thanks Tom!
Yesterday I tried sampling the PWM digitally with the Arduino and didn't have a lot of luck, maybe because the frequency is so high, and I also tried a simple RC filter to try to make an analog signal with slightly better results, but it seems like a noisy and unnecessarily complex method of communicating.
Thanks Tom!
- TomKerekes
- Posts: 2676
- Joined: Mon Dec 04, 2017 1:49 am
Re: Setting PWM prescale
Hi amowry,
KFLOP's UART pins are fixed to IO45 and IO46
You might consider SPI which KFLOP does well with software bit-banging at ~400KBits/sec (faster than the UART). See the SPI V6.c example.
KFLOP's UART pins are fixed to IO45 and IO46
You might consider SPI which KFLOP does well with software bit-banging at ~400KBits/sec (faster than the UART). See the SPI V6.c example.
Regards,
Tom Kerekes
Dynomotion, Inc.
Tom Kerekes
Dynomotion, Inc.
Re: Setting PWM prescale
I'll check it out. Thanks Tom!
Re: Setting PWM prescale
It turned out that K2 isn't using the UART but is using SPI, so I hooked up the Arduino to the UART and everything is working fine. I do have one question: when I send a char to the Arduino, KFLOP seems to send a "0" first, and then whatever char I'm trying to send. Do you have an idea of why it would be sending two bytes? Thanks! I've attached my code.
- Attachments
-
- K2_SpindlePWM.c
- (560 Bytes) Downloaded 111 times
- TomKerekes
- Posts: 2676
- Joined: Mon Dec 04, 2017 1:49 am
Re: Setting PWM prescale
Hi amowry,
Initializes the UART which sends a null character. You don't need to do that every time a character is sent. You might just do it once in your Initialization Program after a power cycle. You might set a Virtual Bit to indicate it has been initialized with:
Code: Select all
EnableRS232Cmds(RS232_BAUD_9600);
DoRS232Cmds = FALSE; // turn off processing RS232 input as commands
Code: Select all
if (!ReadBit(1024))
{
EnableRS232Cmds(RS232_BAUD_9600);
DoRS232Cmds = FALSE; // turn off processing RS232 input as commands
SetBit(1024);
}
Regards,
Tom Kerekes
Dynomotion, Inc.
Tom Kerekes
Dynomotion, Inc.