 |

|
Elektronika.lt portalo forumas
Jūs esate neprisijungęs lankytojas. Norint dalyvauti diskusijose, būtina užsiregistruoti ir prisijungti prie forumo.
Prisijungę galėsite kurti naujas temas, atsakyti į kitų užduotus klausimus, balsuoti forumo apklausose.
Administracija pasilieka teisę pašalinti pasisakymus bei dalyvius,
kurie nesilaiko forumo taisyklių.
Pastebėjus nusižengimus, prašome pranešti.
Dabar yra 2025 07 19, 03:30. Visos datos yra GMT + 2 valandos.
|
|
|
 |
Forumas » Mikrovaldikliai » SHT71+atmega8 (16)
|
Jūs negalite rašyti naujų pranešimų į šį forumą Jūs negalite atsakinėti į pranešimus šiame forume Jūs negalite redaguoti savo pranešimų šiame forume Jūs negalite ištrinti savo pranešimų šiame forume Jūs negalite dalyvauti apklausose šiame forume
|
|
|
 |
 |
 |
SHT71+atmega8 (16) |
Parašytas: 2010 02 08, 13:11 |
|
|
|
Sveiki,
Siuo metu bandau susieti SHT71( dregmes ir temp daviklis) su atmega16 (galima ir atmega8). Problemeles tokios. Pajungiau SHT clock koja prie atmega16 B porto "0" pino, data prie "1" pino, taip pat ir 10komu varza is Vcc i data koja, zdz viska pagal datasheeta. Sxemute per PC com porta prijungiama ir per Hyper Terminal parodo kokia dregeme ir temp. Man ismeta tik:"Temp: ? Feuchte(dregme): ? Taupunkt: ? Status: ? Error: ? "
Problemos ir klausimai tokie:
1. Ar teisingai pajungiau/kaip tesingai pajungti sht davikli?
2. Kur ikisti kelias eilutes kodo, kad ijungtu LED ant A pino "0" kojos( nes i main funkcija iterpus, kazkodel aktyvuojami visi PORTA input registro bitai. Tas pats ir su kitais portais. Atmega16 dirba visiniu 8MHz dazniu
VIsas avrstudio projektas: http://www.failai.in/show/F/9416E5F42/sht71iratmega16.rar
Prisegu su avr studio 4.18 rasytus kodus:
main.c failas:
Kodas: |
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include <stdio.h>
#include <compat/deprecated.h>
#include "sht75.h"
#define LED_DDR DDRA
#define LED_PORT PORTA
#define LED_POWER_PIN 4
#ifndef F_CPU
/* In neueren Version der WinAVR/Mfile Makefile-Vorlage kann
F_CPU im Makefile definiert werden, eine nochmalige Definition
hier wuerde zu einer Compilerwarnung fuehren. Daher "Schutz" durch
#ifndef/#endif
Dieser "Schutz" kann zu Debugsessions führen, wenn AVRStudio
verwendet wird und dort eine andere, nicht zur Hardware passende
Taktrate eingestellt ist: Dann wird die folgende Definition
nicht verwendet, sondern stattdessen der Defaultwert (8 MHz?)
von AVRStudio - daher Ausgabe einer Warnung falls F_CPU
noch nicht definiert: */
#define F_CPU 8000000UL // Systemtakt in Hz - Definition als unsigned long beachten
// Ohne ergeben sich unten Fehler in der Berechnung
#endif
#define BAUD 9600UL // Baudrate
// Berechnungen
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // clever runden
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Reale Baudrate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!
#endif
#define MSG_size 34 // Davon 2byte für Status (MSG Nr, MSG Länge)
#define Anzahl_Meldungen 240 // Anzahl der Meldungen
unsigned char rep;
static unsigned char index;
static unsigned char inbuf[50];
unsigned char error=0;
unsigned char checksum;
char disp_buffer[80];
//****************************************************************************
void answer_uart(unsigned char c){
while (!(UCSRA & (1<<UDRE))) /* warten bis Senden moeglich */
{
}
UDR = c;
}
//****************************************************************************
int uart_putc(unsigned char c)
{
while (!(UCSRA & (1<<UDRE)))
{
}
UDR = c;
return 0;
}
//****************************************************************************
void uart_puts (char *s)
{
while (*s)
{
uart_putc(*s);
s++;
}
}
//----------------------------------------------------------------------------
ISR(USART_RXC_vect)
{
unsigned int c;
c = UDR;
if ((c==0xFF) & (inbuf[0]==0xAA) & (inbuf[1]== 0xAA))
{
answer_uart(0x15);
index = 0;
cli();
wdt_enable(0);
while(1) {};
}
else
{
inbuf[index] = c;
index++;
}
}
//----------------------------------------------------------------------------
// USART Init
void init_uart(void)
{
UBRRL = UBRR_VAL;
UCSRB = UCSRB | (1<<RXEN) | (1<<TXEN) | (1<<RXCIE);
// UCSRB = UCSRB | (1<<TXEN);
UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
// sbi(LED_DDR, LED_POWER_PIN); // Set pin to output.
// sbi(LED_PORT, LED_POWER_PIN);
}
int main(void) // Mainprogramm
{
cli();
// Disable Watchdog. Wird vom Bootloader benötigt.
MCUCSR = 0;
GICR = 0x00;
wdt_disable();
init_uart();
sht75_array sht75;
uint8_t sht75_status;
uint8_t sht75_error;
sbi(LED_DDR, LED_POWER_PIN); // Set pin to output.
cbi(LED_PORT, LED_POWER_PIN);
ShtInit();
sei();
answer_uart(0x06);
_delay_ms(1000);
ShtReset();
ShtReadEverything(&sht75);
sht75_error = ShtReadStatus(&sht75_status);
answer_uart(sht75_error);
if (sht75_error == 0)
{
sprintf(disp_buffer,"Temp: %+02.2f Feuchte: %2.2f Taupunkt: %+02.2f Status: %02x Error: %02x\n\r",sht75.Temperature,sht75.Humidity,sht75.Dewpoint,sht75_status,sht75_error);
uart_puts(disp_buffer);
}
sbi(LED_PORT, LED_POWER_PIN);
while(1) {
}
}
|
sht75.c failas:
Kodas: |
//----------------------------------------------------------------------------------
//
// Sensirion SHTxx Humidity Sensor Library
//
// Library for using the SHT1x humidity and temperature
// sensors from Sensirion (http://www.sensirion.com).
// Based on Sensirion application note "Sample Code SHTxx", adapted by
// Brian Low (mportobello@hotmail.com)
//
// History:
// 2003-Jul-03 BL - Created
// 2005-Mar-05 bue - Adapted by Daniel Buergin
//
//----------------------------------------------------------------------------------
// Knowhow:
// - Making a Pin high: PORTB |= (1<<2);
// - Making a Pin low : PORTB &= ~(1<<2);
//----------------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include <stdio.h>
#include <math.h>
//#include <stdlib.h>
#include "sht75.h"
//----------------------------------------------------------------------------------
// Set the DDR Register to 1 (high)
// Result: PIN is ready for Output
void enable_data(void) { SHT_DDR |= (1 << SHT_DATA); } // Drive DATA pin
// Set the DDR Register to 0 (low) and the PORT Register to 1 (high)
// Result: PIN is ready for Input with Pull-UP
void disable_data(void) { SHT_DDR &= ~(1 << SHT_DATA);
SHT_PORT |= (1 << SHT_DATA); }
// Set DATA PORT Register to 1 (high)
void data_high(void) { SHT_PORT |= _BV(SHT_DATA); }
// Set DATA PORT Register to 0 (low)
void data_low(void) { SHT_PORT &= ~_BV(SHT_DATA); }
// Set CLOCK PORT Register to 1 (high)
void clock_high(void) { SHT_PORT |= _BV(SHT_CLOCK); }
// Set CLOCK PORT Register to 0 (low)
void clock_low(void) { SHT_PORT &= ~_BV(SHT_CLOCK); }
// Set CLOCK DDR Register to 1 (high)
void enable_clock(void) { SHT_DDR |= (1 << SHT_CLOCK); }
//----------------------------------------------------------------------------------
// Initialize AVR i/o pins.
//----------------------------------------------------------------------------------
void ShtInit(void)
{
enable_clock();
clock_low();
disable_data();
}
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
//
//----------------------------------------------------------------------------------
void transstart(void)
{
enable_data();
asm volatile ("nop"::);
data_high();
clock_low();
asm volatile ("nop"::);
asm volatile ("nop"::);
clock_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
data_low();
asm volatile ("nop"::);
asm volatile ("nop"::);
clock_low();
asm volatile ("nop"::);
asm volatile ("nop"::);
asm volatile ("nop"::);
asm volatile ("nop"::);
asm volatile ("nop"::);
asm volatile ("nop"::);
clock_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
data_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
clock_low();
}
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
//----------------------------------------------------------------------------------
void ShtReset(void)
{
unsigned char i;
// Set DATA Line to Input
enable_data();
asm volatile ("nop"::);
data_high();
// Prepare CLOCK-Line for Pulses
clock_low();
// Send 9 Pulses on the CLOCK-Line
for(i=0;i<10;i++)
{
clock_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
clock_low();
asm volatile ("nop"::);
asm volatile ("nop"::);
}
transstart(); //transmission start
}
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
//----------------------------------------------------------------------------------
char read_byte(void)
{
unsigned char i,val=0;
// Set the DATA Line to Input.
// Important: If there is no external Pull-Up Resistor
disable_data();
asm volatile ("nop"::);
// Read in 8 bits, LSB first
for (i=0x80;i>0;i/=2)
{
clock_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
if (bit_is_set(SHT_PIN, SHT_DATA))
{
val=(val | i); // Got a Bit
}
clock_low();
asm volatile ("nop"::);
asm volatile ("nop"::);
}
// Send ACK
enable_data();
asm volatile ("nop"::);
data_low();
asm volatile ("nop"::);
clock_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
asm volatile ("nop"::);
clock_low();
asm volatile ("nop"::);
asm volatile ("nop"::);
disable_data();
return val;
}
//----------------------------------------------------------------------------------
// Writes a byte on the Sensibus and checks the acknowledge.
// Returns 0 if the successful
//----------------------------------------------------------------------------------
char write_byte(unsigned char value)
{
unsigned char i;
unsigned char error = 0;
// Set the DATA Line to Output
enable_data();
asm volatile ("nop"::);
// Write each bit one at a time, LSB first
for (i=0x80;i>0;i/=2)
{
if (i & value)
{
data_high(); // Write a 1
}
else
{
data_low(); // Write a 0
}
clock_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
clock_low();
asm volatile ("nop"::);
asm volatile ("nop"::);
}
disable_data();
asm volatile ("nop"::);
// Read ACK
clock_high();
asm volatile ("nop"::);
asm volatile ("nop"::);
error = bit_is_set(SHT_PIN, SHT_DATA);
clock_low();
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
// Read temperature from the sensor.
// Returns 0xFFFF if the measurment failed
//----------------------------------------------------------------------------------
int ShtMeasure(unsigned char mode)
{
unsigned int temp = 0xFFFF;
unsigned char checksum;
unsigned char c;
unsigned int error = 0;
// Signal start of communications
transstart();
// Request measurement
error = write_byte(mode);
if (error > 0)
{
printf("Error in write_byte\n");
}
// Sensor lowers the data line when measurement
// is complete. Wait up to 2 seconds for this.
for (c=0; c<20; c++)
{
if (! bit_is_set(SHT_PIN, SHT_DATA))
{
break;
}
_delay_ms(1000); //my_delay_10ms(30);
}
// Read the measurement
if (! bit_is_set(SHT_PIN, SHT_DATA))
{
temp = read_byte();
temp = temp << 8;
temp += read_byte();
checksum = read_byte();
}
return temp;
}
//----------------------------------------------------------------------------------------
// Read the Sensordata Temperature and Humidity and calculates
// the real Temperature and the Temperature compensated Humidity
// Calculates also the DewPoint
//
// input: Pointer to a Struct
// ouput: Nothing
//----------------------------------------------------------------------------------------
void ShtReadEverything(sht75_array *sht75_struct)
{
int T;
int RH;
double RHlinear = 0.0;
double RHtrue = 0.0;
double Temp = 0.0;
double logEx = 0.0;
double DewPoint = 0.0;
T = ShtMeasure(SHT_TEMPERATURE);
RH = ShtMeasure(SHT_HUMIDITY);
// Calculate True Temperature (see also Sensirion SHT75 Datasheet)
Temp = ((double) T)*0.01 - 40.0;
// Calculate Treu Humidity (see also Sensirion SHT75 Datasheet)
RHlinear = -4.0 + 0.0405 * ((double) RH) - 0.0000028 * ((double) RH);
RHtrue = (Temp - 25) * (0.01 + 0.00008 * (double) RH) + RHlinear;
// Calculate Dewpoint (see also Sensirion SHT75 Datasheet)
logEx = 0.66077 + (7.5 * Temp / (237.3 + Temp)) + (log10 (RHtrue) -2);
DewPoint = ((0.66077 - logEx) * 237.3) / (logEx - 8.16077);
(*sht75_struct).Temperature = Temp;
(*sht75_struct).Humidity = RHtrue;
(*sht75_struct).Dewpoint = DewPoint;
}
//----------------------------------------------------------------------------------
// Reads the status register with checksum (8-bit)
//----------------------------------------------------------------------------------
char ShtReadStatus(unsigned char *p_value)
{
unsigned char error=0;
unsigned char checksum=0;
transstart(); //transmission start
error = write_byte(STATUS_REG_R); //send command to sensor
*p_value = read_byte(); //read status register (8-bit)
checksum = read_byte(); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
// Writes the status register . Note this library only supports the default
// 14 bit temp and 12 bit humidity readings.
//----------------------------------------------------------------------------------
//
//char ShtWriteStatus(unsigned char value)
//{
// unsigned char error=0;
// transstart(); //transmission start
// error += write_byte(STATUS_REG_W); //send command to sensor
// error += write_byte(value); //send value of status register
// return error; //error>=1 in case of no response form the sensor
//}
|
sht75.h failas:
Kodas: |
//----------------------------------------------------------------------------------
//
// Sensirion SHTxx Humidity Sensor Library
//
// Library for using the SHT1x humidity and temperature
// sensors from Sensirion (http://www.sensirion.com).
// Based on Sensirion application note "Sample Code SHTxx", adapted by
// Brian Low (mportobello@hotmail.com)
//
// History:
// 2003-Jul-03 BL - Created
// 2005-Mar-05 bue - Adapted by Daniel Buergin
//
//----------------------------------------------------------------------------------
#ifndef __sht75_h
#define __sht75_h
//#include "main.h"
#define SHT_TEMPERATURE 0x03 // Measure temp - for ShtMeasure
#define SHT_HUMIDITY 0x05 // Measure humidity - for ShtMeasure
#define SHT_DDR DDRB // Port with clock and data pins
#define SHT_PORT PORTB // Port with clock and data pins
#define SHT_PIN PINB // Port with clock and data pins
#define SHT_CLOCK 0 // Pin used to output clock to SHT
#define SHT_DATA 1 // Pin used to read/output data from/to SHT
#define SHT_DELAY 25 // uS delay between clock rise/fall
#define STATUS_REG_W 0x06 // Command to read status register
#define STATUS_REG_R 0x07 // Command to write status register
#define RESET 0x1e // Command for soft reset (not currently used)
// Struct for the Data from the Sensor
typedef struct sht75st
{
double Temperature;
double Humidity;
double Dewpoint;
} sht75_array;
void ShtInit(void);
void ShtReset(void);
int ShtMeasure(unsigned char mode);
void ShtReadEverything(sht75_array *sht75_struct);
char ShtReadStatus(unsigned char *p_value);
// char ShtWriteStatus(unsigned char value);
#endif
|
[/code] |
|
|
|
|
 |
 |
SHT71+atmega8 (16) |
Parašytas: 2010 02 08, 14:56 |
|
|
|
Kaip manai, kas cia tas litanijas skaitys.... |
|
|
|
|
 |
SHT71+atmega8 (16) |
Parašytas: 2010 02 08, 15:10 |
|
|
|
Edzma rašo: |
Kaip manai, kas cia tas litanijas skaitys.... |
Tikrai ne tu... |
|
|
|
|
 |
SHT71+atmega8 (16) |
Parašytas: 2010 02 08, 15:15 |
|
|
|
MarcaZ rašo: |
Edzma rašo: |
Kaip manai, kas cia tas litanijas skaitys.... |
Tikrai ne tu... |
Nepyk, bet cia ir zinantis neskaitys, cia reikia marias laiko....eikejo akcentuot koki tai epizoda ,kur neaisku... |
|
|
|
|
 |
SHT71+atmega8 (16) |
Parašytas: 2010 02 08, 16:17 |
|
|
|
Edzma rašo: |
MarcaZ rašo: |
Edzma rašo: |
Kaip manai, kas cia tas litanijas skaitys.... |
Tikrai ne tu... |
Nepyk, bet cia ir zinantis neskaitys, cia reikia marias laiko....eikejo akcentuot koki tai epizoda ,kur neaisku... |
Neturi ka veikti?
Zinantis supras, kad cia tikrai nereikia "mariu" laiko ) |
|
|
|
|
 |
SHT71+atmega8 (16) |
Parašytas: 2010 02 08, 17:25 |
|
|
|
Na pranesimas tvarkingai parasytas tai galima zinantiems ir paskaityt
Nelabai ka siuo atveju suprantu, bet sakai
Citata: |
Kur ikisti kelias eilutes kodo, kad ijungtu LED ant A pino "0" kojos( nes i main funkcija iterpus, kazkodel aktyvuojami visi PORTA input registro bitai |
tai siulyciau ijungiant rasyti DDRA|=(1<<0); PORTA|=(1<<0); o ne sbi..ir kazkas ten.. |
|
_________________ Aš pažeidžiau forumo taisykles ir reklamavau paraše. ...Bet uz reklama tai susimokejau  |
|
|
|
|
 |
Google paieška forume |
|
 |
Naujos temos forume |
|
 |
FS25 Tractors
Farming Simulator 25 Mods,
FS25 Maps,
FS25 Trucks |
 |
ETS2 Mods
ETS2 Trucks,
ETS2 Bus,
Euro Truck Simulator 2 Mods
|
 |
FS22 Tractors
Farming Simulator 22 Mods,
FS22 Maps,
FS25 Mods |
 |
VAT calculator
VAT number check,
What is VAT,
How much is VAT |
 |
FS25 Mods
FS25 Harvesters,
FS25 Tractors Mods,
FS25 Maps Mods |
 |
Dantų protezavimas
All on 4 implantai,
Endodontija mikroskopu,
Dantų implantacija |
 |
FS25 Mods
FS25 Maps,
FS25 Cheats,
FS25 Install Mods |
 |
FS25 Mods
Farming Simulator 25 Mods,
FS25 Maps |
 |
ATS Trailers
American Truck Simulator Mods,
ATS Trucks,
ATS Maps |
 |
Football Training Kit
Football Training Equipment,
Football Skills,
Football Training |
|

|
 |