找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3245|回复: 0
打印 上一主题 下一主题
收起左侧

ICC-AVR DS1821S温度报警程序

[复制链接]
跳转到指定楼层
楼主
ID:125100 发表于 2016-6-4 17:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用DS1821的测温报警输出功能,已经过实测,可以用的,已批量用于产品。

  1. //ICC-AVR application builder : 2015-10-8 14:15:46
  2. // Target : M8
  3. // Crystal: 8.0000Mhz
  4. /*新片烧录程序,电容加热温度高低设置成0x14,0x13;化雪板设置成0X02,0X00*/
  5. #include <iom8v.h>
  6. #include <macros.h>


  7. #define uchar         unsigned char
  8. #define uint         unsigned int
  9. #define sint         short int
  10. #define uint8        uchar
  11. #define uint16        unsigned short
  12. #define        uint32        unsigned int   
  13. #define bool        uchar

  14. #define DS1620 0      
  15. #define CLK           1          
  16. #define        RST           2
  17. #define        LEDL   3
  18. #define        LEDH   2
  19. #define TEMPH  0x02            
  20. #define TEMPL  0x00               

  21. void Delay_ms(uint ms)
  22. {
  23.         unsigned int i,j;
  24.         for(i=0;i<ms;i++)
  25.                 for(j=0;j<1141;j++);        //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
  26. }

  27. void Delay_us(uint us)/////6us
  28. {
  29.         unsigned int i,j;
  30.        
  31.         for(i=0;i<us;i++);
  32.                 //for(j=0;j<1;j++);        //114是在8MHz晶振下,通过软件仿真反复实验得到的数值

  33. }
  34. void Delay(uint ms)//////60us
  35. {
  36.         unsigned int i,j;
  37.         for(i=0;i<ms;i++)
  38.                 for(j=0;j<60;j++);        //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
  39. }
  40. void Delay40(uint ms)//////60us
  41. {
  42.         unsigned int i,j;
  43.         for(i=0;i<ms;i++)
  44.                 for(j=0;j<40;j++);        //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
  45. }
  46. void Delay_1slot(unsigned int x)/////5us
  47. {
  48.                 unsigned int i;
  49.                 i=x;
  50.                 while(i>0)i--;
  51. }
  52. void Delay_1us(uint ms)
  53. {
  54.         unsigned int i;
  55.         i=0;
  56.         i=0;
  57.         i=0;
  58.         i=0;
  59.        
  60. }

  61. void port_init(void)
  62. {
  63. PORTB = 0x07;
  64. DDRB  = 0x07;
  65. PORTC = 0x00; //m103 output only
  66. DDRC  = 0x00;
  67. PORTD = 0x0f;
  68. DDRD  = 0x0f;
  69. }
  70. void led_rotation(void)
  71. {
  72.     uchar j;
  73.         for(j=0;j<4;j++)//循环点亮
  74.         {
  75.                 PORTD&=~BIT(3-j);
  76.                 Delay_ms(60);
  77.                 PORTD|=BIT(3-j);
  78.         }
  79. }

  80. //UART0 initialize
  81. // desired baud rate: 9600
  82. // actual: baud rate:9615 (0.2%)
  83. // char size: 8 bit
  84. // parity: Disabled
  85. void uart0_init(void)
  86. {
  87. UCSRB = 0x00; //disable while setting baud rate
  88. UCSRA = 0x00;
  89. UCSRC = BIT(URSEL) | 0x06;
  90. UBRRL = 0x33; //set baud rate lo
  91. UBRRH = 0x00; //set baud rate hi
  92. UCSRB = 0x18;
  93. }

  94. //call this routine to initialize all peripherals
  95. void init_devices(void)
  96. {
  97. //stop errant interrupts until set up
  98. CLI(); //disable all interrupts
  99. port_init();
  100. // uart0_init();
  101. MCUCR = 0x00;
  102. GICR  = 0x00;
  103. TIMSK = 0x00; //timer interrupt sources
  104. // SEI(); //re-enable interrupts
  105. //all peripherals are now initialized
  106. }

  107. /****************************************************************************
  108. * 名称:UartSendByte()
  109. * 功能:向串口发送字节数据。
  110. * 入口参数:data                要发送的数据
  111. * 出口参数:无
  112. ****************************************************************************/
  113. void  uart0SendByte(uint8 data)
  114. {  
  115.         while( !( UCSRA & (1<<UDRE) ) );        //上次发送有没有完成
  116.         UDR = data;                               //发送数据
  117. }
  118. /****************************************************************************
  119. * 名称:UartSendString()
  120. * 功能:向串口发送字符串。
  121. * 入口参数:data                要发送的数据
  122. * 出口参数:无
  123. ****************************************************************************/
  124. void  uart0SendString(uint8 *ptr)
  125. {
  126.         while(*ptr)
  127.         {
  128.                 uart0SendByte(*ptr++);
  129.         }
  130.         uart0SendByte(0x0D);
  131.         uart0SendByte(0x0A);        //结尾发送回车换行
  132. }
  133. /****************************************************************************
  134. * 名称:UartRcvByte()
  135. * 功能:从串口接收字节数据。
  136. * 入口参数:无
  137. * 出口参数:data                接收到的数据
  138. ****************************************************************************/
  139. uint8 uart0RcvByte(void)        //接收采用查询方式
  140. {
  141.         while( !( UCSRA & (1<<RXC) ) );        //有没有接收到数据
  142.         return UDR;                                              //获取并返回数据
  143. }

  144. void ds1821_reset()
  145. {
  146. //uchar ack;
  147. DDRB|=BIT(DS1620);
  148. PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  149. Delay_us(600);
  150. PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  151. //Delay(1);
  152. DDRB&=~BIT(DS1620);
  153. while((PINB&BIT(DS1620)));
  154. while(!(PINB&BIT(DS1620)));
  155. //  Delay_us(150);
  156. // ack=PINB;
  157. //  ack&=0x01;
  158.   Delay_us(50);
  159. // return ack;
  160. }

  161. void DS1821_WriteByte(uint8 dat)
  162. {
  163. uint8 i;
  164. DDRB|=BIT(DS1620);
  165. for(i=0;i<8;i++)
  166. {
  167.         PORTB&=~BIT(DS1620);
  168.         Delay_1slot(0);
  169.         Delay_1slot(0);
  170.     if (dat&0x01)
  171.         { // bit = 1 LSB first
  172.            PORTB|=BIT(DS1620);
  173.     }
  174.         else
  175.         { // bit = 0
  176.       PORTB&=~BIT(DS1620);
  177.     }
  178.         Delay40(1);
  179.         PORTB|=BIT(DS1620);
  180.         Delay_1slot(0);
  181.     dat >>= 1;
  182.        
  183.        
  184.   }
  185. DDRB&=~BIT(DS1620);
  186. }

  187. uint8 DS1821_ReadByte(void)
  188. {
  189. uint8 dat,i;
  190. dat=0;
  191. for(i=0;i<8;i++)
  192. {
  193.   dat >>= 1;
  194.   DDRB|=BIT(DS1620);//设置p01为输出状态
  195.   PORTB&=~BIT(DS1620);
  196.   PORTB|=BIT(DS1620);
  197.   DDRB&=~BIT(DS1620);;//设置p01为输入状态
  198.   PORTB&=~BIT(RST);
  199.   PORTB|=BIT(RST);
  200.   if(PINB&BIT(DS1620))   
  201.   {
  202.     dat|=0x80; // msb 優先
  203.   }
  204.    Delay40(1);//等40us, 加上其它程式碼, 每個bit有80us
  205. }     
  206. return dat;
  207. }

  208. void DS1821_switchmode(void)
  209. {
  210.   uint8 i;
  211.   DDRB|=BIT(DS1620);
  212.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  213. //  Delay_ms(3000);
  214.         Delay_1slot(1);
  215.   PORTB&=~BIT(CLK);
  216.   Delay_1slot(2);
  217.   
  218.   for(i=0;i<16;i++)
  219.   {
  220.    PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  221.   //Delay_1slot(1);
  222.           PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  223.   //Delay_1slot(1);
  224.   
  225.   
  226.   }
  227.   /*
  228.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  229.   Delay_1slot(1);
  230.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  231.   Delay_1slot(1);
  232.   
  233.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  234.   Delay_1slot(1);
  235.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  236.   Delay_1slot(1);
  237.   
  238.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  239.   Delay_1slot(1);
  240.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  241.   Delay_1slot(1);
  242.   
  243.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  244.   Delay_1slot(1);
  245.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  246.   Delay_1slot(1);
  247.   
  248.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  249.   Delay_1slot(1);
  250.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  251.   Delay_1slot(1);

  252.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  253.   Delay_1slot(1);
  254.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  255.   Delay_1slot(1);

  256.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  257.   Delay_1slot(1);
  258.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  259.   Delay_1slot(1);
  260.   
  261.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  262.   Delay_1slot(1);
  263.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  264.   Delay_1slot(1);
  265.       
  266.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  267.   Delay_1slot(1);
  268.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  269.   Delay_1slot(1);  

  270.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  271.   Delay_1slot(1);
  272.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  273.   Delay_1slot(1);

  274.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  275.   Delay_1slot(1);
  276.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  277.   Delay_1slot(1);
  278.   
  279.   PORTB&=~BIT(DS1620);                //是低则将单总线拉低
  280.   Delay_1slot(1);
  281.   PORTB|=BIT(DS1620);                        //是高则将单总线拉高
  282.   Delay_1slot(1);
  283.   */
  284.   Delay_1slot(2);
  285.   PORTB|=BIT(CLK);
  286. }

  287. /*/看门狗启动函数
  288. void WDT_ON()
  289. {
  290.         WDTCR=0x0f;                //WDE=1-看门狗使能,WDP0:1:2=1:1:1-2秒喂狗。               
  291. }

  292. //看门狗关闭函数
  293. void WDT_OFF()
  294. {
  295.         WDTCR|=BIT(WDTOE)|BIT(WDE);        //制造4个周期关闭时间
  296.         WDTCR&=~BIT(WDE);                        //关闭看门狗
  297. }*/


  298. int main()
  299. {
  300. uint8 data;
  301. char a;
  302. int d,j,i,cntl=0,cnth=0,cntm=0;
  303. init_devices();
  304. led_rotation();
  305. //uart0SendByte(0x80);
  306. ds1821_reset();
  307. DS1821_switchmode();
  308. Delay_us(2);

  309. // uart0SendByte(0x81);
  310. /*  DS1821_switchmode();                //模式转换
  311.   ds1821_reset();
  312.   DS1821_WriteByte(0x0c);  //写状态寄存器值
  313.   DS1821_WriteByte(0x02);
  314.   Delay_us(4);
  315. // data=DS1821_ReadByte();     */  //模式转换,芯片不需要
  316.   
  317. //  uart0SendByte(data);
  318. // uart0SendByte(0x82);
  319. //  data=&0x04;
  320. //  if(data==0x00)  PORTD&=~BIT(2);        //工作在1线mode,点灯3
  321. ds1821_reset();
  322. // Delay_us(4);
  323. // uart0SendByte(0x83);
  324. DS1821_WriteByte(0x01);  //写入温度上限
  325. DS1821_WriteByte(TEMPH);
  326.   Delay_us(4);
  327. ds1821_reset();
  328. DS1821_WriteByte(0x02);  //写入温度下限
  329. DS1821_WriteByte(TEMPL);
  330.   Delay_us(4);
  331. ds1821_reset();
  332. DS1821_WriteByte(0xa1);  //读温度上限
  333. data=DS1821_ReadByte();
  334. if(data==TEMPH)
  335.     PORTD&=~BIT(LEDH);       
  336. else
  337.     PORTD|=BIT(LEDH);
  338. ds1821_reset();
  339. DS1821_WriteByte(0xa2);   //读温度下限
  340. data=DS1821_ReadByte();
  341. if(data==TEMPL)
  342.         PORTD&=~BIT(LEDL);       
  343.   else       
  344.         PORTD|=BIT(LEDL);
  345.        
  346. ds1821_reset();       
  347. DS1821_WriteByte(0x0c);  //写状态寄存器值
  348. DS1821_WriteByte(0x46);
  349. Delay_us(4);
  350. //return 0;   
  351. while(1);
  352. }
  353. //  uart0SendByte(0x84);
  354.   
  355. // ds1821_reset();
  356. // DS1821_WriteByte(0xee);   //开始温度转换指令
  357. //Delay_us(4);


  358. //WDT_ON();
  359. // while(1)
  360. // {
  361. //WDR();

  362. // uart0SendByte(0x55);
  363.    
  364. // ds1821_reset();
  365. /*  DS1821_WriteByte(0xac);
  366.   data=DS1821_ReadByte();

  367.   if((data&0x04)!=0)
  368.   {
  369.            cntm++;
  370.           if(cntm%2==0)
  371.                    PORTD&=~BIT(CLK);       
  372.           else       
  373.                 PORTD|=BIT(CLK);
  374.        
  375. }

  376. uart0SendByte(data);
  377. Delay_us(4);
  378.    
  379.   ds1821_reset();
  380. DS1821_WriteByte(0xa1);
  381. data=DS1821_ReadByte();

  382. if(data==0x02)
  383. {
  384.   cnth++;
  385.   if(cnth%2==0)
  386.            PORTD&=~BIT(LEDH);       
  387.   else       
  388.         PORTD|=BIT(LEDH);
  389.        
  390. }

  391. uart0SendByte(data);
  392. Delay_us(4);
  393.    ds1821_reset();
  394. DS1821_WriteByte(0xa2);
  395. data=DS1821_ReadByte();
  396.   
  397.   if(data==0x00)
  398. {
  399.   cntl++;
  400.   if(cntl%2==0)
  401.            PORTD&=~BIT(LEDL);       
  402.   else       
  403.         PORTD|=BIT(LEDL);
  404.        
  405. }


  406. uart0SendByte(data);
  407. Delay_us(4);


  408.    ds1821_reset();
  409. DS1821_WriteByte(0xaa);
  410. data=DS1821_ReadByte();
  411. uart0SendByte(data);


  412. //  WDT_OFF();
  413. //uart0SendByte(0x55);
  414. Delay_ms(2000);

  415. // PORTB&=~BIT(RST);
  416. //PORTB&=~BIT(RST);
  417. // PORTB|=BIT(RST);
  418. //Delay_1slot(1);
  419. //i++;
  420. // i++;
  421. }



  422. return 0;   
  423. }*/
复制代码


main.rar

2.63 KB, 下载次数: 22, 下载积分: 黑币 -5

温度报警程序

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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