This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Motor Controller with MSP430 Launchpad.

Other Parts Discussed in Thread: MSP430G2231

I want to use the MSP430G2231 (preferably) as a chip to drive a 40 amp H-bridge. I thought that instead of discrete logic a microcontroller would be nice. I picked up the launchpad because it's so cheap and I see where TI is going with it. Seems like a good Idea to use the msp430.

 

Inputs:

There is a PWM signal at 333.333Hz so 1 3ms period. it is "null" at 1.5ms full CW at 2ms and full CCW at 1ms. So I need an input for this.

 

Now for the outputs:

There need to be two outputs. Both can NEVER be high at the same time. Both are PWMs and need to go from 0-100% duty cycle. Output A is "activated when the input is greater than 1.5ms. Output B is "activated" when input is less than 1.5ms.

 

An extra feature: (which is easy but I should mention anyway)

I need to use an ADC channel to measure the voltage drop across a 1ohm resistor (to measure current).

 

It seems to be that there can only be one PWM output on this chip. Can I just "bit-bang" the PWM outputs and read the input with the actual timer.

  • Selim Tanriverdi said:
    Both can NEVER be high at the same time

    Oh, they can. That's easy. I guess you mean they must not. That's not as easy and depends on your programming skill. The outputs do as you say. if you say wrong, they do wrong :)

    Selim Tanriverdi said:
    Both are PWMs and need to go from 0-100% duty cycle.

    That's no problem, except if oyu want them to be 333.333Hz PWM frequency and need to be set in 1% steps. However, if you only need them 0% or 100% but nothing in between, then you're down to a simple static digital output that is either high or low. And not oscillatng at all.

    Selim Tanriverdi said:
    An extra feature: (which is easy but I should mention anyway)
    I need to use an ADC channel to measure the voltage drop across a 1ohm resistor (to measure current).

    This may prove as less easy than imagined. Usually, the current sensors are not tied to GND with one pin, but rathe rfloating somewhere in the middle of the voltage range. The ADC10 does not have differential inputs. And it cannot sample two channels simultaneously (so you could just calculatie the difference). So depending on the position of the sensor, you'd need an external OpAmp doing the difference buoilding. And also the amplification, since it makes not much sense to measure current on a 10 bit ADC in the lower few bits of its operating range: on 1 Ohm, 40A are 40V -> 1600W, so I don't think that's what you mean. On 1mOhm, however, 40A are only 40mV (still 1.6W), which on the ADC10 is a value of only 27.

    However, the description of the current measurement is way more clear than the description of the PWM detection task. So could you please clarify, or better draw a timing diagram. Your current description is a bit fuzzy.

  •  Oh, they can. That's easy. I guess you mean they must not. That's not as easy and depends on your programming skill. The outputs do as you say. if you say wrong, they do wrong :)

    I realize this... but what I want is something in the code that makes it so the MSP430 doesn't allow for both to be high. as protection. I can do it with discrete logic but why do that when you have a microcontroller.

     

     

    That's no problem, except if oyu want them to be 333.333Hz PWM frequency and need to be set in 1% steps. However, if you only need them 0% or 100% but nothing in between, then you're down to a simple static digital output that is either high or low. And not oscillatng at all.

    The frequency does not matter so much, and the more resolution the better. the minimum resolution should be 1-5% duty cycle.

     

     This may prove as less easy than imagined. Usually, the current sensors are not tied to GND with one pin, but rathe rfloating somewhere in the middle of the voltage range. The ADC10 does not have differential inputs. And it cannot sample two channels simultaneously (so you could just calculatie the difference). So depending on the position of the sensor, you'd need an external OpAmp doing the difference buoilding. And also the amplification, since it makes not much sense to measure current on a 10 bit ADC in the lower few bits of its operating range: on 1 Ohm, 40A are 40V -> 1600W, so I don't think that's what you mean. On 1mOhm, however, 40A are only 40mV (still 1.6W), which on the ADC10 is a value of only 27.

    Well it does not have to be that way. I just want to make it so that somewhere around 40A it just shuts the bridge off. Just some sort of overcurrent protection. Like if someone decides to stick a wire between the motor outputs the transistors won't just explode.

     

     However, the description of the current measurement is way more clear than the description of the PWM detection task. So could you please clarify, or better draw a timing diagram. Your current description is a bit fuzzy.

    Sorry not being very clear.

    Here is a little picture I drew to clarify my inputs and outputs. the frequencies don't matter except the input will be 333.333Hz. for sure (to keep the period/times the same).

     

     

    hope that clears some things up.

     

     

     

    Thank You!

  • Let me see if I understood you correctly.

    You have one PWM input signal, IN, The frequency is ~333.3Hz and the duty cycle is between 33.3% and 66.7%.

    You need to generate two PWM output signals, OUT-A and OUT-B. The frequencies can be ~10kHz in both cases. The duty cycles are as follows:

    (a) When duty cycle of IN is between 66.7% and 50%, duty cycle of OUT-A is between 100% and 0%. Duty cycle of OUT-B is 0%.
    (b) When duty cycle of IN is 50%, duty cycle of OUT-A is 0%. Duty cycle of OUT-B is 0%
    (c) When duty cycle of IN is between 50% and 33.3%, duty cycle of OUT-A is 0%. Duty cycle of OUT-B is between 0% and 100%

    If the above is correct, I think it is not difficult to accomplish with a G2231 running at ~10MHz. You use the CPU to detect IN, and count CPU cycles to determine its duty cycle. You use Timer CC1 to generate either OUT-A or OUT-B (the other one outputs a Low.)

    Because you have to count the CPU cycles, you should write it in Assembly. Counting CPU cycles is easy but very tedious.

    Yes, you can write it in C. After it is compiled, look at the assembly code the c-compiler generated and count the CPU cycles.

  • Okay, this diagram clear ssome things. And rises others.
    First, the incoming signal is 333Hz, not kHz as I assumed (the dreaded decimal point/1000 separator issue with localized formats). Makes things easier.
    For a raw measurement you can use an interrupt-capable port pin and measure the time between a rising and the next falling edge. (raw measurement because ISR controlled timing measurement is less precise than hardware.-based) This gives you the signal timing. However, you can only know after the second edge wha tthe frequency is. Else you'd need a crystal orb to see what will come in future. So your diagram timing is not possible. The reaction of the outputs will always be on the falling edge of teh new input timing, not on the previous rising one. Since you only then know what kind of input signal you have.
    Using a CCR unit for this job would be easier, but if you need them for the PWM output...

    Then, well, I still don't know what you mean with 'full on' in conjunction with 'Outputs can be any frequency'. On PWM, 100% DC (full on) means that the signal is never low and 100% of the time high. And a signal that does not switch between high and low has no frequency (or rather a frequency of 0Hz). So you can as well use a GPIO pin for this and set it to high or low as you want.

    Selim Tanriverdi said:
    I want is something in the code that makes it so the MSP430 doesn't allow for both to be high. as protection.

    There is no such thing. There is no 'exclusive-OR' circuit that you could activate for two outputs. If you program the pis right, they ar enever high at the same time. If you program them wrong, they can be everything.
    Any 'protection code' that will configure the pins in the way you want, can be undone by later code that changes the outputs.

    In case of PWMs, as the CCR units act synchronous to the timer clock, proper configuration will start them poroperly, and they won't drift apart then. However, if you change the output states, you can easily misconfigure them again. There's nothing you can do except proper programming. Or use an external logic circuit.

    I guess, what you want to do is getting the signal from sort of a rotation/angle/speed sensor and use the outputs to drive a motor or servo to accelerate/decelerate if the measured rotation speed is too low/high.

  • You understood me perfectly.

     

    How would I go about actually writing it? I've tried to look through example code TI gives you but it doesn't make much sense to me because I've never programmed anything this low level. I've done C but I've done it with VEX robotics microcontroller (which has  PIC in it) they have a custom bootloader which makes things super simple.

     

    I've never even worked with assembly before.

     

    Sorry for being such a novice.

  • In that case, you better use G2xx3 which has two TimerA modules. Use the other Timer to capture and time the IN. This way you do not need to count CPU cycles yourself.

    But you still have to learn how the Timer works.

     

  • You still didn't write what you mean with 'full on'.
    If it is indeed 100% DC or 0% DC only, then you don't need the timer to generate a PWM. Then you can use the timer for capturing the input signal timing.
    Else, you'd be better with using an MSP with two timers, one for capturing, one for PWM output, like OCY said.

    And in any case, you should care fully read (and understand) the complete chapters about ports (GPIO) and TimerA.
    And for the PWM timing, you'll also need the clock system chapter.

  • sorry I guess that wasn't described very well. when it is between 1.5ms and 2ms the output will be between 0% and 100% therefore a 2ms input would produce a 100% output aka a regular DC voltage.

     

    Where can I find these chapters? the user guide?

  • Okay, so teh output will a PWM signal with an increase of 20% per 0.1ms of the input signal, startign with 0% on 1.5ms and ending at 100% on 2ms intput signal width.

    Then indeed you'll need a tiemr for the PWM output. Th ecalculatiosn are relatively easy. There are several threads in this dorum discussing PWM by timer.

    However, I strongly suggest using an MSP with two timers, as you'll need to do the input signal measurement with a timer if you need a linear progression of the output. A plain port interrupt with its latency times would be too inprecise and introduce a high detection jitter.
    I'd recommend an MSP with a TImerA for the measurement and a TimerB for the PWM, as TimerB allows synchronized setting of the PWM values, so you don't have to worry about where in a PWM cycle the output currently is.

    The reason why you need two timers is, that in PWM mode, teh counting range of the timer is limited to the PWm frequency, and your input signal has a much lower frequency than the PWM output has, so you simply cannot count high enough to calculate the frequency without counting overflows at the PWM frequency rate and worrying about latencies and execution times.

    Selim Tanriverdi said:
    Where can I find these chapters? the user guide?

    Yes.

  • any specific chip? I'm not really familiar with msp430 or any TI MCUs.

     

    I'd love to do this with msp430 because initial investment is so much lower than that of AVR. 

  • Selim Tanriverdi said:
    any specific chip?

    The users guides are for whole families. They cover every module that may or may not be present in any family member but is identical if present. In your case it woudl be the 2x family users guide,  slau144.pdf

    THe device datasheet hold all information that is specific to oen or more MSPs, such as timings, voltage levels, accuracy information, pinouts etc. But the basic module funcitonality is described in the users guide.

    Selim Tanriverdi said:
    I'd love to do this with msp430 because initial investment is so much lower than that of AVR. 

    Not only the initial investment. We use AVRs too (the ATMega128), but just because our access point needs a network controller and more ram then any MSP had when we started this project. The memory-mapped I/O of the ATMega was the main thing missing on the MSP for this purpose. However, the MSP is still our first choice for all the smaller projects.

**Attention** This is a public forum