|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
#include "modbus.h"
|
|
|
|
|
|
|
|
|
|
@ -7,6 +8,8 @@
|
|
|
|
|
|
|
|
|
|
uint16_t holdingRegisters[4];
|
|
|
|
|
|
|
|
|
|
volatile uint32_t led_on_timer[3];
|
|
|
|
|
|
|
|
|
|
// return raw ADC data
|
|
|
|
|
uint32_t HX711_get_data(uint8_t clk_PIN, uint8_t data_PIN, uint8_t gain_for_next_conv){
|
|
|
|
|
uint32_t data = 0;
|
|
|
|
|
@ -62,16 +65,20 @@ void modbusGet(void) {
|
|
|
|
|
{
|
|
|
|
|
switch(rxbuffer[1]) {
|
|
|
|
|
case fcReadHoldingRegisters:
|
|
|
|
|
PORTC ^= 1 << 1; // toggle led
|
|
|
|
|
PORTC |= 1 << 0;
|
|
|
|
|
led_on_timer[0]=1000;
|
|
|
|
|
|
|
|
|
|
while(PINB & (1<<4)); // wait for data ready
|
|
|
|
|
|
|
|
|
|
uint32_t tmp = HX711_get_data(3, 4, 128);
|
|
|
|
|
holdingRegisters[0] = tmp & 0xFFFF;
|
|
|
|
|
holdingRegisters[1] = (tmp>>16) & 0xFFFF;
|
|
|
|
|
holdingRegisters[2] = led_on_timer[1];
|
|
|
|
|
modbusExchangeRegisters(holdingRegisters,0,4);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PORTC |= 1 << 1;
|
|
|
|
|
led_on_timer[1]=1000;
|
|
|
|
|
modbusSendException(ecIllegalFunction);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
@ -97,4 +104,11 @@ int main(void){
|
|
|
|
|
|
|
|
|
|
ISR(TIMER0_OVF_vect) { //this ISR is called 9765.625 times per second
|
|
|
|
|
modbusTickTimer();
|
|
|
|
|
|
|
|
|
|
for(uint8_t i=0; i<2; i++){
|
|
|
|
|
if(led_on_timer[i]>0)
|
|
|
|
|
led_on_timer[i]--;
|
|
|
|
|
else
|
|
|
|
|
PORTC &= ~(1<<i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|