Wed, 16 Nov 2011 17:04:10 +0100
added receiver testing stuff
receiver/Makefile | file | annotate | diff | comparison | revisions | |
receiver/README | file | annotate | diff | comparison | revisions | |
receiver/driver | file | annotate | diff | comparison | revisions | |
receiver/main.c | file | annotate | diff | comparison | revisions | |
receiver/main.h | file | annotate | diff | comparison | revisions |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/receiver/Makefile Wed Nov 16 17:04:10 2011 +0100 @@ -0,0 +1,92 @@ +PRG=main + +MCU=atmega16 +FUSES=-U lfuse:w:0x2f:m -U hfuse:w:0xc8:m +BOOTFUSES=-U lfuse:w:0xff:m -U hfuse:w:0xce:m + +#F_CPU=4185600 +#F_CPU = 14318000 + +F_CPU = 8000000 + +BAUD=38400 +ISP_BAUD = 115200 + +#SRC = main.c seriald.c driver/ADC.c driver/clock.c driver/timer.c +#SRC = main.c driver/rs232.c driver/manchester.c +SRC = main.c driver/rs232.c + +################################################################### +# You possibly do not need to change settings below this marker +################################################################### + +# Binaries to be used +# You may add the path to them if they are not in the PATH variable. +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRDUDE = avrdude +PERL = perl + +# Optional library search path +LIBS = + +# Compiler options for all c source files +CFLAGS += -g -Wall -mmcu=$(MCU) -DBAUD=$(BAUD) -DF_CPU=$(F_CPU)UL -std=gnu99 +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -Wstrict-prototypes +CFLAGS += -Wundef +#CFLAGS += -save-temps + +# optimize for size +CFLAGS += -Os +# dont optimize +#CFLAGS += -O0 + +# Linker options +LDFLAGS = -Wl,-Map,$(PRG).map + +# Enable floating-point support in printf +#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm + +################################################################### +# TARGET DEFINITIONS: + + +all: code + +code: $(PRG).hex + +$(PRG).elf: $(SRC:.c=.o) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +%.lst: %.elf + $(OBJDUMP) -h -S $< > $@ + +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + rm $(PRG).elf + rm $(PRG).map + +program: code + $(AVRDUDE) -c stk500v2 -b $(ISP_BAUD) -i 1 -p $(MCU) -V -U flash:w:$(PRG).hex:i + +fuse: + $(AVRDUDE) -c stk500 -p $(MCU) -V $(FUSES) + +clean: + rm -rf *.o *.elf *.elf.src *.s *.i + rm -rf driver/*.o + +upgrade: code + $(RESETCOMMAND) + ./bootloader -d $(NETDEV) -b $(UPGRADE_BAUD) -p $(PRG).hex + +bootloader: bootload.hex + $(AVRDUDE) -p $(MCU) -c stk500 -V -U flash:w:bootload.hex:i + +bootfuses: + $(AVRDUDE) -p $(MCU) -c stk500 $(BOOTFUSES)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/receiver/README Wed Nov 16 17:04:10 2011 +0100 @@ -0,0 +1,1 @@ +Just for testing at the moment \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/receiver/driver Wed Nov 16 17:04:10 2011 +0100 @@ -0,0 +1,1 @@ +../blackbox/driver \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/receiver/main.c Wed Nov 16 17:04:10 2011 +0100 @@ -0,0 +1,229 @@ +#include <avr/interrupt.h> +#include <avr/io.h> +#include <avr/wdt.h> +#include <avr/eeprom.h> +#include <stdlib.h> +#include <stdint.h> +#include <avr/pgmspace.h> + +#include "main.h" + +#include "driver/rs232.h" +//#include "driver/manchester.h" + +volatile uint8_t datalen = 0; +char data[10]; // 8 bytes data buffer + string termination + +static char buffer[RS232_BUFSIZE+1]; +static uint8_t buffer_len; + +volatile uint8_t bitbuf_len = 0; +volatile uint16_t bitbuf = 0; +volatile uint16_t bitbuf_out = 0; +volatile uint8_t bitbuf_out_len = 0; +volatile uint8_t timeout = 0; +volatile uint8_t lastbit = 0; + + + +// USART0 RX interrupt +ISR ( USART_RXC_vect ) { + char c = UDR; + + // check for buffer overflow + if (buffer_len==sizeof(buffer)) { + buffer_len=0; + if (c == 27) { + // escape sequence, store to empty buffer + buffer[buffer_len++] = c; + } + } else { + // collect characters until end of line + if (c == 27) { + // escape sequence, clear buffer + buffer_len = 0; + buffer[buffer_len++] = c; + } else if ( (c==0xff) && (buffer_len > 3) ) { + buffer[buffer_len]=0; + + // packet end received, parse the received packet + + // wait for the next packet + buffer_len=0; + } else { + buffer[buffer_len++]=c; + } + } +} + + +#define PULSE_IN PIND +#define PULSE_BIT 2 + +#define TIMER_DIVISOR 8 +#define VALUE_100US 0.47e-4 +#define TIMER_100US 0xff - (uint8_t)(VALUE_100US * F_CPU/TIMER_DIVISOR) + +ISR ( INT0_vect ) { + writeBit(GICR, INT0, 0); // disable INT0 interrupt + // Startsignal erkannt, ab hier den Timer0 starten, + // der liest dann alle 100µs den Zustand ein und schreibt das + // empfangene Bit in den Puffer + lastbit = 0b10000001; // initialize + + TCNT0 = TIMER_100US; + TIMSK |= 1<<TOIE0; //enable timer0 interrupt +} + +ISR ( TIMER0_OVF_vect ) { + uint8_t sr = SREG; + TCNT0 = TIMER_100US; // reset timer for next bit time + writeBit(PORTD, 4, 0); + // read the current bit value, store it to 16bit buffer + if ( (PULSE_IN & _BV(PULSE_BIT)) == 0 ) { +//RS232_putc('L'); + + if ( (lastbit & 1) == 1) { + // bit cleared, second block + // insert bit + bitbuf |= 1; + bitbuf = bitbuf << 1; + bitbuf_len++; + lastbit = 0; // set block number0 & signal state to 0 + } else { + // bit cleared, first block + lastbit = 1; // set block number1 & signal state to 0 + } + } else { +//RS232_putc('H'); + if ( (lastbit & 1) == 1) { + // bit set, second block + if ( (lastbit & _BV(7)) == 0) { + // insert zero bit + bitbuf = bitbuf << 1; + bitbuf_len++; + lastbit = 0b10000000; // set block number0 & signal state to 1 + } else { + // no change, transmission END! + lastbit = 0xff; + } + } else { + // bit set, first block + lastbit = 0b10000001; // set block number1 & signal state to 1 + } + } + if ( (bitbuf_len == 16) | (lastbit == 0xFF) ) { + // 16 bits full or transmission end + // export to uart + writeBit(TIMSK, TOIE0, 0); // disable timer0 interrupt + if (bitbuf > 0) { + bitbuf_out = bitbuf; + bitbuf_out_len = bitbuf_len; + } + bitbuf = 0; + bitbuf_len = 0; + GICR |= _BV(INT0); // Enable INT0 + } + + writeBit(PORTD, 4, 1); + SREG = sr; +} + +volatile uint8_t pulse_counter = 0; +volatile uint8_t measure_progress = 0; +volatile uint8_t car_id = 0; +ISR(TIMER2_OVF_vect) { + //TCNT2 = 0x90; + pulse_counter++; +} + +ISR (INT1_vect) { + // measure pulse width + //RS232_putc('x'); + if (measure_progress == 0) { + // start measuring + pulse_counter = 0; + measure_progress = 1; + // set INT1 to rising edge (stops current measurement) + //MCUCR |= _BV(ISC10); + writeBit(PORTD, 4, 1); + } else { + // stop measuring + car_id = pulse_counter; + measure_progress = 0; + // set INT1 to falling edge (initiates next measurement) + //MCUCR &= ~_BV(ISC10); + writeBit(PORTD, 4, 0); + } +} + + +int main(void) +{ + uint i; +char s[30]; + + TCCR0 = 0; //timer off + //TCCR0 = (1<<CS00); //divide by 1 + TCCR0 = (1<<CS01); //divide by 8 + //TCCR0 = (1<<CS01) | (1<<CS00); //divide by 64 + //TCCR0 = (1<<CS02); //divide by 256 + //TCCR0 = (1<<CS02) | (1<<CS00); //divide by 1024 + + //TIMSK = 1<<TOIE0; //enable timer interrupt + // Timer will be enabled on INT0 + + // configure timer2 for pulse measurement + //TCCR2 = (1<<CS21); // divide 8 + TCCR2 = (1<<CS20); // divide 1 + TIMSK = 1<<TOIE2; + + // configure INT0+1 for falling edge + MCUCR = _BV(ISC00) | _BV(ISC11); +// GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0/2 + GICR = _BV(INT1) ; // Enable INT0/2 + + writeBit(DDRD, 4, 1); + + + RS232_init(); // initialize RS485 interface + RS232_puts_p(PSTR("INIT OK\n")); + + itoa (TIMER_100US, s, 10); + RS232_puts(s); + + RS232_puts_p(PSTR(" timer\n")); + i = 0; + + sei(); + while (1) { + // main loop + + if (car_id != 0) { + itoa( car_id, s, 10); + car_id = 0; + RS232_puts( s ); + RS232_putc('\n'); + + } + /*cli(); + i = rc5_data; // read two bytes from interrupt ! + rc5_data = 0; + sei(); + if (i) { + itoa( i, s, 10); + RS232_puts( s ); + } */ +/* + if (p_len < 0xFF) p_len++; + if( (p_bit ^ PULSE_IN) & 1<<PULSE_BIT ){ // change detect + p_bit = ~p_bit; // 0x00 -> 0xFF -> 0x00 + itoa( p_len, s, 10); + RS232_puts(s); + RS232_puts_p(PSTR("\n")); + p_len = 0; + } +*/ + } // main loop end +}; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/receiver/main.h Wed Nov 16 17:04:10 2011 +0100 @@ -0,0 +1,29 @@ +#ifndef MAIN_H +#define MAIN_H + +#include <avr/wdt.h> + +#define uchar unsigned char +#define uint unsigned int + +// Macro used to write to a single I/O pin +#define writeBit(port,bit,value) { if ((value)>0) (port) |= (1<<bit); else (port) &= ~(1<<bit); } + +// Macro used to read from a single I/O pin +#define readBit(port,bit) (((port) >> (bit)) & 1) + +// we have internal AREF... +// #define EXT_AREF 1 + +// RESET ROUTINE FOR DOING A SOFT-RESET: +#define soft_reset() \ +do \ +{ \ + wdt_enable(WDTO_15MS); \ + for(;;) \ + { \ + } \ +} while(0) + + +#endif