working prototype
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#include "uart.h"
|
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
#include "yaMBSiavr.h"
|
||||||
|
|
||||||
|
uint16_t inputStatus[9];
|
||||||
|
|
||||||
void init_clk(void)
|
void init_clk(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
// ========= System Clock configuration =========
|
// ========= System Clock configuration =========
|
||||||
// Set to external 16Mhz crystal, using the PLL at *2
|
// Set to external 16Mhz crystal, using the PLL at *2
|
||||||
// set it to be a 12-16Mhz crystal with a slow start-up time.
|
// set it to be a 12-16Mhz crystal with a slow start-up time.
|
||||||
@@ -28,18 +29,47 @@ void init_clk(void)
|
|||||||
// ==============================================
|
// ==============================================
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void){
|
void init_timer_100us(){
|
||||||
init_clk();
|
TCC0.CTRLA |= TC_CLKSEL_DIV256_gc;
|
||||||
init_uart();
|
TCC0.PER = 13; // ==> 9615us
|
||||||
PORTD.DIRSET = 1 << 5;
|
TCC0.INTCTRLA |= TC_OVFINTLVL_MED_gc;
|
||||||
sei();
|
}
|
||||||
|
|
||||||
for(;;){
|
void modbusGet(void) {
|
||||||
char tmp = USARTD0.DATA;
|
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
||||||
if(!tmp)
|
{
|
||||||
tmp='a';
|
switch(rxbuffer[1]) {
|
||||||
USARTD0.DATA = tmp;
|
case fcReadHoldingRegisters:
|
||||||
PORTD.OUTTGL = 1 << 5;
|
inputStatus[0]=(~PORTC.IN) & 0xFF;
|
||||||
_delay_ms(1000);
|
modbusExchangeRegisters(inputStatus, 0, 8);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
modbusSendException(ecIllegalFunction);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
init_clk();
|
||||||
|
PORTD.DIRSET = 1 << 5;
|
||||||
|
|
||||||
|
PORTCFG.MPCMASK = 0xFF;
|
||||||
|
PORTC.PIN1CTRL = PORT_OPC_PULLUP_gc;
|
||||||
|
|
||||||
|
init_timer_100us();
|
||||||
|
|
||||||
|
PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
|
||||||
|
sei();
|
||||||
|
|
||||||
|
modbusSetAddress(1);
|
||||||
|
modbusInit();
|
||||||
|
|
||||||
|
for(;;){
|
||||||
|
modbusGet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(TCC0_OVF_vect){
|
||||||
|
modbusTickTimer();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
TARGET = main.elf
|
TARGET = main.elf
|
||||||
FILES = main fifo uart yaMBSiavr
|
FILES = main yaMBSiavr
|
||||||
MCU = atxmega32a4
|
MCU = atxmega32a4
|
||||||
PROGC = x32a4
|
PROGC = x32a4
|
||||||
CC = avr-gcc
|
CC = avr-gcc
|
||||||
@@ -7,7 +7,7 @@ TOOL = atmelice_pdi
|
|||||||
|
|
||||||
BUILDDIR = build
|
BUILDDIR = build
|
||||||
|
|
||||||
DEFINES = -DF_CPU=32000000UL
|
DEFINES = -DF_CPU=32000000UL -DBOARDTYPE=9ch_pt100
|
||||||
|
|
||||||
CFLAGS =-mmcu=$(MCU) -O2 -Wall $(DEFINES) -std=c99 -ffunction-sections -fdata-sections
|
CFLAGS =-mmcu=$(MCU) -O2 -Wall $(DEFINES) -std=c99 -ffunction-sections -fdata-sections
|
||||||
LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections
|
LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections
|
||||||
|
|||||||
@@ -222,7 +222,8 @@ ISR(UART_TRANSMIT_COMPLETE_INTERRUPT)
|
|||||||
modbusReset();
|
modbusReset();
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
const uint16_t bsel = 208;
|
||||||
|
const int8_t bscale = 0;
|
||||||
void modbusInit(void)
|
void modbusInit(void)
|
||||||
{
|
{
|
||||||
#ifndef __AVR_ATxmega32A4__
|
#ifndef __AVR_ATxmega32A4__
|
||||||
@@ -240,12 +241,28 @@ void modbusInit(void)
|
|||||||
transceiver_rxen();
|
transceiver_rxen();
|
||||||
#endif
|
#endif
|
||||||
BusState=(1<<TimerActive);
|
BusState=(1<<TimerActive);
|
||||||
#elif
|
#else
|
||||||
//BRR
|
//8bit
|
||||||
//RX, TX enable
|
USARTD0.CTRLC = USART_CHSIZE0_bm | USART_CHSIZE1_bm;
|
||||||
|
|
||||||
|
//38400
|
||||||
|
USARTD0.BAUDCTRLA = bsel;
|
||||||
|
USARTD0.BAUDCTRLB = 0 | (bsel >> 8) | (bscale << USART_BSCALE0_bp);
|
||||||
|
|
||||||
|
USARTD0.CTRLB = USART_TXEN_bm | USART_RXEN_bm;
|
||||||
|
|
||||||
//Interrupt enable (levels)
|
//Interrupt enable (levels)
|
||||||
//Physical type
|
USARTD0.CTRLA |= USART_RXCINTLVL_HI_gc;
|
||||||
//Busstate
|
USARTD0.CTRLA |= USART_TXCINTLVL_HI_gc;
|
||||||
|
|
||||||
|
PORTD_OUTSET = PIN3_bm;
|
||||||
|
PORTD_DIRSET = PIN3_bm;
|
||||||
|
PORTD_OUTCLR = PIN2_bm;
|
||||||
|
PORTD_DIRCLR = PIN2_bm;
|
||||||
|
|
||||||
|
//TODO Physical type 485
|
||||||
|
|
||||||
|
BusState=(1<<TimerActive);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,6 +284,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
|
||||||
USARTD0_CTRLA |= USART_DREINTLVL_HI_gc;
|
USARTD0_CTRLA |= USART_DREINTLVL_HI_gc;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -219,7 +219,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 485 //possible values: 485, 232
|
#define PHYSICAL_TYPE 232//485 //possible values: 485, 232
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#define modbusBaudrate 38400
|
#define modbusBaudrate 38400
|
||||||
|
|||||||
Reference in New Issue
Block a user