Compare commits

...

5 Commits

@ -46,14 +46,16 @@ void io_setup(void){
void modbusGet(void) { void modbusGet(void) {
if (modbusGetBusState() & (1<<ReceiveCompleted)) if (modbusGetBusState() & (1<<ReceiveCompleted))
{ {
PORTD.OUTSET = 1 << 5;
switch(rxbuffer[1]) { switch(rxbuffer[1]) {
case fcReadHoldingRegisters: ; case fcReadHoldingRegisters: ;
modbusExchangeRegisters((uint16_t*)ch_values, 0, 18); modbusExchangeRegisters((uint16_t*)ch_values, 0, 20);
break; break;
default: default:
modbusSendException(ecIllegalFunction); modbusSendException(ecIllegalFunction);
break; break;
} }
PORTD.OUTCLR = 1 << 5;
} }
} }

@ -5,7 +5,7 @@
#include "pt100.h" #include "pt100.h"
uint16_t adc_buffer[AVRG]; uint16_t adc_buffer[AVRG];
volatile float ch_values[9]; volatile float ch_values[10];
void adc_init(void){ void adc_init(void){
ADCA.CTRLB = ADC_FREERUN_bm; ADCA.CTRLB = ADC_FREERUN_bm;
@ -70,15 +70,21 @@ ISR(DMA_CH0_vect){
ADCA.CTRLB = 0; // stop ADC ADCA.CTRLB = 0; // stop ADC
uint8_t old_pin = pin; uint8_t old_pin = pin;
pin=(pin+1)%9; pin=(pin+1)%10;
ADCA.CH0.MUXCTRL = pin << ADC_CH_MUXPOS_gp; // give MUX time to switch during averaging ADCA.CH0.MUXCTRL = pin << ADC_CH_MUXPOS_gp; // give MUX time to switch during calculation
float temp = avrg(adc_buffer, AVRG); float temp = avrg(adc_buffer, AVRG);
temp = temp - ADC_0V_COUNT; temp = temp - ADC_0V_COUNT;
temp = (temp)/ (4096-ADC_0V_COUNT) * ADC_MAX_VOLT; temp = (temp)/ (4096-ADC_0V_COUNT) * ADC_MAX_VOLT;
temp = (temp * REF_OHMS)/(SUPPLY_VOLTS - temp);
temp = (temp-100)/0.3927; //pt100 formula if(old_pin <= 8){ /* pt100 temperature channels */
ch_values[old_pin] = temp; temp = (temp * REF_OHMS)/(SUPPLY_VOLTS - temp);
temp = (temp-100)/0.3927; //pt100 formula
ch_values[old_pin] = temp;
}
if(old_pin == 9){ /* 24V supply voltage measurement */
// resistor devider 33k and 1k
ch_values[old_pin] = temp * 34;
}
DMA.CH0.TRIGSRC = DMA_CH_TRIGSRC_ADCA_CH0_gc; DMA.CH0.TRIGSRC = DMA_CH_TRIGSRC_ADCA_CH0_gc;
ADCA.CTRLB = ADC_FREERUN_bm; // start ADC again ADCA.CTRLB = ADC_FREERUN_bm; // start ADC again

@ -8,7 +8,7 @@
#define ADC_0V_COUNT 170 #define ADC_0V_COUNT 170
#define ADC_MAX_VOLT 1.95 #define ADC_MAX_VOLT 1.95
extern volatile float ch_values[9]; extern volatile float ch_values[10];
void adc_init(void); void adc_init(void);
void dma_init(void); void dma_init(void);

@ -74,18 +74,12 @@ void modbusSetAddress(unsigned char newadr)
#if PHYSICAL_TYPE == 485 #if PHYSICAL_TYPE == 485
void transceiver_txen(void) void transceiver_txen(void)
{ {
#if BOARD_TYPE == bType5chLedDim
PORTD|=(1<<7);
#endif
TRANSCEIVER_ENABLE_PORT|=(1<<TRANSCEIVER_ENABLE_PIN); TRANSCEIVER_ENABLE_PORT|=(1<<TRANSCEIVER_ENABLE_PIN);
} }
void transceiver_rxen(void) void transceiver_rxen(void)
{ {
TRANSCEIVER_ENABLE_PORT&=~(1<<TRANSCEIVER_ENABLE_PIN); TRANSCEIVER_ENABLE_PORT&=~(1<<TRANSCEIVER_ENABLE_PIN);
#if BOARD_TYPE == bType5chLedDim
PORTD&=~(1<<7);
#endif
} }
#endif #endif
@ -209,8 +203,8 @@ ISR(UART_TRANSMIT_INTERRUPT)
#ifndef __AVR_ATxmega32A4__ #ifndef __AVR_ATxmega32A4__
UART_CONTROL&=~(1<<UART_UDRIE); UART_CONTROL&=~(1<<UART_UDRIE);
#else #else
USARTD0_CTRLA &= ~(USART_DREINTLVL_gm); UART.CTRLA &= ~(USART_DREINTLVL_gm);
#endif #endif//__AVR_ATxmega32A4__
} }
} }
@ -236,34 +230,32 @@ void modbusInit(void)
UCSRC = (3<<UCSZ0); //Frame Size UCSRC = (3<<UCSZ0); //Frame Size
#endif #endif
UART_CONTROL = (1<<TXCIE)|(1<<RXCIE)|(1<<RXEN)|(1<<TXEN); // USART receiver and transmitter and receive complete interrupt UART_CONTROL = (1<<TXCIE)|(1<<RXCIE)|(1<<RXEN)|(1<<TXEN); // USART receiver and transmitter and receive complete interrupt
#if PHYSICAL_TYPE == 485 #else//URSEL
TRANSCEIVER_ENABLE_PORT_DDR|=(1<<TRANSCEIVER_ENABLE_PIN); UART.CTRLC = USART_CHSIZE_8BIT_gc;
transceiver_rxen();
#endif
BusState=(1<<TimerActive);
#else
//8bit
USARTD0.CTRLC = USART_CHSIZE0_bm | USART_CHSIZE1_bm;
//38400 // set baudrate
USARTD0.BAUDCTRLA = bsel; UART.BAUDCTRLA = UART_BSEL;
USARTD0.BAUDCTRLB = 0 | (bsel >> 8) | (bscale << USART_BSCALE0_bp); UART.BAUDCTRLB = 0 | (UART_BSEL >> 8) | (UART_BSCALE << USART_BSCALE0_bp);
USARTD0.CTRLB = USART_TXEN_bm | USART_RXEN_bm; UART.CTRLB = USART_TXEN_bm | USART_RXEN_bm;
//Interrupt enable (levels) //Interrupt enable (levels)
USARTD0.CTRLA |= USART_RXCINTLVL_HI_gc; UART.CTRLA |= USART_RXCINTLVL_HI_gc;
USARTD0.CTRLA |= USART_TXCINTLVL_HI_gc; UART.CTRLA |= USART_TXCINTLVL_HI_gc;
PORTD_OUTSET = PIN3_bm; //PORTD_OUTSET = PIN3_bm;
PORTD_DIRSET = PIN3_bm; PORTD_DIRSET = PIN3_bm;
PORTD_OUTCLR = PIN2_bm; //PORTD_OUTCLR = PIN2_bm;
PORTD_DIRCLR = PIN2_bm; //PORTD_DIRCLR = PIN2_bm;
//TODO Physical type 485 #endif//__AVR_ATxmega32A4__
#if PHYSICAL_TYPE == 485
TRANSCEIVER_ENABLE_PORT_DDR|=(1<<TRANSCEIVER_ENABLE_PIN);
transceiver_rxen();
#endif
BusState=(1<<TimerActive); BusState=(1<<TimerActive);
#endif
} }
/* @brief: Sends a response. /* @brief: Sends a response.
@ -284,8 +276,7 @@ void modbusSendMessage(unsigned char packtop)
#ifndef __AVR_ATxmega32A4__ #ifndef __AVR_ATxmega32A4__
UART_CONTROL|=(1<<UART_UDRIE); UART_CONTROL|=(1<<UART_UDRIE);
#else #else
//TODO use define for usartd UART.CTRLA |= USART_DREINTLVL_HI_gc;
USARTD0_CTRLA |= USART_DREINTLVL_HI_gc;
#endif #endif
BusState&=~(1<<ReceiveCompleted); BusState&=~(1<<ReceiveCompleted);

@ -57,15 +57,11 @@ THE POSSIBILITY OF SUCH DAMAGE.
/* /*
* Definitions for transceiver enable pin. * Definitions for transceiver enable pin.
*/ */
#if BOARD_TYPE == bType5chLedDim
#define TRANSCEIVER_ENABLE_PORT PORTD #define TRANSCEIVER_ENABLE_PORT PORTD_OUT
#define TRANSCEIVER_ENABLE_PIN 5 #define TRANSCEIVER_ENABLE_PIN 4
#define TRANSCEIVER_ENABLE_PORT_DDR DDRD #define TRANSCEIVER_ENABLE_PORT_DDR PORTD_DIR
#else
#define TRANSCEIVER_ENABLE_PORT PORTD
#define TRANSCEIVER_ENABLE_PIN 2
#define TRANSCEIVER_ENABLE_PORT_DDR DDRD
#endif
/** /**
* @brief * @brief
* At the moment the user has to set the value for Baudrate and * At the moment the user has to set the value for Baudrate and
@ -188,11 +184,16 @@ THE POSSIBILITY OF SUCH DAMAGE.
#define UART_UDRIE UDRIE #define UART_UDRIE UDRIE
#elif defined(__AVR_ATxmega32A4__) #elif defined(__AVR_ATxmega32A4__)
#define UART USARTD0
#define UART_TRANSMIT_COMPLETE_INTERRUPT USARTD0_TXC_vect #define UART_TRANSMIT_COMPLETE_INTERRUPT USARTD0_TXC_vect
#define UART_RECEIVE_INTERRUPT USARTD0_RXC_vect #define UART_RECEIVE_INTERRUPT USARTD0_RXC_vect
#define UART_TRANSMIT_INTERRUPT USARTD0_DRE_vect #define UART_TRANSMIT_INTERRUPT USARTD0_DRE_vect
#define UART_DATA USARTD0.DATA #define UART_DATA USARTD0.DATA
//38400 Baud at 32MHz system clock
#define UART_BSEL 51
#define UART_BSCALE 0
#else #else
#error "no definition available" #error "no definition available"
@ -219,7 +220,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
* Use 485 or 232, default: 485 * Use 485 or 232, default: 485
* Use 232 for testing purposes or very simple applications that do not require RS485 and bus topology. * Use 232 for testing purposes or very simple applications that do not require RS485 and bus topology.
*/ */
#define PHYSICAL_TYPE 232//485 //possible values: 485, 232 #define PHYSICAL_TYPE 485 //possible values: 485, 232
/* /*
#define modbusBaudrate 38400 #define modbusBaudrate 38400

@ -0,0 +1,62 @@
import time
from simple_pid import PID
from itertools import count
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import minimalmodbus
tempSens = minimalmodbus.Instrument('/dev/ttyUSB1', 1)
tempSens.serial.baudrate = 38400
OutputDriver = minimalmodbus.Instrument('/dev/ttyUSB1', 1)
OutputDriver.serial.baudrate = 38400
pid = PID(10, 0.01, 0.1, setpoint=100)
plt.style.use('fivethirtyeight')
x_values = []
y_values = []
index = count()
def animate(i):
try:
temp = tempSens.read_float(1, functioncode=4, byteorder=0)
y_values.append(temp)
x_values.append(next(index))
#print(temp)
print(tempSens.read_float(11, functioncode=4, byteorder=0))
# print(temp, end='\t')
# control = pid(temp)
# if(control>100):
# control = 100;
# control = (control/1000.0)
# if(control < 0):
# control = 0
# print(control, end='\t')
# print(0.1-control)
#
# if(control > 0):
# OutputDriver.write_bit(0, 1, functioncode=15)
#
# time.sleep(control)
#
# if(control<0.1):
# OutputDriver.write_bit(0, 0, functioncode=15)
#
# time.sleep(0.1-control)
plt.cla()
plt.scatter(x_values, y_values)
except Exception as e: print(e)
ani = FuncAnimation(plt.gcf(), animate, 2000)
plt.tight_layout()
plt.show()
Loading…
Cancel
Save