Contoh Pemrograman AVR DGN Sensor
Contoh Pemrograman AVR DGN Sensor
JAN 5
Posted by pccontrol
Gambar Sckematik
Program AVR
/*********************************************
*********************************************/
#include <90s8535.h>
#include <stdio.h>
#include <delay.h>
#include <math.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B
#endasm
#include <lcd.h>
typedef union
{ unsigned int i; float f;} value;
enum {TEMP,HUMI};
typedef struct{
unsigned char second; //enter the current time, date, month, and year
unsigned char minute;
unsigned char hour;
unsigned char date;
unsigned char month;
unsigned int year;
}time;
time t;
char lcd_buffer[33];
// Function Prototypes
interrupt [TIM2_OVF] void timer2_ovf_isr(void);
char not_leap(void);
//SHT Functions
char SHT_WriteByte(unsigned char value);
char SHT_ReadByte(unsigned char ack);
void s_transstart(void);
void s_connectionreset(void);
char s_softreset(void);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
void calc_sth11(float *p_humidity ,float *p_temperature);
float calc_dewpoint(float h,float t);
void main(void)
{
// Declare your local variables here
value humi_val, temp_val;
unsigned char error, checksum, status;
float dew_point;
t.hour = 23;
t.minute = 30;
t.second = 00;
t.date = 22;
t.month = 8;
t.year = 2003;
// Port B initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=Out Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=0 State6=T State7=T
PORTD=0x00;
DDRD=0x20;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: TOSC1 pin
// Clock value: PCK2/128
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x08;
TCCR2=0x05;
TCNT2=0x00;
OCR2=0x00;
PORTB.1 = 0; // ClockLow
DDRB.1 = 1; // SCK is an output
s_connectionreset();
/*
while (1)
{
s_transstart(); //transmission start
for (i=0;i<65535;i++)
if(SHT_DATA_IN==0) break; //wait until sensor has finished the measurement
sprintf(lcd_buffer,T= %u %u %u,MSB,LSB,checksum);
lcd_gotoxy(0,1);
lcd_puts(lcd_buffer);
delay_ms(500);
}
*/
while (1)
{
error=0;
// Check heater pin
if(HEAT_SW)
status = 0b00000100; // Heater On
else status = 0b00000000; // Heater Off
s_write_statusreg(&status);
//-
// reads a byte form the Sensibus and gives an acknowledge in case of ack=1
//-
char SHT_ReadByte(unsigned char ack)
{
unsigned char i,val=0;
SHT_DATA_OUT=0; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SHT_SCK=1; //clk for SENSI-BUS
if (SHT_DATA_IN) val=(val | i); //read bit
SHT_SCK=0;
}
SHT_DATA_OUT=ack; //in case of ack==1 pull down DATA-Line
SHT_SCK=1; //clk #9 for ack
delay_us(5); //pulswith approx. 5 us
SHT_SCK=0;
SHT_DATA_OUT=0; //release DATA-line
return val;
}
//-
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
//-
void s_transstart(void)
{
SHT_DATA_OUT=0;
SHT_SCK=0; //Initial state
delay_us(1);
SHT_SCK=1;
delay_us(1);
SHT_DATA_OUT=1;
delay_us(1);
SHT_SCK=0;
delay_us(5);
SHT_SCK=1;
delay_us(1);
SHT_DATA_OUT=0;
delay_us(1);
SHT_SCK=0;
}
//-
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
//-
void s_connectionreset(void)
{
unsigned char i;
SHT_DATA_OUT=0; SHT_SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SHT_SCK=1;
delay_us(1);
SHT_SCK=0;
}
s_transstart(); //transmission start
}
//-
// resets the sensor by a softreset
//-
char s_softreset(void)
{
unsigned char error=0;
s_connectionreset(); //reset communication
error+=SHT_WriteByte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
//-
// makes a measurement (humidity/temperature) with checksum
//-
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
//-
// calculates temperature [C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [C]
//-
//
// calculates dew point
// input: humidity [%RH], temperature [C]
// output: dew point [C]
//
//-
// reads the status register with checksum (8-bit)
//-
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
{
unsigned char error=0;
s_transstart(); //transmission start
error=SHT_WriteByte(STATUS_REG_R); //send command to sensor
*p_value=SHT_ReadByte(ACK); //read status register (8-bit)
*p_checksum=SHT_ReadByte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//-
// writes the status register with checksum (8-bit)
//-
char s_write_statusreg(unsigned char *p_value)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=SHT_WriteByte(STATUS_REG_W);//send command to sensor
error+=SHT_WriteByte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}