基于MSP430F149的應(yīng)用實例
#include
#include
#define RS 0x01//寄存器選擇信號(1--Command, 0--data)
#define RW 0x02//讀/寫選擇信號
#define EN 0x04//使能信號
#define BIT_0 0x01
#define BIT_1 0x02
#define BIT_2 0x04
#define BIT_3 0x08
#define BIT_4 0x10
#define BIT_5 0x20
#define BIT_6 0x40
#define BIT_7 0x80
#define START 0x01 //CPLD 選擇
#define RD 0x02
#define A0 0x04
#define A1 0x08
#define CO1 0x10
#define CO2 0x20
#define RES 0x40
#define DATA_DIS_TIME 4 //測量完成后,數(shù)據(jù)停留顯示4秒,然后清除,顯示Ready
//{0x00,0x00,0x00,0x1F,0x08,0x04,0x04,0x00},/*"7",0*/
/*-----------建立自定義演示用字符庫-----------*/
const unsigned int CGTAB[8][8]={0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02, /* "年""代碼=0 */
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00, /* "月"代碼=1 */
0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00, /* "日"代碼=2 */
0x11,0x0a,0x04,0x1f,0x04,0x1f,0x04,0x00, /* "¥"代碼=3 */
0x0e,0x00,0x1f,0x0a,0x0a,0x0a,0x13,0x00, /* "元"代碼=4 */
0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00, /* "℃"代碼=5 */
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE, /* "滾動條"代碼=6 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; /* "空白條"代碼=7 */
unsigned char gb_Flag,ReceiveData;
unsigned char StopFlag;
unsigned char gb_Time,gb_cnt,gb_zi;
unsigned int gb_Timer1s;
unsigned char gb_Timer500ms;
unsigned long int MeasureData,wDat0,wDat1;
unsigned char ui,udata[4];
unsigned char ReadyMeaFlag;
union FtoByte{
float a;
unsigned char b[4];
}fx;
void DisChar(unsigned char Dt);
void DisUartChar(void);
//extern void write_eeprom(unsigned char ctrl_addres,unsigned char addres,unsigned char value);
//extern unsigned char read_eeprom(unsigned char ctrl_addres,unsigned char addres);
//extern void IIC_Send(unsigned char sDATA);
//extern unsigned char IIC_Recive(void);
extern void RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
unsigned char ControlByte);
/*串口0接受中斷響應(yīng)程序*/
interrupt [0x12] void Uart0(void)
{
ReceiveData=U0RXBUF; //接受數(shù)據(jù)
IFG1=0x00; //清除中斷標(biāo)志位
fx.b[ui]=ReceiveData;
ui++;
if(ui>3)
{ DisUartChar(); //顯示接收到的字符
ui=0;
}
}
/*Timer_A0捕獲下降沿中斷響應(yīng)程序*/
interrupt [0x0C] void Time_A0(void)
{
CCTL2&=0xFFFE;
if(gb_Flag&BIT_2)
{
gb_Flag&=~BIT_2; //去抖動
gb_Flag|=BIT_1; //有按鍵按下
}
}
/*WDT 定時中斷響應(yīng)程序(定時:250ms)*/
interrupt [0x14] void WDT(void)
{
if((gb_Flag&BIT_7)==BIT_7)
{
if(++gb_Timer1s==4)
{
gb_Timer1s=0;
gb_Flag|=BIT_0;
}
}
else
{
if(++gb_Timer500ms==2)
{
gb_Timer500ms=0;
gb_Flag|=BIT_1;
}
if((gb_Flag&BIT_6)==BIT_6) //顯示測量數(shù)據(jù)
if(++gb_Timer1s==DATA_DIS_TIME*4) { gb_Flag&=~BIT_6; gb_Flag|=BIT_5; }
if((gb_Flag&BIT_4)==BIT_4) //測量中,有鍵按下
if(++gb_Timer1s==8) { gb_Flag&=~BIT_4; StopFlag=1; }
}
//if(++gb_cnt ==6) { gb_cnt=0; P2OUT=gb_zi; gb_zi=~gb_zi;}} //一秒定時到
//if((++gb_Time)>25) { gb_Flag|=BIT_2; gb_Time=0; } /*去抖動*/
}
/*向LCD寫指令函數(shù)*/
void WriteCom(unsigned char ch)
{
unsigned char busy;
P2DIR=0xFF;
P3DIR|=0x0F;
P3OUT&=~RS; /*RS=0*/
P3OUT|=RW; /*R/W=1 */
//P2OUT=0xFF;
P2DIR=0x00;
//P3OUT|=EN; /*EN=1 */
do{
P3OUT|=EN; /*EN=1 */
busy=P2IN;
P3OUT&=~EN; /*EN=0 */
busy&=0x80;
//P3OUT&=~EN; /*EN=0 */
}while(busy==0x80);
P3OUT&=~RW; /*R/W=0*/
P2DIR=0xFF;
P2OUT=ch; //Write command
P3OUT|=EN; /*EN=1 */
P3OUT&=~EN; /*EN=0 */
}
void WriteDat(unsigned char ch) /*向LCD寫數(shù)據(jù)函數(shù)*/
{
unsigned char busy;
//P2DIR=0xFF;
P3DIR|=0x0F;
P3OUT&=~RS; /*RS=0*/
P3OUT|=RW; /*R/W=1 */
//P2OUT=0xFF;
P2DIR=0x00;
//P3OUT|=EN; /*EN=1 */
do{
P3OUT|=EN; /*EN=1 */
busy=P2IN;
P3OUT&=~EN; /*EN=0 */
busy&=0x80;
//P3OUT|=EN; /*EN=1 */
//P3OUT&=~EN; /*EN=0 */
}while(busy==0x80);
P3OUT|=RS; /*RS=1, Write data*/
P3OUT&=~RW; /*R/W=0*/
P2DIR=0xFF;
P2OUT=ch;//0x62; //Write into data
P3OUT|=EN; /*EN=1 */
P3OUT&=~EN; /*EN=0 */
}
void InitHd44780(void)
{
unsigned char i,j;
P2DIR=0xFF;
P3DIR|=0x0F;
P2OUT=0x30;
//WriteCom(0x30);
P3OUT&=~RS; /*RS=0*/
P3OUT&=~RW; /*R/W=0 */
for(i=0;i<3;i++)
{
//P2OUT=0x30;
P3OUT|=EN; /*EN=1 */
P3OUT&=~EN; /*EN=0 */
for(j=0;j<100;j++);
}
//WriteCom(0x38); //set run mode,8 bit data-port(no 4 bit)
P2OUT=0x38;
P3OUT|=EN; /*EN=1 */
P3OUT&=~EN; /*EN=0 */
WriteCom(0x38); //set run mode,8 bit data-port(no 4 bit)
WriteCom(0x01); //Clr Screen
WriteCom(0x06); //Set input mode
WriteCom(0x0C); //set displa mode
}
/**************************/
/* 建立自定義函數(shù) */
/**************************/
void CGRAM(void)
{
unsigned char i,j;
WriteCom(0x40);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
WriteDat(CGTAB[i][j]);
}
}
}
/*void Uart0Init(void)
{
//P3DIR=0x10; //發(fā)送端為輸出,接受端為輸入 P3.4---TX0; P3.5---RX0
//P3SEL=0xF0; //高4位是外部功能接口
P3SEL |= 0x30; // 將P3.4,5使用外圍模塊 = USART0 TXD/RXD
P3DIR |= 0x10; // 將P3.4設(shè)為輸出(發(fā)),P3.5默認(rèn)為輸入(收)
U0CTL=0x10; //8 bit數(shù)據(jù)
U0MCTL=0x6D; //2400--0x6D, 9600--0x4A, 4800--0xEF
U0TCTL=0x10; //波特率時鐘源采用ACLK
U0RCTL=0x00;
U0BR0=0x0D; //2400--x0D, 9600--0x03, 4800--0x06
U0BR1=0x00;
ME1=0xC0; //發(fā)送、接受允許
IE1=0x40; //接受中斷允許
}*/
void Uart0Init(void)
{
//WDTCTL = WDTPW + WDTHOLD; // 停止WDT
UCTL0 = CHAR; // 設(shè)串口控制寄存器,設(shè)為8位字符格式
UTCTL0 = SSEL0; // 設(shè)串口控制寄存器所使用的時鐘,選擇UCLK = ACLK
UBR00 = 0x03; // 波特率設(shè)置32k/2400 - 13.65 0x0D
UBR10 = 0x00;
UMCTL0 = 0x4A; // 波特率調(diào)整器設(shè)置 0x6B
ME1 |= UTXE0 + URXE0; // 模塊允許寄存器設(shè)置,使能USART0 TXD/RXD
IE1 |= URXIE0; // 中斷允許寄存器設(shè)置,接收中斷允許
P3SEL |= 0x30; // 將P3.4,5使用外圍模塊 = USART0 TXD/RXD
P3DIR |= 0x10; // 將P3.4設(shè)為輸出(發(fā)),P3.5默認(rèn)為輸入(收)
_EINT(); // 全局中斷使能
}
void SendData(void)
{
unsigned char i;
unsigned char zd[5];
for(i=0;i<5;i++)
{
zd[i]=0x50+i;
while ((IFG1 & UTXIFG0) == 0); // USART0發(fā)送UTXIFG0=1,表示UTXBUF準(zhǔn)備好發(fā)送一下字符
TXBUF0 =zd[i]; // 將收到緩沖區(qū)字符送發(fā)送區(qū)
}
}
void InitMcu430(void)
{
unsigned char ti;
//_BIS_SR(OSCOFF); //Disable LFXT1
BCSCTL1&=~XT2OFF; // XT2OFF=0
//BCSCTL1|=XTS;
BCSCTL2|=SELS+SELM1;
do{
IFG1&=~OFIFG; //Clear OscFault flag
for(ti=255;ti>1;ti--);
}while((IFG1 & OFIFG)==OFIFG);
for(ti=255;ti>1;ti--);
IFG1&=~OFIFG;
//SCTL1&=~XTS;
//BCSCTL1|=0x30; //8分頻
BCSCTL1&=0x8F; //1分頻, ACLK=32768HZ
// ------------------------Setup Timer_A----------------------
TACTL=0x01C4; //Timer_A控制寄存器:連續(xù)模式,輸入時鐘8分頻,時鐘源為ACLK,Timer暫停
CCTL0=0x8110; //捕獲/比較控制寄存器0:捕獲模式,下降沿捕獲,異步捕獲,選CCI0A
CCTL2=0x4110; //捕獲/比較控制寄存器2:捕獲模式,上升沿捕獲,異步捕獲,選CCI2A
CCR2=0;
// -----------------------------------------------------------
//WDTCTL = WDTPW+0x0019; //500ms timer,MCLK
WDTCTL =WDT_ADLY_250; //ACLK
P1DIR=0x00; //set P1 input
P2DIR=0xFF; //set P2,P3,P5 is output
P3DIR|=0x0F;
P4DIR=0xFF;
P5DIR=0xFF;
P1SEL=0x00;
P2SEL=0x00;
P3SEL=0x00;
P4SEL=0x00;
P5SEL=0x00;
P6SEL=0xFF; //P6 外部模塊功能
P1SEL|=0x0A; //P1.3 外部模塊功能
IE1=0x01; // WDTIE Enable
gb_Timer1s=0;
gb_Timer500ms=0;
gb_Flag=0;
gb_Time=0;
gb_cnt=0;
gb_zi=0x55;
ui=0;
_EINT();
}
void DisChar(unsigned char Dt)
{
unsigned char Dx[2],i;
Dx[0]=Dt/10;
Dx[1]=Dt%10;
WriteCom(0x86); //set CGRAM address(0x80-0x8F :First line, 0xc0-0xCF :Second line)
for(i=0;i<2;i++)
{
WriteDat(0x30+Dx[i]);
}
}
void DisCharzz(unsigned char Dt)
{
unsigned char Dx[2],i;
Dx[0]=Dt/10;
Dx[1]=Dt%10;
WriteCom(0x80); //set CGRAM address(0x80-0x8F :First line, 0xc0-0xCF :Second line)
for(i=0;i<2;i++)
{
WriteDat(0x30+Dx[i]);
}
}
void DisUartChar(void)
{
char s2[16],i;
i=0;
WriteCom(0xC0); //set CGRAM address(0x80-0x8F :First line, 0xc0-0xCF :Second line)
sprintf(s2,"%3.2f",fx.a); //Y_1210
while(s2[i]!=