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.

Proper method for sending data in output Ports

Other Parts Discussed in Thread: MSP430G2553

I was working on interfacing of LCD (16x2) with msp430g2553 and I came up with following code.

The code is working well.But I have a problem with it.

If i change P1OUT= to  P1OUT|=  the code doesn't work. what's the reason behind this.

main.c

#include <msp430.h>
#include "byte.h"

int main()
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
lcd_init();
lcd_data('A');
return 0;
}

* byte.c
*
* Created on: Nov 8, 2017
* Author: AmitKumar
*/
#include <msp430.h>
#include "byte.h"

#define D0 BIT0
#define D1 BIT1
#define D2 BIT2
#define D3 BIT3
#define RS BIT4
#define EN BIT5
void lcd_init()
{
P1DIR|=D0+D1+D2+D3+RS+EN;
P1OUT|=D0+D1+D2+D3+RS+EN;
lcd_command(0x33);
lcd_command(0x32);
lcd_command(0x28);
lcd_command(0x0E);
lcd_command(0x01);
lcd_command(0x06);
lcd_command(0x80);
}

void lcd_command(unsigned char a)
{
int rs;
rs=0;
lcd_write(a,rs);
}
void lcd_data(unsigned char a)
{
int rs;
rs=1;
lcd_write(a,rs);
}
void lcd_write(unsigned char byte,int rs)
{
if(rs==0)
{
P1OUT&=~RS;
}
else
{
P1OUT|=RS;
}
P1OUT=((P1OUT & 0XF0)|((byte & 0xF0)>>4));   //(here is problem)
P1OUT|=EN;
P1OUT&=~EN;
__delay_cycles(3000);
P1OUT=((P1OUT & 0XF0)|(byte & 0x0F));  //(here is the problem)
P1OUT|=EN;
P1OUT&=~EN;
__delay_cycles(3000);


}

 

byte.h
*
* Created on: Nov 8, 2017
* Author: AmitKumar
*/

#ifndef BYTE_H_
#define BYTE_H_
extern void lcd_init();
extern void lcd_write(unsigned char,int);
extern void lcd_command(unsigned char);
extern void lcd_data(unsigned char);

#endif /* BYTE_H_ */

  • Hi,

    P1OUT|= x is equivalent to P1OUT = P1OUT | x. So, if P1OUT has a set bit that is not set in x, it will still be set in P1OUT, which does not look like what you want based on your example. According to your example, if one of the lower 4 bits of P1OUT is set, then the final value of P1OUT will have that bit set, even if it is not set in "byte". This will change your logic, and will give you a different value than if you just set P1OUT by using "=".

    Regards,
    Nathan

**Attention** This is a public forum