Microcontroller Experiment 2 – LCD/LED Experiment (MSP430F6638)

1 Experiment purpose: Understand how to use LCD/LED TM1638

2 Experiment content: 1 Display 01234567 on LCD or LED

2 Design electronic clock (starting time 23:59:45)

3 Combined with Experiment 3, use a timer to control the operation of the clock

Three experimental circuit diagram:

?4 Experimental ideas and procedures:

1.Display 01234567 on LCD

#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 initialization
void LcdGo(unsigned char Dot); //Turn on or off the LCD
void LcdBlink(unsigned char doit); // Display or hide the display content
void LCD_Clear(void); // Clear the screen
void Init_TS3A5017DR(void); // Configure TS3A5017DR IN1 and IN2
void Backlight_Enable(void); // Turn on the backlight
/* Private functions -------------------------------------------------- -----------*/
/*!
*Function: configure LCD segment output function
*Input parameters: none
*Return value: None
*/
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;
}
/****************************************************** ****************************
Turn LCD on or off
1: open 0: closed
*************************************************** ***************************/
/*!
*Function: turn on or off the LCD
*Input parameters: Dot
* When Dot is 1: Turn on the LCD display
* When Dot is 0: turn off the LCD display
*Return value: None
*/
void LcdGo(unsigned char Dot)
{
if(Dot==1)
{
//Open LCD display
LCDBCTL0 |= LCDON;
}
else if(Dot==0)
{
//Close LCD display
LCDBCTL0 &= ~LCDON;
}
}
/*!
*Function: display or hide display content
*Input parameters: none
*Return value: None
*/
void LcdBlink(unsigned char doit)
{
if(doit==0)
{
LCDBCTL0 &= ~LCDSON;
}
else if(doit==1)
{
LCDBCTL0 |= LCDSON;
}
}
/*!
*Function: Clear the LCD display content
*Input parameters: none
*Return value: None
*/
void LCD_Clear(void)//clear screen
{
 unsigned char index;
 for (index=0; index<12; index + + )
 {
 LCDMEM[index] = 0;
 }
}
/*!
*Function: configure TS3A5017DR channel
*Input parameters: none
*Return value: None
*/
void Init_TS3A5017DR(void)
{
 // Configure TS3A5017DR IN1 and IN2
 P1DIR |= BIT6 + BIT7; //P3.4 : IN1 ; P3.5 : IN2 set as output
 P1OUT & amp;= ~BIT7; //IN1 = 0
 P1OUT |= BIT6; //IN2 = 1
}
/*!
*Function: LCD segment backlight enable
*Input parameters: none
*Return value: None
*/
void Backlight_Enable(void)
{
 P8DIR |= BIT0;
 P8OUT |= BIT0;
}
/*!
*Function function: main function
*Input parameters: none
*Return value: None
*/
int main()
{
 int i,j;
 WDTCTL = WDTPW + WDTHOLD;//Stop WDT
 Init_TS3A5017DR(); // Configure TS3A5017DR IN1 and IN2
 Init_lcd(); // LCD initialization
 Backlight_Enable(); // Turn on backlight
 LcdGo(1); //Open the LCD module
 LCD_Clear(); // Clear screen
 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. Design an electronic clock (display starting time 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 initialization
void LcdGo(unsigned char Dot); //Turn on or off the LCD
void LcdBlink(unsigned char doit); // Display or hide the display content
void LCD_Clear(void); // Clear the screen
void Init_TS3A5017DR(void); // Configure TS3A5017DR IN1 and IN2
void Backlight_Enable(void); // Turn on the backlight
/* Private functions -------------------------------------------------- -----------*/
/*!
 *Function: configure LCD segment output function
 *Input parameters: none
 *Return value: None
 */
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;
}
/****************************************************** ****************************
Turn LCD on or off
1: open 0: closed
 *************************************************** ***************************/
/*!
 *Function: turn on or off the LCD
 *Input parameters: Dot
 * When Dot is 1: Turn on the LCD display
 * When Dot is 0: turn off the LCD display
 *Return value: None
 */
void LcdGo(unsigned char Dot)
{
    if(Dot==1)
    {
        //Open LCD display
        LCDBCTL0 |= LCDON;
    }
    else if(Dot==0)

    {
        //Close LCD display
        LCDBCTL0 &= ~LCDON;
    }
}
/*!
 *Function: display or hide display content
 *Input parameters: none
 *Return value: None
 */
void LcdBlink(unsigned char doit)
{
    if(doit==0)
    {
        LCDBCTL0 &= ~LCDSON;
    }
    else if(doit==1)
    {
        LCDBCTL0 |= LCDSON;
    }
}
/*!
 *Function: Clear the LCD display content
 *Input parameters: none
 *Return value: None
 */
void LCD_Clear(void)//clear screen
{
    unsigned char index;
    for (index=0; index<12; index + + )
    {
        LCDMEM[index] = 0;
    }
}
/*!
 *Function: configure TS3A5017DR channel
 *Input parameters: none
 *Return value: None
 */
void Init_TS3A5017DR(void)
{
    // Configure TS3A5017DR IN1 and IN2
    P1DIR |= BIT6 + BIT7; //P3.4 : IN1 ; P3.5 : IN2 set as output
    P1OUT & amp;= ~BIT7; //IN1 = 0
    P1OUT |= BIT6; //IN2 = 1
}
/*!
 *Function: LCD segment backlight enable
 *Input parameters: none
 *Return value: None
 */

void Backlight_Enable(void)
{
    P8DIR |= BIT0;
    P8OUT |= BIT0;
}
/*!
 *Function function: main function
 *Input parameters: none
 *Return value: None
 */
int main()
{
    WDTCTL = WDTPW + WDTHOLD;//Stop WDT
    Init_TS3A5017DR(); // Configure TS3A5017DR IN1 and IN2
    Init_lcd(); // LCD initialization
    Backlight_Enable(); // Turn on backlight
    LcdGo(1); //Open the LCD module
    LCD_Clear(); // Clear screen
    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];//The first number displays 2’
        LCDMEM[1] = char_gen[3];//The first number displays 3’
        LCDMEM[2] = char_gen[5];//The first number displays 5’
        LCDMEM[3] = char_gen[9];//The first number displays 9’
        LCDMEM[4] = char_gen[4];//The first number displays 4’
        LCDMEM[5] = char_gen[5];//The first number displays 5’
    }
}

3. Use timer interrupt for timing and modify hours, minutes and seconds. data

#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 initialization
void LcdGo(unsigned char Dot); //Turn on or off the LCD
void LcdBlink(unsigned char doit); // Display or hide the display content
void LCD_Clear(void); // Clear the screen
void Init_TS3A5017DR(void); // Configure TS3A5017DR IN1 and IN2
void Backlight_Enable(void); // Turn on the backlight
/* Private functions -------------------------------------------------- -----------*/
/*!
 *Function: configure LCD segment output function
 *Input parameters: none
 *Return value: None
 */
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;
}
/****************************************************** ****************************
Turn LCD on or off
1: open 0: closed
 *************************************************** ***************************/
/*!
 *Function: turn on or off the LCD
 *Input parameters: Dot
 * When Dot is 1: Turn on the LCD display
 * When Dot is 0: turn off the LCD display
 *Return value: None
 */
void LcdGo(unsigned char Dot)
{
    if(Dot==1)
    {
        //Open LCD display
        LCDBCTL0 |= LCDON;
    }
    else if(Dot==0)

    {
        //Close LCD display
        LCDBCTL0 &= ~LCDON;
    }
}
/*!
 *Function: display or hide display content
 *Input parameters: none
 *Return value: None
 */
void LcdBlink(unsigned char doit)
{
    if(doit==0)
    {
        LCDBCTL0 &= ~LCDSON;
    }
    else if(doit==1)
    {
        LCDBCTL0 |= LCDSON;
    }
}
/*!
 *Function: Clear the LCD display content
 *Input parameters: none
 *Return value: None
 */
void LCD_Clear(void)//clear screen
{
    unsigned char index;
    for (index=0; index<12; index + + )
    {
        LCDMEM[index] = 0;
    }
}
/*!
 *Function: configure TS3A5017DR channel
 *Input parameters: none
 *Return value: None
 */
void Init_TS3A5017DR(void)
{
    // Configure TS3A5017DR IN1 and IN2
    P1DIR |= BIT6 + BIT7; //P3.4 : IN1 ; P3.5 : IN2 set as output
    P1OUT & amp;= ~BIT7; //IN1 = 0
    P1OUT |= BIT6; //IN2 = 1
}
/*!
 *Function: LCD segment backlight enable
 *Input parameters: none
 *Return value: None
 */

void Backlight_Enable(void)
{
    P8DIR |= BIT0;
    P8OUT |= BIT0;
}
/*!
 *Function: main function
 *Input parameters: none
 *Return value: None
 */
int main()
{
    WDTCTL = WDTPW + WDTHOLD;//Stop WDT
    Init_TS3A5017DR(); // Configure TS3A5017DR IN1 and IN2
    Init_lcd(); // LCD initialization
    Backlight_Enable(); // Turn on backlight
    LcdGo(1); //Open the LCD module
    LCD_Clear(); // Clear screen
    LCDMEM[0] = char_gen[2];//The first number displays 2’
    LCDMEM[1] = char_gen[3];//The first number displays 3’
    LCDMEM[2] = char_gen[5];//The first number displays 5’
    LCDMEM[3] = char_gen[9];//The first number displays 9’
    LCDMEM[4] = char_gen[4];//The first number displays 4’
    LCDMEM[5] = char_gen[5];//The first number displays 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); //It acts as a delay and will not jump or display directly.
        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];
            LCDMEM[4] = char_gen[sec/10];
            LCDMEM[3] = char_gen[mun];
            LCDMEM[2] = char_gen[mun/10];
            LCDMEM[1] = char_gen[hou];
            LCDMEM[0] = char_gen[hou/10];
       // LCDMEM[j] = char_gen[j];//Modify this place, hou,mun,sec
            delay_ms(1000);
       }
}