一 实验目的:理解LCD/LED TM1638使用方法
二 实验内容:1在LCD或LED上显示01234567
2 设计电子钟 (起始时间23:59:45)
3 结合实验三,用定时器控制时钟的运行
三 实验电路图:
四 实验思路与程序:
1.在LCD上显示01234567
#include <msp430f6638.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define CPU_F ((double)1000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
// LCD segment definitions.
#define d 0x01
#define c 0x20
#define b 0x40
#define a 0x80
#define dp 0x10
#define g 0x04
#define f 0x08
#define e 0x02
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const char char_gen[] = { // As used in 430 Day Watch Demo board
a+b+c+d+e+f, // Displays "0"
b+c, // Displays "1"
a+b+d+e+g, // Displays "2"
a+b+c+d+g, // Displays "3"
b+c+f+g, // Displays "4"
a+c+d+f+g, // Displays "5"
a+c+d+e+f+g, // Displays "6"
a+b+c, // Displays "7"
a+b+c+d+e+f+g, // Displays "8"
a+b+c+d+f+g, // Displays "9"
a+b+c+e+f+g, // Displays "A"
c+d+e+f+g, // Displays "b"
a+d+e+f, // Displays "c"
b+c+d+e+g, // Displays "d"
a+d+e+f+g, // Displays "E"
a+e+f+g, // Displays "f"
a+b+c+d+f+g, // Displays "g"
c+e+f+g, // Displays "h"
b+c, // Displays "i"
b+c+d, // Displays "j"
b+c+e+f+g, // Displays "k"
d+e+f, // Displays "L"
a+b+c+e+f, // Displays "n"
a+b+c+d+e+f+g+dp // Displays "full"
};
/* Private function prototypes -----------------------------------------------*/
void Init_lcd(void); // LCD初始化
void LcdGo(unsigned char Dot); //打开或关闭液晶
void LcdBlink(unsigned char doit); // 显示或者消隐显示内容
void LCD_Clear(void); // 清屏
void Init_TS3A5017DR(void); // Configure TS3A5017DR IN1 and IN2
void Backlight_Enable(void); // 打开背光
/* Private functions ---------------------------------------------------------*/
/*!
*函数功能:配置LCD segment output功能
*输入参数:无
*返回值: 无
*/
void Init_lcd(void)
{
LCDBCTL0 =LCDDIV0 + LCDPRE0 + LCDMX1 + LCDSSEL + LCDMX1 + LCD4MUX ;
LCDBPCTL0 = LCDS0 + LCDS1 + LCDS2 + LCDS3 + LCDS4 + LCDS5 + LCDS6+
LCDS7 + LCDS8
+ LCDS9 + LCDS10 + LCDS11 ;
P5SEL = 0xfc;
}
/*****************************************************************************
打开或关闭液晶
1:打开 0:关闭
*****************************************************************************/
/*!
*函数功能:打开或者关闭液晶
*输入参数:Dot
* Dot为1时:打开液晶显示
* Dot为0时:关闭液晶显示
*返回值: 无
*/
void LcdGo(unsigned char Dot)
{
if(Dot==1)
{
//打开液晶显示
LCDBCTL0 |= LCDON;
}
else if(Dot==0)
{
//关闭液晶显示
LCDBCTL0 &= ~LCDON;
}
}
/*!
*函数功能:显示或者消隐显示内容
*输入参数:无
*返回值: 无
*/
void LcdBlink(unsigned char doit)
{
if(doit==0)
{
LCDBCTL0 &= ~LCDSON;
}
else if(doit==1)
{
LCDBCTL0 |= LCDSON;
}
}
/*!
*函数功能:清除LCD的显示内容
*输入参数:无
*返回值: 无
*/
void LCD_Clear(void)//清屏
{
unsigned char index;
for (index=0; index<12; index++)
{
LCDMEM[index] = 0;
}
}
/*!
*函数功能:配置TS3A5017DR通道
*输入参数:无
*返回值: 无
*/
void Init_TS3A5017DR(void)
{
// Configure TS3A5017DR IN1 and IN2
P1DIR |= BIT6 + BIT7; //P3.4 : IN1 ; P3.5 : IN2 set as output
P1OUT &= ~BIT7; //IN1 = 0
P1OUT |= BIT6; //IN2 = 1
}
/*!
*函数功能:LCD segment 背光使能
*输入参数:无
*返回值: 无
*/
void Backlight_Enable(void)
{
P8DIR |= BIT0;
P8OUT |= BIT0;
}
/*!
*函数功能:主函数
*输入参数:无
*返回值: 无
*/
int main()
{
int i,j;
WDTCTL = WDTPW + WDTHOLD;// Stop WDT
Init_TS3A5017DR(); // Configure TS3A5017DR IN1 and IN2
Init_lcd(); // LCD初始化
Backlight_Enable(); // 打开背光
LcdGo(1); // 打开液晶模块
LCD_Clear(); // 清屏
while(1)
{
for (i=0;i<8;i++) // Display "01234567"
{
for(j=0;j<8;j++)
{
LCDMEM[j] = char_gen[i];
}
delay_ms(1000);
}
}
}
2.设计电子钟(显示起始时间23:59:45)
#include <msp430f6638.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define CPU_F ((double)1000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
// LCD segment definitions.
#define d 0x01
#define c 0x20
#define b 0x40
#define a 0x80
#define dp 0x10
#define g 0x04
#define f 0x08
#define e 0x02
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const char char_gen[] = { // As used in 430 Day Watch Demo board
a+b+c+d+e+f, // Displays "0"
b+c, // Displays "1"
a+b+d+e+g, // Displays "2"
a+b+c+d+g, // Displays "3"
b+c+f+g, // Displays "4"
a+c+d+f+g, // Displays "5"
a+c+d+e+f+g, // Displays "6"
a+b+c, // Displays "7"
a+b+c+d+e+f+g, // Displays "8"
a+b+c+d+f+g, // Displays "9"
a+b+c+e+f+g, // Displays "A"
c+d+e+f+g, // Displays "b"
a+d+e+f, // Displays "c"
b+c+d+e+g, // Displays "d"
a+d+e+f+g, // Displays "E"
a+e+f+g, // Displays "f"
a+b+c+d+f+g, // Displays "g"
c+e+f+g, // Displays "h"
b+c, // Displays "i"
b+c+d, // Displays "j"
b+c+e+f+g, // Displays "k"
d+e+f, // Displays "L"
a+b+c+e+f, // Displays "n"
a+b+c+d+e+f+g+dp // Displays "full"
};
/* Private function prototypes -----------------------------------------------*/
void Init_lcd(void); // LCD初始化
void LcdGo(unsigned char Dot); //打开或关闭液晶
void LcdBlink(unsigned char doit); // 显示或者消隐显示内容
void LCD_Clear(void); // 清屏
void Init_TS3A5017DR(void); // Configure TS3A5017DR IN1 and IN2
void Backlight_Enable(void); // 打开背光
/* Private functions ---------------------------------------------------------*/
/*!
*函数功能:配置LCD segment output功能
*输入参数:无
*返回值: 无
*/
void Init_lcd(void)
{
LCDBCTL0 =LCDDIV0 + LCDPRE0 + LCDMX1 + LCDSSEL + LCDMX1 + LCD4MUX ;
LCDBPCTL0 = LCDS0 + LCDS1 + LCDS2 + LCDS3 + LCDS4 + LCDS5 + LCDS6+
LCDS7 + LCDS8
+ LCDS9 + LCDS10 + LCDS11 ;
P5SEL = 0xfc;
}
/*****************************************************************************
打开或关闭液晶
1:打开 0:关闭
*****************************************************************************/
/*!
*函数功能:打开或者关闭液晶
*输入参数:Dot
* Dot为1时:打开液晶显示
* Dot为0时:关闭液晶显示
*返回值: 无
*/
void LcdGo(unsigned char Dot)
{
if(Dot==1)
{
//打开液晶显示
LCDBCTL0 |= LCDON;
}
else if(Dot==0)
{
//关闭液晶显示
LCDBCTL0 &= ~LCDON;
}
}
/*!
*函数功能:显示或者消隐显示内容
*输入参数:无
*返回值: 无
*/
void LcdBlink(unsigned char doit)
{
if(doit==0)
{
LCDBCTL0 &= ~LCDSON;
}
else if(doit==1)
{
LCDBCTL0 |= LCDSON;
}
}
/*!
*函数功能:清除LCD的显示内容
*输入参数:无
*返回值: 无
*/
void LCD_Clear(void)//清屏
{
unsigned char index;
for (index=0; index<12; index++)
{
LCDMEM[index] = 0;
}
}
/*!
*函数功能:配置TS3A5017DR通道
*输入参数:无
*返回值: 无
*/
void Init_TS3A5017DR(void)
{
// Configure TS3A5017DR IN1 and IN2
P1DIR |= BIT6 + BIT7; //P3.4 : IN1 ; P3.5 : IN2 set as output
P1OUT &= ~BIT7; //IN1 = 0
P1OUT |= BIT6; //IN2 = 1
}
/*!
*函数功能:LCD segment 背光使能
*输入参数:无
*返回值: 无
*/
void Backlight_Enable(void)
{
P8DIR |= BIT0;
P8OUT |= BIT0;
}
/*!
*函数功能:主函数
*输入参数:无
*返回值: 无
*/
int main()
{
WDTCTL = WDTPW + WDTHOLD;// Stop WDT
Init_TS3A5017DR(); // Configure TS3A5017DR IN1 and IN2
Init_lcd(); // LCD初始化
Backlight_Enable(); // 打开背光
LcdGo(1); // 打开液晶模块
LCD_Clear(); // 清屏
while(1)
{
// for (i=0; i<6; i++) // Display "012345"
// {
// for(j=0;j<6;j++)
// {
// LCDMEM[j] = char_gen[i];
// }
// delay_ms(1000);
// }
LCDMEM[0] = char_gen[2];//第一个数字显示‘2’
LCDMEM[1] = char_gen[3];//第一个数字显示‘3’
LCDMEM[2] = char_gen[5];//第一个数字显示‘5’
LCDMEM[3] = char_gen[9];//第一个数字显示‘9’
LCDMEM[4] = char_gen[4];//第一个数字显示‘4’
LCDMEM[5] = char_gen[5];//第一个数字显示‘5’
}
}
3.用定时器中断进行计时,修改时、分、秒的数据
#include <msp430f6638.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define CPU_F ((double)1000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
// LCD segment definitions.
#define d 0x01
#define c 0x20
#define b 0x40
#define a 0x80
#define dp 0x10
#define g 0x04
#define f 0x08
#define e 0x02
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const char char_gen[] = { // As used in 430 Day Watch Demo board
a+b+c+d+e+f, // Displays "0"
b+c, // Displays "1"
a+b+d+e+g, // Displays "2"
a+b+c+d+g, // Displays "3"
b+c+f+g, // Displays "4"
a+c+d+f+g, // Displays "5"
a+c+d+e+f+g, // Displays "6"
a+b+c, // Displays "7"
a+b+c+d+e+f+g, // Displays "8"
a+b+c+d+f+g, // Displays "9"
a+b+c+e+f+g, // Displays "A"
c+d+e+f+g, // Displays "b"
a+d+e+f, // Displays "c"
b+c+d+e+g, // Displays "d"
a+d+e+f+g, // Displays "E"
a+e+f+g, // Displays "f"
a+b+c+d+f+g, // Displays "g"
c+e+f+g, // Displays "h"
b+c, // Displays "i"
b+c+d, // Displays "j"
b+c+e+f+g, // Displays "k"
d+e+f, // Displays "L"
a+b+c+e+f, // Displays "n"
a+b+c+d+e+f+g+dp // Displays "full"
};
/* Private function prototypes -----------------------------------------------*/
void Init_lcd(void); // LCD初始化
void LcdGo(unsigned char Dot); //打开或关闭液晶
void LcdBlink(unsigned char doit); // 显示或者消隐显示内容
void LCD_Clear(void); // 清屏
void Init_TS3A5017DR(void); // Configure TS3A5017DR IN1 and IN2
void Backlight_Enable(void); // 打开背光
/* Private functions ---------------------------------------------------------*/
/*!
*函数功能:配置LCD segment output功能
*输入参数:无
*返回值: 无
*/
void Init_lcd(void)
{
LCDBCTL0 =LCDDIV0 + LCDPRE0 + LCDMX1 + LCDSSEL + LCDMX1 + LCD4MUX ;
LCDBPCTL0 = LCDS0 + LCDS1 + LCDS2 + LCDS3 + LCDS4 + LCDS5 + LCDS6+
LCDS7 + LCDS8
+ LCDS9 + LCDS10 + LCDS11 ;
P5SEL = 0xfc;
}
/*****************************************************************************
打开或关闭液晶
1:打开 0:关闭
*****************************************************************************/
/*!
*函数功能:打开或者关闭液晶
*输入参数:Dot
* Dot为1时:打开液晶显示
* Dot为0时:关闭液晶显示
*返回值: 无
*/
void LcdGo(unsigned char Dot)
{
if(Dot==1)
{
//打开液晶显示
LCDBCTL0 |= LCDON;
}
else if(Dot==0)
{
//关闭液晶显示
LCDBCTL0 &= ~LCDON;
}
}
/*!
*函数功能:显示或者消隐显示内容
*输入参数:无
*返回值: 无
*/
void LcdBlink(unsigned char doit)
{
if(doit==0)
{
LCDBCTL0 &= ~LCDSON;
}
else if(doit==1)
{
LCDBCTL0 |= LCDSON;
}
}
/*!
*函数功能:清除LCD的显示内容
*输入参数:无
*返回值: 无
*/
void LCD_Clear(void)//清屏
{
unsigned char index;
for (index=0; index<12; index++)
{
LCDMEM[index] = 0;
}
}
/*!
*函数功能:配置TS3A5017DR通道
*输入参数:无
*返回值: 无
*/
void Init_TS3A5017DR(void)
{
// Configure TS3A5017DR IN1 and IN2
P1DIR |= BIT6 + BIT7; //P3.4 : IN1 ; P3.5 : IN2 set as output
P1OUT &= ~BIT7; //IN1 = 0
P1OUT |= BIT6; //IN2 = 1
}
/*!
*函数功能:LCD segment 背光使能
*输入参数:无
*返回值: 无
*/
void Backlight_Enable(void)
{
P8DIR |= BIT0;
P8OUT |= BIT0;
}
/*!
*函数功能:主函数
*输入参数:无
*返回值: 无
*/
int main()
{
WDTCTL = WDTPW + WDTHOLD;// Stop WDT
Init_TS3A5017DR(); // Configure TS3A5017DR IN1 and IN2
Init_lcd(); // LCD初始化
Backlight_Enable(); // 打开背光
LcdGo(1); // 打开液晶模块
LCD_Clear(); // 清屏
LCDMEM[0] = char_gen[2];//第一个数字显示‘2’
LCDMEM[1] = char_gen[3];//第一个数字显示‘3’
LCDMEM[2] = char_gen[5];//第一个数字显示‘5’
LCDMEM[3] = char_gen[9];//第一个数字显示‘9’
LCDMEM[4] = char_gen[4];//第一个数字显示‘4’
LCDMEM[5] = char_gen[5];//第一个数字显示‘5’
while(1)
{
int i,j;
for(j=0;j<4;j++)
{
LCDMEM[5] = char_gen[j+5];
delay_ms(1000);
}
LCDMEM[4] = char_gen[5];
LCDMEM[5] = char_gen[0];
for(j=0;j<10;j++)
{
LCDMEM[5] = char_gen[j];
delay_ms(1000);
}
LCDMEM[0] = char_gen[0];
LCDMEM[1] = char_gen[0];
LCDMEM[2] = char_gen[0];
LCDMEM[3] = char_gen[0];
LCDMEM[4] = char_gen[0];
LCDMEM[5] = char_gen[0];
delay_ms(1000); //起到延时作用,不会直接跳不显示
for (i=0; i<6; i++)
{
sec++;
if(sec==60){
sec=0;
mun++;
}
if(mun==60){
mun=0;
hou++;
}
if(hou==24){
hou=0;
}
LCDMEM[5] = char_gen[sec%10];
LCDMEM[4] = char_gen[sec/10];
LCDMEM[3] = char_gen[mun%10];
LCDMEM[2] = char_gen[mun/10];
LCDMEM[1] = char_gen[hou%10];
LCDMEM[0] = char_gen[hou/10];
// LCDMEM[j] = char_gen[j];//修改该处,hou,mun,sec
delay_ms(1000);
}
}