Page 1 of 3
Setting PWM prescale
Posted: Fri Jul 10, 2020 7:51 pm
by amowry
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!
Re: Setting PWM prescale
Posted: Fri Jul 10, 2020 8:01 pm
by amowry
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.
Re: Setting PWM prescale
Posted: Fri Jul 10, 2020 9:33 pm
by TomKerekes
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.
Re: Setting PWM prescale
Posted: Fri Jul 10, 2020 9:45 pm
by amowry
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!
Re: Setting PWM prescale
Posted: Fri Jul 10, 2020 9:51 pm
by TomKerekes
My mistake. I see now.
Re: Setting PWM prescale
Posted: Sat Jul 11, 2020 3:05 pm
by amowry
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!
Re: Setting PWM prescale
Posted: Sat Jul 11, 2020 5:40 pm
by TomKerekes
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.
Re: Setting PWM prescale
Posted: Sat Jul 11, 2020 5:44 pm
by amowry
I'll check it out. Thanks Tom!
Re: Setting PWM prescale
Posted: Mon Jul 13, 2020 8:25 pm
by amowry
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.
Re: Setting PWM prescale
Posted: Mon Jul 13, 2020 9:09 pm
by TomKerekes
Hi amowry,
Code: Select all
EnableRS232Cmds(RS232_BAUD_9600);
DoRS232Cmds = FALSE; // turn off processing RS232 input as commands
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
if (!ReadBit(1024))
{
EnableRS232Cmds(RS232_BAUD_9600);
DoRS232Cmds = FALSE; // turn off processing RS232 input as commands
SetBit(1024);
}