Setting PWM prescale

Moderators: TomKerekes, dynomotion

amowry
Posts: 51
Joined: Fri Jan 26, 2018 1:00 am

Setting PWM prescale

Post by amowry » Fri Jul 10, 2020 7:51 pm

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!
K2_SpindlePWM.c
(778 Bytes) Downloaded 134 times

amowry
Posts: 51
Joined: Fri Jan 26, 2018 1:00 am

Re: Setting PWM prescale

Post by amowry » Fri Jul 10, 2020 8:01 pm

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.

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: Setting PWM prescale

Post by TomKerekes » Fri Jul 10, 2020 9:33 pm

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.
Regards,

Tom Kerekes
Dynomotion, Inc.

amowry
Posts: 51
Joined: Fri Jan 26, 2018 1:00 am

Re: Setting PWM prescale

Post by amowry » Fri Jul 10, 2020 9:45 pm

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!

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: Setting PWM prescale

Post by TomKerekes » Fri Jul 10, 2020 9:51 pm

My mistake. I see now.
Regards,

Tom Kerekes
Dynomotion, Inc.

amowry
Posts: 51
Joined: Fri Jan 26, 2018 1:00 am

Re: Setting PWM prescale

Post by amowry » Sat Jul 11, 2020 3:05 pm

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!

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: Setting PWM prescale

Post by TomKerekes » Sat Jul 11, 2020 5:40 pm

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.
Regards,

Tom Kerekes
Dynomotion, Inc.

amowry
Posts: 51
Joined: Fri Jan 26, 2018 1:00 am

Re: Setting PWM prescale

Post by amowry » Sat Jul 11, 2020 5:44 pm

I'll check it out. Thanks Tom!

amowry
Posts: 51
Joined: Fri Jan 26, 2018 1:00 am

Re: Setting PWM prescale

Post by amowry » Mon Jul 13, 2020 8:25 pm

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 112 times

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: Setting PWM prescale

Post by TomKerekes » Mon Jul 13, 2020 9:09 pm

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);
}
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply