Форум по микроконтроллерам: Термостат На Ds18b20 И Atmega8 - Форум по микроконтроллерам

Перейти к содержимому

Страница 1 из 1
  • Вы не можете создать новую тему
  • Вы не можете ответить в тему

Термостат На Ds18b20 И Atmega8

#1 Пользователь офлайн   Building 

  • Новичок
  • Pip
  • Группа: Пользователи
  • Сообщений: 1
  • Регистрация: 04 Ноябрь 09

Отправлено 04 Ноябрь 2009 - 22:31

Hi all,


I found this circuit and I built it:



I have a problem. Everything it's ok, but my circuit isn't work well. The pcb and parts were tesed. I try it two different sensor too (DS18B20), but the value of display more than 10 degrees. I use the v4.rar version. I started to learn programming. Can anybody to help me? Where can I modify on the "tr2.c" code? I would like to use this circuit.

Thanks and regards




code:

/*
Дěčňđčй Ěоńčн. ă. Ĺлĺц.
*/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h> /*F_CPU = 4000000; _delay_ms max = 65,535ms; _delay_us max = 192us */


#include "tr2.h"

/*
4000000 meg внуňđ
*/


ISR(TIMER2_OVF_vect){
if ( --time[_led] == 0 ) time_flag|= BIT(_led);
if ( --time[_01sek] == 0 ) time_flag|= BIT(_01sek);
}

void avr_init(void)
{
TCCR2=BIT(CS20);
TIMSK=BIT(TOIE2);//BIT(TOIE0)|

DDRDRIVE|=DRIVE_TEN_1|DRIVE_TEN_2;
PORTDRIVE|=swt_t2;
DDRZN|=BITZN;
DDRLED=0xFF;

sei();

return;
}
//-------------------EEPROM-------------------------
unsigned char EEPROM_read(unsigned int uiAddress)
{
while(EECR & (1<<EEWE));
EEAR = uiAddress;
EECR |= (1<<EERE);
return EEDR;
}

void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
if ( EEPROM_read(uiAddress)!=ucData){
while(EECR & (1<<EEWE));
cli();
EEAR = uiAddress;
EEDR = ucData;
EECR |= (1<<EEMWE);
EECR |= (1<<EEWE);
sei();
}
}
//-------------END------EEPROM----------------------


//**********1wire************



void pullup_off(void){
DDR_1wire&=~pwire0;
DDR_1wire&=~pwire1;
PORT_1wire&=~pwire0;
PORT_1wire&=~pwire1;
}



unsigned char readbit(void){
unsigned char i;
cli();
DDR_1wire|=out_1wire;//line down

asm("nop");
asm("nop");//1.25us
DDR_1wire&=~out_1wire;//line up through 1.25us
_delay_us(11);
i=PIN_1wire;
sei();
_delay_us(47);
return i;
}

void readbyte(unsigned char *pw0, unsigned char *pw1) {
unsigned char i,r,p;
pullup_off();
*pw0=0;
*pw1=0;
for(i=0, r=1; i<8; i++){
p=readbit();
if ( p & pwire0 ) *pw0|=r;
if ( p & pwire1 ) *pw1|=r;
r<<=1;
}
PORT_1wire|=out_1wire;
DDR_1wire|=out_1wire;
}

void writebit0(void){
cli();
DDR_1wire|=out_1wire;
_delay_us(60);
DDR_1wire&=~out_1wire;
sei();
}

void writebit1(void){
cli();
DDR_1wire|=out_1wire;
_delay_us(3);
DDR_1wire&=~out_1wire;
sei();
_delay_us(58);
}

void writebyte(unsigned char byte){
unsigned char i;
pullup_off();
for(i=0; i<8; i++){
if (byte&BIT(0))
writebit1();
else
writebit0();
byte>>=1;
}
PORT_1wire|=out_1wire;
DDR_1wire |=out_1wire;
}

void detectPresence(void){
unsigned char i;
pullup_off();
out_1wire=pwire0|pwire1;

DDR_1wire|=pwire0;
DDR_1wire|=pwire1;
_delay_ms(0.48);
DDR_1wire&=~pwire0;
DDR_1wire&=~pwire1;

cli();
_delay_us(60);
i=PIN_1wire;
sei();

if ( i & pwire0 ) out_1wire&=~pwire0;
if ( i & pwire1 ) out_1wire&=~pwire1;

if ( out_1wire ){
_delay_ms(0.42);
i=PIN_1wire;
if (!( i & pwire0 )) out_1wire&=~pwire0;
if (!( i & pwire1 )) out_1wire&=~pwire1;
}
if (out_1wire==0) state_wire=0;
else{
PORT_1wire|=out_1wire;
DDR_1wire|=out_1wire;
}
}

//***************************
void scratchpad_save(void){
detectPresence();
if (out_1wire){
writebyte(0xCC);//SKIP ROM [CCh]
writebyte(0x4E);//WRITE SCRATCHPAD [4Eh]
writebyte(0);// TH
writebyte(0);// TL
writebyte(0x7F);// 20 Statys 10bit //writebyte(0x60);// Statys 12bit byte 4 Configuration Register* 0x7F - 12 bit 750mSek; 1F-9bit 94msek
detectPresence();
if (out_1wire){// EEPROM
writebyte(0xCC);//SKIP ROM [CCh]
writebyte(0x48);//COPY SCRATCHPAD [48h]

PORT_1wire|=out_1wire;
DDR_1wire|=out_1wire;
}
}
}
//*******end***1wire************

//**************************************
void result_in_bcd(int result)
{//0000 0000 0000 (1)0000 // (2)0000 (3)0000 (4)0000 (5)0000 hi2 hi3 low4 low5
unsigned char dig_out=0;
bcd_num1=0;
bcd_num2=0;
bcd_num3=0;
bcd_num4=0;

while (result >= 1000) {
result -= 1000;
dig_out ++;
}
bcd_num1=dig_out;
dig_out = 0;
while (result >= 100) {
result -= 100;
dig_out ++;
}
bcd_num2=dig_out;
dig_out = 0;
while (result >= 10) {
result -= 10;
dig_out ++;
}
bcd_num3=dig_out;
bcd_num4=result;
}
//**************************************************
void format_t(unsigned char p){
int t_point;
t_point=(unsigned int)(tempir[p*2+1]<<8);//msb
t_point|=tempir[p*2];//lsb;
result_in_bcd( (t_point&0x0F)*625 );
t_stat[p+4]=(t_point>>4)*10+bcd_num1;
}

void receive_t(unsigned char *t){

if ( state_wire==0 )
{
state_wire=1;
detectPresence();
return;
}

if ( state_wire==1 )
{
state_wire=2;
writebyte(0xCC);//SKIP ROM [CCh]
writebyte(0x44);//CONVERT T [44h]
time_wire=100;//87; // time= time_wire/100
return;
}

if ( state_wire==2 )
{
if (!time_wire) state_wire=3;
return;
}

if ( state_wire==3 )
{
state_wire=4;
detectPresence();
return;
}

if ( state_wire==4 )
{
state_wire=5;
writebyte(0xCC);//SKIP ROM [CCh]
writebyte(0xBE);//READ SCRATCHPAD [BEh]
return;
}
if ( state_wire==5 )
{
state_wire=0;
readbyte(t ,t+2);
readbyte(t+1,t+3);

format_t(0);
format_t(1);


if (t_stat[ds0] <= t_stat[0]) PORTDRIVE |= DRIVE_TEN_1;
else
if (t_stat[ds0] >= t_stat[1]) PORTDRIVE &= ~DRIVE_TEN_1;

// вűőод ďđо čнвĺđňčđовŕн
if (t_stat[ds0] <= t_stat[2]) PORTDRIVE &= ~DRIVE_TEN_2;
else
if (t_stat[ds0] >= t_stat[3]) PORTDRIVE |= DRIVE_TEN_2;

return;
}
}

//**************************************
void t_in_bcd(signed int t, unsigned char pin_wire){
unsigned char znak=0;

if ( !(pin_wire & out_1wire) ){
lcd_buffer[0] =font[f_seg_g];
lcd_buffer[1] =font[f_seg_g];
lcd_buffer[2] =font[f_seg_g];
lcd_buffer[3] =font[f_seg_g];
}
else
{
if ( t<0 ){
znak=1;
result_in_bcd( t * -1 );
}
else
result_in_bcd( t );

if ( bcd_num1 ){
lcd_buffer[0]=font[bcd_num1];
lcd_buffer[1]=font[bcd_num2];
}
else
if ( bcd_num2 ){
lcd_buffer[1]=font[bcd_num2];
if ( znak ) lcd_buffer[0]=font[f_seg_g];
}
else
if ( znak ) lcd_buffer[1]=font[f_seg_g];
lcd_buffer[2]=font[bcd_num3]|font[tochka];
lcd_buffer[3]=font[bcd_num4];
}
}

//-------------------------------------
void skan_key(void)
{
unsigned char pin=0;
key=0;
//PORTZN &= ~BITZN;
#ifdef katod
PORTZN |= BITZN;
#else
PORTZN &= ~BITZN;
#endif
DDRLED=BIT(7);
PORTLED=(unsigned char)~BIT(7);
_delay_us(40);
pin=PINLED|BIT(7);
if ( pin!= 0xFF ){
time_view_zadanie=700;//0.05
if (++anti_dr==10)
{
key= ~pin;
}
if (anti_dr==btn_speed){
anti_dr=0;
if (++n_btn_sp==4) btn_speed=15;
}
}
else{
btn_speed=200;
n_btn_sp=0;
anti_dr=0;
}
DDRLED=0xFF;
}

//-------------------------------------
void display(void)
{
#ifdef katod
PORTZN |= BITZN;

if ( znmesto==0 ) PORTZN &= ~ZN0;//PORTZN &= ~ZN0;//
else
if ( znmesto==1 ) PORTZN &= ~ZN1;//PORTZN &= ~ZN1;//
else
if ( znmesto==2 ) PORTZN &= ~ZN2;
else
if ( znmesto==3 ) PORTZN &= ~ZN3;
PORTLED=lcd_buffer[znmesto];
#else
PORTZN &= ~BITZN;

if ( znmesto==0 ) PORTZN |= ZN0;//PORTZN &= ~ZN0;//
else
if ( znmesto==1 ) PORTZN |= ZN1;//PORTZN &= ~ZN1;//
else
if ( znmesto==2 ) PORTZN |= ZN2;
else
if ( znmesto==3 ) PORTZN |= ZN3;
PORTLED=~lcd_buffer[znmesto];
#endif
}
//-------------------------------------

void key_action(void)
{

if (key){
if (view_zadanie)
{
if ( key & btn_plus ){
if (t_stat[view_zadanie-1]<997) t_stat[view_zadanie-1]++; else t_stat[view_zadanie-1]=-537;
time[_viewp]=5;
}
if ( key & btn_minus ){
if (t_stat[view_zadanie-1] > -540) t_stat[view_zadanie-1]--; else t_stat[view_zadanie-1]=997;
time[_viewp]=5;
}
}
if ( key & btn_set )
{
if ( view_zadanie){
EEPROM_write(view_zadanie*2-2, t_stat[view_zadanie-1] >> 8 );
EEPROM_write(view_zadanie*2-1, t_stat[view_zadanie-1] );
}
if (++view_zadanie==5) view_zadanie=0;

time_flag |= BIT(_viewp);
time[_viewp]=100;
}
}
}


//-------------------------------------
void data_led(void)
{
unsigned char i;

for(i=0;i<4;i++)
lcd_buffer[i]=0x0;

if (view_zadanie)
{
if ( time_flag & BIT(_viewp) )
{
lcd_buffer[1]=font[f_P];
lcd_buffer[2]=font[view_zadanie];
}
else
t_in_bcd(t_stat[view_zadanie-1],pwire0);

}
else
{
if ( PINDRIVE & swt_t2 )
t_in_bcd(t_stat[ds0],pwire0);
else
t_in_bcd(t_stat[ds1],pwire1);
}

}
//-------------------------------------

void data_eeprom_read(void){
unsigned char i,a;
for (a=i=0; i<4 ; i++)
{
t_stat[i] = EEPROM_read(a++)<<8;
t_stat[i]|= EEPROM_read(a++);
}

}

int main(void)
{
cli();
avr_init();

data_eeprom_read();

PORTDRIVE &= ~(DRIVE_TEN_1|DRIVE_TEN_2);


detectPresence();
if (out_1wire){
writebyte(0xCC);//SKIP ROM [CCh]
writebyte(0xBE);//READ SCRATCHPAD [BEh]
readbyte(&tempir[0],&tempir[0]);//LSB
readbyte(&tempir[0],&tempir[0]);//MSB
readbyte(&tempir[0],&tempir[0]);
readbyte(&tempir[0],&tempir[0]);
readbyte(&tempir[0],&tempir[1]);
//byte 4 Configuration Register* 0x1F-9bit 94msek; 3F-10bit 188msek; 0x5F-11bit 375msek; 0x7F - 12 bit 750mSek;
if ( ((tempir[0]!=0x7F)&&(pwire0 & out_1wire))
|| ((tempir[1]!=0x7F)&&(pwire1 & out_1wire)) ){
scratchpad_save();//10ms
time[0]=255;
while(time[0]);
time[0]=255;
while(time[0]);
}
tempir[0]=0;
}

while(1)
{
if ( time_flag & BIT(_led) )//
{
time_flag &= ~BIT(_led);
time[_led]=70;//~0.00
data_led();
skan_key();
key_action();
display();
if ( ++znmesto==4 )
{
znmesto=0;
}
receive_t(&tempir[0]);
}
if ( time_flag & BIT(_01sek) )
{
time_flag &= ~BIT(_01sek);
time[_01sek]=156;//~0,01
time_wire--;
if (--time_view_zadanie==0) view_zadanie=0;
if (--time[_viewp]==0) time_flag &= ~BIT(_viewp);
if ( !(pwire0 & out_1wire) ) PORTDRIVE &= ~(DRIVE_TEN_1|DRIVE_TEN_2);//PORTDRIVE &= ~DRIVE_TEN_1;//
}
}

}
0

Поделиться темой:


Страница 1 из 1
  • Вы не можете создать новую тему
  • Вы не можете ответить в тему

1 человек читают эту тему
0 пользователей, 1 гостей, 0 скрытых пользователей