求大神帮忙看下延时怎么改

2019-07-15 08:23发布

帮忙
QQ图片20180518150703.png
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
泰小呆
2019-07-15 16:54
  1. //程序头函数
  2. #include <reg52.h>
  3. //显示函数

  4. //宏定义
  5. #define uint unsigned int
  6. #define uchar unsigned char
  7. #define Data_ADC0809 P1

  8. //管脚声明
  9. sbit LED_yanwu= P2^1;
  10. sbit baojing= P2^5;
  11. //ADC0809
  12. sbit ST=P3^3;
  13. sbit EOC=P3^6;
  14. sbit OE=P3^2;
  15. //按键
  16. sbit Key1=P2^6;
  17. sbit Key2=P2^7;
  18. sbit Key3=P3^7;
  19.    signed char y;                                     //浓度值全局变量
  20. uint c;                                           //浓度值全局变量
  21. bit bdata flag=0;
  22. //co含量变量
  23. uchar nongdu;
  24. uchar yushe_yanwu=45;
  25. //按钮模式|  
  26. uchar Mode=0;

  27. //函数声明
  28. extern uchar ADC0809();
  29. extern void Key();

  30. void delay(uint z)
  31. {
  32.         uint i,j;
  33.         for(i=0;i<z;i++)
  34.         for(j=0;j<121;j++);
  35. }

  36. //ADC0809读取信息
  37. uchar ADC0809()
  38. {
  39.         uchar yanwu_=0x00;
  40.         //转化初始化
  41.         ST=0;
  42.         //开始转换
  43.         ST=1;
  44.         ST=0;
  45.         //外部中断等待AD转换结束
  46.         while(EOC==0)
  47.         //读取转换的AD值
  48.         OE=1;
  49.         yanwu_=Data_ADC0809;
  50.         OE=0;
  51.         return yanwu_;
  52. }

  53. void Key()       
  54. {   //模式选择          
  55. void write_com()
  56.         if(Key1==0)
  57.         {
  58.                 while(Key1==0);
  59.                 if(Mode>=3) Mode=0;
  60.                 else
  61.                 {                 
  62.           
  63.                 }
  64.         }
  65.     if(Key2==0&&Mode!=0)
  66.         {
  67.                 while(Key2==0);
  68.                 switch(Mode)
  69.                 {
  70.                         case 1:
  71.                         {         
  72.                
  73.                                 yushe_yanwu++;
  74.                                 if(yushe_yanwu>=255)
  75.                                 yushe_yanwu=255;
  76.                                 write_com(0x80+13);
  77.                                 write_data(0x30+yushe_yanwu/100);
  78.                                 write_data(0x30+yushe_yanwu%100/10);
  79.                                 write_data(0x30+yushe_yanwu%10);
  80.                                 break;                               
  81.                         }
  82.                 }
  83.         }
  84.         if(Key3==0&&Mode!=0)
  85.         {
  86.                 while(Key3==0);
  87.                 switch(Mode)
  88.                 {
  89.                         case 1:
  90.                         {
  91.                                 yushe_yanwu--;
  92.                                 if(yushe_yanwu<=0)
  93.                                 yushe_yanwu=0;
  94.                                 write_data(0x30+yushe_yanwu%100/10);
  95.                                 write_data(0x30+yushe_yanwu%10);
  96.                                 break;                               
  97.                         }
  98.                 }
  99.         }

  100.         delay(200);
  101. }
  102. /*****************************************************************
  103. iic控制1602 lcd
  104. *****************************************************************/
  105.    #include <reg51.h>
  106. #include <intrins.h>
  107. #define uchar unsigned char
  108. #define uint unsigned int
  109. sbit SCL = P1^5;
  110. sbit SDA = P1^6;
  111. char ADDR = 0x7E;
  112. void delay(int ms);
  113. void IIC_start(void);
  114. void IIC_stop(void);
  115. void IIC_writeByte(char temp);
  116. void send_command(char comm);
  117. void send_data(char data1);
  118. void init_lcd(void);
  119. void clear_lcd(void);
  120. void print_lcd(int x, int y, char *str);
  121. void print_customer(int x, int y, char str[]);
  122. void main(){
  123. unsigned char table[]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00}; //×?·???
  124. unsigned char table1[]={
  125. 0x03,0x07,0x0f,0x1f,0x1f,0x1f,0x1f,0x1f,
  126. 0x18,0x1E,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
  127. 0x07,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
  128. 0x10,0x18,0x1c,0x1E,0x1E,0x1E,0x1E,0x1E,
  129. 0x0f,0x07,0x03,0x01,0x00,0x00,0x00,0x00,
  130. 0x1f,0x1f,0x1f,0x1f,0x1f,0x0f,0x07,0x01,
  131. 0x1f,0x1f,0x1f,0x1f,0x1f,0x1c,0x18,0x00,
  132. 0x1c,0x18,0x10,0x00,0x00,0x00,0x00,0x00
  133. };//????°?
  134. init_lcd();
  135. print_lcd(0,0,"Hello! >_<");
  136. print_lcd(0,1," I Love U -Gao W");
  137. //print_customer(0,0,table);
  138. while(1);
  139. }
  140. void delay(int ms){
  141. unsigned char y ;
  142. while(ms--){
  143. for(y = 0 ; y<250 ; y++){
  144. _nop_() ;
  145. _nop_() ;
  146. _nop_() ;
  147. _nop_() ;
  148. }
  149. }
  150. }
  151. void IIC_start(void){
  152. SDA=1;
  153. _nop_();
  154. SCL=1;
  155. _nop_();
  156. _nop_();
  157. _nop_();
  158. _nop_();
  159. _nop_();
  160. SDA=0;
  161. _nop_();
  162. _nop_();
  163. _nop_();
  164. _nop_();
  165. _nop_();
  166. SCL=0;
  167. }
  168. //????????
  169. void IIC_stop(void){
  170. SDA=0;
  171. _nop_();
  172. SCL=1;
  173. _nop_();
  174. _nop_();
  175. _nop_();
  176. _nop_();
  177. _nop_();
  178. SDA=1;
  179. _nop_();
  180. _nop_();
  181. _nop_();
  182. _nop_();
  183. }
  184. void IIC_writeByte(char temp){
  185. char i;
  186. for(i=0;i<8;i++){
  187. SDA=(bit)(temp & 0x80) ;
  188. temp <<=1;
  189. _nop_();
  190. _nop_();
  191. SCL=1;
  192. _nop_();
  193. _nop_();
  194. _nop_();
  195. _nop_();
  196. _nop_();
  197. SCL=0;
  198. }
  199. _nop_();
  200. _nop_();
  201. _nop_();
  202. _nop_();
  203. SDA=1;
  204. _nop_();
  205. _nop_();
  206. _nop_();
  207. _nop_();
  208. SCL=1;
  209. _nop_();
  210. _nop_();
  211. _nop_();
  212. while(SDA);
  213. _nop_();
  214. SCL=0;
  215. }
  216. void send_command(char comm){
  217. char tmp;
  218. IIC_start();
  219. IIC_writeByte(ADDR);

  220. tmp = comm & 0xF0;
  221. tmp |= 0x0C; //RS = 0, RW = 0, EN = 1
  222. IIC_writeByte(tmp);
  223. delay(20);
  224. tmp &= 0xFB; //Make EN = 0
  225. IIC_writeByte(tmp);

  226. tmp = (comm & 0x0F) << 4 ;
  227. tmp |= 0x0C; //RS = 0, RW = 0, EN = 1
  228. IIC_writeByte(tmp);
  229. delay(20);
  230. tmp &= 0xFB; // Make EN = 0
  231. IIC_writeByte(tmp);
  232. //stop_8574();
  233. }
  234. void send_data(char data1){
  235. char tmp;
  236. IIC_start();
  237. IIC_writeByte(ADDR);

  238. tmp = data1 & 0xF0;
  239. tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
  240. IIC_writeByte(tmp);
  241. delay(20);
  242. tmp &= 0xFB; //Make EN = 0
  243. IIC_writeByte(tmp);

  244. tmp = (data1 & 0x0F) << 4 ;
  245. tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
  246. IIC_writeByte(tmp);
  247. delay(20);
  248. tmp &= 0xFB ; // Make EN = 0
  249. IIC_writeByte(tmp);
  250. }
  251. //LCD????"?
  252. void init_lcd(void){
  253. send_command(0x33);
  254. delay(50) ;
  255. send_command(0x32);
  256. delay(50) ;
  257. send_command(0x28);
  258. delay(50) ;
  259. send_command(0x0C);
  260. delay(50) ;
  261. send_command(0x06);
  262. delay(50) ;
  263. send_command(0x01);
  264. delay(50) ;
  265. }
  266. //Clear Screen
  267. void clear_lcd(void){
  268. send_command(0x01);
  269. }
  270. void print_lcd(int x, int y, char *str){
  271. char addr;
  272. if( x < 0){
  273. x = 0;
  274. }
  275. if(x > 15){
  276. x = 15;
  277. }
  278. if(y<0){
  279. y = 0;
  280. }
  281. if(y > 1){
  282. y = 1;
  283. }
  284. // Move cursor
  285. addr = 0x80 + 0x40 * y + x;
  286. send_command(addr);
  287. while (*str) {
  288. send_data(*str++);
  289. }
  290. }
  291. void print_customer(int x, int y, char *str){
  292. unsigned char i,tmp;
  293. for(i=0;i<8;i++){
  294. send_command(0x40+i);
  295. send_data(*str++);
  296. }
  297. }
  298. /****************************************************
  299. 结束
  300. ****************************************************/
  301. void main()
  302. {
  303.         check_nongdu();
  304.         check_nongdu();
  305.         Init_lcd();
  306.         while(1)
  307.         {
  308.                 yanwu=ADC0809();
  309.                 check_nongdu();
  310.                 Key();

  311.                 if(Mode==0)
  312.                 {
  313.                         Display_lcd(yushe_yanwu,c,nongdu);
  314.                         if(nongdu>=yushe_yanwu)
  315.                         {
  316.                                 LED_yanwu=0;
  317.                                 baojing=0;
  318.                         }
  319.                         else
  320.                         {
  321.                                 LED_yanwu=1;
  322.                         }
  323.                         if((nongdu<yushe_yanwu))
  324.                         {
  325.                                 baojing=1;
  326.                         }
  327.                 }
  328.         }
  329. }       
复制代码

一周热门 更多>