【TI 技术资料分享】msp430+12864

2019-07-30 17:16发布

本帖最后由 胡斯哲 于 2015-3-14 17:48 编辑

12864并口显示图片
  1. #include  <msp430x14x.h>
  2. #include <math.h>

  3. #define  LCD_RS_High   P6OUT|=0x04   // LCD_RS  高电平   显示数据         P62
  4. #define  LCD_RS_Low    P6OUT&=0xfb   // LCD_RS  低电平   指令数据   



  5. #define  LCD_RW_High   P6OUT|=0x10   // LCD_RW  高电平   数据读到DB0_DB7   P64
  6. #define  LCD_RW_Low    P6OUT&=0xef   // LCD_RW  低电平   DB0_DB7 数据写入IR DR


  7. #define  LCD_EN_High   P6OUT|=0x08   // LCD_EN  高电平   高有效            P63
  8. #define  LCD_EN_Low    P6OUT&=0xf7   // LCD_EN  低电平   使能信号

  9. #define LCD_PSB_High   P3OUT|=0x01;  //  并口   高电平
  10. #define LCD_PSB_Low    P3OUT&=0xfe;  //  串口   低电平    P30

  11. //P67 悬空
  12. unsigned char  pic1[]=      // 图片代码
  13. {
  14. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFC,0x00,
  15. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x3F,0xFF,0xBC,0x00,
  16. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFC,0x80,0x00,
  17. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xF8,0x00,0x00,
  18. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xF8,0x10,0x00,
  19. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFC,0x10,0x00,
  20. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xFF,0x5B,0xB0,
  21. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,
  22. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,
  23. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,
  24. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
  25. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
  26. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
  27. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  28. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  29. 0x00,0x00,0x00,0x00,0x18,0x30,0x00,0x00,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  30. 0x00,0x00,0x00,0x00,0x10,0x30,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  31. 0x00,0x00,0x00,0x00,0x30,0x20,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  32. 0x00,0x00,0x00,0x03,0xFC,0xF0,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  33. 0x00,0x00,0x00,0x00,0x40,0x20,0x00,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  34. 0x00,0x00,0x00,0x00,0x90,0x27,0xC0,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  35. 0x00,0x00,0x00,0x01,0x93,0xFC,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  36. 0x00,0x00,0x00,0x01,0x10,0x20,0x00,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  37. 0x00,0x00,0x00,0x03,0xFC,0x3C,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  38. 0x00,0x00,0x00,0x00,0x10,0x06,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  39. 0x00,0x00,0x00,0x00,0x10,0x84,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  40. 0x00,0x00,0x00,0x03,0xFC,0x2C,0x00,0x00,0x00,0x37,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  41. 0x00,0x00,0x00,0x00,0x10,0x38,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  42. 0x00,0x00,0x00,0x00,0x10,0x10,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  43. 0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  44. 0x00,0x00,0x00,0x00,0x10,0x06,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
  45. 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46. 0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
  47. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
  48. 0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
  49. 0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
  50. 0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x3F,0x0F,0xFF,0xFF,0xFF,0xBF,
  51. 0x00,0x00,0x00,0x00,0x00,0x0F,0xE0,0x00,0x00,0x00,0x7F,0x4F,0xFF,0xFF,0xFF,0xFF,
  52. 0x00,0x00,0x00,0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0xFF,0x0F,0xFF,0xFF,0xFF,0xFF,
  53. 0x00,0x00,0x00,0x00,0x00,0x3F,0xFC,0x00,0x00,0x00,0x7F,0x0F,0xFF,0xFF,0xFF,0xBF,
  54. 0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0xFF,0x07,0xFF,0x7F,0xFF,0xFF,
  55. 0x00,0x00,0x00,0x00,0x03,0xFF,0xFE,0x00,0x00,0x00,0x7F,0x01,0xCF,0x7F,0xFF,0xFF,
  56. 0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x01,0xFF,0x10,0x00,0xFF,0xFF,0xFF,
  57. 0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0x00,0x00,0x00,0xFF,0x03,0x87,0xFF,0xFF,0xFF,
  58. 0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0x80,0x00,0x01,0xFF,0x1F,0xC7,0xFF,0xFF,0xFF,
  59. 0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0x80,0x00,0x03,0xFD,0x1F,0xC7,0xFF,0xFF,0xFF,
  60. 0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0x80,0x00,0x01,0xFC,0x3F,0xFF,0xFF,0xFF,0xFF,
  61. 0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xC0,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  62. 0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0x80,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  63. 0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xC0,0x00,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  64. 0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  65. 0x00,0x00,0x00,0x00,0x00,0x01,0x3F,0xC0,0x10,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  66. 0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0x02,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  67. 0x00,0x00,0x00,0x00,0x00,0x00,0x47,0xFF,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  68. 0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  69. 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  70. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  71. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  72. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  73. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  74. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  75. 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  76. 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  77. 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,

  78. };

  79. void delay_ms(unsigned int timer)
  80. {
  81.   unsigned int i;
  82.     for(i=0;i<1000;i--)
  83.        for(;timer>0;timer--)
  84.          ;
  85. }


  86. unsigned char lcd_busy()
  87. {                          
  88.     unsigned char result;
  89.    
  90.      P4DIR=0x00;   
  91.      LCD_RS_Low;          //  指令数据
  92.      LCD_RW_High;
  93.      LCD_EN_High;
  94.         

  95.      result = P4IN&0x80;   // 读取数据口数据
  96.      LCD_EN_Low;
  97.      
  98.      return(result);
  99. }
  100. /*写命令到液晶*/
  101. void LCD_WCMD(unsigned char cmd)
  102. {                          
  103.   while(lcd_busy());

  104.      P4DIR=0xff;          // OUT;
  105.      LCD_RS_Low;          //  指令数据
  106.      LCD_RW_Low;
  107.   //   LCD_EN_Low;
  108.      LCD_EN_High;
  109.      P4OUT = cmd;
  110.    

  111.      LCD_EN_Low;
  112. }
  113. /*写数据到液晶*/
  114. void LCD_WDAT(unsigned char dat)
  115. {                          
  116.    while(lcd_busy());
  117.    
  118.      P4DIR=0xff;           // OUT;
  119.      LCD_RS_High;          //  数据
  120.      LCD_RW_Low;
  121.   //   LCD_EN_Low;
  122.      
  123.      LCD_EN_High;
  124.      P4OUT = dat;
  125.      
  126.    
  127.      LCD_EN_Low;
  128. }
  129. unsigned char ReadByte(void)
  130. {
  131.     unsigned char byReturnValue ;
  132.     while(lcd_busy());
  133.     P4DIR=0xff;
  134.     P4OUT = 0xff;
  135.     P4DIR=0x00;
  136.     LCD_RS_High; ;
  137.     LCD_RW_High; ;
  138.     LCD_EN_Low ;
  139.     LCD_EN_High ;
  140.     byReturnValue=P4IN ;
  141.     LCD_EN_Low ;
  142.    
  143.     return byReturnValue ;
  144. }

  145. /*显示的字符串*/
  146. void Display(unsigned char X,unsigned char Y,unsigned char *string)
  147. {

  148.    if (X==0)
  149.      {X=0x80;}
  150.    else if (X==1)
  151.      {X=0x90;}
  152.    else if (X==2)
  153.      {X=0x88;}
  154.    else if (X==3)
  155.      {X=0x98;}

  156.      LCD_WCMD(X+Y);
  157.    
  158.    while(*string )
  159.     {                         //显示字符
  160.       LCD_WDAT(*string++);
  161.     }

  162. }
  163. void Display_number(unsigned char X,unsigned char Y,unsigned char number)
  164. {
  165.     if (X==0)
  166.      {X=0x80;}
  167.    else if (X==1)
  168.      {X=0x90;}
  169.    else if (X==2)
  170.      {X=0x88;}
  171.    else if (X==3)
  172.      {X=0x98;}

  173.      LCD_WCMD(X+Y);
  174.      LCD_WDAT(48+number);
  175. }
  176. /*时钟初始化*/
  177. void Clock_init()
  178. {
  179.    unsigned char t;
  180.    BCSCTL1 = RSEL0 + RSEL1 + RSEL2;         // XT2on, max RSEL
  181.    BCSCTL1&=~XT2OFF;                        //打开XT2振荡器
  182.    do
  183.    {
  184.       IFG1 &= ~OFIFG;                       // 清除振荡器失效标志
  185.       for (t = 255; t > 0; t--);            // 延时,等待XT2起振
  186.    }
  187.    while ((IFG1 & OFIFG) != 0);             // 判断XT2是否起振
  188.    
  189.    BCSCTL2 |= SELS+SELM_2;                  // SMCLK = XT2
  190. }
  191. /*液晶初始化*/
  192. void LCD_init()
  193. {
  194.    LCD_WCMD(0x34);      //扩充指令操作
  195.    LCD_WCMD(0x30);      //基本指令操作
  196.    LCD_WCMD(0x0C);      //显示开,关光标
  197.    LCD_WCMD(0x01);      //清除LCD的显示内容
  198.       
  199. }

  200. /*------------------------------------------------
  201.                       清屏
  202. ------------------------------------------------*/
  203. void ClrScreen()
  204. {
  205.    LCD_WCMD(0x01);
  206.    delay_ms(15);
  207. }
  208. /*------------------------------------------------
  209.                  设置到绘图模式
  210. ------------------------------------------------*/
  211. void SetGraphicMode()
  212. {
  213.    LCD_WCMD(0x36);       //选择8bit数据流 图形模式
  214.    delay_ms(20);

  215. }
  216. /*------------------------------------------------
  217.                    显示图片
  218. ------------------------------------------------*/
  219. void LCD_PutGraphic(unsigned char  *img)
  220. {
  221.    int i,j;
  222. //显示上半屏内容设置
  223.    for(i=0;i<32;i++)            
  224.     {
  225.       LCD_WCMD(0x80 + i); //SET  垂直地址 VERTICAL ADD
  226.       LCD_WCMD(0x80);     //SET  水平地址 HORIZONTAL ADD
  227.       for(j=0;j<16;j++)
  228.        {
  229.          LCD_WDAT(*img);
  230.          img++;
  231.        }
  232.     }
  233. //显示下半屏内容设置
  234.         for(i=0;i<32;i++)            
  235.     {
  236.             LCD_WCMD(0x80 + i); //SET 垂直地址 VERTICAL ADD
  237.             LCD_WCMD(0x88);     //SET 水平地址 HORIZONTAL ADD
  238.             for(j=0;j<16;j++)
  239.        {
  240.                        LCD_WDAT(*img);
  241.                        img++;
  242.        }
  243.     }  
  244.            
  245. }
  246. /*-------------------------------------------------------------------
  247.                                 增加画点子程序
  248. -------------------------------------------------------------------*/
  249. void DrawPoint(unsigned char X,unsigned char Y,unsigned char Color)
  250. {
  251.     unsigned char Row,Tier,Tier_bit ;
  252.     unsigned char ReadOldH,ReadOldL ;
  253.     LCD_WCMD(0x34);
  254.     LCD_WCMD(0x36);
  255.     Tier=X>>4 ;
  256.     Tier_bit=X&0x0f ;
  257.     if(Y<32)
  258.     {
  259.         Row=Y ;
  260.     }
  261.     else
  262.     {
  263.         Row=Y-32 ;
  264.         Tier+=8 ;
  265.     }
  266.     LCD_WCMD(Row+0x80);
  267.     LCD_WCMD(Tier+0x80);
  268.     ReadByte();
  269.     ReadOldH=ReadByte();
  270.     ReadOldL=ReadByte();
  271.     LCD_WCMD(Row+0x80);
  272.     LCD_WCMD(Tier+0x80);
  273.     if(Tier_bit<8)
  274.     {
  275.         switch(Color)
  276.         {
  277.             case 0 :
  278.             ReadOldH&=(~(0x01<<(7-Tier_bit)));
  279.             break ;
  280.             case 1 :
  281.             ReadOldH|=(0x01<<(7-Tier_bit));
  282.             break ;
  283.             case 2 :
  284.             ReadOldH^=(0x01<<(7-Tier_bit));
  285.             break ;
  286.             default :
  287.             break ;
  288.         }
  289.         LCD_WDAT(ReadOldH);
  290.         LCD_WDAT(ReadOldL);
  291.     }
  292.     else
  293.     {
  294.         switch(Color)
  295.         {
  296.             case 0 :
  297.             ReadOldL&=(~(0x01<<(15-Tier_bit)));
  298.             break ;
  299.             case 1 :
  300.             ReadOldL|=(0x01<<(15-Tier_bit));
  301.             break ;
  302.             case 2 :
  303.             ReadOldL^=(0x01<<(15-Tier_bit));
  304.             break ;
  305.             default :
  306.             break ;
  307.         }
  308.         LCD_WDAT(ReadOldH);
  309.         LCD_WDAT(ReadOldL);
  310.     }
  311.     LCD_WCMD(0x30);
  312. }

  313. //----------------------------------------------
  314. //                        清整个GDRAM空间
  315. //-----------------------------------------------
  316. void clrgdram()
  317. {
  318.     unsigned char x,y ;
  319.     for(y=0;y<64;y++)
  320.     for(x=0;x<16;x++)
  321.     {
  322.         LCD_WCMD(0x34);
  323.         LCD_WCMD(y+0x80);
  324.         //行地址
  325.         LCD_WCMD(x+0x80);
  326.         //列地址
  327.         LCD_WCMD(0x30);
  328.         LCD_WDAT(0x00);
  329.         LCD_WDAT(0x00);
  330.     }
  331. }
  332. void  main( void )
  333. {
  334. //char i;
  335. unsigned char x1,y1,colour=1 ;
  336.   WDTCTL = WDTPW + WDTHOLD;     // Stop watchdog timer to prevent time out reset
  337.   Clock_init();                 // 时钟初始化

  338.   P6DIR=0x1c;
  339.   P3DIR=0x01;
  340.   LCD_PSB_High;                // 液晶显示  并口方式  
  341.   
  342.   LCD_init();
  343. /*-----------------------------------------------------------------------------
  344.                               显示字符
  345. -----------------------------------------------------------------------------*/
  346.   /*Display(0,0,"电子园");
  347.   Display(1,0,"www.cepark.com");
  348.   Display(2,1,"2013年06月4日");
  349.   Display(3,5,"numen");        // 第四行要显示的内容
  350.   */
  351.   /*-----------------------------------------------------------------------------
  352.                               显示图片
  353. -----------------------------------------------------------------------------*/
  354. /* ClrScreen();                                                  //清屏
  355.   SetGraphicMode();                                             //设置成绘图模式
  356.   LCD_PutGraphic(pic1);                                         //调入一幅图画
  357.   for(i=0;i<20;i++)
  358.   delay_ms(200);*/
  359.   /*----------------------------------------------
  360.                         打点程序
  361. ----------------------------------------------*/
  362.   clrgdram();
  363.   ClrScreen();
  364.   for(x1=0;x1<128;x1++)
  365.   {
  366.       y1=10*sin(0.314*x1)+20; //T=2pi/w
  367.       DrawPoint(x1,y1,colour);
  368.         
  369.   }
  370.   delay_ms(5000);
  371.   _BIS_SR(LPM0_bits);

  372. }
复制代码
0条回答

一周热门 更多>