• AVR Freaks

Helpful ReplyHot!Pulse width modulation

Author
dj_quaver
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2019/10/04 01:34:26
  • Location: 0
  • Status: offline
2019/11/11 00:50:21 (permalink)
0

Pulse width modulation

Hello everyone,
I am trying to build an obstacle avoiding robot (https://www.microchip.com/forums/m1115137.aspx) but I feel unsure about what PWM signifies and if I really need it. I thought about mounting a HC-SR04 US sensor on a servo motor.
 
The pic32mx has only one pin (pin 10) that is PWM.
Do I need it for both the servo and the HC-SR04 US sensor?
For the sensor, I thought it was only configuring echo as input and trigger as input, and wait for 10 microseconds?
I am really unsure about where to start right now...
#1
jg_ee
Super Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2015/04/30 10:54:52
  • Location: Colorado
  • Status: offline
Re: Pulse width modulation 2019/11/11 16:26:50 (permalink) ☄ Helpfulby dj_quaver 2019/11/16 22:06:07
0
Sounds like you are getting overwhelmed thinking about starting the project. 
 
It looks like you have the development board and sensors already, so I would suggest you start with just hooking up the PIC to either the range sensor or the servo and start controlling it based on the datasheets.  Just doing that will clarify things for you and allow you to come here and ask some more specific questions once you start developing the firmware.  I bet many people on here would be happy to help once you get a little further along on your development.
#2
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Pulse width modulation 2019/11/11 16:34:42 (permalink) ☄ Helpfulby dj_quaver 2019/11/16 22:06:24
0
It's unclear what your experience level is.
Have you got to the point of flashing an LED once per second with a PIC yet?
Don't laugh, you need to get a lot of fundamentals covered just to do that correctly!
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
labrat50
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/10/24 21:55:32
  • Location: 0
  • Status: offline
Re: Pulse width modulation 2019/11/12 00:23:21 (permalink)
0
There are a handful of PIC32 books out there. Take a look at those.
#4
dj_quaver
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2019/10/04 01:34:26
  • Location: 0
  • Status: offline
Re: Pulse width modulation 2019/11/16 22:19:53 (permalink)
0
Thank you everyone!
You are right, I got a bit overwhelmed. Now I got the sensor working with:
- the trigger and the echo are set as respectively RD11 and RD5


35 IC4/PMCS1/PMA14/INT4/RD11 *trig

34 PMRD/CN14/RD5 * echo


-  TMR3 has a period of 1us (TMR2 is configured for 10us to send the first pulse and TMR1 is configured to 100ms to wait. The docs recommend to wait for more than 60ms.)

TMR3 = 0x0;
PR3 = (80e6/256)*1e-6;
IPC(3) = 0x1D; //set slightly lower priority than timer 2
T3CON = 0x70;
T3CONSET = 0x8000;

- taking start time when the trigger is set with ticks from TM3
- taking stop time when the echo goes up.
Many example project recommend to measure the echo but I only got crap values (too few ticks) when I did that? I cannot understand why.
- I did timeStop-timeStart/58.8 which gives me a distance in cm according to the docs.
- I used the leds in PORTE to flash when the distance is between 20 and 5 cm. It's kind of working. Not perfectly but the leds are going down when I put an object closer and closer.


if(globalDistance >= 20){

PORTESET = 0x80;

}

else if(globalDistance >= 15 && globalDistance < 20){
PORTESET = 0x40;
}
else if(globalDistance >= 10 && globalDistance < 15){
PORTESET = 0x20;
}
else if(globalDistance >= 5 && globalDistance < 10){
PORTESET = 0x10;
}
else if(globalDistance < 5){
PORTESET = 0x8;
}
else{
PORTESET = 0x4;
}

 
Here come more specific questions:
1. I will start working with the servo now. I see that the servo needs an input of 5V but my PIC32MX has only the female 5v0 out pin that I already use for the sensor. There is a 3V male left. How should I do? Should I connect the power source to the moterdriver (that is connected to 4 1.5V batteries under the car)?
2. Should I reduce the period of TMR1 to 60ms to have better measurements?
post edited by dj_quaver - 2019/11/17 05:11:07
#5
acharnley
Super Member
  • Total Posts : 405
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: Pulse width modulation 2019/11/20 01:40:09 (permalink) ☄ Helpfulby dj_quaver 2019/11/21 22:24:40
5 (1)
First a wee optimisation in your code:
 

if(globalDistance >= 20){

PORTESET = 0x80;

}

else if(globalDistance >= 15 && globalDistance < 20){
PORTESET = 0x40;
}


becomes



if(globalDistance >= 20){

PORTESET = 0x80;

}

else if(globalDistance >= 15){
PORTESET = 0x40;
}




 
#6
acharnley
Super Member
  • Total Posts : 405
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: Pulse width modulation 2019/11/20 01:54:07 (permalink) ☄ Helpfulby dj_quaver 2019/11/21 22:24:27
5 (1)
Secondly, approach.


I can only go of the PIC32MM which I have some experience with but the MX should have increased functionality so it should apply. I haven't fully understand what you're tyring to do but skimming it I saw "echo" so have assumed a sonar/radar type application.

There's more PWM functionality then you imagine although they are referred to as CCP, and the units have a capture ability which you can use to gain accurate measurement. MCC is your friend here, it'll quickly help you learn the features and set them up. You can then optimise the code later on and perhaps ditch MCC altogether once you have a deeper understanding.

You have two options, first use the capture side of a CCP.
 
[fecking microchip error - see attached]

A more basic approach, use a timer with a gate.
[fecking microchip error - see attached]
 
Now ideally you want to send the pulse and start the timer at exactly the same time. Doing it in software would introduce a slight lag. For the PIC32MM at least there are 4x CLC modules. Use one of these as the clock to the capture and as the output source.
[fecking microchip error - see attached]
post edited by acharnley - 2019/11/20 01:55:59

Attached Image(s)

#7
acharnley
Super Member
  • Total Posts : 405
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: Pulse width modulation 2019/11/20 02:07:03 (permalink)
0
Thirdly,

If your servo requires an "ON" voltage above 3.3V it's called TTL.

I assume by "female" you mean a PIN which is 5 volt tolerant. Normally these are not PPS mappable as they're used for modules that may use 5V, like USB. It's best not to rely on them I find otherwise you're locking yourself into a certain pin which can hinder PCB design later on. You can use any pin using a level translator, which can be just a voltage divider using two resistors.

Outputting 5V from a 3.3V device is not possible so you'll need another level translator. The simplest way to do it is a NPN transistor with a pull high. This inverts the signal though and the PIC pins are floating at start-up so for a very brief period your output is "1". You can use a second transistor to invert it if it's a problem or go for a level translator chip.

Alternatively the PIC16/18 series are 5V and are IMO better suited to hardware operations. You could use one of these to do the measurements and report the value back to your PIC32 over UART. I did this for AC measurements using a cheap PIC16F15323 interfaced to a PIC32MM which worked out at half the cost of going for a MX chip with 74 series level translator chip.


#8
dj_quaver
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2019/10/04 01:34:26
  • Location: 0
  • Status: offline
Re: Pulse width modulation 2019/11/21 22:42:09 (permalink)
0
Thank you for all the replies!
 
By female I meant that on the chipkit uno32 (that uses the pic32mx microcontroller) I have one output for 5V, with which I need to feed both the sensor and the servo. I decided to solder two cables, plugg one in the sensor and one in the servo.
 
I will implement the recommended optimisations in the code.
 
My servo is a SG90. It has the below duty cycle:
  • 0 degrees - the centre position with a 1.5 ms pulse;
  • + 90 degrees with a ~2 ms pulse; and
  • - 90 degrees with a  ~1 ms pulse.
I have another question about the documentation.
If the chipKIT Pin # 5 is connected to PMWR/OC5/IC5/CN13/RD4, what do I need to write to?
Right now I am writing to OC5RS, why do I need the OC5R (read only) for exactly?
Do I need to do something with PORTD bit 4?
 


/* Interrupt Service Routine */

void user_isr(void)

{

//to be done later

}

/*10 PMWR/OC5/IC5/CN13/RD4*/

void init(void)

{

// configure PWM on OC5 (RD4)

TMR2 = 0x0;                        // reset timer

PR2 = (80e6 / 256) / 50;      // 20 ms must be 50hz

T2CONSET = 0x8070;           // T2CON must be 0x70 (bits 6 to 4) for prescaling 1:256

OC5RS = 469;                     // set pulse width to 1.5 ms to center servo, ie ((80e6 / 256) / 50)*(1.5/20)

OC5R = 469;                       // set pulse width to 1.5 ms to center servo to start? Don't understand

OC5CONSET = 0x8000;        //ON: Output Compare Peripheral On bit

OC5CONSET = 0x6;             // OCM<2:0>: Output Compare Mode Select bits 110 = PWM mode on OCx; Fault pin disabled

enable_interrupt();

return;

}

/* Just testing movements right now */

void sensorwork(void)

{

delay(1500);

OC5RS = (469)*2;

delay(1500);

OC5RS = (469);

}



#9
Jump to:
© 2019 APG vNext Commercial Version 4.5