CODE GIAO TIẾP MSP430G2553 VÀ MODULE THỜI GIAN THỰC
BQ32000 HIỂN THỊ LCD
NGUYỄN BỈNH NHÂN
0798186889
/>1. File main. Giao tiếp LCD đã tích hợp vào trong hàm main lun
nha…
//-----------------------------------------------------------------------------//KHAI BAO THU VIEN
#include "msp430g2553.h"
#include "intrinsics.h"
#include "string.h"
#include "stdio.h"
//-----------------------------------------------------------------------------union reg {
struct bit {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
}_BIT;
unsigned char _BYTE;
};
union reg* P2_DIR=(union reg*)0x2a; //khai bao dia chi o nho
union reg* P2_OUT=(union reg*)0x29;
union reg* P2_SEL=(union reg*)0x2e;
union reg* P2_IN=(union reg*)0x28;
union reg* P1_SEL=(union reg*)0x26;
union reg* P1_DIR=(union reg*)0x22; //khai bao dia chi o nho
union reg* P1_OUT=(union reg*)0x21;
union reg* P1_IN=(union reg*)0x20;
//
========================================
========================================
======================================
//KHAI BAO BIEN RTC:
int sec=00,min=00,hour=00,set=0, sophut=0, sogio=0,
sogio1=0, sophut1=0, sogio2=0, sophut2=0, min_caidat=0,
dem=0, temp_caidat_max=0, temp_caidat_min, humi_caidat=0;
unsigned char calen[]={'H','B','T','N','S','B','C'};
unsigned char CH_T1[4], CH_T2[4];
//
========================================
======================================
//DINH NGHIA GIAO TIEP DHT11:
//===dinh nghia chan cho DHT11====:
#define DHT_IN (P1_IN->_BIT.b7)
#define DHT_OUT (P1_OUT->_BIT.b7)
#define DHT_DIR (P1_DIR->_BIT.b7)
//================================
//BIEN DHT11:
unsigned char nhietdo, doam;
//
========================================
======================================
//DINH NGHIA LCD:
#define MCLK_F 1 // frequency of Master Clock in MHz
/
*****************************************************
***********************
* HARD DEFINITIONS
*****************************************************
*************************/
/* Display ON/OFF Control definitions */
#define DON
0x0F /* Display on
*/
#define DOFF
0x0B /* Display off
#define CURSOR_ON
0x0F /* Cursor on
#define CURSOR_OFF 0x0D /* Cursor off
#define BLINK_ON
#define BLINK_OFF
0x0F /* Cursor Blink
*/
*/
*/
*/
0x0E /* Cursor No Blink */
/* Cursor or Display Shift definitions */
#define SHIFT_CUR_LEFT
#define SHIFT_CUR_RIGHT
#define SHIFT_DISP_LEFT
0x04 /* Cursor shifts to the left */
0x05 /* Cursor shifts to the right
*/
0x06 /* Display shifts to the left */
#define SHIFT_DISP_RIGHT 0x07 /* Display shifts to the right
*/
/* Function Set definitions */
#define FOUR_BIT
0x2C /* 4-bit Interface
*/
#define EIGHT_BIT 0x3C /* 8-bit Interface
*/
#define LINE_5X7
0x30 /* 5x7 characters, single line */
#define LINE_5X10 0x34 /* 5x10 characters
*/
#define LINES_5X7 0x38 /* 5x7 characters, multiple line */
//
========================================
======================
// Your YM1602C 16x2 must be supplied with a voltage between
4.5V-5.5V
// However, signal pins can understand 3.3V logic level from
MSP430 as '1'
// Check your hardware connecting and map it to these
definitions below
// Map: LCD PIN <-> Output_Port _ out/dir -> Output_Pin
#define LCD_RS
#define LCD_RS_DIR
#define LCD_EN
#define LCD_EN_DIR
P1_OUT -> _BIT.b0
P1_DIR -> _BIT.b0
P1_OUT -> _BIT.b1
P1_DIR -> _BIT.b1
// This driver just help you to "write" LCD, not to read LCD
"status".
// So that, you must pull your pin LCD_RW to GND (0V).
#define LCD_DATA_4
P1_OUT -> _BIT.b2
#define LCD_DATA_4_DIR P1_DIR -> _BIT.b2
#define LCD_DATA_5
P1_OUT -> _BIT.b3
#define LCD_DATA_5_DIR P1_DIR -> _BIT.b3
#define LCD_DATA_6
P1_OUT -> _BIT.b4
#define LCD_DATA_6_DIR P1_DIR -> _BIT.b4
#define LCD_DATA_7
P1_OUT -> _BIT.b5
#define LCD_DATA_7_DIR P1_DIR-> _BIT.b5
//
========================================
============
/*or another PINS MAPPING TABLE:
#define LCD_RS
P2_out -> _bit.b1
#define LCD_RS_DIR
#define LCD_EN
P2_dir -> _bit.b1
P2_out -> _bit.b2
#define LCD_EN_DIR
P2_dir -> _bit.b2
#define LCD_DATA_4
P2_out -> _bit.b4
#define LCD_DATA_4_DIR P2_dir -> _bit.b4
#define LCD_DATA_5
P2_out -> _bit.b5
#define LCD_DATA_5_DIR P2_dir -> _bit.b5
#define LCD_DATA_6
P1_out -> _bit.b6
#define LCD_DATA_6_DIR P1_dir -> _bit.b6
#define LCD_DATA_7
P1_out -> _bit.b7
#define LCD_DATA_7_DIR P1_dir -> _bit.b7
*/
/****************************************************
************************
* MODULE PRIVATE FUNCTIONS:
* void lcd_delay_us (unsigned long t);
* void lcd_delay_ms (unsigned long t);
* void lcd_put_byte(unsigned char rs, unsigned char data);
*
*****************************************************
*************************/
//
========================================
======================================
//DINH NGHIA I2C:
//I2C PIN CONFIG:
#define SCL
BIT1
#define SDA
BIT0 //
#define P2
P2IN //
#define SCL_1
#define SCL_0
P2OUT |= SCL //
P2OUT &= ~SCL //
#define SCLdir_1 P2DIR |= SCL //
#define SCLdir_0 P2DIR &= ~SCL //
#define SDA_1
#define SDA_0
P2OUT |= SDA //
P2OUT &= ~SDA //
#define SDAdir_1 P2DIR |= SDA //
#define SDAdir_0 P2DIR &= ~SDA //
//
========================================
======================================
//CHUONG TRINH DELAY:
void delay_ms(unsigned int ms){
while(ms--){
__delay_cycles(1000);
}
}
unsigned int value;
//================================
void lcd_delay_us (unsigned long t)
{
int i;
for (i = 0; i
__delay_cycles(MCLK_F);
}
void lcd_delay_ms (unsigned long t)
{
int i;
for (i = 0; i
__delay_cycles(MCLK_F*1000);
}
//
========================================
======================================
//THU VIEN LCD 1602:
//
*****************************************************
************************
// Send a byte of data (rs == 1) or command (rs == 0) to LCD
//
*****************************************************
************************
void lcd_put_byte(unsigned char rs, unsigned char data)
{
LCD_RS = 0;
if(rs) LCD_RS = 1;
lcd_delay_us(20);
LCD_EN = 0;
// send the high nibble
if (data&BIT4) LCD_DATA_4 = 1;
else LCD_DATA_4 = 0;
if (data&BIT5) LCD_DATA_5 = 1;
else LCD_DATA_5 = 0;
if (data&BIT6) LCD_DATA_6 = 1;
else LCD_DATA_6 = 0;
if (data&BIT7) LCD_DATA_7 = 1;
else LCD_DATA_7 = 0;
lcd_delay_us(20);
LCD_EN = 1;
lcd_delay_us(20);
LCD_EN = 0;
// send the low nibble
if (data&BIT0) LCD_DATA_4 = 1;
else LCD_DATA_4 = 0;
if (data&BIT1) LCD_DATA_5 = 1;
else LCD_DATA_5 = 0;
if (data&BIT2) LCD_DATA_6 = 1;
else LCD_DATA_6 = 0;
if (data&BIT3) LCD_DATA_7 = 1;
else LCD_DATA_7 = 0;
lcd_delay_us(20);
LCD_EN = 1;
lcd_delay_us(20);
LCD_EN = 0;
}
/****************************************************
************************
* MODULE PUPLIC FUNCTIONS:
* void lcd_init(void)
* void lcd_clear(void)
* void lcd_gotoxy(unsigned char col, unsigned char row)
* void lcd_putc(char c)
* void lcd_puts(const char* s)
* void lcd_clr_puts (const char* s1 , const char* s2)
* void lcd_put_num (unsigned long val, char dec, unsigned char
neg)
*****************************************************
*************************/
//
*****************************************************
************************
// Initialization for LCD module
//
*****************************************************
************************
void lcd_init(void)
{
// Set all signal pins as output
LCD_RS_DIR = 1;
LCD_EN_DIR = 1;
LCD_DATA_4_DIR = 1;
LCD_DATA_5_DIR = 1;
LCD_DATA_6_DIR = 1;
LCD_DATA_7_DIR = 1;
LCD_RS = 0;
LCD_EN = 0;
lcd_delay_ms(200);
// delay for power on
// reset LCD
lcd_put_byte(0,0x30);
lcd_delay_ms(50);
lcd_put_byte(0,0x30);
lcd_delay_ms(50);
lcd_put_byte(0,0x32);
lcd_delay_ms(200);
lcd_delay_ms(2);
// delay for LCD reset
// wait for LCD
lcd_put_byte(0,FOUR_BIT & LINES_5X7);
type
lcd_delay_ms(2);
// wait for LCD
// Set LCD
lcd_put_byte(0,DOFF&CURSOR_OFF&BLINK_OFF);
display off
lcd_delay_ms(2);
// wait for LCD
lcd_put_byte(0,DON&CURSOR_OFF&BLINK_OFF);
display on
lcd_delay_ms(2);
//
//
// wait for LCD
lcd_put_byte(0,0x01);
// clear display and move
cursor to home
lcd_delay_ms(2);
// wait for LCD
lcd_put_byte(0,SHIFT_CUR_LEFT);
mode
lcd_delay_ms(2);
// cursor shift
// wait for LCD
lcd_put_byte(0,0x01);
// clear display and move
cursor to home
lcd_delay_ms(2);
// wait for LCD
}
//
*****************************************************
************************
// Clear the LCD
//
*****************************************************
************************
void lcd_clear(void)
{
lcd_put_byte(0,0x01);
lcd_delay_ms(2);
// display off
// wait for LCD
}
//
*****************************************************
************************
// Move the pointer of LCD to coordinate (column, row)
// Note that cursor is at (0,0) after reset of clear LCD
//
*****************************************************
************************
void lcd_gotoxy(unsigned char col, unsigned char row)
{
unsigned char address;
if(row!=0)
address=0x40;
else
address=0;
address += col;
lcd_put_byte(0,0x80|address);
lcd_delay_ms(2);
// wait for LCD
}
//
*****************************************************
************************
// Put a writable character on LCD
// If the character is '\f', clear the LCD
// If the character is '\n', move cursor to the second line
//
*****************************************************
************************
void lcd_putc(char c)
{
switch(c){
case '\f':
lcd_put_byte(0, 0x01);
lcd_delay_ms(2);
// wait for LCD
break;
case '\n':
lcd_gotoxy(0, 0x01);
break;
default:
lcd_put_byte(1, c);
lcd_delay_ms(2);
// wait for LCD
break;
}
}
//
*****************************************************
************************
// Put a string s[] on LCD.
// Please note that the string is ended with '/0' (or 0x00)
//
*****************************************************
************************
void lcd_puts(const char* s)
{
while(*s){
lcd_putc(*s++);
}
}
//
*****************************************************
************************
// Clear LCD. Put string s1[] on line 1 and string s2[] on line 2
//
*****************************************************
************************
void lcd_clr_puts (const char* s1 , const char* s2)
{
lcd_clear ();
lcd_puts (s1);
lcd_gotoxy (0,1);
lcd_puts (s2);
}
//
*****************************************************
************************
// Put a constant or a number stored in 'val' on LCD
// The number must be multiplied and convert in type "unsigned
long"
//
or "unsigned int"
// dec: amount of digits after '.'
// neg: sign of the number, neg = 0 if the number is positive
//
// Example 1: lcd_put_num(val, 3, 0); // val = 12345
//
will put "12.345" on the LCD
// Example 2: lcd_put_num(val, 6, 1); // val = 12345
//
will put "-0.012345" on the LCD
//
// Note: This function may contain some bugs. If you can detect
and fix them
//
, please contact me :d
//
*****************************************************
************************
void lcd_put_num (unsigned long val, char dec, unsigned char
neg)
{
char i, j, digit,k;
long total;
long temp;
for (i = 0, total = val; total > 0;i++) // count number
total /= 10;
total = i;
if (neg !=0 )
lcd_putc ('-');
if ((total - dec) <=0) // if total < dec put 0.xxx
lcd_putc('0');
else
{
for (i=0; i< (total-dec); i++)
{
temp = 1;
for (j=1;j<(total-i);j++)
temp *=10;
digit = (val/temp)%10;
lcd_putc(digit + 0x30);
}
}
if (dec > 0)
{
lcd_putc('.');
if ( (dec - total)> 0) // ex: val = 55; dec = 3 put 0.055
{
for ( i = 0; i < (dec-total);i++)
lcd_putc('0');
k = 0;
}
else
k = total - dec;
for (i=k; i< total; i++)
{
temp = 1;
for (j=1;j<(total-i);j++)
temp *=10;
digit = (val/temp)%10;
lcd_putc(digit + 0x30);
}
}
}
/
*****************************************************
*************************
* END OF LCD.C
*****************************************************
**************************/