找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3602|回复: 0
收起左侧

MSP430、DS1302、LCD1602程序

[复制链接]
ID:100029 发表于 2015-12-22 21:39 | 显示全部楼层 |阅读模式
  1. #include "io430.h"
  2. unsigned char tt0[]="Time:";
  3. unsigned char tt1[]="Data:";
  4. unsigned char tt2[]="week:";
  5. unsigned char tt3[]="temp:";
  6. //static char ch[7][3]={"Sun","Mon","Tue","Wed","Thu","Tri","Sat"};
  7. unsigned  char TT,temp1;
  8. unsigned  char temp2;
  9. void delay(unsigned char us)
  10. {
  11.   while(us--);
  12. }
  13. unsigned char Reset(void)
  14. {
  15.    unsigned char k;
  16.    P2DIR |=BIT6;
  17.    P2OUT &=~BIT6;
  18.    delay(145);
  19.    P2OUT |=BIT6;
  20.    delay(10);  
  21.    P2DIR &=~BIT6;
  22.    k=P2IN;
  23.    delay(90);
  24.    return k;
  25. }
  26. //---------------------------
  27. unsigned char ReadByte(void)
  28. {
  29.    unsigned char j,buf=0;
  30.    for(j=0;j<8;j++)
  31.    {
  32.         buf=buf>>1;
  33.         P2DIR |=BIT6;
  34.         P2OUT &=~BIT6;
  35.         __no_operation();
  36.         __no_operation();
  37.         P2OUT |=BIT6;
  38.         __no_operation();
  39.         __no_operation();
  40.         __no_operation();
  41.         __no_operation();
  42.         __no_operation();
  43.         __no_operation();
  44.         __no_operation();
  45.         P2DIR &=~BIT6;
  46.         if(0x40 & P2IN==1)
  47.             buf|=0x80;
  48.             delay(8);
  49.      }
  50.    return buf;
  51. }
  52. //-------------------------------
  53. void WriteByte(unsigned char dat)
  54. {
  55.     unsigned char j;
  56.     P2DIR |=BIT6;
  57.     for(j=0;j<8;j++)
  58.      {
  59.        if(dat&0x01)
  60.          {
  61.            P2OUT &=~BIT6;
  62.            __no_operation();
  63.            __no_operation();
  64.            __no_operation();
  65.             P2OUT |=BIT6;
  66.             delay(10);
  67.          }
  68.       else
  69.          {
  70.            P2OUT &=~BIT6;
  71.            delay(10);
  72.            P2OUT |=BIT6;
  73.            __no_operation();
  74.            __no_operation();
  75.            __no_operation();
  76.          }
  77.    dat=dat>>1;
  78. }
  79. }
  80. //------------------
  81. unsigned char Convert(void)
  82. {
  83. if(Reset() & 0x40==0x00)
  84.      {
  85.         WriteByte(0xcc);
  86.         WriteByte(0x44);
  87.         return 0xff;
  88.      }
  89. else  
  90.      {  
  91.          return 0x00;
  92.      }
  93. }
  94. //---------------------------------------
  95. void ReadFlash(void)
  96. {
  97. unsigned char Lsb,Msb;
  98.     if(Reset() & 0x40==0x00)
  99.       {
  100.         WriteByte(0xcc);
  101.         WriteByte(0xbe);
  102.         Lsb=ReadByte();
  103.         Msb=ReadByte();
  104.         temp1=Lsb;
  105.         temp2=Msb;
  106.       }
  107.      else
  108.       {
  109.         temp1=0;
  110.         temp2=0;
  111.       }
  112. }
  113. void w1602com(unsigned char dat)
  114. {
  115.   P2DIR |=0x07;
  116.   P1DIR  =0xFF;
  117.   P2OUT &=~(BIT0 + BIT1);
  118.   P1OUT=dat;
  119.   P2OUT &=~BIT2;
  120.   delay(2);
  121.   P2OUT |=BIT2;
  122.   delay(4);
  123.   P2OUT &=~BIT2;
  124. }
  125. void w1602dat(unsigned char dat)
  126. {
  127.   P2DIR |=0x07;
  128.   P1DIR  =0xFF;
  129.   P2OUT |=BIT0;
  130.   P2OUT &=~BIT1;
  131.   P1OUT=dat;
  132.   P2OUT &=~BIT2;
  133.   delay(2);
  134.   P2OUT |=BIT2;
  135.   delay(4);
  136.   P2OUT &=~BIT2;
  137. }
  138. void init1602()
  139. {
  140.          w1602com(0x38);
  141.          w1602com(0x06);
  142.          w1602com(0x0c);
  143.          w1602com(0x01);
  144. }
  145. unsigned char r1302(unsigned char addr)
  146. {
  147.         unsigned char n,BB,AA=0x00;
  148.         P2DIR |=BIT3+BIT4+BIT5;
  149.         P2OUT &=~(BIT3+BIT4+BIT5);
  150.         __no_operation();
  151.       
  152.         P2OUT |=BIT3;
  153.         BB=addr;
  154. for(n=0;n<8;n++)
  155.     {
  156.       P2OUT &=~BIT4;
  157.       if(BB & 0x01)
  158. P2OUT |=BIT5;
  159.       else
  160.         P2OUT &=~BIT5;
  161.       P2OUT |=BIT4;
  162.       BB=BB>>1;
  163.     }
  164. P2DIR &=~BIT5;
  165. for(n=0;n<8;n++)
  166.     {
  167.       if(P2IN & 0x20)
  168.          AA |=0x80;
  169.        P2OUT |=BIT4;
  170.        AA=AA>>1;
  171.        P2OUT &=~BIT4;
  172.     }
  173. P2OUT &=~BIT3;
  174. return(AA);
  175. }
  176. int main( void )
  177. {
  178.   // Stop watchdog timer to prevent time out reset
  179.   WDTCTL = WDTPW + WDTHOLD;
  180.   unsigned char i;
  181.    init1602();
  182.    while(1)
  183.    {
  184.         w1602com(0x80);
  185. for(i=0;i<5;i++)
  186.            w1602dat(tt0[i]);  
  187.         w1602dat(0x30+(r1302(0x85)>>4 & 0x0f));
  188.         w1602dat(0x30+(r1302(0x85) & 0x0f));
  189.         w1602dat(0x2d);
  190.         w1602dat(0x30+(r1302(0x83)>>4 & 0x0f));
  191.         w1602dat(0x30+(r1302(0x83) & 0x0f));
  192.         w1602dat(0x2d);
  193.         w1602dat(0x30+(r1302(0x81)>>4 & 0x0f));
  194.         w1602dat(0x30+(r1302(0x81) & 0x0f));
  195.       
  196.         w1602com(0xc0);
  197.         for(i=0;i<5;i++)
  198.            w1602dat(tt1[i]);  
  199.         w1602dat(0x30+(r1302(0x87)>>4 & 0x0f));
  200.         w1602dat(0x30+(r1302(0x87) & 0x0f));
  201.         w1602dat(0x2d);
  202.         w1602dat(0x30+(r1302(0x89)>>4 & 0x0f));
  203.         w1602dat(0x30+(r1302(0x89) & 0x0f));
  204.         w1602dat(0x2d);
  205.         w1602dat(0x30+(r1302(0x8d)>>4 & 0x0f));
  206.         w1602dat(0x30+(r1302(0x8d) & 0x0f));     
  207.    }   
  208. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表