Advertisement
Guest User

Blue_Key

a guest
Sep 21st, 2008
1,542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.42 KB | None | 0 0
  1. #include <pic.h>
  2. #include <htc.h>
  3. #include <stdlib.h>
  4.  
  5. //__CONFIG(XT&WDTDIS&LVPDIS);
  6. __CONFIG(0x1832);  
  7.  
  8. void lcd_line1(void);               //function prototypes
  9. void lcd_line2(void);
  10.  
  11. void init();                   //I/O init
  12. void lcd_init();               //LCD init
  13. void write(char x);            //display one byte
  14. void lcd_enable();             //LCD display setting.
  15.  
  16. void delay(void);
  17. void clk_write(unsigned char);
  18. unsigned char clk_read(void);
  19. void clkburst(void);
  20. void datafix(void);
  21.  
  22.  
  23. #define RS RA1
  24. #define RW RA2
  25. #define E  RA3
  26.  
  27. #define RST         RB5
  28. #define DATA        RB4
  29. #define SCLK        RB0
  30. #define DATA_Tris TRISB4
  31.  
  32. char number[16] = "  :  :          ";
  33. char daydate[16] = "  :  :20        ";
  34. char sec[] = "           ";
  35.  
  36.  
  37.  
  38. void main(void)
  39. {
  40.     char x;
  41.     unsigned char a,b;
  42.     a=0;
  43.     number[2] = number[5] = 0x3a;
  44.     daydate[2] = daydate[5] = 0x3a;
  45.     daydate[6] = 0x32;
  46.     daydate[7] = 0x30;
  47.     TRISB = 0;
  48.     TRISD = 0;
  49.     RST = 0;                        //1302 reset low
  50.     SCLK = 0;                       //1302 clock low
  51.     RW = 0;                         //set lcd R/W low
  52.     E = 0;                          //set lcd E low
  53.  
  54.     delay();
  55.  
  56.  
  57.  
  58.         init();                //I/O init
  59.         lcd_init();            //LCD init
  60.  
  61.     while(1)
  62.     {
  63.         RST = 1;                    //read hours
  64.         clk_write(0xbf);            //burst read
  65.         sec[0] = clk_read();        //sec
  66.         sec[1] = clk_read();        //min      
  67.         sec[2] = clk_read();        //hour
  68.         sec[3] = clk_read();        //date
  69.         sec[4] = clk_read();        //month
  70.         sec[5] = clk_read();        //day
  71.         sec[6] = clk_read();        //year
  72.         sec[7] = clk_read();        //control
  73.         RST = 0;
  74.         datafix();                  //convert for display
  75.  
  76.     PORTD=0x02;                 //clr screen
  77.     lcd_enable();
  78.         lcd_line1();           //display company's website  
  79.         PORTD=0xC0;            //set the 2nd line display address
  80.         lcd_enable();          //LCD display setting.      
  81.         lcd_line2();           //display company's tel number.  
  82.  
  83.  
  84.     }
  85. }
  86.  
  87. void datafix(void)
  88. {
  89.     char temp,x;
  90.     char *foo = sec;
  91.     char bleh[] = "                ";
  92.     for(x=0;x<14;x+=2)
  93.     {
  94.         temp = *foo;
  95.         temp &+ 0xf0;
  96.         temp >>= 4;
  97.         bleh[x] = temp + 0x30;
  98.         temp = *foo;
  99.         temp &= 0x0f;
  100.         bleh[x+1] = temp + 0x30;
  101.         foo++;
  102.     }
  103.     number[6]=bleh[0];          //sec
  104.     number[7]=bleh[1];
  105.     number[3]=bleh[2];          //min
  106.     number[4]=bleh[3];
  107.     number[0]=bleh[4];          //hour
  108.     number[1]=bleh[5];
  109.     daydate[3]=bleh[6];         //day
  110.     daydate[4]=bleh[7];
  111.     daydate[0]=bleh[8];         //month
  112.     daydate[1]=bleh[9];
  113.     daydate[8]=bleh[12];            //year
  114.     daydate[9]=bleh[13];
  115. }
  116.  
  117. void clk_write(unsigned char dat)
  118. {
  119.     char x;
  120.     for(x=0;x<8;x++)
  121.     {
  122.         if(dat & 0x01)                  //put bit on data pin
  123.         {
  124.             DATA = 1;
  125.         }
  126.         else DATA = 0;
  127.         SCLK = 1;                       //toggle clock to latch bit
  128.         SCLK = 0;
  129.         dat >>= 1;                      //move next bit into place
  130.     }
  131. }
  132.  
  133. unsigned char clk_read(void)
  134. {
  135.     char x;
  136.     unsigned char dat,tmpdat;
  137.     dat = 0;
  138.     DATA_Tris = 1;                      //make DATA input
  139.     for(x=0;x<8;x++)
  140.     {
  141.         if(DATA)                        //read a bit
  142.         {
  143.             tmpdat |= 1;
  144.         } else {
  145.             tmpdat |= 0;
  146.         }
  147.         tmpdat <<= 7;
  148.         dat >>= 1;
  149.         dat |= tmpdat;
  150.         SCLK = 1;                       //toggle clock to latch bit
  151.         SCLK = 0;
  152.     }
  153.     DATA_Tris = 0;
  154.     return(dat);
  155. }
  156.  
  157.  
  158.  
  159.    
  160. void lcd_16number(int num)
  161. {
  162.     number[0]=(char)(abs(num/10000)+0x30);
  163.     num=num-abs(num/10000)*10000;
  164.     number[1]=(char)(abs(num/1000)+0x30);
  165.     num=num-abs(num/1000)*1000;
  166.     number[2]=(char)(abs(num/100)+0x30);
  167.     num=num-abs(num/100)*100;
  168.     number[3]=(char)(abs(num/10)+0x30);
  169.     num=num-abs(num/10)*10;
  170.     number[4]=(char)(num+0x30);
  171. }
  172.  
  173.  
  174.  
  175.  
  176. //---------------------------------------
  177. //I/O init
  178. void init()
  179.  {
  180.     ADCON1=0x07;               //a port as ordinary i/o.
  181.     TRISA=0x00;                //a port as output.
  182.     TRISD=0x00;                //d port as output.
  183.     return;
  184.  }
  185.  
  186. //---------------------------------------
  187. //LCD init
  188. void lcd_init()
  189.  {
  190.     PORTD=0x01;                 //clr screen
  191.     lcd_enable();
  192.     PORTD=0x38;                //8 bits 2 lines 5*7 mode.  
  193.     lcd_enable();
  194.     PORTD=0x0C;                //display on,cursor on,blink on.
  195.     lcd_enable();
  196.     PORTD=0x06;                //character not move,cursor rotate right.
  197.     lcd_enable();
  198.     PORTD=0x80;                //กฐWWW.PIC16.COM"
  199.     lcd_enable();
  200.     return;
  201.  }
  202.  
  203.  
  204.  
  205. //--------------------------------------
  206. //write a byte to lcd.
  207. void write(char x)
  208.  {
  209.   PORTD=x;                   //data send to PORTD
  210.   RS = 1;                      //is data not command
  211.   RW = 0;                      //is write not read
  212.   E = 0;                       //pull low enable signal
  213.   delay();                   //for a while
  214.   E = 1;                       //pull high to build the rising edge.
  215.  
  216.     return;    
  217. }
  218.  
  219. //--------------------------------------
  220. //lcd display setting
  221. void lcd_enable()
  222.  {
  223.    RS=0;                     //is command not data
  224.    RW=0;                     //is write not read.
  225.    E=0;                      //pull low enable signal.          
  226.    delay();                  //for a while.                      
  227.    E=1;                      //pull high to build the rising edge
  228.      
  229.     return;
  230. }
  231.  
  232.  
  233.  
  234.  void lcd_line1()
  235.  {
  236.     int i;
  237.     for(i=0;i<16;++i)       //total 16 bytes to display.
  238.        {
  239.          write(number[i]);       //search table to display
  240.        }
  241.     return;
  242.  }
  243.  
  244.  void lcd_line2()
  245.  {
  246.     int i;
  247.     for(i=0;i<16;++i)       //total 16 bytes to display.
  248.        {
  249.          write(daydate[i]);       //search table to display
  250.        }
  251.     return;
  252.  }
  253.  
  254.  
  255. void delay(void)
  256. {
  257.     long int y;
  258.         for(y=0;y<100;y++){}
  259. }
  260.  
  261.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement