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.

How to Implement the code for a signal Generator?

Other Parts Discussed in Thread: MSP430F6638, CODECOMPOSER, MSP430F6630

Hi Everyone,

I need to write a simple function generator for my MSP430F6638 MCU.  Does anyone have any sample code?

Thanks,

Arash

  • Arash Taheri said:

    I need to write a simple function generator for my MSP430F6638 MCU.  Does anyone have any sample code?

    Here's where you can browse through TI examples:

    MSP430Ware

    TI Microcontroller application notes

    Some of the timer examples might be a good starting point.

  • Arash Taheri said:
    I need to write a simple function generator for my MSP430F6638 MCU.

    There's nothing simple with a function generator.

    you cna look here for a simple implementation that outputs a fixed-frequency sine wave through DAC using DMA.

    However, when it comes to variable frequency, frequency modulation (wobble), attenuation, different waveforms, things start to be really complicated.
    Square wave is rather simple, triangle and sawtooth are a bit more complex, and sine wave is quite difficult. And the achieavable maximum signal frequency (and quality) rapidly drops.

    So what are your parameters?

  • @Jens-Michael:

    Thanks for the feedback.  I'm using the MSP430F6630 MCU and I'm trying to generate a signal using CodeComposer 5.1 with the following properties:

    - Frequency of X Hz

    - Repetition Rate of Y Hz

    - Amptlitude of A volts

    - Square wave signal

    I'm very new to MSP430.  Do you have a sample code that can generate this signal? 

    I'm working on a project and I've been using the function generators in the lab to feed such signal to my hardware.  I decided to use MSP430 since I'm moving the project forward...

  • Arash Taheri said:
    - Frequency of X Hz
    - Repetition Rate of Y Hz
    - Amptlitude of A volts

    Well, knowing the range of X, Y and A would be important too. (well, what's the difference between X and Y? Or do you mean pulse/pause ratio/Duty cycle?)

    Arash Taheri said:
    - Square wave signal

    This makes calculations and handling much easier, as an action is only needed at the edges.

    I can even think of a setup that only needs software action on a change of the waveform: You'll need an OpAmp with rail-to-rail inputs and outputs (at least GND rail, if you have a supply >VCC) and assymetrical supply.
    The amplitude is sent from DAC through a 100k resistor to positive input, also the output of a timer CCR (e.g. TB.1). The OpAmp output is looped to negative input through 100k, and also, VCC is routed to the negative input through 100k. THis way, when the timer output is low, the negative input is much higher than the DAC output and the OpAmp output goes to the negative rail (GND). If the timer output is high, it will compensate the VCC on the negative input and the OpAmp will replicate the DAC voltage on the output. It is like an analog switch but also doing an impedance change, so depending on the OpAmp, you can drive several mA. If this is not necessary, a cheap analog switch from the CMOS CD4xxx series will do too, but with less performance.

    However, gating the DAC output voltage by a timer output rahter than changing the DAC voltage to fit the signal allows usign the PWM hardware mechanisms of the MSP timers. This greatly increases the timing precision of the generated wave. (and using TiemrB instead of TimerA helps synchronizing the signal changes to the wave edges)
    And allow sthe CPU to stay in low power mode most of the time unless a change is needed (user input).

  • Thanks a lot for the response.  I have provided specifics for the signal I'd like to generate using MSP430F6638:

    Frequency of the signal is:  100 Hz

    Amplitude:  3.3 Volts

    Form:  Square wave

    The signal is high for about 10 microseconds.

    Summary: This signal I need is a square wave signal that is 3.3 V in amplitude.  The amount of time that the signal is HIGH is 10 microseconds.  The frequency of the signal is 10 Hz.  How can I generate this exact signal using my MCU?

    Thanks a lot in advance

    Arash

  • Study the hardware Timers...

  • 10Hz or 100Hz?

    100Hz would be 10ms cycle time and 10µs pulse time, 1000:1. Simple

    100Hz would be 100ms cycle time adnb 10µs pulse time, 10.000:1. Not so simple.

    First case: have ACLK or SMCLK configured to 1MHz. Configure the timer to run in up mode using this clock.
    Set CCR0 to 9999 (10ms = 10000 clock pulses). Set CCR1 to 10 and configure CCR1 to compare mode and reset/set output mode.

    When the timer overflows to 0, the output goes high, when it counts to 10 the output will go low. then the timer continues to count to 9999 and then will overflow to 0 again. So the output is high for 10µs and low for 9990µs, 10ms total.

    The difficult thing with 100ms cycle time is that you can't easily generate 100kHz clock for 10µs per clock tick (CCR1 would be 1 instead of 10 then) and hte maximum count fo rth etimer is 65536 and not 100.000. So you'll have to do some more math to find the optimum combination of clock signal and timer ticks for 10µs pulse and 100ms cycle time.

    Once the tiemr is set up, the output signal will come stable without any further code intervention.

  • Hello Jens-Michael,

    sorry for the typo.  The frequency of my square wave is 100 HZ.

    I have attached a picture to illustrate what I need.  I am working with a transducer and have designed a transmitter/receiver hardware module to basically excite this transducer and receive the echo signals.  My transducer works best with the attached square-wave signal.  Therefore, I need to generate a signal that has the following features:

    3.3 V in amplitude

    frequency:  100 Hz

    Time that the signal is HIGH:  10 Microseconds

    So, you mean that it is hard to generate a signal with this specification?  I'm very new to MSP430 units and really appreciate your help.

    Thank you very much

    Arash

  • Arash Taheri said:
    3.3 V in amplitude
    frequency:  100 Hz
    Time that the signal is HIGH:  10 Microseconds
    So, you mean that it is hard to generate a signal with this specification?


    No, it's rather simple. You need VCC=3.3V (for the output amplitude), a 1MHz clock (1µs tick) and a timerA module (all MSPs have at least one).
    Setup:
    source TimerA with 1MHz clock, up mode.
    CCR0 configured for a cycle of 10000 clock ticks (CCR0=9999)
    CCR1 configured for capture mode, output mode reset/set. trigger on tick 10 (CCR1=10).
    port pin connected with TA.1 configured for module, output direction.

    That's all. Now the hardware will generate a fine 100Hz signal with 0.1% duty cycle withouzt any further software intervention.

  • Thanks Jens-Michael for your answer.  I've been struggling to write the code. How many lines of code should it be?  As I mentioned, I'm very new to MSP430 and my code doesn't work.  If it's very easy for you to write, can you send the code to me?

    Thanks a million

    Arash

  • Arash Taheri said:
    How many lines of code should it be?

    As few as possible and as much as needed. Besides the startup loop when settign up the crystal for the tiemr clock (if any is used at all), it is a straight line of register settings. Not more than 10-20 lines in total.

    Thsi specific question is not a matter of programming, but rather a matter of setting up the hardware registers and then let the hardware do the job.

    Depending on the MSP you use, you might want to try GRACE (currently for 2x family only).

**Attention** This is a public forum