標題: MSP430單片機控制LCD12864顯示文字 [打印本頁]

作者: wendi    時間: 2013-7-11 03:08
標題: MSP430單片機控制LCD12864顯示文字
#include  <msp430x14x.h>
#include "displaydata.h"
typedef unsigned char uchar;
typedef unsigned int  uint;

//控制位的宏定義
#define BIT(x)  (1 << (x))
#define RCLK_1    P3OUT |= BIT3
#define RCLK_0    P3OUT &= ~BIT3
#define cyCS      0    //P3.0,片選信號
#define cySID     1    //P3.1,串行數(shù)據(jù)  
#define cyCLK     2    //P3.2,同步時鐘
#define cyPORT    P3OUT  
#define cyDDR     P3DIR
extern const unsigned char shuzi_table[];
uchar flag = 0;
void Send(unsigned char type,unsigned char transdata);
void Ini_Lcd(void);
void Clear_GDRAM(void);
void Disp_HZ(unsigned char addr,const unsigned char * pt,unsigned char num);
void Draw_PM(const unsigned char *ptr);
void Draw_TX(unsigned char Yaddr,unsigned char Xaddr,const unsigned char * dp) ;
void Disp_SZ(unsigned char addr,unsigned char shuzi);



/*******************************************
函數(shù)名稱:delay_Nus
功    能:延時N個us的時間
參    數(shù):n--延時長度
返回值  :無
********************************************/
void delay_Nus(uint n)
{
    uchar i;
    for(i = n;i > 0;i--)
        _NOP();
}
/*******************************************
函數(shù)名稱:delay_1ms
功    能:延時約1ms的時間
參    數(shù):無
返回值  :無
********************************************/
void delay_1ms(void)
{
    uchar i;
    for(i = 150;i > 0;i--)     _NOP();
}  
/*******************************************
函數(shù)名稱:delay_Nms
功    能:延時N個ms的時間
參    數(shù):無
返回值  :無
********************************************/
void delay_Nms(uint n)
{
    uint i = 0;
     
    for(i = n;i > 0;i--)
        delay_1ms();
}
/*******************************************
函數(shù)名稱:Ini_Lcd
功    能:初始化液晶模塊
參    數(shù):無
返回值  :無
********************************************/
void Ini_Lcd(void)
{   
    cyDDR |= BIT(cyCLK) + BIT(cySID) + BIT(cyCS);   //相應(yīng)的位端口設(shè)置為輸出
    delay_Nms(100);                 //延時等待液晶完成復(fù)位
    Send(0,0x30);  /*功能設(shè)置:一次送8位數(shù)據(jù),基本指令集*/
    delay_Nus(72);
    Send(0,0x02);  /*DDRAM地址歸位*/
    delay_Nus(72);
    Send(0,0x0c);  /*顯示設(shè)定:開顯示,不顯示光標,不做當前顯示位反白閃動*/
    delay_Nus(72);
    Send(0,0x01);  /*清屏,將DDRAM的位址計數(shù)器調(diào)整為“00H”*/
    delay_Nus(72);
    Send(0,0x06);  /*功能設(shè)置,點設(shè)定:顯示字符/光標從左到右移位,DDRAM地址加1*/
    delay_Nus(72);      
}  
/*******************************************
函數(shù)名稱:Send
功    能:MCU向液晶模塊發(fā)送1一個字節(jié)的數(shù)據(jù)
參    數(shù):type--數(shù)據(jù)類型,0--控制命令,1--顯示數(shù)據(jù)
          transdata--發(fā)送的數(shù)據(jù)
返回值  :無
********************************************/
void Send(uchar type,uchar transdata)
{
    uchar firstbyte = 0xf8;
    uchar temp;
    uchar i,j = 3;

    if(type) firstbyte |= 0x02;
     
    cyPORT |= BIT(cyCS);            
    cyPORT &= ~BIT(cyCLK);      
    while(j > 0)
    {
        if(j == 3) temp = firstbyte;
        else if(j == 2) temp = transdata&0xf0;
        else  temp = (transdata << 4) & 0xf0;
         
        for(i = 8;i > 0;i--)
        {
            if(temp & 0x80) cyPORT |= BIT(cySID);
            else            cyPORT &= ~BIT(cySID);         
            cyPORT |= BIT(cyCLK);               
            temp <<= 1;
            cyPORT &= ~BIT(cyCLK);
        }
        //三個字節(jié)之間一定要有足夠的延時,否則易出現(xiàn)時序問題
        if(j == 3) delay_Nus(600);
        else       delay_Nus(200);
         
        j--;
    }
     
    cyPORT &= ~BIT(cySID);      
    cyPORT &= ~BIT(cyCS);      
}
/*******************************************
函數(shù)名稱:Clear_GDRAM
功    能:清除液晶GDRAM內(nèi)部的隨機數(shù)據(jù)
參    數(shù):無
返回值  :無
********************************************/
void Clear_GDRAM(void)
{
    uchar i,j,k;
     
    Send(0,0x34);        //打開擴展指令集
    i = 0x80;            
    for(j = 0;j < 32;j++)
    {
        Send(0,i++);      
        Send(0,0x80);  
        for(k = 0;k < 16;k++)
        {
            Send(1,0x00);
        }
    }
    i = 0x80;
    for(j = 0;j < 32;j++)
    {
        Send(0,i++);      
        Send(0,0x88);      
        for(k = 0;k < 16;k++)
        {
            Send(1,0x00);
        }
    }   
    Send(0,0x30);        //回到基本指令集  
}
/*******************************************
函數(shù)名稱:Disp_HZ
功    能:顯示漢字程序
參    數(shù):addr--顯示位置的首地址
          pt--指向顯示數(shù)據(jù)的指針
          num--顯示數(shù)據(jù)的個數(shù)
返回值  :無
********************************************/
void Disp_HZ(uchar addr,const uchar * pt,uchar num)
{
    uchar i;
         
    Send(0,addr);         
    for(i = 0;i < (num*2);i++)
       Send(1,*(pt++));
}
/*******************************************
函數(shù)名稱:Draw_PM
功    能:在整個屏幕上畫一個圖片
參    數(shù):ptr--指向保存圖片位置的指針
返回值  :無
********************************************/
void Draw_PM(const uchar *ptr)
{
    uchar i,j,k;
     
    Send(0,0x34);        //打開擴展指令集
    i = 0x80;            
    for(j = 0;j < 32;j++)
    {
        Send(0,i++);      
        Send(0,0x80);  
        for(k = 0;k < 16;k++)
        {
            Send(1,*ptr++);
        }
    }
    i = 0x80;
    for(j = 0;j < 32;j++)
    {
        Send(0,i++);      
        Send(0,0x88);      
        for(k = 0;k < 16;k++)
        {
            Send(1,*ptr++);
        }
    }
    Send(0,0x36);               //打開繪圖顯示
    Send(0,0x30);               //回到基本指令集   
}
/*******************************************
函數(shù)名稱:Draw_TX
功    能:在液晶上描繪一個16*16的圖形
參    數(shù):Yaddr--Y地址,
          Xaddr--X地址
          dp--指向保存圖形數(shù)據(jù)的指針
返回值  :無
********************************************/
void Draw_TX(uchar Yaddr,uchar Xaddr,const uchar * dp)
{
    uchar j;
    uchar k = 0;

    Send(0,0x34);           //使用擴展指令集,關(guān)閉繪圖顯示
    for(j = 0;j < 16;j++)
    {
        Send(0,Yaddr++);     //Y地址  
        Send(0,Xaddr);       //X地址
        Send(1,dp[k++]);     //送兩個字節(jié)的顯示數(shù)據(jù)
        Send(1,dp[k++]);               
    }
    Send(0,0x36);           //打開繪圖顯示
    Send(0,0x30);           //回到基本指令集模式
}
/*******************************************
函數(shù)名稱:Disp_SZ
功    能:顯示一個兩位數(shù)字
參    數(shù):addr--顯示地址
          數(shù)字--顯示的數(shù)字
返回值  :無
********************************************/
void Disp_SZ(uchar addr,uchar shuzi)
{
    uchar tmp0,tmp1;
     
    tmp0 = shuzi / 10;
    tmp1 = shuzi % 10;  
     
    Send(0,addr);
    Send(1,shuzi_table[tmp0]);
    Send(1,shuzi_table[tmp1]);
}
/************************主函數(shù)***********************/
void main(void)
{
    uchar i,j;
     
    WDTCTL = WDTPW + WDTHOLD;            //關(guān)閉看門狗
     
    TACTL = TASSEL_1 + MC_1;             //計數(shù)時鐘ACLK, 增計數(shù)模式
    CCR0 = 32768 - 1;
    CCTL0 |= CCIE;                       //使能CCR0比較中斷
    _EINT();
    Ini_Lcd();
    while(1){
              Disp_HZ(0x81,line1,6);
              Disp_HZ(0x8a,line2,4);
              flag = 0;
              while(flag < 3);                    //等待三秒鐘
              Send(0,0x01);                       //清屏
              CCR0 = (32768 - 1) / 5;
              flag = 0;
              for(i = 0; i < 4; i++)
              {
                  Send(0, 0x0f);                  //打開游標
                  switch(i)
                  {
                  case 0: Send(0,0x81);break;
                  case 1: Send(0,0x91);break;
                  case 2: Send(0,0x89);break;
                  case 3: Send(0,0x99);break;
                  }
                  for(j = 0; j < 12; j++)
                  {
                      Send(1, shige[i*12 + j]);
                      if(j == 11)
                          Send(0, 0x0c);                    //關(guān)閉游標
                      flag = 0;
                      while(!flag);                         //等待1/5秒         
                  }   
               }
              CCR0 = 32768 - 1;
              flag = 0;
              while(flag < 2);                    //等待2秒鐘
              Send(0,0x01);                       //清屏
              Draw_PM(school);                    //顯示文字型畫面            
              flag = 0;
              while(flag < 3);                    //等待三秒鐘
              Draw_PM(QQ);                        //顯示QQ形象
              flag = 0;
              while(flag < 3);                    //等待三秒鐘
              Send(0,0x01);                       //清屏
              Disp_HZ(0x92,jieshu,4);             //結(jié)束文字   
   
    }
}
/*******************************************
函數(shù)名稱:TimerA_ISR
功    能:定時器A的中斷服務(wù)函數(shù)
參    數(shù):無
返回值  :無
********************************************/
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA_ISR(void)
{
    flag++;   
}

作者: jlzhao1_10    時間: 2013-7-17 23:03
#include "displaydata.h"
這個在哪?  




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1