单片机:AVR 的ATMEGA128L
温湿度传感器是SHT71,测量结果用串口发送出来
程序如下:
#include <iom128.h>
#include <macros.h>
#include "sensor.h"
#pragma interrupt_handler USART_RX_interrupt:19 //串口0接收中断
unsigned char MCU_receive_byte(void);
void Sent_command(unsigned char command);
void MCU_receive_word(void);
void Port_ini(void);
void SHT_ACK();
void SHT_end(void);
void SHT_ini(void);
void SHT_reset(void);
void SHT_start();
void SHT_measurement(unsigned char measure);
void USART_RX_interrupt(void);
void USART_TX(unsigned char data);
void USART_ini(void);
void WDT_ini(void);
//
//------------------------------
//
// 主函数
//
//------------------------------
//
void main(void)
{
unsigned int i;
//WDT_ini();
CLI();
Port_ini();
SHT_ini();
USART_ini();
SEI();
//---for test----
USART_TX(0xaa);
//---------------
for(;;)
{
//WDR(); //看门狗清零
SHT_measurement(TEMPERATURE);
for(i=0;i<65535;i++)
{
}
}
}
//
//------------------------------
//
// 串口0单字节发送
//
//------------------------------
//
void USART_TX(unsigned char data)
{
while(!(UCSR0A&(1<<UDRE0)))
{
}
UDR0 = data;
}
//
//------------------------------
//
// 串口初始化
//
//------------------------------
//
void USART_ini(void)
{
UBRR0H = 0x00;
UBRR0L = 0xCF; //波特率2400
UCSR0C = 0x06; //异步模式,无奇偶校验,一个停止位,8位数据
UCSR0B = 0x80|(1<<RXEN0)|(1<<TXEN0); //发送、接收使能
}
//
//------------------------------
//
// SHT71启动信号
//
//------------------------------
//
void SHT_start()
{
DDRD = DDRD|0x03; //?????
PORTD = CLK_L ;
PORTD = DATA_H;
NOP();
NOP();
PORTD = CLK_H;
NOP();
NOP();
PORTD = DATA_L;
NOP();
NOP();
PORTD = CLK_L;
NOP();
NOP();
PORTD = CLK_H;
NOP();
NOP();
PORTD = DATA_H;
NOP();
NOP();
PORTD = CLK_L;
}
//
//------------------------------
//
// SHT71 控制命令发送
// command = 0x03---温度
// 0x05---湿度
//
//------------------------------
//
void Sent_command(unsigned char command)
{
unsigned char model = 0x80;
unsigned char i;
DDRD = DDRD|0x03; //输出
//PORTD = CLK_L;
for(i=0;i<7;i++)
{
PORTD = CLK_L;
if((command & model) == 0)
{
PORTD = DATA_L;
}
else
{
PORTD = DATA_H;
}
PORTD = CLK_H;
//NOP();
model = model>>1;
//PORTD = CLK_L;
}
PORTD = CLK_L;
if((command & model) == 0)
{
PORTD = DATA_L;
}
else
{
PORTD = DATA_H;
}
//PORTD = DATA_L;
// NOP();
PORTD = CLK_H;
DDRD = DDRD&0xFD;
NOP();
PORTD = CLK_L;
while((PIND&0x02)==0x02)
{
}
//PORTD = 0x55;
PORTD = CLK_H;
NOP();
PORTD = CLK_L;
NOP();
}
//
//------------------------------
//
// SHT71 等待应答信号
//
//------------------------------
//
void SHT_ACK()
{
//PORTA = ~(0xFF);
while((PIND&0x02)==0x02)
{
}
//PORTA = 0x55;
}
//
//------------------------------
//
// SHT71 接收一个字节
//
//------------------------------
//
unsigned char MCU_receive_byte(void)
{
unsigned char i;
unsigned char model = 0x00;
unsigned char temp;
PORTD = CLK_L;
NOP();
for(i=0;i<8;i++)
{
PORTD = CLK_H;
//PORTD = CLK_L;
if((PIND & 0x02)==0)
{
temp = 0;
}
else
{
temp = 1;
}
model = model|(temp<<(7-i));
PORTD = CLK_L;
}
DDRD = DDRD|0x02; //单片机输出ACK
//PORTD = CLK_L;
PORTD = DATA_L; //
NOP();
PORTD = CLK_H;
DDRD = DDRD&0xFD; //单片机输入
PORTD = CLK_L;
return model;
}
//
//------------------------------
//
// SHT71 接收结果
//
//------------------------------
//
void MCU_receive_word(void)
{
unsigned char temp;
temp = MCU_receive_byte();
USART_TX(temp);
temp = MCU_receive_byte();
USART_TX(temp);
}
//
//------------------------------
//
// SHT71 启动一次测量
//
//------------------------------
//
void SHT_measurement(unsigned char measure)
{
//DDRD = DDRD|0x03; //?????
SHT_start();
Sent_command(measure);
SHT_ACK();
MCU_receive_word();
SHT_end();
}
//
//------------------------------
//
// SHT71 结束一次测量
//
//------------------------------
//
void SHT_end(void)
{
PORTD = CLK_L;
PORTD = DATA_H;
}
//
//------------------------------
//
// SHT71 复位
//
//------------------------------
//
void SHT_reset(void)
{
unsigned char i;
//DDRD = DDRD|0x03;
PORTD = DATA_H;
PORTD = CLK_L;
for (i=0;i<20 ;i++ )
{
PORTD = CLK_H;
NOP();
PORTD = CLK_L;
}
}
//
//------------------------------
//
// 看门狗初始化
//
//------------------------------
//
void WDT_ini(void)
{
WDTCR = 0x0f;
}
//
//------------------------------
//
// SHT71初始化
//
//------------------------------
//
void SHT_ini(void)
{
unsigned int i;
SHT_reset();
for(i=0;i<65535;i++)
{
}
}
//
//------------------------------
//
// 端口初始化
//
//------------------------------
//
void Port_ini(void)
{
DDRA = 0xFF;
DDRC = 0xFF;
DDRD = DDRD|0x03; //?????
}
//
//------------------------------
//
// 串口接收中断初始化
//
//------------------------------
//
void USART_RX_interrupt(void)
{
unsigned char i;
i= UDR0;
}
头文件:
#ifndef __SENSOR_H
#define __SENSOR_H
#define CLK_H PORTD|0x01
#define CLK_L PORTD&0xFE
#define DATA_H PORTD|0x02
#define DATA_L PORTD&0xFD
#define TEMPERATURE 0x03
#define HUMINITY 0x05
#endif
zigbee_904218839 2007-11-24 15:28
你好,我想请都如果要做TCP/IP协议一定要熟悉TCP/IP协议吗?
emot
liubaojian@yeah.net
用户472938 2007-11-14 12:59
不错
mubo_996067292 2006-12-7 16:37
不错,支持一下啊,多写点东西啊