Chinaunix首页 | 论坛 | 博客
  • 博客访问: 56189
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-10-23 10:35
文章存档

2016年(1)

2012年(7)

2011年(10)

分类: C/C++

2011-07-22 20:41:25

  1. /***********STC12C5A60S2 12MHZ ***************/
  2. #include<reg52.h>
  3. #include <intrins.h>
  4. #include"1602.h"
  5. #define uchar unsigned char
  6. #define uint unsigned int
  7. sbit led=P1^0;
  8. sbit DCLK=P2^1;
  9. sbit Dout=P2^2;
  10. sbit CE= P2^3;

  11. uchar code display[]={'0','1','2','3','4','5','6','7','8','9'};
  12.  void init() //AD初始化
  13. {
  14.     DCLK=1;
  15.     Dout=1;
  16.     CE=1;
  17. }

  18. unsigned int readADS()         //读取AD值,SPI
  19. {
  20.     unsigned int back_up=0;
  21.     unsigned char i=0;
  22.     bit data1=0;
  23.     Dout=1;

  24.     DCLK=0;
  25.     delay2us();
  26.     delay2us();

  27.     CE=1;
  28.     delay2us();
  29.     delay2us();
  30.     CE=0;
  31.     //sample
  32.     DCLK=1;
  33.     delay2us();
  34.     delay2us();

  35.     DCLK=0;
  36.     delay2us();
  37.     delay2us();
  38.     DCLK=1;
  39.     delay2us();
  40.     delay2us();

  41.     DCLK=0;
  42.     delay2us();
  43.     delay2us();
  44.     DCLK=1;
  45.     delay2us();
  46.     delay2us();

  47.     for(i=0;i<12;i++)
  48.     {
  49.         
  50.         DCLK=0;
  51.         delay2us();
  52.         data1=Dout;     //read data
  53.         if(data1)
  54.         {
  55.               back_up|=(0x0800>>i);
  56.         }
  57.         DCLK=1;
  58.         delay2us();

  59.     }
  60.     CE=1;     //close transfer
  61.     return back_up;
  62. }

  63. void main()
  64. {
  65. unsigned long int temp=0;
  66. unsigned long int ad=0;
  67. unsigned char wa=0,qi=0,ba=0,sh=0,ge=0;
  68.     LCD_init();
  69.     LCD_cls();
  70.     init();
  71.      ad=readADS();
  72.      LCD_write_string(0,0,"FRE:00.00000 MHz");
  73. while(1)
  74.     {
  75.     delay(1);    
  76.     led=0;
  77.     delay(1);
  78.     
  79.     led=1;
  80.     delay(1);
  81.     
  82.     ad=readADS();
  83.     wa=ad/10000;
  84.     qi=ad%10000/1000;
  85.     ba=ad%10000%1000/100;
  86.     sh=ad%10000%1000%100/10;
  87.     ge=ad%10000%1000%100%10/1;
  88.     LCD_write_char(0,1,display[wa]); //显示二进制对于十进制
  89.     LCD_write_char(1,1,display[qi]);
  90.       LCD_write_char(2,1,display[ba]);
  91.     LCD_write_char(3,1,display[sh]);
  92.     LCD_write_char(4,1,display[ge]);
  93.     temp=ad*1221;
  94.     LCD_write_char(6,1,display[temp/1000000]);
  95.     LCD_write_char(7,1,'.');
  96.     LCD_write_char(8,1,display[temp%1000000/100000]);
  97.     LCD_write_char(9,1,display[temp%1000000%100000/10000]);
  98.     LCD_write_char(10,1,display[temp%1000000%100000%10000/1000]);
  99.     LCD_write_char(11,1,display[temp%1000000%100000%10000%1000/100]);
  100.     LCD_write_char(12,1,display[temp%1000000%100000%10000%1000%100/10]);
  101.     LCD_write_char(13,1,display[temp%1000000%100000%10000%1000%100%10/1]);
  102.     LCD_write_char(14,1,' ');
  103.     LCD_write_char(15,1,'V');
  104.     }    
  105. }


  106. sbit LCD_DB4= P0^4;
  107. sbit LCD_DB5= P0^5;
  108. sbit LCD_DB6= P0^6;
  109. sbit LCD_DB7= P0^7; // 四线数据传输
  110. sbit LCD1602_RS=P2^0;
  111. sbit LCD1602_RW=P3^7;//实际上没有读LCM,RW脚可以直接接地
  112. sbit LCD1602_EN=P3^6;                                     


  113. void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);    //在指定位置显示字符
  114. void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//在指定位置显示字符串
  115. void LCD_cls(void);//清屏
  116. void LCD_en_command(unsigned char command);
  117. void LCD_en_dat(unsigned char temp);
  118. void LCD_set_xy( unsigned char x, unsigned char y );
  119. void LCD_init(void);
  120. void SET_LCD(unsigned char IO_temp);

  121. void lcddelay(void);

  122. unsigned char LCDIO;

  123. void delay(unsigned int z) //延时Z MS子函数     //MCU STC12C5A60S2 12MHZ
  124. {
  125.     unsigned int x,y;
  126.     for(x=z;x>0;x--)
  127.         for(y=915;y>0;y--);    
  128. }

  129. void delay2us()
  130. {
  131.     _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
  132. }
  133. void LCD_cls(void)
  134. {
  135.     LCD_en_command(0x01);     //清屏指令
  136.     delay(2);
  137. }

  138. void LCD_en_command(unsigned char command) //指令使能
  139. {
  140.     LCD1602_RS=0; //命令选择开启
  141.     LCD1602_RW=0; //写入模式
  142.     LCD1602_EN=0; //使能初始
  143.                                 
  144.     LCDIO=(command & 0xf0);//写入高四位数据命令开启
  145.     SET_LCD(LCDIO);
  146.     LCD1602_EN=1;
  147.     delay(5);
  148.     LCD1602_EN=0; //关闭使能

  149.     LCDIO=(command & 0x0f)<<4; //写入低四位数据命令开启,送至高四位写入
  150.     SET_LCD(LCDIO);
  151.     LCD1602_EN=1;                            
  152.     delay(5);//
  153.     LCD1602_EN=0;
  154. }

  155. void SET_LCD(unsigned char IO_temp)        //四位I/O口定义
  156. {
  157.     LCD_DB7=IO_temp&0x80;
  158.     LCD_DB6=IO_temp&0x40;
  159.     LCD_DB5=IO_temp&0x20;
  160.     LCD_DB4=IO_temp&0x10;
  161. }

  162. void LCD_en_dat(unsigned char dat)     //数据写入驱动
  163. {
  164.     LCD1602_RS=1;
  165.     LCD1602_RW=0;
  166.     LCD1602_EN=0;

  167.     LCDIO=(dat & 0xf0);
  168.     SET_LCD(LCDIO);
  169.     LCD1602_EN=1;
  170.     delay(5);//    时间太短写不进去
  171.     LCD1602_EN=0;

  172.     LCDIO=(dat & 0x0f)<<4;
  173.     SET_LCD(LCDIO);
  174.     LCD1602_EN=1;
  175.     delay(5);//    
  176.     LCD1602_EN=0;
  177. }

  178. void LCD_set_xy( unsigned char x, unsigned char y ) //行选择
  179. {
  180.     unsigned char address;
  181.     if (y ==0)
  182.         address = 0x80 + x;
  183.     else
  184.         address = 0xC0 + x;
  185.         LCD_en_command(address);
  186. }

  187. void LCD_write_char( unsigned x,unsigned char y,unsigned char dat) //字符写入
  188. {
  189.     LCD_set_xy( x, y );
  190.     LCD_en_dat(dat);
  191. }

  192. void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)     //字符串写入
  193. {
  194.     LCD_set_xy( X, Y ); //set address
  195.     while (*s) // write character
  196.     {
  197.         LCDIO=*s;
  198.         SET_LCD(LCDIO);
  199.         LCD_en_dat(*s);
  200.         s ++;
  201.     }
  202. }

  203. void LCD_init(void) //1602初始化
  204. {
  205.     LCD_en_command(0x33);
  206.     delay(20);
  207.     LCD_en_command(0x32);
  208.     delay(20);
  209.     LCD_en_command(0x08);
  210.     delay(5);
  211.     LCD_en_command(0x01);
  212.     delay(5);
  213.     LCD_en_command(0x06);
  214.     delay(5);
  215.     LCD_en_command(0x0c);
  216.     delay(5);
  217. }
阅读(1906) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~