Chinaunix首页 | 论坛 | 博客
  • 博客访问: 551639
  • 博文数量: 156
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1183
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-22 11:42
文章分类

全部博文(156)

文章存档

2015年(67)

2014年(89)

分类: C/C++

2014-11-27 19:43:51


  1. #include<msp430f249.h>
  2. #include<stdbool.h>


  3. #define uchar unsigned char


  4. void delay_1ms();
  5. void delay_10us();


  6. void delay_10us()
  7. {
  8.     unsigned int i =100;
  9.     i--;
  10.     i--;
  11.     i--;
  12.     i--;
  13.     i--;
  14.     i--;
  15. }
  16. void delay_1ms()
  17. {
  18.     uchar i,j;
  19.     for(i=0;i>2;i++)
  20.     {
  21.         for(j=0;j<20;j++);
  22.     }
  23. }


  24. bool lcd_1602_busy()
  25. {
  26.     uchar tmp;
  27.     P1DIR = 0xff;
  28.     P1OUT = 0xff;
  29.     //lcd_1602_RW = 1;
  30.     //lcd_1602_RS = 0;
  31.     P2DIR=0xff;
  32.     P2OUT |=BIT1;//BIT1=0x0002
  33.     P2OUT &=~BIT2;//bit2=0x0004 =0000 0100 ~bit2 =1111 1011
  34.     
  35.     P2OUT |=BIT0;
  36.     delay_10us();
  37.     P1DIR =0x00;
  38.     tmp = P1IN;
  39.     P2OUT &=~BIT0;
  40.     //lcd_1602_EN = 0;

  41.     return (bool)(0x80 & tmp);
  42. }


  43. void lcd_1602_write_cmd(uchar cmd)
  44. {
  45.     while(lcd_1602_busy());
  46.     
  47.     P2DIR=0xff;
  48.     P2OUT &=~BIT1;
  49.     //lcd_1602_RW = 0;
  50.     P2OUT &= ~BIT2;
  51.     //lcd_1602_RS = 0;
  52.     P1DIR =0xff;
  53.     P1OUT =cmd;
  54.     //P0 = cmd;

  55.     P2OUT |=BIT0;
  56.     //lcd_1602_EN = 1;
  57.     delay_10us();
  58.     P2OUT &= ~BIT0;
  59.     //lcd_1602_EN = 0;
  60. }


  61. void lcd_1602_write_data(uchar dat)
  62. {
  63.     while(lcd_1602_busy());
  64.     
  65.     P2DIR =0xff;
  66.     P2OUT &=~BIT1;
  67.     //lcd_1602_RW = 0;
  68.     //lcd_1602_RS = 1;
  69.     //P0 = dat;
  70.     P2OUT |=BIT2;
  71.     P1DIR=0xff;
  72.     P1OUT=dat;
  73.     //lcd_1602_EN =1;
  74.     delay_10us();
  75.     P2OUT &= ~BIT0;
  76.     //lcd_1602_EN = 0;
  77. }


  78. void lcd_1602_init()
  79. {
  80.     lcd_1602_write_cmd(0x01);
  81.     lcd_1602_write_cmd(0x02);
  82.     lcd_1602_write_cmd(0x06);
  83.     lcd_1602_write_cmd(0x38);
  84.     lcd_1602_write_cmd(0x01);
  85.     delay_1ms();
  86. }


  87. void lcd_1602_write_char(uchar x, uchar y, uchar dat)
  88. {
  89.     if(x == 0)
  90.     {
  91.         lcd_1602_write_cmd(0x80 +y%16);
  92.         lcd_1602_write_data(dat);
  93.     }

  94.     else
  95.     {
  96.         lcd_1602_write_cmd(0xc0 + y%16);
  97.         lcd_1602_write_data(dat);
  98.     }
  99. }


  100. void lcd_1602_write_string(uchar x, uchar y, uchar *string)
  101. {
  102.     while(*string != '\0')
  103.     {
  104.         lcd_1602_write_char(x,y++,*(string));
  105.     }
  106. }
  107. void main()
  108. {
  109.     P1SEL=0X00;
  110.     P2SEL=0X00;
  111.     
  112.     lcd_1602_write_cmd(0x0e);


  113.     lcd_1602_write_cmd(0x80);
  114.     lcd_1602_write_data('s');
  115.     lcd_1602_write_data('i');
  116.     lcd_1602_write_data('l');
  117.     lcd_1602_write_data('i');
  118.     lcd_1602_write_data('v');
  119.     lcd_1602_write_data('a');
  120. }


阅读(1208) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~