标题:
ICC-AVR DS1821S温度报警程序
[打印本页]
作者:
chenpenny128
时间:
2016-6-4 17:30
标题:
ICC-AVR DS1821S温度报警程序
用DS1821的测温报警输出功能,已经过实测,可以用的,已批量用于产品。
//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晶振下,通过软件仿真反复实验得到的数值
}
void Delay_us(uint us)/////6us
{
unsigned int i,j;
for(i=0;i<us;i++);
//for(j=0;j<1;j++); //114是在8MHz晶振下,通过软件仿真反复实验得到的数值
}
void Delay(uint ms)//////60us
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<60;j++); //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
}
void Delay40(uint ms)//////60us
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<40;j++); //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
}
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++)//循环点亮
{
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()
* 功能:向串口发送字节数据。
* 入口参数:data 要发送的数据
* 出口参数:无
****************************************************************************/
void uart0SendByte(uint8 data)
{
while( !( UCSRA & (1<<UDRE) ) ); //上次发送有没有完成
UDR = data; //发送数据
}
/****************************************************************************
* 名称:UartSendString()
* 功能:向串口发送字符串。
* 入口参数:data 要发送的数据
* 出口参数:无
****************************************************************************/
void uart0SendString(uint8 *ptr)
{
while(*ptr)
{
uart0SendByte(*ptr++);
}
uart0SendByte(0x0D);
uart0SendByte(0x0A); //结尾发送回车换行
}
/****************************************************************************
* 名称:UartRcvByte()
* 功能:从串口接收字节数据。
* 入口参数:无
* 出口参数:data 接收到的数据
****************************************************************************/
uint8 uart0RcvByte(void) //接收采用查询方式
{
while( !( UCSRA & (1<<RXC) ) ); //有没有接收到数据
return UDR; //获取并返回数据
}
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为输出状态
PORTB&=~BIT(DS1620);
PORTB|=BIT(DS1620);
DDRB&=~BIT(DS1620);;//设置p01为输入状态
PORTB&=~BIT(RST);
PORTB|=BIT(RST);
if(PINB&BIT(DS1620))
{
dat|=0x80; // msb 優先
}
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);
}
/*/看门狗启动函数
void WDT_ON()
{
WDTCR=0x0f; //WDE=1-看门狗使能,WDP0:1:2=1:1:1-2秒喂狗。
}
//看门狗关闭函数
void WDT_OFF()
{
WDTCR|=BIT(WDTOE)|BIT(WDE); //制造4个周期关闭时间
WDTCR&=~BIT(WDE); //关闭看门狗
}*/
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(); //模式转换
ds1821_reset();
DS1821_WriteByte(0x0c); //写状态寄存器值
DS1821_WriteByte(0x02);
Delay_us(4);
// data=DS1821_ReadByte(); */ //模式转换,芯片不需要
// 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); //写状态寄存器值
DS1821_WriteByte(0x46);
Delay_us(4);
//return 0;
while(1);
}
// uart0SendByte(0x84);
// ds1821_reset();
// DS1821_WriteByte(0xee); //开始温度转换指令
//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, 下载次数: 22, 下载积分: 黑币 -5
温度报警程序
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1