Author Topic: WebbotLib PWM Resolution  (Read 607 times)

0 Members and 1 Guest are viewing this topic.

Offline totalisTopic starter

  • Full Member
  • ***
  • Posts: 89
  • Helpful? 0
WebbotLib PWM Resolution
« on: January 22, 2014, 10:10:44 AM »
Hi All,

I am intending to use the PWM pins on the Arduino to control a precision power supply.  I am trying to set a 16-bit PWM pin to a better accuracy than 0-100% 

Is there any way I can use the full 16-bit range of the PWM pins using WebbotLib?

Thanks

T

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: WebbotLib PWM Resolution
« Reply #1 on: January 22, 2014, 06:40:31 PM »
What Version of WebbotLib are you using (Version 1.xx, Version 2.xx, or Studio?). Are you using C or C++.
What Device have you added to your project to do the PWM?
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline totalisTopic starter

  • Full Member
  • ***
  • Posts: 89
  • Helpful? 0
Re: WebbotLib PWM Resolution
« Reply #2 on: January 22, 2014, 06:57:52 PM »
I have been using version 1.15 (old I know, but I am used to it and i'm waiting to find out what your IDE is like :))

I am using AVR studio 5 as the environment, and I did not use the project designer to setup the defaults (although I think I might have to start doing so)

It is in C on an ATMEGA328P

T

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: WebbotLib PWM Resolution
« Reply #3 on: January 22, 2014, 10:04:30 PM »
Man - you're in the stone age! But that's fine. So if you didn't use Project Designer then you must be setting up the PWM yourself via some of the timer functions. Am sure that you can then access the 16 bit as well. But perhaps some sample code (PM to me if you want to keep it private).
Quote
waiting to find out what your IDE is like :))
Already being used by a number of folk!
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline totalisTopic starter

  • Full Member
  • ***
  • Posts: 89
  • Helpful? 0
Re: WebbotLib PWM Resolution
« Reply #4 on: January 23, 2014, 03:52:16 AM »
I took a year out of programming to finish my Degree.  I come back to find all the clever things have happened to WebbotLib, but that it will take a small amount of time to get used to...

Anyway, code:

Code: [Select]
#include "sys/atmega328P.h"
#include "a2d.h"
#include "uart.h"
#include "pwm.h"
#include "servos.h"
#include "rprintf.h"
#include "spi.h"

#define Speed ADC_NUMBER_TO_CHANNEL(0)

uint16_t currValue = 0;
int outputVal = 0;

int index = 0; //used to keep track of incoming byte position
int rxdata[4]; //create the array to be used as the buffer
int cmd[4]; //To store the command for processing
boolean execute = FALSE; //Execute?

int AOut = 111;

void myReceiveCallBack(unsigned char data, void* device){
//Recieve interrupt, called with each incoming byte
rxdata[index] = data; //Puts the incoming data into the rxdata array at position ch
index++; //increment index
if(index == 4){
index = 0;
cmd[0] = rxdata[0];
cmd[1] = rxdata[1];
cmd[2] = rxdata[2];
cmd[3] = rxdata[3];
execute = TRUE;
}
}

void appInitHardware(void) {

uartInit(UART0, (BAUD_RATE)115200);
uartAttach(UART0, &myReceiveCallBack, NULL);
rprintfInit(&uart0SendByte);
rprintfStr("Hello");
pwmInitHertz(D3, 500, 50, null);
pin_make_input(D7,FALSE);

}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

if(execute == TRUE){
execute = FALSE;
if(cmd[0] == 'a'){ //Channel A
if(cmd[1] >= '0' && cmd[1] <= '9' && cmd[2] >= '0' || cmd[2] <= '9' && cmd[3] >= '0' || cmd[3] <= '9'){
cmd[1] -= '0';
cmd[2] -= '0';
cmd[3] -= '0';
AOut = cmd[1] * 100 + cmd[2] * 10 + cmd[3];
}
}
}
if(AOut > 100) currValue = a2dConvert10bit(Speed) / 10;
else currValue = AOut;

pwmSetDutyCycle(D3,(currValue));
spiDeviceSendByte(&sevenSegment,numOut[(currValue/10)]);

if (pin_is_low(D7)){
rprintfStr("Button");
index = 0;
}

return 5000;
}

The problem is that using "pwmSetDutyCycle" only lets you go up in steps of 1%

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: WebbotLib PWM Resolution
« Reply #5 on: January 23, 2014, 07:22:44 PM »
Given that pwmSetDutyCycle works for 8 or 16 bit timers then its not something I really want to change to behave differently.
However: if you look at the source for it you'll see

Code: [Select]
// Set the duty cycle
void pwmSetDutyCycle(const IOPin* pin, DUTY_CYCLE duty){
const TimerCompare* channel = compareFromIOPin(pin);
if(channel){
const Timer* timer = compareGetTimer(channel);
uint32_t top = timerGetTOP(timer);

// Limit the duty cycle
if(duty>100)
duty=100;

uint16_t delay = interpolateU(duty, 0,100, 0,top);

// Change the duty cycle
compareSetThreshold(channel,delay);
}
}

They key part is :
Code: [Select]
uint16_t delay = interpolateU(duty, 0,100, 0,top); which converts your input 0% to 100%,  into a value from 0 to top - ie sets the duty cycle of the pwm.
You could produce your own version of this routine that takes the current timer 'top' value, calculates a more exact version for 'delay' (0= permanently low up to top which is permanently high) which it passes to compareSetThreshold
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: WebbotLib PWM Resolution
« Reply #6 on: January 23, 2014, 08:40:12 PM »
One small warning: I have found that setting the PWM compare value to 0 does *not* cause the PWM to output constant-low. It will output a very short spike each time the timer cycles. To make it output constant-low, you have to turn off the output compare unit.
This may or may not actually matter in this case, but I thought I'd point it out!

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: WebbotLib PWM Resolution
« Reply #7 on: January 24, 2014, 01:23:39 PM »
One small warning: I have found that setting the PWM compare value to 0 does *not* cause the PWM to output constant-low. It will output a very short spike each time the timer cycles. To make it output constant-low, you have to turn off the output compare unit.
This may or may not actually matter in this case, but I thought I'd point it out!

Thanks for pointing that out. You can turn off/on the compare unit in WebbotLib if you need to do this - but you will also need to make the pin into an output pin and set it low (otherwise it will be a floating input pin).
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here