Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.74 MB, 44 trang )
Hình 12: Khối xử lý
Hình 13: Khối hiển thị
3.2, Code Chương Trình Hiển Thị Điện Áp và Tần Số lên LCD
LCD.h
#define delay_us(x) __delay_cycles(x*2ul)
#define delay_ms(x) __delay_cycles(x*1100ul)
typedef unsigned char uchar;
union reg
{ unsigned char _byte;
//khai bao 1byte
struct bit
24
{
//dinh nghia bit trong byte vua khai bao
unsigned char b0:1; //bit 0 trong byte
unsigned char b1:1; //bit 1 trong byte
unsigned char b2:1; //bit 2 trong byte
unsigned char b3:1; //bit 3 trong byte
unsigned char b4:1; //bit 4 trong byte
unsigned char b5:1; //bit 5 trong byte
unsigned char b6:1; //bit 6 trong byte
unsigned char b7:1; //bit 7 trong byte
} _bit;
};
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;
/********************** I/O LCD ******************************/
#define LCD_RS
P2_out->_bit.b5
#define LCD_EN
P2_out->_bit.b4
25
#define LCD_D4
P2_out->_bit.b3
#define LCD_D5
P2_out->_bit.b0
#define LCD_D6
P2_out->_bit.b1
#define LCD_D7
P2_out->_bit.b2
LCD.c
#include "LCD.h"
/***************************************************************************/
void LCD_Enable(void)
{
LCD_EN=1;
delay_us(3);
LCD_EN=0;
delay_us(50);
}
//Ham Gui 4 Bit Du Lieu Ra LCD
void LCD_Send4Bit( unsigned char Data )
{
LCD_D4=Data&0x01;
LCD_D5=(Data>>1)&1;
LCD_D6=(Data>>2)&1;
LCD_D7=(Data>>3)&1;
}
26
// Ham Gui 1 Lenh Cho LCD
void LCD_SendCommand (unsigned char command )
{
LCD_Send4Bit ( command >>4 );/* Gui 4 bit cao */
LCD_Enable () ;
LCD_Send4Bit ( command );
/* Gui 4 bit thap*/
LCD_Enable () ;
}
// Ham Khoi Tao LCD
void LCD_Init ( void )
{
LCD_Send4Bit(0x00);
delay_ms(20);
LCD_RS=0;
//output_low(LCD_RW);
LCD_Send4Bit(0x03);
LCD_Enable();
delay_ms(5);
LCD_Enable();
delay_us(100);
LCD_Enable();
LCD_Send4Bit(0x02);
27
LCD_Enable();
LCD_SendCommand( 0x28 ); // giao thuc 4 bit, hien thi 2 hang, ki tu 5x8
LCD_SendCommand( 0x0c);
// cho phep hien thi man hinh
LCD_SendCommand( 0x06 ); // tang ID, khong dich khung hinh
LCD_SendCommand( 0x01 ); // xoa toan bo khung hinh
}
void LCD_Gotoxy(unsigned char x, unsigned char y)
{
unsigned char address;
if(!y)
address = (0x80+x);
else
address = (0xC0+x);
delay_us(1000);
LCD_SendCommand(address);
delay_us(50);
}
// Ham Xoa Man Hinh LCD
void LCD_Clear()
{
LCD_SendCommand(0x01);
delay_ms(10);
28
}
// Ham Gui 1 Ki Tu Len LCD
void LCD_PutChar ( unsigned char Data )
{
LCD_RS=1;
LCD_SendCommand( Data );
LCD_RS=0;
}
void LCD_Puts (unsigned char *s)
{
while (*s)
{
LCD_PutChar(*s);
s++;
}
}
void LCD_PutNumber(long Num)
{
int temp = Num;
int i = 0, c[5];
if (temp != 0)
29
{
while(temp)
{
c[i++] = temp%10;
temp /= 10;
}
while(i) LCD_PutChar(c[--i] + '0');
}
else LCD_PutChar('0');
}
Main.h
/********************** MSP430 Config **************************/
#define
MCLK_F
1
/***************** Describe Function ***************************/
void UART_Init()
{
P1SEL |= BIT1 + BIT2 ;
// Set P1.1 as RXD
P1SEL2 |= BIT1 + BIT2 ;
// Set P1.2 as TXD
//Config
UCA0CTL1 |= UCSSEL_2;
UCA0BR0 = 104;
// 9600
UCA0BR1 = 0;
30
UCA0MCTL |= UCBRS0;
UCA0CTL1 &=~ UCSWRST;
IE2 |= UCA0RXIE;
// **Initialize USCI state machine**
// Enable USCI_A0 RX interrupt
__bis_SR_register(GIE);
}
/****************************************/
/****************************************/
void UART_Put_Char( unsigned char c)
{
while(!(IFG2 & UCA0TXIFG)); // Wait until buffer empty
UCA0TXBUF = c;
// Assign character
}
/****************************************/
void UART_Put_Int(unsigned char i)
{
while(!IFG2 & UCA0TXIFG)
UCA0TXBUF = i;
}
/****************************************/
void UART_Put_String(char* s)
{
while(*s != '\0')
31
{
UART_Put_Char(*s);
s++;
}
}
/****************************************/
void UART_PutNumber(long Num)
{
long temp = Num;
int i = 0, c[10];
if (temp != 0)
{
while(temp)
{
c[i++] = temp%10;
temp /= 10;
}
while(i) UART_Put_Char(c[--i] + '0');
}
else UART_Put_Char('0');
}
32
/***************************************************************/
Main.c
//////////////////////////////////////////////////////
#include "msp430g2553.h"
#include "lcd_1602.c"
#include "main.h"
/////////////////////////////////////////////////////////////////////////////
// define functions
void init_GPIO(void);
void init_ADC(void);
void init_TIMER0(void);
unsigned int vol_value(void);
// define varible
union reg* P1_IFG = (union reg*)0x23;
#define P1IFG_0
P1_IFG->_bit.b0
//////////////////////////////////////////////////////////////////////////////
int ccr0 = 0;
int count= 0;
long t= 0;
long f = 0;
//////////////////////////////////////////////////////////////////////////////
33
void main (void)
{
WDTCTL = WDTPW + WDTHOLD;
// Stop WDT
init_GPIO();
init_ADC();
init_TIMER0();
BCSCTL1 = CALBC1_1MHZ; // Set SMCLK to 1 MHz
DCOCTL = CALDCO_1MHZ;
__bis_SR_register(GIE); //Ngat toan cuc
// ADC10CTL0 |= ENC;
// enable ADC10
//LCD
LCD_Init();
delay_ms(10);
UART_Init();
unsigned int vol;
// khai bao bien dien ap
while(1)
{
LCD_Gotoxy(0,0);
LCD_Puts("Uhd XC:");
LCD_Gotoxy(0,1);
LCD_Puts("F=");
// /*
34