working temperature measurement on ch0
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include "yaMBSiavr.h"
|
#include "yaMBSiavr.h"
|
||||||
|
|
||||||
uint16_t inputStatus[9];
|
uint16_t inputStatus[9];
|
||||||
@@ -35,13 +38,44 @@ void init_timer_100us(){
|
|||||||
TCC0.INTCTRLA |= TC_OVFINTLVL_MED_gc;
|
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) {
|
void modbusGet(void) {
|
||||||
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
||||||
{
|
{
|
||||||
switch(rxbuffer[1]) {
|
switch(rxbuffer[1]) {
|
||||||
case fcReadHoldingRegisters:
|
case fcReadHoldingRegisters: ;
|
||||||
inputStatus[0]=(~PORTC.IN) & 0xFF;
|
float tmp[4];
|
||||||
modbusExchangeRegisters(inputStatus, 0, 8);
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
modbusSendException(ecIllegalFunction);
|
modbusSendException(ecIllegalFunction);
|
||||||
@@ -54,6 +88,8 @@ int main(void){
|
|||||||
init_clk();
|
init_clk();
|
||||||
PORTD.DIRSET = 1 << 5;
|
PORTD.DIRSET = 1 << 5;
|
||||||
|
|
||||||
|
adc_init();
|
||||||
|
|
||||||
PORTCFG.MPCMASK = 0xFF;
|
PORTCFG.MPCMASK = 0xFF;
|
||||||
PORTC.PIN1CTRL = PORT_OPC_PULLUP_gc;
|
PORTC.PIN1CTRL = PORT_OPC_PULLUP_gc;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user