久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4040|回復: 0
打印 上一主題 下一主題
收起左側(cè)

ICC-AVR DS1821S溫度報警程序

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:125100 發(fā)表于 2016-6-4 17:30 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
用DS1821的測溫報警輸出功能,已經(jīng)過實測,可以用的,已批量用于產(chǎn)品。

  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晶振下,通過軟件仿真反復實驗得到的數(shù)值
  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晶振下,通過軟件仿真反復實驗得到的數(shù)值

  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晶振下,通過軟件仿真反復實驗得到的數(shù)值
  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晶振下,通過軟件仿真反復實驗得到的數(shù)值
  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++)//循環(huán)點亮
  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. * 功能:向串口發(fā)送字節(jié)數(shù)據(jù)。
  110. * 入口參數(shù):data                要發(fā)送的數(shù)據(jù)
  111. * 出口參數(shù):無
  112. ****************************************************************************/
  113. void  uart0SendByte(uint8 data)
  114. {  
  115.         while( !( UCSRA & (1<<UDRE) ) );        //上次發(fā)送有沒有完成
  116.         UDR = data;                               //發(fā)送數(shù)據(jù)
  117. }
  118. /****************************************************************************
  119. * 名稱:UartSendString()
  120. * 功能:向串口發(fā)送字符串。
  121. * 入口參數(shù):data                要發(fā)送的數(shù)據(jù)
  122. * 出口參數(shù):無
  123. ****************************************************************************/
  124. void  uart0SendString(uint8 *ptr)
  125. {
  126.         while(*ptr)
  127.         {
  128.                 uart0SendByte(*ptr++);
  129.         }
  130.         uart0SendByte(0x0D);
  131.         uart0SendByte(0x0A);        //結(jié)尾發(fā)送回車換行
  132. }
  133. /****************************************************************************
  134. * 名稱:UartRcvByte()
  135. * 功能:從串口接收字節(jié)數(shù)據(jù)。
  136. * 入口參數(shù):無
  137. * 出口參數(shù):data                接收到的數(shù)據(jù)
  138. ****************************************************************************/
  139. uint8 uart0RcvByte(void)        //接收采用查詢方式
  140. {
  141.         while( !( UCSRA & (1<<RXC) ) );        //有沒有接收到數(shù)據(jù)
  142.         return UDR;                                              //獲取并返回數(shù)據(jù)
  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為輸出狀態(tài)
  195.   PORTB&=~BIT(DS1620);
  196.   PORTB|=BIT(DS1620);
  197.   DDRB&=~BIT(DS1620);;//設置p01為輸入狀態(tài)
  198.   PORTB&=~BIT(RST);
  199.   PORTB|=BIT(RST);
  200.   if(PINB&BIT(DS1620))   
  201.   {
  202.     dat|=0x80; // msb 優(yōu)先
  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. /*/看門狗啟動函數(shù)
  288. void WDT_ON()
  289. {
  290.         WDTCR=0x0f;                //WDE=1-看門狗使能,WDP0:1:2=1:1:1-2秒喂狗。               
  291. }

  292. //看門狗關(guān)閉函數(shù)
  293. void WDT_OFF()
  294. {
  295.         WDTCR|=BIT(WDTOE)|BIT(WDE);        //制造4個周期關(guān)閉時間
  296.         WDTCR&=~BIT(WDE);                        //關(guān)閉看門狗
  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();                //模式轉(zhuǎn)換
  311.   ds1821_reset();
  312.   DS1821_WriteByte(0x0c);  //寫狀態(tài)寄存器值
  313.   DS1821_WriteByte(0x02);
  314.   Delay_us(4);
  315. // data=DS1821_ReadByte();     */  //模式轉(zhuǎn)換,芯片不需要
  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);  //寫狀態(tài)寄存器值
  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);   //開始溫度轉(zhuǎn)換指令
  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, 下載次數(shù): 22, 下載積分: 黑幣 -5

溫度報警程序

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 在线观看国产www | 一二区成人影院电影网 | 久久一区视频 | 成人免费视频网站在线观看 | 一道本一区二区 | 九九在线视频 | 亚洲国产高清在线观看 | 久久蜜桃精品 | 国产精品久久久久久久久久久久冷 | 久久久精品久 | 久久精品av | 欧美一级片中文字幕 | 在线观看毛片网站 | 久草视频网站 | 久久久久久国产免费视网址 | 午夜影视免费片在线观看 | 国产精品毛片无码 | 99re热精品视频 | 亚洲欧美第一视频 | 九九九国产| 亚洲精品日韩一区二区电影 | 国产激情在线播放 | 亚洲精品国产综合区久久久久久久 | 99re在线视频 | 欧美亚洲视频 | 国产精品久久国产精品99 gif | 中文字幕在线一区二区三区 | 久久久亚洲 | 国产免国产免费 | 黄页网址在线观看 | 日韩欧美国产一区二区 | 久久久www成人免费精品 | 亚洲精品美女视频 | 色狠狠一区 | 欧美aaaaaaaa| 国产日韩一区二区 | 91精品国产高清一区二区三区 | 天天搞天天操 | 尤物视频在线免费观看 | 美女一区 | 久久久久久久久蜜桃 |