Use msp430g2553 to control the 1602 LCD to display time, and you can set the time by pressing the button. I made two modes: forward timing and countdown.
/************************************************************************
msp430g2553 and 1602 pin connection
* PIN1 --> Ground
* PIN2 --> VCC (must be connected to +5V)
* PIN3 --> floating during simulation, 2K resistor in actual circuit --> ground (the resistor can be 500-2k, changing the resistor can change the brightness of the character display, and improper connection of the resistor will result in nothing being displayed)
* PIN4 --> RS --> P1.6
* PIN5 --> R/W --> GND
* PIN6 --> EN --> P1.7
* PIN7 --> D0 not connected
* PIN8 --> D1 not connected
* PIN9 --> D2 not connected
* PIN10 --> D3 not connected
* PIN11 --> D4 --> P2.4
* PIN12 --> D5 --> P2.5
* PIN13 --> D6 --> P2.6
* PIN14 --> D7 --> P2.7
* PIN15 --> VCC
* PIN16 --> Ground
* Connection between msp430g2553 and key pins
*k1-->p1.2
*k2-->p1.3
*k3-->p2.1
*k4-->p1.1
Note: I use a matrix keyboard. If you use independent buttons, you need to connect resistors
********************************************************************/
code show as below:
#include
#include
#include
#define LCD_EN_PORT P1OUT //The following 2 should be set to the same port
#define LCD_EN_DDR P1DIR
#define LCD_RS_PORT P1OUT //The following 2 should be set to the same port
#define LCD_RS_DDR P1DIR
#define LCD_DATA_PORT P2OUT //The following 3 should be set to the same port
#define LCD_DATA_DDR P2DIR //Be sure to use the upper 4 bits
#define LCD_RS BIT6
#define LCD_EN BIT7
#define LCD_DATA BIT7|BIT6|BIT5|BIT4 //4-bit data line connection mode
//Function declaration
void LCD_init(void);
void LCD_init_first(void);
void LCD_en_write1(void); //Rising edge enable
void LCD_en_write2(void); //Falling edge enable
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 char
add,unsigned char date);
void delay_1ms(void);
void delay_nus(unsigned int n) ;
void delay_nms(unsigned int n);
void SetTime() ; void
SetMode();
void CalledByTimerA();
unsigned char LCDBuf1[]={"Hello world"};//The content to be displayed in the first line
unsigned char LCDBuf2[]={"10:30:00" }; //The content to be displayed in the second line is
unsigned char shi1,fen1,miao1,aa,shi2,fen2,miao2;
unsigned char miao=0;
unsigned char fen=30;
unsigned char shi=10;
unsigned char aa=0;//Counter
int set_flag=0;//Time setting flag
int time_flag=0;//Select
void main()//Main function
{
WDTCTL = WDTPW + WDTHOLD; // Turn off watchdog
LCD_init_first();
LCD_init();
delay_nms(100);
BCSCTL3 |= LFXT1S_2; // Set LFXT1 to vol clock, i.e. 12kHZ
CCTL0|= CCIE; //Set capture/compare control register, CCIE=0x0010, enable capture compare interrupt
CCR0 =182; //Set capture/compare register, initial value is 12000, for ACLK clock frequency of 12khz, equivalent to 1s
TA0CTL = TASSEL_1 +TACLR+MC_1;
P1DIR|=BIT0;//P1.0 is LED, display button works
P1OUT|=BIT0+BIT3;//P1IN low level jump
P1REN|=BIT2+BIT3+BIT1;//Pull-up resistor, it seems that it cannot be used without it
P2REN|=BIT1;
LCD_write_string(0,0," Mode ");
delay_nms(10);
LCD_write_string(0,1,"k1(up) k2(down) ");
SetMode();
LCD_write_string(0,0," ");
delay_nms(10);
LCD_write_string(0,1," ");
delay_nms(10);
LCD_write_string(0,0,LCDBuf1);
delay_nms(10);
LCD_write_string(0,1,LCDBuf2);
_EINT(); //Enable interrupt, this is an internal process supported by the C compiler.
set_flag=0;
while(1)
{
P1OUT&=~BIT0;
SetTime();
P1OUT=BIT0+BIT3;
}
}
/**********************************************************
K1-Enter and set the current time.
K2-Set the hour.
K3-Set the minute.
K4-Confirm and complete the setting.
******************************************************/
void SetTime()
{
if(!(P1IN&BIT2))
{
delay_nms(10);
// P1OUT&=~BIT0;
set_flag=1;
TA0CTL&=0xffcf; //Stop mode
}
else if(!(P1IN&BIT3) && set_flag)
{
delay_nms(50);
shi=(shi+1)%24;
display_HMS( 0x40,shi);
LCD_write_command(0x80+0x41);
// P1OUT&=~BIT0;
}
else if (!(P2IN&BIT1) && set_flag)
{
delay_nms(50);
// P1OUT&=~BIT0;
fen=(fen+1)%60;
display_HMS(0x43,fen);
LCD_write_command(0x80+0x44) ;
}
else if (!(P1IN&BIT1)&& set_flag)
{
delay_nms(10);
TA0CTL = TASSEL_1 +TACLR+MC_1;
// P1OUT&=~BIT0;
set_flag=0;
}
else
{
// P1OUT=BIT0+BIT3;
}
}
/******************************************************
K1-mode 1, time
K2-mode 2, countdown
K3-confirmation
************************************************/
void SetMode()
{
while(1)
{
if(!(P1IN&BIT2))
{
time_flag=1;
}
else if(!(P1IN&BIT3))
{
time_flag=2;
}
if(time_flag!=0 &&!(P2IN&BIT1))
{
break;
}
}
LCD_write_command(0x0c); //Display on, off cursor, no blinking
}
/****************************************************
Display hours, minutes and seconds
****************************************************/
void display_HMS(unsigned char add,unsigned char date)
{
unsigned char shi,ge;
shi=date/10;
ge=date%10;
LCD_write_command(add+0x80);
LCD_write_data(0x30+shi);
LCD_write_data(0x30+ge);
}
/************************************************************
LCD1602 LCD initialization function
****************************************************/
void LCD_init_first(void) //LCD1602 LCD initialization function (hot start)
{
delay_nms(500);
LCD_DATA_DDR|=LCD_DATA; //Data port direction is output
LCD_EN_DDR|=LCD_EN; //Set EN direction to output
LCD_RS_DDR|=LCD_RS; //Set RS direction to output
delay_nms(50);
LCD_write_command(0x30);
delay_nms(50);
LCD_write_command(0x30);
delay_nms(5);
LCD_write_command(0x30);
delay_nms(500);
}
void LCD_init(void)
{
delay_nms(500);
LCD_DATA_DDR|=LCD_DATA; //Data port direction is output
LCD_EN_DDR|=LCD_EN; //Set EN direction to output
LCD_RS_DDR|=LCD_RS; //Set RS direction to output
delay_nms(500);
LCD_write_command(0x28); //4-bit data interface
delay_nms(50);
LCD_write_command(0x28); //4-bit data interface
delay_nms(50);
LCD_write_command(0x28); //4-bit data interface
delay_nms(50);
LCD_en_write2();
delay_nms(50);
LCD_write_command(0x28); //4-bit data interface
delay_nms(500);
LCD_write_command(0x01); //Clear screen
LCD_write_command(0x0c); //Display on and off cursor, no blinking
LCD_write_command(0x06); //Set input mode, increment without shifting
delay_nms(50);
}
/********************************************************
LCD enable rising edge
********************************************************/
void LCD_en_write1(void)
{
LCD_EN_PORT&=~LCD_EN;
delay_nus(10);
LCD_EN_PORT|=LCD_EN;
}
/********************************************************
LCD enable falling edge
****************************************************/
void LCD_en_write2(void)
{
LCD_EN_PORT|=LCD_EN;
delay_nus(10);
LCD_EN_PORT&=~LCD_EN;
}
/********************************************************
Write instruction function
****************************************************/
void LCD_write_command(unsigned char command)
{
delay_nus(16);
P2SEL=0x00;
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_en_write1();
LCD_DATA_PORT&=0X0f; //clear high four bits
LCD_DATA_PORT|=command&0xf0; //write high four bits
delay_nus(16);
LCD_en_write2();
command=command<<4; //move low four bits to high four bits
LCD_en_write1();
LCD_DATA_PORT&=0x0f; //clear high four bits
LCD_DATA_PORT|=command&0xf0; //write low four bits
LCD_en_write2();
}
/******************************************************
Write data function
************************************************/
void LCD_write_data(unsigned char data)
{
delay_nus(16);
P2SEL=0x00;
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_en_write1(); //E rising edge
LCD_DATA_PORT&=0X0f; //clear high four bits
LCD_DATA_PORT|=data&0xf0; //write high four bits
delay_nus(16);
LCD_en_write2();
data=data<<4; //Move the lower four bits to the upper four bits
LCD_en_write1();
LCD_DATA_PORT&=0X0f; //Clear the high four bits
LCD_DATA_PORT|=data&0xf0; //Write the lower four bits
LCD_en_write2();
}
/*******************************************************
Write address function
************************************************/
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 writes a string at any position, column x=0~15, row y=0,1
*******************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //write addresswhile
(*s) //write display character
{
LCD_write_data( *s );
s++;
}
}
/*******************************************************
LCD writes characters at any position, column x=0~15, row y=0,1
********************************************************/
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data)
{
LCD_set_xy( X, Y ); //write addressLCD_write_data
( data);
}
/*******************************************************
1us delay function
************************************************/
void delay_1us(void)
{
asm("nop");
}
/********************************************************
N us delay function
****************************************************/
void delay_nus(unsigned int n)
{
unsigned int i;
for (i=0;i
}
/**************************************************
1ms delay function
***************************************************/
void delay_1ms(void)
{
unsigned int i;
for (i=0;i<1140;i++);
}
/******************************************************
N ms delay function
***************************************************/
void delay_nms(unsigned int n)
{
unsigned int i=0;
for (i=0;i
}
/****************************************************
Interrupt response function mode 1
************************************************/
void CalledByTimerA()
{
if(aa>=60)//One second is up
{
aa=0;//clear 0
miao++;
}
if(miao>=60)//one point
{
miao=0;
fen++;
}
if(fen>=60)
{
fen=0;
shi++;
LCD_write_command(0x80+0x45);//Display ":" between minutes and seconds
LCD_write_data(':');
}
if(shi>=24)
{
shi=0;
display_HMS(0x43,shi);
LCD_write_command(0x80+0x42);//Display ":" between hours and minutes
LCD_write_data(':');
}
display_HMS(0x46,miao);
display_HMS(0x43,fen);
display_HMS(0x40,shi);
LCD_write_command(0x87);
LCD_write_command(0x84);
}
/****************************************************
Interrupt response function mode 2
************************************************/
void CalledByTimerAForCountDown()
{
if(aa>=60)
{
aa=0;
if(miao<=0)
{
if(fen<=0)
{
if(shi<=0)
{
}
else
{
shi--;
fen=59;
miao=59;
}
}
else
{
fen--;
miao=59;
}
}
else
{
miao--;
}
LCD_write_command(0x80+0x45);//Display ":" between minutes and seconds
LCD_write_data(':');
LCD_write_command(0x80+0x42);//Display ":" between hours and minutes
LCD_write_data(':');
display_HMS(0x46,miao);
display_HMS(0x43,fen);
display_HMS(0x40,shi);
LCD_write_command(0x87);
LCD_write_command(0x84);
}
}
/******************************************************
Timer interrupt
************************************************/
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
aa++;
if(time_flag==1)
{
CalledByTimerA();
}
else if(time_flag==2)
{
CalledByTimerAForCountDown();
}
}
Previous article:msp430 button control LED light
Next article:MCU: Matrix keyboard and LCD1602
Recommended ReadingLatest update time:2024-11-15 07:29
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- I bought a ZT303 multimeter.
- Design techniques for machine learning algorithm generation (provided by ST official, Chinese subtitles)
- Add a startup program to the itop4412 development board
- IWR1642 Vehicle Occupant Detection Routine Problem
- How can I use an oscilloscope to capture pulses within a certain time range, such as capturing waveforms between 100us and 200us?
- Products + DC brushless motor control + drive kit
- Due to the epidemic, you are under home quarantine. Has your salary been reduced?
- ARM9-compatible soft-core processor design: based on FPGA
- 5G era promotes PCB development in multiple fields
- What is the principle of keyless entry for cars?