Сообщение от Godzilla82
|
Получаешь iBuf = 165 или что?
Я пока не увидел всего кода. Какой источник тактирования?
Если можно, проект полность. Подтягивающие резисторы на плюс какого номинала? Правильно ли прошиты фьюзы?
|
резисторы номиналом 4к7, источник тактирования внутренний на 8 МГц
Проект только начинает свою жизнь, поэтому приведу как есть, со всеми. Чтение температуры пока отключено, на дисплей выводится только время.
iBuf получается не 165 а судя по всему 165*100+165
Цитата:
|
Вот это лучше писать так
iBuf=(min&127)*100+(sek&127): Что б всякое лишнее из регистров RTC не лезло куда попало.
|
ОК, спасибо, сейчас перепишу.
Вот листинг программы, пока со всеми ошибками и с кучей мусора, проект только начинает быть, поэтому пока не до красоты.
#include ‹mega48.h›
#include ‹delay.h›
// I2C Bus functions
#asm
.equ __i2c_port=0x05 ;PORTB
.equ __sda_bit=1
.equ __scl_bit=2
#endasm
#include ‹i2c.h›
#asm
.equ __w1_port=0x08 ;PORTC
.equ __w1_bit=0
#endasm
#include ‹1wire.h›
#include ‹delay.h›
#define DS18B20_FAMILY 0x28
#define DS_READ_ROM 0x33
#define DS_SKIP_ROM 0xcc
#define DS_CONVERT_T 0x44
#define DS_WRITE_SCRATCHPAD 0x4e
#define DS_READ_SCRATCHPAD 0xbe
#define DS_12BITS 0x7f
typedef unsigned char byte;
typedef struct {
byte family;
byte serial[6];
byte crc;
}trom;
typedef struct {
union {
struct {
byte l;
byte h;
}b;
int w;
}temp;
union {
struct {
byte l;
byte h;
}b;
int w;
}th;
byte config;
byte res[3];
byte crc;
}tscratchpad;
trom ds_rom;
tscratchpad ds_sp;
bit ds_ok=0;
// DS1307 Real Time Clock functions
#include ‹ds1307.h›
#define SH_CP PORTB.3
#define DS PORTB.4
#define ST_CP PORTB.5
unsigned int iBuf,temp=0;
unsigned int copy_temp=0;
unsigned char counter,symbol, delim=0;
unsigned char hour,min,sec; //часы, минуты, секунды
unsigned char day,month,year; //день, месяц, год
unsigned char dig_table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6 f};
unsigned char led_buff[4]={0,0,0,0};
void ds_init()
{
byte i;
byte *p;
#asm("cli")
if (w1_init()!=0)
{
p=(byte*)&ds_rom;
w1_write(DS_READ_ROM);
for (i=0;i‹8;i++) *p++=w1_read();
if (w1_dow_crc8(&ds_rom,
==0)
{
if (ds_rom.family==DS18B20_FAMILY)
{
w1_init();
w1_write(DS_SKIP_ROM);
w1_write(DS_WRITE_SCRATCHPAD);
w1_write(0);
w1_write(0);
w1_write(DS_12BITS);
ds_ok=1;
}
}
}
#asm("sei")
}
void ds_calc() {
w1_init();
w1_write(DS_SKIP_ROM);
w1_write(DS_CONVERT_T);
}
void ds_read() {
byte i;
byte *p=(byte*)&ds_sp;
w1_init();
w1_write(DS_SKIP_ROM);
w1_write(DS_READ_SCRATCHPAD);
for (i=0;i‹9;i++) *p++=w1_read();
if (w1_dow_crc8(&ds_sp,9)!=0) return;
temp=(ds_sp.temp.w*5)››3;
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
++delim;
PORTB.0=!PORTB.0;
if (delim==30)
{
rtc_get_time(&hour,&min,&sec); //считать время
rtc_get_date(&day,&month,&year); //считать дату
// ds_calc();
// ds_read();
// iBuf=temp;
iBuf=min*100+sec;
led_buff[3]=dig_table[iBuf/1000];
iBuf %=1000;
led_buff[2]=dig_table[iBuf/100];
iBuf %=100;
led_buff[1]=dig_table[iBuf/10];
iBuf %=10;
led_buff[0]=dig_table[iBuf];
delim=0;
}
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=Out
// State7=T State6=T State5=0 State4=0 State3=0 State2=T State1=T State0=0
PORTB=0x00;
DDRB=0x39;
// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 7,813 kHz
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x05;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;
// I2C Bus initialization
i2c_init();//**********************
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: Off
// SQW/OUT pin state: 0
rtc_init(0,0,0);//**********************
rtc_set_time(23,59,59); //установить время: часы, минуты, секунды
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x01;
ds_init();
while (ds_ok==0);
// Global enable interrupts
#asm("sei")
while (1)
{
copy_temp = 0xff-led_buff[symbol];
for (counter = 0; counter ‹ 8; counter++)
{
DS = 1-(counter==(symbol+1));
SH_CP = 1;
SH_CP = 0;
}
for (counter = 0; counter ‹ 8; counter++)
{
DS = copy_temp & 0x01;
//Дергаем ногой чтоб пропихнуть бит в регистр
SH_CP = 1;
SH_CP = 0;
copy_temp = copy_temp ›› 1; // Сдвигаем все биты переменной темp влево на один бит
}
//Дергаем ногой и защёлкиваем данные.
ST_CP = 1;
ST_CP = 0;
++symbol;
if (symbol==4){symbol=0;}
}
}