working prototype

master
Eggert Jung 5 years ago
parent 3056c3fd11
commit 31c1e30b67

@ -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)
// ============================================== // ==============================================
} }
void init_timer_100us(){
TCC0.CTRLA |= TC_CLKSEL_DIV256_gc;
TCC0.PER = 13; // ==> 9615us
TCC0.INTCTRLA |= TC_OVFINTLVL_MED_gc;
}
void modbusGet(void) {
if (modbusGetBusState() & (1<<ReceiveCompleted))
{
switch(rxbuffer[1]) {
case fcReadHoldingRegisters:
inputStatus[0]=(~PORTC.IN) & 0xFF;
modbusExchangeRegisters(inputStatus, 0, 8);
break;
default:
modbusSendException(ecIllegalFunction);
break;
}
}
}
int main(void){ int main(void){
init_clk(); init_clk();
init_uart();
PORTD.DIRSET = 1 << 5; 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(); sei();
modbusSetAddress(1);
modbusInit();
for(;;){ for(;;){
char tmp = USARTD0.DATA; modbusGet();
if(!tmp)
tmp='a';
USARTD0.DATA = tmp;
PORTD.OUTTGL = 1 << 5;
_delay_ms(1000);
} }
} }
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

Loading…
Cancel
Save