|
用DS1821的測溫報警輸出功能,已經(jīng)過實測,可以用的,已批量用于產(chǎn)品。
- //ICC-AVR application builder : 2015-10-8 14:15:46
- // Target : M8
- // Crystal: 8.0000Mhz
- /*新片燒錄程序,電容加熱溫度高低設置成0x14,0x13;化雪板設置成0X02,0X00*/
- #include <iom8v.h>
- #include <macros.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define sint short int
- #define uint8 uchar
- #define uint16 unsigned short
- #define uint32 unsigned int
- #define bool uchar
- #define DS1620 0
- #define CLK 1
- #define RST 2
- #define LEDL 3
- #define LEDH 2
- #define TEMPH 0x02
- #define TEMPL 0x00
- void Delay_ms(uint ms)
- {
- unsigned int i,j;
- for(i=0;i<ms;i++)
- for(j=0;j<1141;j++); //1141是在8MHz晶振下,通過軟件仿真反復實驗得到的數(shù)值
- }
- void Delay_us(uint us)/////6us
- {
- unsigned int i,j;
-
- for(i=0;i<us;i++);
- //for(j=0;j<1;j++); //114是在8MHz晶振下,通過軟件仿真反復實驗得到的數(shù)值
- }
- void Delay(uint ms)//////60us
- {
- unsigned int i,j;
- for(i=0;i<ms;i++)
- for(j=0;j<60;j++); //1141是在8MHz晶振下,通過軟件仿真反復實驗得到的數(shù)值
- }
- void Delay40(uint ms)//////60us
- {
- unsigned int i,j;
- for(i=0;i<ms;i++)
- for(j=0;j<40;j++); //1141是在8MHz晶振下,通過軟件仿真反復實驗得到的數(shù)值
- }
- void Delay_1slot(unsigned int x)/////5us
- {
- unsigned int i;
- i=x;
- while(i>0)i--;
- }
- void Delay_1us(uint ms)
- {
- unsigned int i;
- i=0;
- i=0;
- i=0;
- i=0;
-
- }
- void port_init(void)
- {
- PORTB = 0x07;
- DDRB = 0x07;
- PORTC = 0x00; //m103 output only
- DDRC = 0x00;
- PORTD = 0x0f;
- DDRD = 0x0f;
- }
- void led_rotation(void)
- {
- uchar j;
- for(j=0;j<4;j++)//循環(huán)點亮
- {
- PORTD&=~BIT(3-j);
- Delay_ms(60);
- PORTD|=BIT(3-j);
- }
- }
- //UART0 initialize
- // desired baud rate: 9600
- // actual: baud rate:9615 (0.2%)
- // char size: 8 bit
- // parity: Disabled
- void uart0_init(void)
- {
- UCSRB = 0x00; //disable while setting baud rate
- UCSRA = 0x00;
- UCSRC = BIT(URSEL) | 0x06;
- UBRRL = 0x33; //set baud rate lo
- UBRRH = 0x00; //set baud rate hi
- UCSRB = 0x18;
- }
- //call this routine to initialize all peripherals
- void init_devices(void)
- {
- //stop errant interrupts until set up
- CLI(); //disable all interrupts
- port_init();
- // uart0_init();
- MCUCR = 0x00;
- GICR = 0x00;
- TIMSK = 0x00; //timer interrupt sources
- // SEI(); //re-enable interrupts
- //all peripherals are now initialized
- }
- /****************************************************************************
- * 名稱:UartSendByte()
- * 功能:向串口發(fā)送字節(jié)數(shù)據(jù)。
- * 入口參數(shù):data 要發(fā)送的數(shù)據(jù)
- * 出口參數(shù):無
- ****************************************************************************/
- void uart0SendByte(uint8 data)
- {
- while( !( UCSRA & (1<<UDRE) ) ); //上次發(fā)送有沒有完成
- UDR = data; //發(fā)送數(shù)據(jù)
- }
- /****************************************************************************
- * 名稱:UartSendString()
- * 功能:向串口發(fā)送字符串。
- * 入口參數(shù):data 要發(fā)送的數(shù)據(jù)
- * 出口參數(shù):無
- ****************************************************************************/
- void uart0SendString(uint8 *ptr)
- {
- while(*ptr)
- {
- uart0SendByte(*ptr++);
- }
- uart0SendByte(0x0D);
- uart0SendByte(0x0A); //結(jié)尾發(fā)送回車換行
- }
- /****************************************************************************
- * 名稱:UartRcvByte()
- * 功能:從串口接收字節(jié)數(shù)據(jù)。
- * 入口參數(shù):無
- * 出口參數(shù):data 接收到的數(shù)據(jù)
- ****************************************************************************/
- uint8 uart0RcvByte(void) //接收采用查詢方式
- {
- while( !( UCSRA & (1<<RXC) ) ); //有沒有接收到數(shù)據(jù)
- return UDR; //獲取并返回數(shù)據(jù)
- }
- void ds1821_reset()
- {
- //uchar ack;
- DDRB|=BIT(DS1620);
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_us(600);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- //Delay(1);
- DDRB&=~BIT(DS1620);
- while((PINB&BIT(DS1620)));
- while(!(PINB&BIT(DS1620)));
- // Delay_us(150);
- // ack=PINB;
- // ack&=0x01;
- Delay_us(50);
- // return ack;
- }
- void DS1821_WriteByte(uint8 dat)
- {
- uint8 i;
- DDRB|=BIT(DS1620);
- for(i=0;i<8;i++)
- {
- PORTB&=~BIT(DS1620);
- Delay_1slot(0);
- Delay_1slot(0);
- if (dat&0x01)
- { // bit = 1 LSB first
- PORTB|=BIT(DS1620);
- }
- else
- { // bit = 0
- PORTB&=~BIT(DS1620);
- }
- Delay40(1);
- PORTB|=BIT(DS1620);
- Delay_1slot(0);
- dat >>= 1;
-
-
- }
- DDRB&=~BIT(DS1620);
- }
- uint8 DS1821_ReadByte(void)
- {
- uint8 dat,i;
- dat=0;
- for(i=0;i<8;i++)
- {
- dat >>= 1;
- DDRB|=BIT(DS1620);//設置p01為輸出狀態(tài)
- PORTB&=~BIT(DS1620);
- PORTB|=BIT(DS1620);
- DDRB&=~BIT(DS1620);;//設置p01為輸入狀態(tài)
- PORTB&=~BIT(RST);
- PORTB|=BIT(RST);
- if(PINB&BIT(DS1620))
- {
- dat|=0x80; // msb 優(yōu)先
- }
- Delay40(1);//等40us, 加上其它程式碼, 每個bit有80us
- }
- return dat;
- }
- void DS1821_switchmode(void)
- {
- uint8 i;
- DDRB|=BIT(DS1620);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- // Delay_ms(3000);
- Delay_1slot(1);
- PORTB&=~BIT(CLK);
- Delay_1slot(2);
-
- for(i=0;i<16;i++)
- {
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- //Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- //Delay_1slot(1);
-
-
- }
- /*
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
-
- PORTB&=~BIT(DS1620); //是低則將單總線拉低
- Delay_1slot(1);
- PORTB|=BIT(DS1620); //是高則將單總線拉高
- Delay_1slot(1);
- */
- Delay_1slot(2);
- PORTB|=BIT(CLK);
- }
- /*/看門狗啟動函數(shù)
- void WDT_ON()
- {
- WDTCR=0x0f; //WDE=1-看門狗使能,WDP0:1:2=1:1:1-2秒喂狗。
- }
- //看門狗關(guān)閉函數(shù)
- void WDT_OFF()
- {
- WDTCR|=BIT(WDTOE)|BIT(WDE); //制造4個周期關(guān)閉時間
- WDTCR&=~BIT(WDE); //關(guān)閉看門狗
- }*/
- int main()
- {
- uint8 data;
- char a;
- int d,j,i,cntl=0,cnth=0,cntm=0;
- init_devices();
- led_rotation();
- //uart0SendByte(0x80);
- ds1821_reset();
- DS1821_switchmode();
- Delay_us(2);
-
- // uart0SendByte(0x81);
- /* DS1821_switchmode(); //模式轉(zhuǎn)換
- ds1821_reset();
- DS1821_WriteByte(0x0c); //寫狀態(tài)寄存器值
- DS1821_WriteByte(0x02);
- Delay_us(4);
- // data=DS1821_ReadByte(); */ //模式轉(zhuǎn)換,芯片不需要
-
- // uart0SendByte(data);
- // uart0SendByte(0x82);
- // data=&0x04;
- // if(data==0x00) PORTD&=~BIT(2); //工作在1線mode,點燈3
- ds1821_reset();
- // Delay_us(4);
- // uart0SendByte(0x83);
- DS1821_WriteByte(0x01); //寫入溫度上限
- DS1821_WriteByte(TEMPH);
- Delay_us(4);
- ds1821_reset();
- DS1821_WriteByte(0x02); //寫入溫度下限
- DS1821_WriteByte(TEMPL);
- Delay_us(4);
- ds1821_reset();
- DS1821_WriteByte(0xa1); //讀溫度上限
- data=DS1821_ReadByte();
- if(data==TEMPH)
- PORTD&=~BIT(LEDH);
- else
- PORTD|=BIT(LEDH);
- ds1821_reset();
- DS1821_WriteByte(0xa2); //讀溫度下限
- data=DS1821_ReadByte();
- if(data==TEMPL)
- PORTD&=~BIT(LEDL);
- else
- PORTD|=BIT(LEDL);
-
- ds1821_reset();
- DS1821_WriteByte(0x0c); //寫狀態(tài)寄存器值
- DS1821_WriteByte(0x46);
- Delay_us(4);
- //return 0;
- while(1);
- }
- // uart0SendByte(0x84);
-
- // ds1821_reset();
- // DS1821_WriteByte(0xee); //開始溫度轉(zhuǎn)換指令
- //Delay_us(4);
-
-
- //WDT_ON();
- // while(1)
- // {
- //WDR();
-
- // uart0SendByte(0x55);
-
- // ds1821_reset();
- /* DS1821_WriteByte(0xac);
- data=DS1821_ReadByte();
-
- if((data&0x04)!=0)
- {
- cntm++;
- if(cntm%2==0)
- PORTD&=~BIT(CLK);
- else
- PORTD|=BIT(CLK);
-
- }
-
- uart0SendByte(data);
- Delay_us(4);
-
- ds1821_reset();
- DS1821_WriteByte(0xa1);
- data=DS1821_ReadByte();
-
- if(data==0x02)
- {
- cnth++;
- if(cnth%2==0)
- PORTD&=~BIT(LEDH);
- else
- PORTD|=BIT(LEDH);
-
- }
-
- uart0SendByte(data);
- Delay_us(4);
- ds1821_reset();
- DS1821_WriteByte(0xa2);
- data=DS1821_ReadByte();
-
- if(data==0x00)
- {
- cntl++;
- if(cntl%2==0)
- PORTD&=~BIT(LEDL);
- else
- PORTD|=BIT(LEDL);
-
- }
-
-
- uart0SendByte(data);
- Delay_us(4);
-
- ds1821_reset();
- DS1821_WriteByte(0xaa);
- data=DS1821_ReadByte();
- uart0SendByte(data);
-
-
- // WDT_OFF();
- //uart0SendByte(0x55);
- Delay_ms(2000);
- // PORTB&=~BIT(RST);
- //PORTB&=~BIT(RST);
- // PORTB|=BIT(RST);
- //Delay_1slot(1);
- //i++;
- // i++;
- }
-
-
-
- return 0;
- }*/
復制代碼
|
-
-
main.rar
2016-6-4 17:27 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
2.63 KB, 下載次數(shù): 22, 下載積分: 黑幣 -5
溫度報警程序
評分
-
查看全部評分
|