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.
I've connected a bluetooth module to my MSP430, and i'm trying to toggle an LED on and off once it receives a single. At the moment I can get the LED to come on through P1.3 however I can not get it to turn off again.
Any help will be greatly appreciated.
This is the code i'm using;
P1SEL &= ~0x08; // Select Port 1 P1.3 (push button)
P1IE |= 0x08; // Port 1 Interrupt Enable P1.3 (push button)
__enable_interrupt(); // Enable interrupts
__bis_SR_register(GIE); // interrupts enabled\
Hi, I don't think you've really posted enough information for us to help. Just a guess but your interrupt routine is supposed to simply toggle your LED on and off?
To toggle try
P1OUT ^= LED;
in your routine. "^" is an exclusive or operator which will toggle whatever bit is defined by LED.
I happen to have an example https://raw.github.com/alanbarr/msp430-launchpad/master/examples/switchLeds/main.c which you may be able to modify to your needs. The code in the while loop sounds to me like what you want in your interrupt handler.
Thanks for the reply.
The code example you provided is near enough achieving what i'm trying to do. However, with your code the LED will only turn off when you hold the button down.
I'm currently using a bluetooth module connected to the MSP430 so that i can send a signal from a terminal program on my PC which will in return turn an external LED on/off.
At the moment, my code only allows the LED to turn on when i send a signal from my terminal program, once the external LED is on, every time i send a signal it will just flash.
What type of Bluetooth module are you using? Without more code its hard to diagnose.
Have you considered your interrupt is being called an even number of times / it isn't a simple pulse? If it is flashing this could well be the reason.
If its a UART module for example you may be receiving 10 bits, which depending on the character being received could be an even number of 1's which would result in the LED appearing to remain on once transmission is finished.
I'm using a Bluetooth Module Slave Wireless Serial Port.
The LED will turn on (with my current code) if i send any characters through the terminal program on the PC.
Here is the full code, if this helps to identify what I'm doing wrong;
#include <msp430g2553.h>
#define TXD BIT1 // TXD on P1.1
#define RXD BIT2 // RXD on P1.2
#define Bit_time 104 // 9600 Baud, SMCLK=1MHz (1MHz/9600)=104
#define Bit_time_5 52 // Time for half a bit.
// ASCII values for the commands
#define MODE1 0x31
#define MODE2 0x32
#define MODE3 0x33
#define MODE4 0x34
unsigned char BitCnt; // Bit count, used when transmitting byte
unsigned int TXByte; // Value sent over UART when Transmit() is called
unsigned int RXByte; // Value recieved once hasRecieved is set
unsigned int i; // for loop variable
bool isReceiving; // Status for when the device is receiving
bool hasReceived; // Lets the program know when a byte is received
void Transmit(void);
void Receive(void);
unsigned int mystate = 0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If cal constants erased, trap CPU!!
}
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
P1SEL |= TXD; // Connected TXD to timer
P1DIR |= TXD;
P1IES |= RXD; // RXD Hi/lo edge interrupt
P1IFG &= ~RXD; // Clear RXD (flag) before enabling interrupt
P1IE |= RXD;
P1DIR = 0X41; // set both leds to output
P1OUT = 0; // and set them off initially
isReceiving = false; // Set initial values
hasReceived = false;
P1SEL &= ~0x00; // Select Port 1 P1.3 (push button)
P1IE |= 0x00; // Port 1 Interrupt Enable P1.3 (push button)
__enable_interrupt(); // Enable interrupts
__bis_SR_register(GIE); // interrupts enabled
OK so a bit more complex than I was expecting :) Could you elaborate on the problem, is it with the switch toggling the LED or the Bluetooth module currently?
There are a few things on first glance with your code which may be causing issues.
Any global variable altered in an ISR should be declared variable (isReceiving etc). Good practise but if you are optimising it also could be problematic.
Here you are anding the register with 0xFF which leaves it as it is, also not sure why you would want to change P1SEL if you want a button, presumably a copied comment.
P1SEL &= ~0x00; // Select Port 1 P1.3 (push button)
Here you are oring it with 0 which leave it as it is.
P1IE |= 0x00; // Port 1 Interrupt Enable P1.3 (push button)
Here you are setting the entire register to 1's, but this is if myState==4, so probably not related to the current problem.
P1SEL = ~0x00; // Select Port 1 P1.3 (push button)
You said you were getting the LED to turn on with a push button, but reading your current code it looks to me like the LED should always turn on (with the absence of external signals) at this point:
if (mystate == 0)
{
P1OUT = 0x01; // LED on
////////////////////////////// THIS IS THE TURN ON SWITCH FROM BLUETOOTH MODULE //////////////////////////////////////////////
P1SEL &= ~0x08; // Select Port 1 P1.3 (push button)
P1IE |= 0x08; // Port 1 Interrupt Enable P1.3 (push button)
}
Assuming this is on a Launchpad I would also recommend the following setup to your switch at the start of main():
But is your current issue turning it off still with a push button or via Bluetooth? Potentially elaborate on what you expect to see happen.
Thanks for your help, however even with your suggestions i can't to get the switch to work properly.
To elaborate i'm trying to get the MSP430G2553 to turn on/off an external LED when it receives a signal from the bluetooth module. At the moment, i can only get the LED to turn on, as i'm not sure of what code, i believe i need to use an internal resistor to control P1.3 output, as this is the point the external LED is connected to i then have the RX and TX connections from the bluetooth module into P1.0 and P1.1 of the MSP430G2553.
I've been trying to get this to work for some time now, so have turned to this forum for some help :)
You say you get the LED to turn on, does it do that without any input be it from bluetooth or switch? Because it looks to me like it should currently turn on more or less at power up?
At the moment the switch should transition through myStates . Have you been able to confirm if the switch is toggling your port_1 interrupt routine?
Also you seem to enable and disable the interrupt for the switch at strange times.
Are you using a Launchpad or is this an independent circuit?
The external LED that i have connected to P1.3 will turn on when it first receives a signal from the blutooth or if i push the button on the MSP430. The external LED will not turn on until i send a signal with the terminal program on the PC. I do not believe that the switch is toggling port_1 interrupt routine.
I have the enable and disable for the interrupt at random positions in the code, as i was experimenting trying to get the LED to turn off. I'm using a Lauchpad, with an external LED wired from P1.3 on the MSP430.
I fired up CCS with your original program unaltered. Could you confirm the following behaviour, as it doesn't appear to be what you previously noted as seeing. (I expected the LED to light as soon as the program was running but you said you only seen it light after serial data was transmitted.) I have no serial data being transferred during this time.
So to confirm, when only using a switch you can turn the LED on and off, but if you turn the LED on with UART it cannot be turned off with the switch?
Yes, if i use the switch on the LaunchPad it turns the LED on/off. However when i use UART it fixes to the on position, also when the LED is fixed in the on position, it is much brighter than it was initially.
I managed to get the LED to turn on and off using the PC terminal program with the commands;
P1OUT ^= 0x01; // LED on
P1OUT ^= 0x00; // LED off
However, after a few times of sending data through the terminal with the external LED turning on and off as it should, it would again fix to the on position and LED1 on the LaunchPad will turn off and the terminal program will stop sending data back.
Then there are other times it will just go strait to the on position and stay fixed, with the LED1 button on the LaunchPad off.
I think this is your problem/solution.
Changing the value of mystate via UART behaves differently to via the switch; the switch will currently never set mystate = 4. The following line is only executed when using UART:
P1SEL = ~0x00; // Select Port 1 P1.3 (push button)
is equivalent to:
P1SEL = 0xFF
which is settling P1SEL to all 1's which is enabling a special function on your LED pin, pin 1.0.
I think you can remove this line altogether.
As to the actual function, it seems it was outputting ACLK to the pin. Which is why the LED would seem dimmer as it is only on for 50% of the time.
Thanks for this, it seems everything i try, i can not get the LED to turn on and off every time without fail.
Either the external LED will go from dim to bright and get stuck on that, or the LED will go off, then eventually come back on when i send enough data through the terminal. Could you suggest alternative code that i could use to achieve this through RX/TX?
Ideally, i would like the external LED to turn off with the command "0" and on with the command "1" sent through the terminal.
I really appreciate your help! :)
**Attention** This is a public forum