|
|
|
|
@ -1,6 +1,9 @@
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include "yaMBSiavr.h"
|
|
|
|
|
|
|
|
|
|
uint16_t inputStatus[9];
|
|
|
|
|
@ -35,13 +38,44 @@ void init_timer_100us(){
|
|
|
|
|
TCC0.INTCTRLA |= TC_OVFINTLVL_MED_gc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void adc_init(void){
|
|
|
|
|
ADCA.CTRLB = 0x00;
|
|
|
|
|
ADCA.REFCTRL = ADC_REFSEL_VCC_gc;
|
|
|
|
|
ADCA.PRESCALER = ADC_PRESCALER_DIV512_gc;
|
|
|
|
|
|
|
|
|
|
uint8_t CalibrationByteL;
|
|
|
|
|
uint8_t CalibrationByteH;
|
|
|
|
|
NVM_CMD = NVM_CMD_READ_CALIB_ROW_gc;
|
|
|
|
|
CalibrationByteL = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, ADCACAL0));
|
|
|
|
|
CalibrationByteH = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1));
|
|
|
|
|
NVM_CMD = NVM_CMD_NO_OPERATION_gc;
|
|
|
|
|
ADCA.CALL = CalibrationByteL;
|
|
|
|
|
ADCA.CALH = CalibrationByteH;
|
|
|
|
|
|
|
|
|
|
ADCA.CTRLA = ADC_ENABLE_bm;
|
|
|
|
|
|
|
|
|
|
ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
|
|
|
|
|
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN0_gc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t adc_read(void){
|
|
|
|
|
ADCA.CH0.CTRL |= ADC_CH_START_bm;
|
|
|
|
|
while(!(ADCA.CH0.INTFLAGS & ADC_CH_CHIF_bm));
|
|
|
|
|
ADCA.CH0.INTFLAGS = ADC_CH_CHIF_bm;
|
|
|
|
|
return ADCA.CH0.RES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void modbusGet(void) {
|
|
|
|
|
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
|
|
|
|
{
|
|
|
|
|
switch(rxbuffer[1]) {
|
|
|
|
|
case fcReadHoldingRegisters:
|
|
|
|
|
inputStatus[0]=(~PORTC.IN) & 0xFF;
|
|
|
|
|
modbusExchangeRegisters(inputStatus, 0, 8);
|
|
|
|
|
case fcReadHoldingRegisters: ;
|
|
|
|
|
float tmp[4];
|
|
|
|
|
tmp[0] = adc_read() - 170;
|
|
|
|
|
tmp[1] = (tmp[0])/ 3926 * 1.95;
|
|
|
|
|
tmp[2] = (tmp[1]*993)/(3.3-tmp[1]);
|
|
|
|
|
tmp[3] = (tmp[2]-100)/0.3927;
|
|
|
|
|
modbusExchangeRegisters(tmp, 0, 8);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
modbusSendException(ecIllegalFunction);
|
|
|
|
|
@ -54,6 +88,8 @@ int main(void){
|
|
|
|
|
init_clk();
|
|
|
|
|
PORTD.DIRSET = 1 << 5;
|
|
|
|
|
|
|
|
|
|
adc_init();
|
|
|
|
|
|
|
|
|
|
PORTCFG.MPCMASK = 0xFF;
|
|
|
|
|
PORTC.PIN1CTRL = PORT_OPC_PULLUP_gc;
|
|
|
|
|
|
|
|
|
|
|