with msp430g2553 control 1602 LCD display time , and can set the time by pressing the button, I do the two modes of timing and Countdown
/*********************************************************************
msp430g2553 and 1602-pin connection conditions
* PIN1-to-ground
* PIN2-to-VCC (must be picked up)
* PIN3-----when the simulation is floating, the actual circuit 2K resistor--------------ground (resistance can be 500-2k, changing the resistance can change the brightness of the character display, bad resistance will cause nothing to show)
* PIN4, RS-P1.6
* PIN5---r/w and GND
* PIN6 to 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 to VCC
* PIN16-to-ground
*msp430g2553 and key pin connection condition
*k1-->p1.2
*k2-->p1.3
*k3-->p2.1
*k4-->p1.1
Description: I use the time matrix keyboard, if the use of independent keys to access the resistor
*****************************************************************/
The code is as follows:
#include <msp430g2253.h>
#include <intrinsics.h>
#include <msp430.h>
#define Lcd_en_port p1out//below 2 to be set to the same port
#define LCD_EN_DDR P1dir
#define Lcd_rs_port p1out//below 2 to be set to the same port
#define LCD_RS_DDR P1dir
#define Lcd_data_port p2out//below 3 to be set to the same port
#define LCD_DATA_DDR P2dir//must use high 4-bit
#define LCD_RS BIT6
#define LCD_EN BIT7
#define Lcd_data bit7| bit6| bit5| BIT4//4-bit Data cable 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 x,unsigned char Y, unsigned char data);
void Display_hms (unsigned 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 first line to display
unsigned char lcdbuf2[]={"10:30:00"}; What is the second line to display
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;//Selection
void main ()//main function
{
Wdtctl = Wdtpw + wdthold; Turn off the 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 to compare interrupts
CCR0 = 182; Set the capture/compare register with an initial value of 12000 for the ACLK clock frequency of 12khz, equivalent to 1s
Ta0ctl = Tassel_1 +taclr+mc_1;
p1dir|=bit0;//p1.0 for LED, display button function
p1out|=bit0+bit3;//p1in Low-level jump
p1ren|=bit2+bit3+bit1;//pull-up resistor, missing it seems to be useless
P2ren|=bit1;
Lcd_write_string (0,0, "Mode");
DELAY_NMS (10);
Lcd_write_string (0,1, "K1 (UP) K2");
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, which is an internal procedure supported by the C compiler.
set_flag=0;
while (1)
{
p1out&=~bit0;
SetTime ();
P1OUT=BIT0+BIT3;
}
}
/******************************************************
K1-Enter the time set now.
K2-set the hour.
k3-set the Minutes.
k4-confirm the setup is complete.
********************************************************/
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-OK
****************************************************/
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, not flashing
}
/****************************************************
Display time 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 liquid crystal 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, off cursor, not flashing
Lcd_write_command (0x06); Set input mode, increment does not shift
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;
}
/****************************************************
Liquid crystal 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; Lofty four-bit
lcd_data_port|=command&0xf0; Write high four-bit
Delay_nus (16);
Lcd_en_write2 ();
command=command<<4; Low four-bit move to high four-bit
Lcd_en_write1 ();
lcd_data_port&=0x0f; Lofty four-bit
lcd_data_port|=command&0xf0; Write low four-bit
Lcd_en_write2 ();
}
/**************************************************
Write Data functions
***************************************************/
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; Lofty four-bit
lcd_data_port|=data&0xf0; Write high four-bit
Delay_nus (16);
Lcd_en_write2 ();
data=data<<4; Low four-bit move to high four-bit
Lcd_en_write1 ();
lcd_data_port&=0x0f; Lofty four-bit
lcd_data_port|=data&0xf0; Write low four-bit
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 write string in any position, column x=0~15, line y=0,1
***************************************************/
void lcd_write_string (unsigned char x,unsigned char y,unsigned char *s)
{
Lcd_set_xy (X, Y); Write address
while (*s)//write display characters
{
Lcd_write_data (*s);
s++;
}
}
/***************************************************
LCD in any position characters, column x=0~15, line y=0,1
****************************************************/
void Lcd_write_char (unsigned char x,unsigned char y,unsigned char data)
{
Lcd_set_xy (X, Y); Write address
Lcd_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<n;i++)
Delay_1us ();
}
/**************************************************
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<n;i++)
Delay_1ms ();
}
/************************************************
Interrupt response function Mode one
*************************************************/
void Calledbytimera ()
{
if (aa>=60)//One second arrived
{
aa=0;//Qing 0
miao++;
}
if (miao>=60)//a point
{
miao=0;
fen++;
}
if (fen>=60)
{
fen=0;
shi++;
Lcd_write_command (0x80+0x45);//Show ":" Between seconds
Lcd_write_data (': ');
}
if (shi>=24)
{
shi=0;
Display_hms (0x43,shi);
Lcd_write_command (0x80+0x42);//Show ":" Between hours
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 two
*************************************************/
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);//Show ":" Between seconds
Lcd_write_data (': ');
Lcd_write_command (0x80+0x42);//Show ":" Between hours
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 ();
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
msp430g2553 Electronic Clock Experiment