当前位置:   article > 正文

蓝桥杯单片机第14届省赛客观题目+程序题目+程序题参考答案_第十四届蓝桥杯单片机省赛真题

第十四届蓝桥杯单片机省赛真题

目录

客观题题目

程序题题目

程序题参考答案

main.h

main.c

Init.h

Init.c

SMG.h

SMG.c

DSQ.h

DSQ.c

YanShi.h

YanShi.c

JZKey.h

JZKey.c

ds1302.h

ds1302.c

iic.h

iic.c

onewire.h

onewire.c

LN555.h

LN555.c

客观题题目

程序题题目

 

 

程序题参考答案

        首先吐槽一下,花300元体验国赛的难度,是真的崩溃。

        3个小时写完,2个小时改bug!!!

  1. NE555本来应该是在国赛考的,这届省赛直接上了。
  2. I2C、DS1302、DS18B20,这3个省赛常考的内容,按照往届省赛的习惯应该是三选二进行考查,但是这届省赛中直接将这3个都给考了,这种做法常见于国赛当中。
  3. 这届赛题的逻辑也更加复杂,出现了两个采集到的数据,进行对比最大值、计算平均值、前后两次采集到的数据比较。
  4. 程序题的题目能达到5页之多。
  5. 客观题也不好做。
  6. 个人感觉,疫情开放后,为了减少国赛的参加人数。

main.h

  1. #ifndef __MAIN_H_
  2. #define __MAIN_H_
  3. #include <STC15F2K60S2.H>
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. #include "Init.h"
  7. #include "SMG.h"
  8. #include "DSQ.h"
  9. #include "YanShi.h"
  10. #include "JZKey.h"
  11. #include "ds1302.h"
  12. #include "iic.h"
  13. #include "onewire.h"
  14. #include "LN555.h"
  15. void LED_Show();
  16. #endif

main.c

  1. #include "main.h"
  2. uchar key_num=0;
  3. uchar key_num_old=0;
  4. uchar JieMian=0;//界面。0:时间;1:回显;2:参数,3:温湿度
  5. uchar JieMian_old;
  6. uchar HuiXian=0;//回显。0:温度;1:湿度;2:时间
  7. char WenDu_CanShu=30;//温度参数,0-99
  8. uint S9_time=0;
  9. uchar shi,fen,miao;
  10. uchar ADC_data;
  11. uchar ADC_data_old;
  12. uchar ChuFa_CiShu=0;//触发次数
  13. uchar WenDu=0;//温度
  14. uchar WenDu_old;
  15. uchar WenDu_temp;
  16. uchar WenDu_Max=0;
  17. uint WenDu_PingJun=0;
  18. uint Hz;
  19. uint Hz_time=0;
  20. uchar ChuFa_Shi,ChuFa_Fen;
  21. uchar ShiDu=0;
  22. uchar ShiDu_old=0;
  23. uchar ShiDu_old_temp;
  24. uchar ShiDu_Max=0;
  25. uint ShiDu_PingJun=0;
  26. uchar JieMian_flag=0;//0:正常运转;1:3秒显示温湿度
  27. uint JieMian_flag_time=0;
  28. uchar YouXiao_flag=1;
  29. uchar L4_time=0;
  30. uint ADC_time=0;
  31. void main(){
  32. Init_BZ();
  33. DSQ_2_Init();
  34. SMG_Init();
  35. JZKey_Init();
  36. LN555_DSQ_0_Init();
  37. Ds1302_XieData(13,3,5);
  38. DS18B20_GetWenDu();
  39. while(1){
  40. key_num_old=key_num;
  41. key_num=JZKey_GetKeynum();
  42. if(key_num_old!=key_num&&JieMian_flag==0){
  43. if(key_num==4){
  44. JieMian++;
  45. JieMian=JieMian%3;
  46. if(JieMian==1){
  47. HuiXian=0;
  48. }
  49. }
  50. else if(key_num==5&&JieMian!=0){
  51. HuiXian++;
  52. HuiXian=HuiXian%3;
  53. }
  54. else if(key_num==8&&JieMian==2){
  55. WenDu_CanShu++;
  56. if(WenDu_CanShu>=99){
  57. WenDu_CanShu=99;
  58. }
  59. }
  60. else if(key_num==9&&JieMian==2){
  61. WenDu_CanShu--;
  62. if(WenDu_CanShu<=0){
  63. WenDu_CanShu=0;
  64. }
  65. }
  66. }
  67. shi=Ds1302_GetShi();
  68. fen=Ds1302_GetFen();
  69. miao=Ds1302_GetMiao();
  70. if(ADC_time>=200){
  71. ADC_time=0;
  72. ADC_data_old=ADC_data;
  73. ADC_data=IIC_ADC_GetData(1);
  74. if(ADC_data_old>ADC_data&&JieMian_flag==0&&ADC_data_old>90&&ADC_data<50){//触发采集
  75. ShiDu_old_temp=ShiDu;
  76. if(Hz<200){
  77. ShiDu=9;
  78. }
  79. else if(Hz>=200&&Hz<=2000){
  80. ShiDu=(80.0/1800.0)*(Hz*1.0)-(10.0/9.0);
  81. }
  82. else if(Hz>2000){
  83. ShiDu=91;
  84. }
  85. if(ShiDu>9&&ShiDu<91){
  86. ShiDu_old=ShiDu_old_temp;
  87. if(ShiDu_Max<ShiDu){
  88. ShiDu_Max=ShiDu;
  89. }
  90. ChuFa_CiShu++;
  91. ShiDu_PingJun=((((ShiDu_PingJun*1.0)/10.0)*((ChuFa_CiShu-1)*1.0)+(ShiDu*1.0))/(ChuFa_CiShu*1.0))*10;
  92. WenDu_old=WenDu;
  93. WenDu=DS18B20_GetWenDu();
  94. if(WenDu_Max<WenDu){
  95. WenDu_Max=WenDu;
  96. }
  97. WenDu_PingJun=((((WenDu_PingJun*1.0)/10.0)*((ChuFa_CiShu-1)*1.0)+(WenDu*1.0))/(ChuFa_CiShu*1.0))*10;
  98. ChuFa_Shi=shi;
  99. ChuFa_Fen=fen;
  100. YouXiao_flag=1;
  101. }
  102. else{
  103. WenDu_temp=DS18B20_GetWenDu();
  104. if(WenDu_Max<WenDu_temp){
  105. WenDu_Max=WenDu_temp;
  106. }
  107. ShiDu=ShiDu_old_temp;
  108. YouXiao_flag=0;
  109. }
  110. JieMian_old=JieMian;
  111. JieMian=3;
  112. JieMian_flag=1;
  113. }
  114. }
  115. }
  116. }
  117. void DSQ_2_ZD() interrupt 12{
  118. ADC_time++;
  119. if(JieMian==1&&HuiXian==2&&key_num==9){
  120. S9_time++;
  121. if(S9_time>=2000){//清除所有已记录的数据
  122. S9_time=0;
  123. WenDu_Max=0;
  124. WenDu_PingJun=0;
  125. ShiDu_Max=0;
  126. ShiDu_PingJun=0;
  127. ChuFa_CiShu=0;
  128. ChuFa_Shi=0;
  129. ChuFa_Fen=0;
  130. }
  131. }
  132. Hz_time++;
  133. if(Hz_time>=1000){
  134. Hz_time=0;
  135. Hz=((unsigned int)TH0<<8)|(unsigned int)TL0;
  136. TH0=0;
  137. TL0=0;
  138. }
  139. if(JieMian_flag){
  140. JieMian_flag_time++;
  141. if(JieMian_flag_time>=3000){
  142. JieMian_flag_time=0;
  143. JieMian_flag=0;
  144. JieMian=JieMian_old;
  145. }
  146. }
  147. if(WenDu>WenDu_CanShu){
  148. L4_time++;
  149. if(L4_time>=200){
  150. L4_time=0;
  151. }
  152. }
  153. else{
  154. L4_time=0;
  155. }
  156. if(JieMian==0){
  157. //SMG_Show(ADC_data_old/100,ADC_data_old/10%10,ADC_data_old%10,16,16,ADC_data/100,ADC_data/10%10,ADC_data%10);
  158. SMG_Show(shi/10,shi%10,17,fen/10,fen%10,17,miao/10,miao%10);
  159. }
  160. else if(JieMian==1){
  161. if(HuiXian==0){
  162. if(ChuFa_CiShu==0){
  163. SMG_Show(12,16,16,16,16,16,16,16);
  164. }
  165. else{
  166. SMG_Show(12,16,WenDu_Max/10,WenDu_Max%10,17,WenDu_PingJun/100,((WenDu_PingJun/10)%10)+32,WenDu_PingJun%10);
  167. }
  168. }
  169. else if(HuiXian==1){
  170. if(ChuFa_CiShu==0){
  171. SMG_Show(18,16,16,16,16,16,16,16);
  172. }
  173. else{
  174. SMG_Show(18,16,ShiDu_Max/10,ShiDu_Max%10,17,ShiDu_PingJun/100,((ShiDu_PingJun/10)%10)+32,ShiDu_PingJun%10);
  175. }
  176. }
  177. else if(HuiXian==2){
  178. if(ChuFa_CiShu==0){
  179. SMG_Show(15,ChuFa_CiShu/10,ChuFa_CiShu%10,16,16,16,16,16);
  180. }
  181. else{
  182. SMG_Show(15,ChuFa_CiShu/10,ChuFa_CiShu%10,ChuFa_Shi/10,ChuFa_Shi%10,17,ChuFa_Fen/10,ChuFa_Fen%10);
  183. }
  184. }
  185. }
  186. else if(JieMian==2){
  187. if(WenDu_CanShu>=10){
  188. SMG_Show(24,16,16,16,16,16,WenDu_CanShu/10,WenDu_CanShu%10);
  189. }
  190. else if(WenDu_CanShu>=0){
  191. SMG_Show(24,16,16,16,16,16,16,WenDu_CanShu);
  192. }
  193. }
  194. else if(JieMian==3){
  195. if(YouXiao_flag==1){
  196. SMG_Show(14,16,16,WenDu/10,WenDu%10,17,ShiDu/10,ShiDu%10);
  197. }
  198. else{
  199. SMG_Show(14,16,16,WenDu_temp/10,WenDu_temp%10,17,10,10);
  200. }
  201. }
  202. LED_Show();
  203. }
  204. void LED_Show(){
  205. uchar L1_2_3=0xff;
  206. uchar L4=0xff;
  207. uchar L5=0xff;
  208. uchar L6=0xff;
  209. if(JieMian==0){
  210. L1_2_3=0xfe;
  211. }
  212. else if(JieMian==1){
  213. L1_2_3=0xfd;
  214. }
  215. else if(JieMian==2){
  216. L1_2_3=0xfb;
  217. }
  218. if(L4_time>=100){
  219. L4=0xf7;
  220. }
  221. if(YouXiao_flag==0){
  222. L5=0xef;
  223. }
  224. if(ChuFa_CiShu>=2&&WenDu_old<WenDu&&ShiDu_old<ShiDu){
  225. L6=0xdf;
  226. }
  227. P0=L1_2_3&L4&L5&L6;
  228. P2=(P2&0x1f)|0x80;
  229. P2=(P2&0x1f)|0x00;
  230. }

Init.h

  1. #ifndef __INIT_H_
  2. #define __INIT_H_
  3. #include <STC15F2K60S2.H>
  4. void Init_BZ();
  5. #endif

Init.c

  1. #include "Init.h"
  2. void Init_BZ(){
  3. P2=(P2&0x1f)|0xa0;
  4. P0=0x00;
  5. P2=(P2&0x1f)|0x80;
  6. P0=0xff;
  7. }

SMG.h

  1. #ifndef __SMG_H_
  2. #define __SMG_H_
  3. #include <STC15F2K60S2.H>
  4. void SMG_Init();
  5. void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);
  6. #endif

SMG.c

  1. #include "SMG.h"
  2. unsigned char code t_display[]={ //????
  3. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  4. 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
  5. //black - H J K L N o P U t G Q r M y
  6. 0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
  7. 0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
  8. void SMG_Init(){
  9. P2=(P2&0x1f)|0xc0;
  10. P0=0xff;
  11. P2=(P2&0x1f)|0xe0;
  12. P0=0xff;
  13. }
  14. void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){
  15. static unsigned char i=0;
  16. i++;
  17. i=i%8;
  18. P2=(P2&0x1f)|0xc0;
  19. switch(i){
  20. case 1:{
  21. P0=0x01;
  22. P2=(P2&0x1f)|0xe0;
  23. P0=~t_display[n1];
  24. break;
  25. }
  26. case 2:{
  27. P0=0x02;
  28. P2=(P2&0x1f)|0xe0;
  29. P0=~t_display[n2];
  30. break;
  31. }
  32. case 3:{
  33. P0=0x04;
  34. P2=(P2&0x1f)|0xe0;
  35. P0=~t_display[n3];
  36. break;
  37. }
  38. case 4:{
  39. P0=0x08;
  40. P2=(P2&0x1f)|0xe0;
  41. P0=~t_display[n4];
  42. break;
  43. }
  44. case 5:{
  45. P0=0x10;
  46. P2=(P2&0x1f)|0xe0;
  47. P0=~t_display[n5];
  48. break;
  49. }
  50. case 6:{
  51. P0=0x20;
  52. P2=(P2&0x1f)|0xe0;
  53. P0=~t_display[n6];
  54. break;
  55. }
  56. case 7:{
  57. P0=0x40;
  58. P2=(P2&0x1f)|0xe0;
  59. P0=~t_display[n7];
  60. break;
  61. }
  62. case 0:{
  63. P0=0x80;
  64. P2=(P2&0x1f)|0xe0;
  65. P0=~t_display[n8];
  66. break;
  67. }
  68. }
  69. P2=(P2&0x1f)|0x00;
  70. }

DSQ.h

  1. #ifndef __DSQ_H_
  2. #define __DSQ_H_
  3. #include <STC15F2K60S2.H>
  4. void DSQ_2_Init();
  5. #endif

DSQ.c

  1. #include "DSQ.h"
  2. void DSQ_2_Init(){
  3. AUXR |= 0x04; //定时器时钟1T模式
  4. T2L = 0x20; //设置定时初值
  5. T2H = 0xD1; //设置定时初值
  6. AUXR |= 0x10; //定时器2开始计时
  7. AUXR=AUXR&0xf7;
  8. IE2=IE2|0x04;
  9. EA=1;
  10. }

YanShi.h

  1. #ifndef __YANSHI_H_
  2. #define __YANSHI_H_
  3. #include <STC15F2K60S2.H>
  4. #include "intrins.h"
  5. void Delay10ms();
  6. #endif

YanShi.c

  1. #include "YanShi.h"
  2. void Delay10ms() //@12.000MHz
  3. {
  4. unsigned char i, j;
  5. i = 117;
  6. j = 184;
  7. do
  8. {
  9. while (--j);
  10. } while (--i);
  11. }

JZKey.h

  1. #ifndef __JZKEY_H_
  2. #define __JZKEY_H_
  3. #include "YanShi.h"
  4. void JZKey_Init();
  5. unsigned char JZKey_GetKeynum();
  6. #endif

JZKey.c

  1. #include "JZKey.h"
  2. void JZKey_Init(){
  3. P32=1;
  4. P33=1;
  5. P42=0;
  6. P44=0;
  7. }
  8. unsigned char JZKey_GetKeynum(){
  9. unsigned char key_num=0;
  10. if(P32==0){
  11. P32=0;
  12. P42=1;
  13. P44=1;
  14. if(P42==0){
  15. Delay10ms();
  16. if(P42==0){
  17. key_num=9;
  18. }
  19. //while(P42==0);
  20. }
  21. else if(P44==0){
  22. Delay10ms();
  23. if(P44==0){
  24. key_num=5;
  25. }
  26. //while(P44==0);
  27. }
  28. }
  29. else if(P33==0){
  30. P33=0;
  31. P42=1;
  32. P44=1;
  33. if(P42==0){
  34. Delay10ms();
  35. if(P42==0){
  36. key_num=8;
  37. }
  38. //while(P42==0);
  39. }
  40. else if(P44==0){
  41. Delay10ms();
  42. if(P44==0){
  43. key_num=4;
  44. }
  45. //while(P44==0);
  46. }
  47. }
  48. JZKey_Init();
  49. return key_num;
  50. }

ds1302.h

  1. #ifndef __DS1302_H
  2. #define __DS1302_H
  3. #include <STC15F2K60S2.H>
  4. #include <intrins.h>
  5. void Write_Ds1302(unsigned char temp);
  6. void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
  7. unsigned char Read_Ds1302_Byte( unsigned char address );
  8. void Ds1302_XieData(unsigned char shi,fen,miao);
  9. unsigned char Ds1302_GetShi();
  10. unsigned char Ds1302_GetFen();
  11. unsigned char Ds1302_GetMiao();
  12. #endif

ds1302.c

  1. /*
  2. 程序说明: DS1302驱动程序
  3. 软件环境: Keil uVision 4.10
  4. 硬件环境: CT107单片机综合实训平台 8051,12MHz
  5. 日 期: 2011-8-9
  6. */
  7. #include "ds1302.h"
  8. sbit SCK=P1^7;
  9. sbit SDA=P2^3;
  10. sbit RST = P1^3; // DS1302复位
  11. void Write_Ds1302(unsigned char temp)
  12. {
  13. unsigned char i;
  14. for (i=0;i<8;i++)
  15. {
  16. SCK=0;
  17. SDA=temp&0x01;
  18. temp>>=1;
  19. SCK=1;
  20. }
  21. }
  22. void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
  23. {
  24. RST=0; _nop_();
  25. SCK=0; _nop_();
  26. RST=1; _nop_();
  27. Write_Ds1302(address);
  28. Write_Ds1302(dat);
  29. RST=0;
  30. }
  31. unsigned char Read_Ds1302_Byte ( unsigned char address )
  32. {
  33. unsigned char i,temp=0x00;
  34. RST=0; _nop_();
  35. SCK=0; _nop_();
  36. RST=1; _nop_();
  37. Write_Ds1302(address);
  38. for (i=0;i<8;i++)
  39. {
  40. SCK=0;
  41. temp>>=1;
  42. if(SDA)
  43. temp|=0x80;
  44. SCK=1;
  45. }
  46. RST=0; _nop_();
  47. SCK=0; _nop_();
  48. SCK=1; _nop_();
  49. SDA=0; _nop_();
  50. SDA=1; _nop_();
  51. return (temp);
  52. }
  53. void Ds1302_XieData(unsigned char shi,fen,miao){
  54. shi=(shi/10)*16+(shi%10);
  55. fen=(fen/10)*16+(fen%10);
  56. miao=(miao/10)*16+(miao%10);
  57. Write_Ds1302_Byte(0x8e,0x00);
  58. Write_Ds1302_Byte(0x84,shi);
  59. Write_Ds1302_Byte(0x82,fen);
  60. Write_Ds1302_Byte(0x80,miao);
  61. Write_Ds1302_Byte(0x8e,0x80);
  62. }
  63. unsigned char Ds1302_GetShi(){
  64. unsigned char date;
  65. date=Read_Ds1302_Byte(0x85);
  66. date=(date/16)*10+(date%16);
  67. return date;
  68. }
  69. unsigned char Ds1302_GetFen(){
  70. unsigned char date;
  71. date=Read_Ds1302_Byte(0x83);
  72. date=(date/16)*10+(date%16);
  73. return date;
  74. }
  75. unsigned char Ds1302_GetMiao(){
  76. unsigned char date;
  77. date=Read_Ds1302_Byte(0x81);
  78. date=(date/16)*10+(date%16);
  79. return date;
  80. }

iic.h

  1. #ifndef _IIC_H
  2. #define _IIC_H
  3. #include <STC15F2K60S2.H>
  4. #include "intrins.h"
  5. void IIC_Start(void);
  6. void IIC_Stop(void);
  7. bit IIC_WaitAck(void);
  8. void IIC_SendAck(bit ackbit);
  9. void IIC_SendByte(unsigned char byt);
  10. unsigned char IIC_RecByte(void);
  11. unsigned char IIC_ADC_GetData(unsigned char di_zhi);
  12. void IIC_DAC_XieData(unsigned char date);
  13. unsigned char IIC_EEPROM_GetData(unsigned char di_zhi);
  14. void IIC_EEPROM_XieData(unsigned char dizhi,date);
  15. #endif

iic.c

  1. /*
  2. 程序说明: IIC总线驱动程序
  3. 软件环境: Keil uVision 4.10
  4. 硬件环境: CT107单片机综合实训平台 8051,12MHz
  5. 日 期: 2011-8-9
  6. */
  7. #include "iic.h"
  8. #define DELAY_TIME 5
  9. #define SlaveAddrW 0xA0
  10. #define SlaveAddrR 0xA1
  11. //总线引脚定义
  12. sbit SDA = P2^1; /* 数据线 */
  13. sbit SCL = P2^0; /* 时钟线 */
  14. void IIC_Delay(unsigned char i)
  15. {
  16. do{_nop_();}
  17. while(i--);
  18. }
  19. //总线启动条件
  20. void IIC_Start(void)
  21. {
  22. SDA = 1;
  23. SCL = 1;
  24. IIC_Delay(DELAY_TIME);
  25. SDA = 0;
  26. IIC_Delay(DELAY_TIME);
  27. SCL = 0;
  28. }
  29. //总线停止条件
  30. void IIC_Stop(void)
  31. {
  32. SDA = 0;
  33. SCL = 1;
  34. IIC_Delay(DELAY_TIME);
  35. SDA = 1;
  36. IIC_Delay(DELAY_TIME);
  37. }
  38. //发送应答
  39. void IIC_SendAck(bit ackbit)
  40. {
  41. SCL = 0;
  42. SDA = ackbit; // 0:应答,1:非应答
  43. IIC_Delay(DELAY_TIME);
  44. SCL = 1;
  45. IIC_Delay(DELAY_TIME);
  46. SCL = 0;
  47. SDA = 1;
  48. IIC_Delay(DELAY_TIME);
  49. }
  50. //等待应答
  51. bit IIC_WaitAck(void)
  52. {
  53. bit ackbit;
  54. SCL = 1;
  55. IIC_Delay(DELAY_TIME);
  56. ackbit = SDA;
  57. SCL = 0;
  58. IIC_Delay(DELAY_TIME);
  59. return ackbit;
  60. }
  61. //通过I2C总线发送数据
  62. void IIC_SendByte(unsigned char byt)
  63. {
  64. unsigned char i;
  65. for(i=0; i<8; i++)
  66. {
  67. SCL = 0;
  68. IIC_Delay(DELAY_TIME);
  69. if(byt & 0x80) SDA = 1;
  70. else SDA = 0;
  71. IIC_Delay(DELAY_TIME);
  72. SCL = 1;
  73. byt <<= 1;
  74. IIC_Delay(DELAY_TIME);
  75. }
  76. SCL = 0;
  77. }
  78. //从I2C总线上接收数据
  79. unsigned char IIC_RecByte(void)
  80. {
  81. unsigned char i, da;
  82. for(i=0; i<8; i++)
  83. {
  84. SCL = 1;
  85. IIC_Delay(DELAY_TIME);
  86. da <<= 1;
  87. if(SDA) da |= 1;
  88. SCL = 0;
  89. IIC_Delay(DELAY_TIME);
  90. }
  91. return da;
  92. }
  93. unsigned char IIC_ADC_GetData(unsigned char di_zhi){
  94. unsigned char date;
  95. IIC_Start();
  96. IIC_SendByte(0x90);
  97. IIC_WaitAck();
  98. IIC_SendByte(di_zhi);
  99. IIC_WaitAck();
  100. IIC_Stop();
  101. IIC_Start();
  102. //EA=0;
  103. IIC_SendByte(0x91);
  104. IIC_WaitAck();
  105. date=IIC_RecByte();
  106. //EA=1;
  107. IIC_SendAck(1);
  108. IIC_WaitAck();
  109. IIC_Stop();
  110. return date;
  111. }
  112. void IIC_DAC_XieData(unsigned char date){
  113. IIC_Start();
  114. IIC_SendByte(0x90);
  115. IIC_WaitAck();
  116. IIC_SendByte(0x40);
  117. IIC_WaitAck();
  118. IIC_SendByte(date);
  119. IIC_WaitAck();
  120. IIC_Stop();
  121. }
  122. unsigned char IIC_EEPROM_GetData(unsigned char di_zhi){
  123. unsigned char date;
  124. IIC_Start();
  125. IIC_SendByte(0xa0);
  126. IIC_WaitAck();
  127. IIC_SendByte(di_zhi);
  128. IIC_WaitAck();
  129. IIC_Stop();
  130. IIC_Start();
  131. IIC_SendByte(0xa1);
  132. IIC_WaitAck();
  133. date=IIC_RecByte();
  134. IIC_SendAck(1);
  135. IIC_WaitAck();
  136. IIC_Stop();
  137. return date;
  138. }
  139. void IIC_EEPROM_XieData(unsigned char dizhi,date){
  140. IIC_Start();
  141. IIC_SendByte(0xa0);
  142. IIC_WaitAck();
  143. IIC_SendByte(dizhi);
  144. IIC_WaitAck();
  145. IIC_SendByte(date);
  146. IIC_WaitAck();
  147. IIC_Stop();
  148. }

onewire.h

  1. #ifndef __ONEWIRE_H
  2. #define __ONEWIRE_H
  3. #include <STC15F2K60S2.H>
  4. float DS18B20_GetWenDu();
  5. #endif

onewire.c

  1. /*
  2. 程序说明: 单总线驱动程序
  3. 软件环境: Keil uVision 4.10
  4. 硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
  5. 日 期: 2011-8-9
  6. */
  7. #include "onewire.h"
  8. sbit DQ = P1^4; //单总线接口
  9. //单总线延时函数
  10. void Delay_OneWire(unsigned int t) //STC89C52RC
  11. {
  12. unsigned char i;
  13. while(t--){
  14. for(i=0;i<12;i++);
  15. }
  16. }
  17. //通过单总线向DS18B20写一个字节
  18. void Write_DS18B20(unsigned char dat)
  19. {
  20. unsigned char i;
  21. for(i=0;i<8;i++)
  22. {
  23. DQ = 0;
  24. DQ = dat&0x01;
  25. Delay_OneWire(5);
  26. DQ = 1;
  27. dat >>= 1;
  28. }
  29. Delay_OneWire(5);
  30. }
  31. //从DS18B20读取一个字节
  32. unsigned char Read_DS18B20(void)
  33. {
  34. unsigned char i;
  35. unsigned char dat;
  36. for(i=0;i<8;i++)
  37. {
  38. DQ = 0;
  39. dat >>= 1;
  40. DQ = 1;
  41. if(DQ)
  42. {
  43. dat |= 0x80;
  44. }
  45. Delay_OneWire(5);
  46. }
  47. return dat;
  48. }
  49. //DS18B20设备初始化
  50. bit init_ds18b20(void)
  51. {
  52. bit initflag = 0;
  53. DQ = 1;
  54. Delay_OneWire(12);
  55. DQ = 0;
  56. Delay_OneWire(80);
  57. DQ = 1;
  58. Delay_OneWire(10);
  59. initflag = DQ;
  60. Delay_OneWire(5);
  61. return initflag;
  62. }
  63. float DS18B20_GetWenDu(){
  64. float wen_du,zs,xs;
  65. unsigned char di,gao;
  66. init_ds18b20();
  67. Write_DS18B20(0xcc);
  68. Write_DS18B20(0x44);
  69. Delay_OneWire(200);
  70. init_ds18b20();
  71. Write_DS18B20(0xcc);
  72. Write_DS18B20(0xbe);
  73. di=Read_DS18B20();
  74. gao=Read_DS18B20();
  75. if(gao>=240){
  76. di=~di;
  77. gao=~gao;
  78. if(di==0xff){
  79. di=0;
  80. gao++;
  81. }
  82. else{
  83. di++;
  84. }
  85. zs=((gao<<4)|(di>>4))*1.0;
  86. xs=(di&0x0f)*0.0625;
  87. wen_du=-(zs+xs);
  88. }
  89. else{
  90. zs=((gao<<4)|(di>>4))*1.0;
  91. xs=(di&0x0f)*0.0625;
  92. wen_du=(zs+xs);
  93. }
  94. return wen_du;
  95. }

LN555.h

  1. #ifndef __LN555_H_
  2. #define __LN555_H_
  3. #include <STC15F2K60S2.H>
  4. void LN555_DSQ_0_Init();
  5. #endif

LN555.c

  1. #include "LN555.h"
  2. void LN555_DSQ_0_Init(){
  3. TMOD=(TMOD&0xf0)|0x05;
  4. TH0=0;
  5. TL0=0;
  6. ET0=0;
  7. TR0=1;
  8. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/120413
推荐阅读
相关标签
  

闽ICP备14008679号