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.

MSP-EXP430FG4618: MSP-EXP430FG4618

Part Number: MSP-EXP430FG4618

hello everyone,

i'm a newbie to this board.i am using CCS and i have a task to turn on the buzzer by pressing the push button (s2)

which turn on the buzzer 1 sec then turn it off.

and again to turn on the buzzer by pressing the push button (s1)

which turn on the buzzer twice then turn off (0.5 second then turn it off and again turn it another 0.5 sec and turn it off).

any help please 

  • i wrote this code but i think there is something missing 

    #include <msp430.h>


    /*****************************************************************
    * main.c
    ****************************************************************/
    #define note 3

    #define A1 15855
    #define A2 1526
    #define MUTE 0

    //******************************************************************
    // Global data
    //******************************************************************
    unsigned int scale[note] = {A1, A2, MUTE};
    unsigned int space[note] = {A1, MUTE, MUTE};
    unsigned int index_note = 0;

    //*****************************************************************
    // Basic Timer Interrupt Service Routine. Run with 1 sec period
    //*****************************************************************
    #pragma vector=BASICTIMER_VECTOR
    __interrupt void basic_timer_ISR(void)
    {


    TBCCR0 = space[index_note]; // get next note


    index_note++; // manage note point
    if (index_note == note)
    index_note = 0;
    }

    int main(void)
    {
    volatile unsigned int i; // volatile to prevent optimization
    WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
    //FLL+ configuration // ACLK - 32.768 kHz
    FLL_CTL0 |= DCOPLUS + XCAP18PF; // DCO+ set, freq = xtal x D x N+1
    SCFI0 |= FN_4; // x2 DCO freq, 8MHz nominal DCO
    SCFQCTL = 121; // (121+1) x 32768 x 2 = 7.99 MHz

    // TimerB configuration
    TBCCR0 = space[0]; // load first tone from sequence
    TBR = 0; // reset TBR
    TBCTL = TBSSEL_2 | CNTL_0 | TBCLGRP_0 |MC_1 | ID_0; // SMCLK, continuous mode

    TBCCTL4 = OUTMOD_3; // CCR4 interrupt enabled
    TBCCR4 = space[0]/2;

    // Basic Timer 1 Configuration
    BTCTL = BTDIV | BT_fCLK2_DIV128; // (ACLK/256)/128
    IE2 |= BTIE; // Enable BT interrupt

    // configuration (Port1)
    P1SEL = 0x00; // P1.0 and P1.2 digital I/O
    P1DIR = 0x00; // P1.0 and P1.2 inputs
    P1IFG = 0x00; // clear P1 flags
    P1IE = 0xFF; // Enable port interrupts


    P1OUT = 0x02; // Set resistor to pull-up, P3.5 low
    P3DIR = 0x20; // Set P3.5 as output and P1.1 as intput
    P3OUT = 0x00;


    while(1)
    {
    if( !(P1IN & 0x01) ) // If push button 1 is pressed
    {
    P3SEL |= 0x20; // P3.5 as special function
    P3DIR |= 0x20; // buzzer on

    for(i=1000; i>0; i--); // delay(1 sec)

    P3OUT &= ~0x20;


    }
    else if( !(P1IN & 0x02) ) // If push button 2 is pressed
    {
    P3SEL |= 0x20; // P3.5 as special function
    P3DIR |= 0x20; // buzzer on

    for(i=500; i>0; i--); // delay 1nd (0.5sec )
    {
    P3OUT &= ~0x20;
    }

    P3SEL |= 0x20; // P3.5 as special function
    P3DIR |= 0x20; // buzzer on

    for(i=500; i>0; i--); {   // delay 2nd (0.5sec )

    P3OUT &= ~0x20;}

    }


    }
    _BIS_SR(GIE); // Interrupts enabled
    }

  • I don't see '#include <msp430.h>" anywhere.

    [Hint: Try the "Insert Code" link -- the one with the "</>" in it.]

  • i edit the code

    have a look please 

  • > P1IE = 0xFF; // Enable port interrupts

    This enables (all) the port 1 pin interrupts but I don't see an ISR (PORT1_VECTOR) for this. As soon as you push a button (and maybe earlier) you'll end up in ISR_Trap.

    Looking at the rest of the code, I don't think you want pin interrupts at all. I suggest you remove this line.

    -------------------------------------------------------------------

    > for(i=1000; i>0; i--); // delay(1 sec)

    At 8MHz, this won't delay for 1 second, more like 1 millisecond. I suggest you not use a for() loop for a delay, since it's very difficult to calibrate, rather something like:

    > __delay_cycles(8000000UL); // 8 million cycles at 8MHz = 1 second

  • Actually i changed my code this is the update i got difficulties at turning the buzzer twice then turn off 

    in this code i just turn it once time and i don't know how to turn it second time any recommendations????????

    #include <msp430.h>

    #define SW1 P1IN&BIT0
    #define SW2 P1IN&BIT1
    void main(void)
    {
    WDTCTL = WDTPW+WDTHOLD; // Stop WDT


    TBCCR0 = 436-1; // PWM Period
    TBCCTL4 = OUTMOD_7; // CCR4 reset/set
    TBCCR4 = 218; // CCR4 PWM duty cycle
    TBCTL = TBSSEL_2+MC_1; // SMCLK, upmode

    int i = 0;
    for (;;) { // Infinite loop
    if ((SW1) == 0) { // If SW1 is pressed
    for (i = 0; i < 2000; i++); // Debounce ~20 ms
    if ((SW1) == 0){
    P3DIR |= BIT5; // P3.5 output
    P3SEL |= BIT5;
    }
    while ((SW1) == 0);
    P3SEL &= ~0x20; // Turn buzz off
    }


    else if ((SW2) == 0) { // If SW1 is pressed

    for (i = 0; i < 1000; i++); // Debounce ~10 ms

    if ((SW2) == 0){
    P3DIR |= BIT5; // P3.5 output
    P3SEL |= BIT5;
    }
    while ((SW2) == 0); // Wait while SW1 is pressed
    P3SEL &= ~0x20; // Turn buzz off

    }
    }


    _BIS_SR(LPM0_bits + GIE); // CPU off

    }

  • Where is it executing if you pause it in the debugger?

    It might be worth a few seconds to check the board to make sure R11 and R12 are installed. (These are the button pullups, located near the buttons.) The photo shows them installed, but I've seen some eval boards where such pullups are not installed.

  • R11 and R12 are installed in my board

    So what do you recommend me to do to make the buzzer turn on and off twice then turn it off

  • As I understand your question:

    1) You push and hold the button. [You expect it to buzz.] It buzzes.

    2) You release the button. [You expect it to stop buzzing.] It stops buzzing.

    3) You push (again) and hold the button. [You expect it to buzz.] It does not buzz.

    The first things to check for this question are R11/R12 [OK] and "where is it executing when you pause the debugger?" [unknown].

    If your question is different from this, you probably need to re-phrase it.

  • you understood my question perfectly.

    But I didn’t understand what do you mean by checking R11and R12 “how to do that”?

  • You already checked R11/R12, so that's fine.

    Next: After you load the code in the debugger and push the button twice, hit the Pause button (two vertical bars) to stop the program and see what line of code it's executing. It may be that it's stuck in a loop that neither of us has noticed.

  • if you can help me with another question related to same code

    i want  the buzzer to turn on then off and turn on then off (two cycles) by just  push the button and release one time (when i push the button and release it buzzer buzz two times then turn off). 

    i do it by counter but it doesn't work

  • It sounds as though you're (still) trying to do delays with for-loops.

    I (still) recommend you use __delay_cycles(): (1) It's easy to calibrate -- you're running at about 1MHz, so 1 million cycles is one second -- and (2) the compiler won't change things behind your back.

**Attention** This is a public forum