k4be 4 лет назад
Родитель
Сommit
cd477e6510
10 измененных файлов с 214 добавлено и 4 удалено
  1. 5 0
      .gitignore
  2. 2 2
      Makefile
  3. 16 0
      dac8571.c
  4. 21 0
      dac8571.h
  5. 3 0
      ds18b20.h
  6. 113 0
      i2c.c
  7. 38 0
      i2c.h
  8. 5 2
      main.c
  9. 7 0
      main.h
  10. 4 0
      sed1335.h

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+*.o
+*.lst
+*.map
+*.elf
+*.hex

+ 2 - 2
Makefile

@@ -1,9 +1,9 @@
 PRG            = SZM2
-OBJ            = main.o 1wire.o gtext.o sed1335.o ds18b20.o display.o
+OBJ            = main.o 1wire.o gtext.o sed1335.o ds18b20.o display.o i2c.o dac8571.o
 MCU_TARGET     = atmega644p
 OPTIMIZE       = -Os
 # -mcall-prologues
-DEFS           = -DF_CPU=16000000
+DEFS           = -DF_CPU=8000000
 LIBS           =
 ASMFLAGS       =
 AVRDUDE        = avrdude -c usbasp -p $(MCU_TARGET)

+ 16 - 0
dac8571.c

@@ -0,0 +1,16 @@
+#include "i2c.h"
+#include "dac8571.h"
+
+void dac8571_set(unsigned int data, unsigned char a0){
+	static unsigned char d[2];
+	unsigned char addr = DAC8571_BASE_ADDR;
+	if(a0)
+		addr |= _BV(2);
+		
+	d[0] = data>>8;
+	d[1] = data&0xff;
+
+	i2cSend(DAC8571_CMD_WRITE_TEMP_AND_LOAD, 2, d, addr);
+	
+}
+

+ 21 - 0
dac8571.h

@@ -0,0 +1,21 @@
+#ifndef DAC8571_H
+#define DAC8571_H
+
+#include <avr/io.h>
+
+#define DAC8571_LOAD1	_BV(5)
+#define DAC8571_LOAD0	_BV(4)
+#define DAC8571_BRCSEL	_BV(2)
+#define DAC8571_PD0		_BV(0)
+
+#define DAC8571_CMD_WRITE_TEMP_DATA		0
+#define DAC8571_CMD_WRITE_TEMP_PWDN		(DAC8571_PD0)
+#define DAC8571_CMD_WRITE_TEMP_AND_LOAD	(DAC8571_LOAD0)
+#define DAC8571_CMD_POWERDOWN			(DAC8571_LOAD0 | DAC8571_PD0)
+#define DAC8571_CMD_UPDATE				(DAC8571_LOAD1)
+
+#define DAC8571_BASE_ADDR	0b10011000
+
+void dac8571_set(unsigned int data, unsigned char a0);
+
+#endif

+ 3 - 0
ds18b20.h

@@ -1,3 +1,5 @@
+#ifndef DS18B20_H
+#define DS18B20_H
 
 extern unsigned char temp_ok;
 extern signed int temps[MAX_SENSOR_NUM];
@@ -5,3 +7,4 @@ extern signed int temps[MAX_SENSOR_NUM];
 unsigned char find_1wire(void);
 void gettemp(void);
 
+#endif

+ 113 - 0
i2c.c

@@ -0,0 +1,113 @@
+#include <util/delay.h>
+#include <avr/io.h>
+#include "i2c.h"
+
+void scl_toggle(void) __attribute__ ((noinline));
+void istart(void) __attribute__ ((noinline));
+void istop(void) __attribute__ ((noinline));
+
+void i2c_delay(void){
+	_delay_us(10);
+}
+
+void scl_toggle(void){
+	i2c_delay();
+	SCLDDR &= ~SCL;
+	i2c_delay();
+	SCLDDR |= SCL;
+}
+void istart(void){
+	SDADDR |= SDA;
+	i2c_delay();
+	SCLDDR |= SCL;
+	i2c_delay();
+}
+
+void istop(void){
+	i2c_delay();
+	SCLDDR &= ~SCL;
+	i2c_delay();
+	SDADDR &= ~SDA;
+	i2c_delay();
+}
+
+unsigned char i2cPutbyte(unsigned char b){
+	signed char a;
+	for(a=7;a>=0;a--){
+		if(b&_BV(a)) SDADDR &= ~SDA; else SDADDR |= SDA;
+		scl_toggle();
+	}
+
+	SDADDR &= ~SDA;			// leave SDL HI
+	i2c_delay();
+	SCLDDR &= ~SCL;	
+	i2c_delay();
+	while(!(SCLPIN&SCL));
+	i2c_delay();		// clock back up
+	b = (SDAPIN & SDA);	// get the ACK bit
+
+	//_delay_us(15);
+	SCLDDR |= SCL;			// not really ??
+	return (b == 0);		// return ACK value
+}
+
+unsigned char i2cGetbyte(unsigned char notlast){
+	signed char a;
+	unsigned char ret=0;
+	SDADDR &= ~SDA;			// make sure pullups are ativated
+
+	for(a=7;a>=0;a--){
+		i2c_delay();
+		SCLDDR &= ~SCL;
+		i2c_delay();
+		if(SDAPIN&SDA) ret |= _BV(a);
+		SCLDDR |= SCL;
+	}
+
+	i2c_delay();
+
+	if(notlast) SDADDR |= SDA; else SDADDR &= ~SDA;
+	scl_toggle();			// clock pulse
+	SDADDR &= ~SDA;			// leave with SDL HI
+	return ret;
+}
+/*
+void i2cInit(void){
+	set_bit( SDADDR, SDA);	// set SDA as output
+	set_bit( SCLDDR, SCL);	// set SCL as output
+	SDADDR &= ~SDA;		// set I/O state and pull-ups
+	SCLDDR &= ~SDA;		// set I/O state and pull-ups
+
+}*/
+
+void i2cSend(unsigned char reg, unsigned char length, unsigned char *data, unsigned char device){ // x24/23 x22/21 20/19
+	unsigned char i;
+	istart();      	// do start transition
+	i2cPutbyte(device); 	// send DEVICE address
+	i2cPutbyte(reg);
+	for(i=0;i<length;i++){
+		i2cPutbyte(data[i]);
+	}
+
+	SDADDR |= SDA;		// clear data line and
+	istop();		// send STOP transition
+}
+
+void i2cReceive(unsigned char reg, unsigned char length, unsigned char *data, unsigned char device){ // x24/23 x22/21 20/19
+	unsigned char i;
+	istart();			// do start transition
+	i2cPutbyte(device);		// send DEVICE address
+	i2cPutbyte(reg);
+	i2c_delay();
+	SCLDDR &= ~SCL;
+	i2c_delay();// do a repeated START
+	istart();			// transition
+	i2cPutbyte(device | READ);	// resend DEVICE, with READ bit set
+
+	for(i=0;i<length;i++){
+		data[i] = i2cGetbyte((i==length-1)?0:1);
+	}
+	SDADDR |= SDA;			// clear data line and
+	istop();			// send STOP transition
+}
+

+ 38 - 0
i2c.h

@@ -0,0 +1,38 @@
+#ifndef I2C_H
+#define I2C_H
+
+/**** I2C bitbanging support ****/
+
+// clock line port
+//#define SCLPORT	PORTB	// i2c clock port
+#define SCLDDR	DDRC	// i2c clock port direction
+#define SCLPIN PINC
+// data line port
+//#define SDAPORT	PORTB	// i2c data port
+#define SDADDR	DDRC	// i2c data port direction
+#define SDAPIN	PINC	// i2c data port input
+// pin assignments
+#define SCL		_BV(PC0)	// i2c clock pin
+#define SDA		_BV(PC1)	// i2c data pin
+
+// defines and constants
+#define READ		0x01	// I2C READ bit
+
+// functions
+
+// initialize I2C interface pins
+//void i2cInit(void);
+
+// send I2C data to <device> register 
+void i2cSend(unsigned char reg, unsigned char length, unsigned char *data, unsigned char device);
+
+// receive I2C data from <device> register 
+void i2cReceive(unsigned char reg, unsigned char length, unsigned char *data, unsigned char device);
+
+unsigned char i2cPutbyte(unsigned char b);
+unsigned char i2cGetbyte(unsigned char notlast);
+
+void istart(void);
+void istop(void);
+
+#endif

+ 5 - 2
main.c

@@ -3,10 +3,10 @@
 #include <avr/pgmspace.h>
 #include "main.h"
 #include "1wire.h"
-
 #include "gtext.h"
 #include "sed1335.h"
 #include "ds18b20.h"
+#include "dac8571.h"
 
 #include "display.h"
 
@@ -32,6 +32,7 @@ void main(void){
 	unsigned char i=0;
 /*	unsigned char state = STATE_DEFAULT;
 	unsigned char oldstate = state;*/
+	unsigned int dac1 = 0, dac2 = 32768;
 	
 	
 	OCR2A = 250; // 16ms
@@ -46,12 +47,14 @@ void main(void){
 	GLCD_Initialize();
 	GLCD_Clear();
 	for(;;){
-		_delay_ms(100);
+		_delay_ms(1);
 		if(i = getkey()){
 			cursor(0,0);
 			disp_num(i);
 			clearline();
 		}
+		dac8571_set(++dac1, VOLT_DAC);
+		dac8571_set(++dac2, CURR_DAC);
 /*		gettemp();
 		control();
 		state = state_processors[state](state);

+ 7 - 0
main.h

@@ -1,3 +1,6 @@
+#ifndef MAIN_H
+#define MAIN_H
+
 #include <avr/interrupt.h>
 
 #define MAX_SENSOR_NUM 1
@@ -11,6 +14,9 @@
 #define KEY_2	2
 #define KEY_OK	4
 
+#define VOLT_DAC	0
+#define CURR_DAC	1
+
 #define ms(x) ((unsigned int)x/16)
 
 struct timers {
@@ -27,3 +33,4 @@ static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigne
 	sei();
 }
 
+#endif

+ 4 - 0
sed1335.h

@@ -1,3 +1,6 @@
+#ifndef SED1335_H
+#define SED1335_H
+
 // value definitions, don't change this
 #define 	SED1335_SYSTEM_SET   	0x40
 #define 	SED1335_SLEEP_IN   		0x53
@@ -99,3 +102,4 @@ void GLCD_GraphicGoTo(unsigned int x, unsigned int y);
 void GLCD_Bitmap(char * bmp, int x, int y, int dx, int dy);
 void GLCD_HardReset(void);
 
+#endif