Using MSP430F66736A, embedded programming, Code Composer 6.1.0
In my app i need to control a relay which is on 2 pins(P5.0- STATE1 and P5.1- STATE2). P4.6 is LED.
It should work like that:
Led is OFF for specific time(for example 2 sec), relay should get t STATE1,
after specific time(3 sec for example) LED is ON and relay should switch to STATE2
What´s wrong with that code? LED is blinking as I wish, but relay is not working(can´t hear any ticking)
This is not full code, just specific lines with configurating P5(relay)
LOOP(doesnt matter)
if (P4OUT == 0x00)
{
cyklus = cyklus + t1;
P5DIR |= BIT1;
P5OUT |= BIT1;
P4OUT ^= BIT6;
TA1CCR0 += (sekunda*t1);
}
//opakovaci cas odpojenia
else
{
cyklus = cyklus + t2;
P5DIR |= BIT0;
P5OUT |= BIT0;
P4OUT = 0x00;
TA1CCR0 += (sekunda*t2);
The problem seems to be with the interfacing of the relay. Have you checked the datasheet of relay that you are using? Relays are current controlled switches and MSP430 controller might not be able to provide enough driving current to the relay because of which the relay would not be latching on (ticking sound that you hear), although it blinks the LED.
You might need to add a switch like a BJT or MOSFET for this purpose along with a higher voltage.
Related
I want to make a device like Knocki(https://knocki.com), which essentially is a relay control using a vibration sensor. i can detect vibrations rn but the problem is, once i knock the relay blinks on and then turns off. i understand this is a lack of programming that is causing this. could someone help me write code which makes it so that when i knock the relay is turned on indefinitely; until I knock again to turn relay off.
And yes u can probably tell that this code is copied from somewhere(https://wiki.keyestudio.com/Ks0068_keyestudio_37_in_1_Sensor_Kit_for_Arduino_Starters#Project_21:_Vibration_Sensor).I took it from the home page of the vibration sensor. the code was initially so that every time i knocked, the onboard Arduino led lit up. Also, right now the relay is blinking faintly every time i knock(Although correctly,in sync with my knocks)
#define SensorLED 13
#define SensorINPUT 3 //Connect the sensor to digital Pin 3 which is Interrupts 1.
unsigned char state = 0;
int Relay = 5;
void setup()
{
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
attachInterrupt(1, blink, FALLING);// Trigger the blink function when the falling edge is detected
}
void loop()
{ if(state!=0)
{
state = 0;
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
}
else
digitalWrite(SensorLED,LOW);
digitalWrite(Relay,lOW);
}
void blink()//Interrupts function
{ state++;
Yes its in your code: The (bad) example works only because there is a
digitalWrite(SensorLED,HIGH);
->>> delay(500);
a delay for 1/2 sec to keep the led on.So as a check put an other delay after the relay line and it should go on for 1/2 sec too (so the led is lit 1 sec in total)
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
delay(500);
Thats just for checking -> NEXT STEP:
Get rid of the delays (see blinkwithoutdelay example in
Arduino->File->Examples->2.Digital -> blinkwithoutdelay
and introduce a second state variable e.g.
bool relayStateOn = false;
to get an independent on/off of the relay and the led.(If thats - what I understand -what you want to do)
If you feed your relay from the board, that is not the problem. Please, check the voltage in your relay when you try to set it on, if your voltage falls down, it means that this output to your relay does not supply the necessary current.
I am fairly new to the arduino topic and try to get a few things to work together.
First i tried setting up a DC motor that can be controlled via PWM, which works perfectly when used standalone. I can start/stop the motor and change speed depending on the value i send to the PWM pin.
Second i tried to use an RF-5V wireless receiver to work with a remote control from remote switched power outlets. For this one i followed the instructions on how to build a 433mhz sniffer.
This all by itself works as well. I can receive diffent codes depending on which keys on the remote i am pressing.
Now the fun part started: I wanted to integrate both of the projects into one, so i could use the remote to start/stop the motor.
So i came up with the following circuit:
(Thanks for some of you pointing out that the circuit does not match the sketch. I made an error when drawing, but even with the cables attached to the right pins, it works as described)
and the following code (which is partly from the instructions mentioned above):
#include <RCSwitch.h>
// init 433MHz lib
RCSwitch mySwitch = RCSwitch();
unsigned long lOldValue=0; // to check for consecutive reads on 433MHz
int motorPin = 5; // PWM-Pin to use for motor
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
// set-up rf receiver
mySwitch.enableReceive(0); // 433MHz Receiver on interrupt 0 => that is pin #2
}
void loop()
{
if (mySwitch.available())
{
int value = mySwitch.getReceivedValue();
// only react, if at least two times same value received
if (value == lOldValue)
{
if (value == 0)
{
Serial.print("Unknown encoding");
}
else
{
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
// One of the keys on the remote
if (value == 274393) {
Serial.println("got start code, starting motor");
analogWrite(motorPin, 100); // start the motor
}
// another key on the remote
if (value == 270384) {
Serial.println("got stop code, stopping motor");
analogWrite(motorPin, 0); // stop the motor
}
}
}
lOldValue = value;
mySwitch.resetAvailable();
}
}
when i run the code and click on the remote, i get different values shown depending on the key i press. So the wireless receiver works as expected.
When i receive the right value for starting the motor, the motor really begins to turn, so this works as well.
And here the fun part starts:
As soon as i use the analogWrite function to send data to the PWM port the motor is connected to, the wireless receiver stops working (or at least I do not get any more values when pressing a key on the remote).
I found a few similar posts/problem descriptions on the net which said to try the following:
Use another pin for PWM (due to possible interrupt conflicts). I tried that as well, same behaviour
Use external power supply instead of USB-Cable, which helped somebody resolve this issue. Not here. Does not work either
So the question is:
Does anybody know how to combine those two things together so a can use the wireless receiver to get commands and switch on/off the motor with it?
I have the same problem in the past. The problem is the ability of arduino to supply them both. I recommend to use external power supply for the receiver or for the motor (it's best to do that for the motor but according to your circuit it's impossible) like the 545043YwRobot and supply the other from the arduino (I hope this is not what you try already, if so i'm sorry).
Hope it's help.
Yoav
Examples of writing to a port seem to always use the port number as a constant, eg,
OCR2A = 180;
How do you write to the port when the port is unknown until run time. For example,
int port = (buttonPressed) ? 0x3b : 0x3c;
portWrite( port, 180 );
What I cannot find is the funtion portWrite(). Does something like that exist?
Robert's answer has some imprecise assertions and an incomplete answer.
Writing directly to port registers you can ruin other settings of the port and sometimes cause permanent damage to controller.
Can ruin other settings: true, you have to know what you are doing (for instance what pins are on the port you are manipulating, and know what are the functions you want to keep.
Can cause permanent damage: not really, or better not because of the port manipulation. If you wire a short circuit to ground and then set it as an output to 1, you can damage it whether you are using the port register or the digitalwrite. You have to be careful in both ways.
Now, returning to your problem, the enumeration is one way, but since the PORTB, PORTC, PORTD are just short name for values, you can set a variable and then use it to indirectly access it.
The type for this kind of variable is a volatile pointer to a byte (volatile means that write and read operations cannot be optimized by the compiler, since the value can change even between two operations):
volatile uint8_t *variablePortRegister;
You just have to load it with the address (so with the & symbol) of the register you want to change:
variablePortRegister = &PORTC;
then use the pointer to change the value
PORTC = 0x12;
becomes
(*variablePortRegister) = 0x12;
This is a short example. For it to work, connect a LED with resistor on arduino pin 5 (bit 5 of PORTD). The LED on the board (labeled L) is connected to pin 13 (bit 5 of PORTB).
The sketch will make one of the two leds blink for five times, then switch to the other. Only port manipulation instructions are used, and you can find that the way to read the port is the same as the one to write it.
volatile uint8_t *myportreg;
unsigned long lastTime;
uint8_t counter;
void setup() {
DDRB |= 0x20;
DDRD |= 0x20;
PORTB = 0;
PORTD = 0;
counter = 99; // trigger the register change immediately
}
void loop() {
if (counter >= 10)
{
counter = 0;
if (myportreg == &PORTD)
myportreg = &PORTB;
else
myportreg = &PORTD;
}
if ((millis() - lastTime) > 500)
{
lastTime = millis();
// change bit 5 of register
*myportreg = 0x20 ^ (*myportreg);
counter++;
}
}
EDIT: as Robert pointed out, it's much better to "use" just the pins you need (in this case, bit 5 of port B and D) rather than setting the whole port; this way you minimize the risk of screwing up something else. This edit is already included in the above code, so the code is correct
The port is a bit in one particular register. If you know the register and the position of the port in that particular register you can try this:
register = (1<<port) || register
to set the port to 1 and
register = (1<<port)^-1 && register
to set the port to 0.
Of course, you will need a switch somewhere to determine the register and the bit of the port in the register given the port name.
I've been working on a project which requires communication with my laptop and the microchip Pic 16f877a and the communication is not working properly, but When i put this example on Proteus 8 works just fine.
I don't have RS232(serial port) on my laptop, but i had bought TTL module. I use external oscillator 8MHz on the board and baud rate of 9600 bps.
When i connect with my laptop and i send to the TTL module the A symbol nothing hapens and when i send a couple of times A fast i recieve strange symbol like '?' or '#' back.
Here's the code for the Pic:
char x;
void main(){
TRISB = 0x00;
PORTB = 0x00;
UART1_Init(9600);
delay_ms(10);
while(1){
if(UART1_Data_Ready())
{
x = UART1_Read();
if(x == 'A')
{
PORTB = 0xFF;
UART1_Write(13);
UART1_Write_Text("A");
UART1_Write(13);
}
else
{
PORTB = 0x00;
}
}
x= '\0';
}
}
Can someone help me ? What's the problem ?
Two possibilities as far as I can see (assuming UART1_Init(9600); is a correct library function):
You don't have the Rx pin set up as an input; setting TRIS registers to 0 makes all the pins on that port outputs.
You are using incorrect parity.
Have you set a breakpoint at line
x = UART1_Read();
To see what you actually get in when you send from your terminal program?
Have you looked on youe scope to see what is actually coming in on the Rx pin?
delay_ms(10); is completely unnecessary.
give
SPBRG = 139
Refer this link to set the respective Baud Rate Generator
i'd like to put the SAM3X chip on sleepmode until a character arrives on the serial port. i was thinking of using an ausiliary flag in the Serial interrupt procedure in order to trigger the wake up procedure? what do you think abou? any advice or any other way i should follow or try?
I recommend reviewing .\arduino-1.5.2\hardware\arduino\sam\cores\arduino\UARTClass.cpp as its UARTClass::begin will detail how the Arduino Framework initializes the SAM's Serial IRQ with:
// Configure interrupts
_pUart->UART_IDR = 0xFFFFFFFF;
_pUart->UART_IER = UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME;
// Enable UART interrupt in NVIC
NVIC_EnableIRQ(_dwIrq);
// Enable receiver and transmitter
_pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN ;
Where you will need to ensure the baud rate generator is not stopped while in sleepmode.
Along with reading the SAM3X data sheet starting with 5.5.3. which leaves the peripheral clocks enabled.
looks like you may be able to insert the wake up into the UARTClass::IrqHandler
void UARTClass::IrqHandler( void )
{
uint32_t status = _pUart->UART_SR;
// Did we receive data ?
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
{
// wake up!!!! Not sure if you even need to wakeup, it should from the sleepmode
_rx_buffer->store_char(_pUart->UART_RHR);
}
With regards to sleeping:
\arduino-1.5.2\hardware\arduino\sam\system\libsam\source\pmc.c
Line 972: void pmc_enable_sleepmode(uint8_t uc_type)
Line 988: void pmc_enable_waitmode(void)
Line 1009: void pmc_enable_backupmode(void)
So I would suspect the following:
pmc_enable_sleepmode(WFI);
would put the unit a sleep and the IrqHandler of the UART_SR_RXRDY would wake itself up, without any code change.
One other alternative would be to use the serial pin's IO as to trigger an interrupt.
attachInterrupt(0, EnableSerialRX, CHANGE);
It would though, loose at least the first byte. the trade off is that you can use the lower power modem of pmc_enable_backupmode() rather than sleep