MSP430G2553 20pins 4位 LCD1602显示
作为初学者,在过去的一周反复查资料,有所小成如下。
- G2553可怜的GPIO口数,计划使用P2高四位,但是实用中发现P2.6和P2.7多功能引脚。(Pin19 is P2.6, P18 is P2.7.)
计划使用P1.6 and P1.7 as I2C communication port.
- 改来改去最终发现P2.6和P2.7无法使用,其默认功能选择晶振XIN和XOUT,
若要使用它们的IO功能,需将P2SEL.6和P2SEL.7置零。
在GPIO初始化里将它们配置为普通IO口之后,果然能正常显示数据了。
l P2SEL&=~(BIT6+BIT7);// Using the second functions;
l P2SEL2&=~(BIT6+BIT7);//Using the second functions;
- Clock Configuration;
TI MCU MSP430 单片机工作的系统时钟被分为了MCLK、SMCLK 和ACLK 三个,可以根据需要关闭其中的一个几个或全部。
l #include <INTRINSICS.h>
__delay_cycles(1000000); //与CPU时钟相关的长延时
l Do you know why 32768 Hz is a standard?
It is because that number is 215
/*
* LCD.c
*
* Created on: 2020��3��25��
* Author: Mikejin
*/
#include"lcd.h"
#include "msp430g2553.h"
#include <intrinsics.h>
//����////////////////////////////////////////////////////////////////////
#define LCD_EN_PORT P1OUT//����2��Ҫ��Ϊͬһ����
#define LCD_EN_DDR P1DIR
#define LCD_RS_PORT P1OUT//����2��Ҫ��Ϊͬһ����
#define LCD_RS_DDR P1DIR
#define LCD_DATA_PORT P2OUT //����3��Ҫ��Ϊͬһ����
#define LCD_DATA_DDR P2DIR //һ��Ҫ�ø�4λ
#define LCD_RS BIT2
#define LCD_EN BIT1
//#define LCD_DATA BIT0|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6|BIT7 //8λ����������ģʽʱʹ��
#define LCD_DATA BIT4|BIT5|BIT6|BIT7 //4λ����������ģʽʱʹ��
////Ԥ���庯��//////////////////////////////////////////////////////////////////
//LCDҺ����������///////////////////////////////////////////////////////////////
//LCD1602Һ����ʼ��
void LCD_init(void)
{
P2SEL&=~(BIT6+BIT7);// Using the second functions;
P2SEL2&=~(BIT6+BIT7);//Using the second functions;
delay_nms(15);
LCD_DATA_DDR|=LCD_DATA; //���ݿڷ���Ϊ���
LCD_EN_DDR|=LCD_EN; //����EN�������
LCD_RS_DDR|=LCD_RS; //����RS�������
delay_1ms(); //��4�в�Ҫ�ģ��������û��ʾ
// LCD_write_command(0x38); //8λ���ݽӿ�ʱ��ʹ�����У��������û��ʾ
LCD_write_command(0x33); //4λ���ݽӿ�ʱ��ʹ�����У��������û��ʾ
delay_1ms(); //
// LCD_write_command(0x38); //8λ���ݽӿ�ʱ��ʹ�����У��������û��ʾ
LCD_write_command(0x32); //4λ���ݽӿ�ʱ��ʹ�����У��������û��ʾ
delay_1ms();
// LCD_write_command(0x38); //8λ���ݽӿ�ʱ��ʹ�����У��������û��ʾ
delay_1ms();
// LCD_write_command(0x38); //8λ���ݽӿ�
LCD_write_command(0x2c); //4λ���ݽӿ�
delay_1ms();
LCD_write_command(0x0c); //��ʾ��
delay_1ms();
LCD_write_command(0x01); //����
delay_1ms();
LCD_write_command(0x06);
}
//Һ��ʹ��
void LCD_en_write(void)
{
LCD_EN_PORT|=LCD_EN;
delay_nus(10);
LCD_EN_PORT&=~LCD_EN;
}
//дָ��
void LCD_write_command(unsigned char command)
{
delay_nus(16);
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f; //�����λ
LCD_DATA_PORT|=command&0xf0; //д����λ
//LCD_DATA_PORT = command; //д8λ
delay_nus(16);
LCD_en_write();
command=command<<4; //����λ�Ƶ�����λ
LCD_DATA_PORT&=0x0f; //�����λ
LCD_DATA_PORT|=command&0xf0; //д����λ
LCD_en_write();
}
//���
void LCD_write_data(unsigned char data)
{
delay_nus(16);
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0X0f; //�����λ
LCD_DATA_PORT|=data&0xf0; //д����λ
//LCD_DATA_PORT = data; //д8λ
LCD_en_write();
data=data<<4; //����λ�Ƶ�����λ
LCD_DATA_PORT&=0X0f; //�����λ
LCD_DATA_PORT|=data&0xf0; //д����λ
LCD_en_write();
}
//д��ַ����
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
LCD_write_command( address);
}
//LCD������λ��д�ַ���
//��x=0~15,��y=0,1
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //д��ַ
while (*s) // д��ʾ�ַ�
{
LCD_write_data( *s );
s ++;
}
}
//LCD������λ��д�ַ�
//��x=0~15,��y=0,1
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data)
{
LCD_set_xy( X, Y ); //д��ַ
LCD_write_data( data);
}
//��ʱ����//////////////////////////////////////////////////////////////////////
//1us��ʱ����
void delay_1us(void)
{
asm(" nop");
}
//N us��ʱ����
void delay_nus(unsigned int n)
{
unsigned int i;
for (i=0;i<n;i++);
delay_1us();
}
//1ms��ʱ����
void delay_1ms(void)
{
unsigned int i;
for (i=0;i<1140;i++);
}
//N ms��ʱ����
void delay_nms(unsigned int n)
{
unsigned int i=0;
for (i=0;i<n;i++);
delay_1ms();
}
/* * LCD.h * * Created on: 2020��3��25�� * Author: Mikejin */ #ifndef LCD_H_ #define LCD_H_ void LCD_init(void); void LCD_en_write(void); void LCD_write_command(unsigned char command); void LCD_write_data(unsigned char data); void LCD_set_xy (unsigned char x, unsigned char y); void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s); void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data); void delay_1ms(void); void delay_nus(unsigned int n); void delay_nms(unsigned int n); #endif /* LCD_H_ */
/**************************************************
* LCD1602 Display
* Description: P2 High 4 Bits for LCD1602 and P1.6 and P1.7 for I2C integrated another programmer.
* LCD1602 The first line showing "Hello!LCD1602D"
* LCD1602 The Second line showing "123456789abcdefg"
*
* Date��2020��4��12�յ��Գɹ�������������Ҫ����ΪLCD��ʼ����
*
* Ӳ����·��MSP430G2553 20pins
* Ӳ�����ӣ�
*
* MSP430��LCD������Ϣ
* LCD1602��4λ�ӿڣ���ʹ��D4-D7���ݿڣ�D0-D3������MCU
* PIN1 --> ��
* PIN2 --> VCC��һ��Ҫ��+5V��
* PIN3 -->����ʱ���գ�ʵ�ʵ�· 2K����-->�� (һ��Ҫ�Ӻã�����û���κ���ʾ)
* PIN4 --> RS --> P1.2
* PIN5 --> R/W --> GND
* PIN6 --> EN --> P1.1
* PIN7 --> D0���� * PIN8 --> D1���� * PIN9 --> D2���� * PIN10 --> D3����
* PIN11 --> D4 --> P2.4
* PIN12 --> D5 --> P2.5
* PIN13 --> D6 --> P2.6
* PIN14 --> D7 --> P2.7
* PIN15 --> VCC��һ��Ҫ��+5V�������Ҫ������Բ���)
* PIN16 --> ��
* ��������MSP430FETȫϵ��JTAG������
* ��������� CCS9.3 ����
**************************************************/
#include "msp430g2553.h"
#include <intrinsics.h>
#include"lcd.h"
void main()
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
LCD_init();
delay_1ms();
while(1)
{
LCD_write_string(1,0,"Hello!LCD1602D");
delay_1ms();
LCD_write_string(0,1,"123456789abcdefg");
}
}