communication:avr
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
communication:avr [2007/12/23 22:24] – Hardware update laurenceb | communication:avr [2007/12/24 23:29] (current) – removed laurenceb | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== AVR radio project (by Laurenceb) ====== | ||
- | This is designed for data downlink in flight, and based on rocketboys pulse shaping technique. The plan is to use the pwm output of an avr to modulate a[[http:// | ||
- | |||
- | ===== Hardware design ===== | ||
- | |||
- | The nxt2 module has an enable pin, that can be used by the avr to save power whilst not transmitting | ||
- | |||
- | {{communication: | ||
- | |||
- | |||
- | This apparently takes 3v logic levels, but there is nothing said about a TTL or 5v enable input, only that the voltage on any pin must not exceed vcc, so, for good measure theres a resistor between the avr and the EN pin. One nice feature of the nxt2 is the fact that there appears to be no schmitt trigger on the data input, allowing it to be driven with analogue signal levels. However, the behaviour when subjected to high frequency pwm input has not been examined, so filtering on the pwm will be needed, using an LCR or RC filter. At 300 baud downlink, the frequency of our desired modulation will be in the 150Hz or less range, whereas at 16Mhz clock, the pwm fundamental frequency is 62Khz, so filtering is a fairly easy job. | ||
- | |||
- | |||
- | {{communication: | ||
- | |||
- | ===== AVR code ===== | ||
- | |||
- | The code has not been tested with anything other than an led as yet, the next aim is to try with a voltage controlled oscillator, and truetty. It will run on a mega48 at 16 or 20Mhz. The RBAUD, CBAUD and F_CPU constants, along with the target uc and all important settings are located at the top of the makefile. The lookup table is then generated by tabletool and stored in program space. You will need the procyon avrlib code timerx8.c, uart.c, and buffer.c to get this to work, but remember to edit the uart code correctly first - [[http:// | ||
- | |||
- | ==== Main sourcecode==== | ||
- | Note that this includes printf debug statements and has inverted enable pin output when debug is defined, to allow an led to go between B.1 and B.2 | ||
- | |||
- | <code c> | ||
- | #include " | ||
- | |||
- | //#define RBAUD x - use the makefile for these | ||
- | //#define CBAUD x | ||
- | //#define pulse_res (F_CPU/ | ||
- | |||
- | |||
- | #ifdef debug | ||
- | int get_char0(FILE* stream) | ||
- | { | ||
- | int c=-1; | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | |||
- | int put_char0(char c, FILE *stream) | ||
- | { | ||
- | | ||
- | | ||
- | return 0; | ||
- | } | ||
- | else | ||
- | { | ||
- | // | ||
- | if(uartAddToTxBuffer(c)) | ||
- | {return 0;} | ||
- | else | ||
- | {return -1;} | ||
- | } | ||
- | } | ||
- | #endif | ||
- | |||
- | |||
- | void overflow() | ||
- | { | ||
- | static u08 bufone; | ||
- | timer1PWMASet(bufone); | ||
- | timer1overflows++; | ||
- | if(radioout) | ||
- | { | ||
- | if(position< | ||
- | {position++; | ||
- | } | ||
- | else | ||
- | { | ||
- | if(position) | ||
- | {position--; | ||
- | } | ||
- | bufone=pgm_read_byte(& | ||
- | } | ||
- | |||
- | void send(unsigned char c) | ||
- | { | ||
- | u08 n; | ||
- | | ||
- | | ||
- | | ||
- | #ifdef debug | ||
- | | ||
- | #endif | ||
- | | ||
- | { | ||
- | radioout=c& | ||
- | timer1overflows=0; | ||
- | while(timer1overflows< | ||
- | #ifdef debug | ||
- | printf(" | ||
- | #endif | ||
- | } | ||
- | | ||
- | | ||
- | | ||
- | #ifdef debug | ||
- | | ||
- | #endif | ||
- | } | ||
- | |||
- | void tuneup() | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | #ifdef debug | ||
- | | ||
- | #else | ||
- | | ||
- | #endif | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | int c; | ||
- | u08 radio=1, | ||
- | DDRB=0b00000110; | ||
- | cbi(PORTB, | ||
- | uartInit(); | ||
- | uartSetBaudRate(CBAUD); | ||
- | #ifdef debug | ||
- | FILE mystdio0 = FDEV_SETUP_STREAM(put_char0, | ||
- | stdout = & | ||
- | stdin = & | ||
- | #endif | ||
- | sei(); | ||
- | timerInit(); | ||
- | timer1SetPrescaler(TIMER_CLK_DIV1); | ||
- | timer1PWMInitICR(255); | ||
- | timer1PWMAOn(); | ||
- | timerAttach(1, | ||
- | tuneup(); | ||
- | #ifdef debug | ||
- | printf(" | ||
- | #endif | ||
- | while(1) | ||
- | { | ||
- | | ||
- | | ||
- | { | ||
- | #ifdef debug | ||
- | printf(" | ||
- | #endif | ||
- | if(lenght> | ||
- | { | ||
- | | ||
- | // | ||
- | | ||
- | | ||
- | } | ||
- | #ifdef debug | ||
- | printf(" | ||
- | #endif | ||
- | send((unsigned char) c); | ||
- | lenght++; | ||
- | nodata=0; | ||
- | } | ||
- | else | ||
- | { | ||
- | radio=0; | ||
- | #ifdef debug | ||
- | sbi(PORTB, | ||
- | printf(" | ||
- | #else | ||
- | cbi(PORTB, | ||
- | #endif | ||
- | for(c=0; | ||
- | {_delay_ms(5); | ||
- | nodata++; | ||
- | if(nodata> | ||
- | { | ||
- | | ||
- | // | ||
- | | ||
- | | ||
- | #ifdef debug | ||
- | | ||
- | #else | ||
- | | ||
- | #endif | ||
- | | ||
- | } | ||
- | } | ||
- | } | ||
- | return 0; | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | ==== Header file ==== | ||
- | |||
- | |||
- | <code c> | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | |||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | |||
- | #define pulse_res_minus2 pulse_res-2 | ||
- | //#define debug //uncomment for debugging | ||
- | |||
- | |||
- | void overflow(void); | ||
- | void tuneup(void); | ||
- | #ifdef debug | ||
- | int put_char0(char c, FILE *stream); | ||
- | int get_char0(FILE *stream); | ||
- | FILE mystdio0; | ||
- | #endif | ||
- | volatile unsigned int timer1overflows; | ||
- | u08 radioout=1; | ||
- | volatile int position; | ||
- | |||
- | </ | ||
- | |||
- | ==== Changes to uart.h ==== | ||
- | |||
- | <code c> | ||
- | #ifndef UART_TX_BUFFER_SIZE | ||
- | //! Number of bytes for uart transmit buffer. | ||
- | /// Do not change this value in uart.h, but rather override | ||
- | /// it with the desired value defined in your project' | ||
- | # | ||
- | #define UART_TX_BUFFER_SIZE 0x00FF | ||
- | # | ||
- | #define UART_TX_BUFFER_SIZE 0x0000 | ||
- | # | ||
- | #endif | ||
- | </ | ||
- | |||
- | ==== Change to uart.c ==== | ||
- | |||
- | |||
- | comment out line 244 as so | ||
- | |||
- | <code c> | ||
- | // | ||
- | </ | ||
- | |||
- | ===== Compile time code ===== | ||
- | |||
- | ==== Tabletool source ==== | ||
- | |||
- | <code c> | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | int main(int argc, char *argv[]) | ||
- | { | ||
- | long F_CPU=(long) atoi(argv[argc-3]); | ||
- | int RBAUD=(int) atoi(argv[argc-2]); | ||
- | int CBAUD=(int) atoi(argv[argc-1]); | ||
- | int pulse_res=(F_CPU/ | ||
- | float c; | ||
- | FILE *fone; | ||
- | printf(" | ||
- | fone = fopen(" | ||
- | fprintf(fone,"// | ||
- | fprintf(fone,"# | ||
- | fprintf(fone," | ||
- | for(c=0; | ||
- | { | ||
- | | ||
- | } | ||
- | fprintf(fone," | ||
- | fclose(fone); | ||
- | // | ||
- | return 0; | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | ==== Makefile ==== | ||
- | |||
- | < | ||
- | ############################################################################### | ||
- | # Makefile for the project Radiopulseshaper | ||
- | ############################################################################### | ||
- | |||
- | ## General Flags - change F_CPU and RBAUD to suit your project | ||
- | ## Begin user editable section | ||
- | PROJECT = Radiopulseshaper | ||
- | MCU = atmega168 | ||
- | TARGET = Radiopulseshaper.elf | ||
- | CC = avr-gcc.exe | ||
- | F_CPU=16000000UL | ||
- | RBAUD=300 | ||
- | CBAUD=4800 | ||
- | ## programmer configuration | ||
- | programmer=avrdude | ||
- | programmerinstallation=$(path) | ||
- | programmerarguments=-p m168 -b 19200 -c avr911 -e -P com7 -U flash: | ||
- | ## For other programmers change the -c flag and the -P flag | ||
- | ## End user editable section | ||
- | |||
- | ## Options common to compile, link and assembly rules | ||
- | COMMON = -mmcu=$(MCU) | ||
- | |||
- | ## Compile options common for all C compilation units. | ||
- | CFLAGS = $(COMMON) | ||
- | CFLAGS += -Wall -gdwarf-2 | ||
- | CFLAGS += -MD -MP -MT $(*F).o -MF dep/ | ||
- | |||
- | ## Assembly specific flags | ||
- | ASMFLAGS = $(COMMON) | ||
- | ASMFLAGS += $(CFLAGS) | ||
- | ASMFLAGS += -x assembler-with-cpp -Wa, | ||
- | |||
- | ## Linker flags | ||
- | LDFLAGS = $(COMMON) | ||
- | LDFLAGS += -Wl, | ||
- | |||
- | |||
- | ## Intel Hex file production flags | ||
- | HEX_FLASH_FLAGS = -R .eeprom | ||
- | |||
- | HEX_EEPROM_FLAGS = -j .eeprom | ||
- | HEX_EEPROM_FLAGS += --set-section-flags=.eeprom=" | ||
- | HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings | ||
- | |||
- | |||
- | ## Libraries | ||
- | LIBS = -lc -lm -lprintf_flt -lprintf_min -lscanf_flt -lscanf_min | ||
- | |||
- | ## Objects that must be built in order to link | ||
- | OBJECTS = Radiopulseshaper.o buffer.o timerx8.o uart.o | ||
- | |||
- | ## Objects explicitly added by the user | ||
- | LINKONLYOBJECTS = | ||
- | |||
- | ## Build | ||
- | all: $(TARGET) Radiopulseshaper.hex Radiopulseshaper.eep Radiopulseshaper.lss size | ||
- | ${programmerinstallation}${programmer}${programmerarguments} | ||
- | |||
- | |||
- | ## Header production code | ||
- | table.h: | ||
- | tabletool ${F_CPU} ${RBAUD} ${CBAUD} | ||
- | |||
- | |||
- | ## Compile | ||
- | Radiopulseshaper.o: | ||
- | $(CC) $(INCLUDES) $(CFLAGS) -c $< | ||
- | |||
- | buffer.o: ../buffer.c | ||
- | $(CC) $(INCLUDES) $(CFLAGS) -c $< | ||
- | |||
- | timerx8.o: ../ | ||
- | $(CC) $(INCLUDES) $(CFLAGS) -c $< | ||
- | |||
- | uart.o: ../uart.c | ||
- | $(CC) $(INCLUDES) $(CFLAGS) -c $< | ||
- | |||
- | ##Link | ||
- | $(TARGET): $(OBJECTS) | ||
- | $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET) | ||
- | |||
- | %.hex: $(TARGET) | ||
- | avr-objcopy -O ihex $(HEX_FLASH_FLAGS) | ||
- | |||
- | %.eep: $(TARGET) | ||
- | -avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0 | ||
- | |||
- | %.lss: $(TARGET) | ||
- | avr-objdump -h -S $< > $@ | ||
- | |||
- | size: ${TARGET} | ||
- | @echo | ||
- | @avr-size -C --mcu=${MCU} ${TARGET} | ||
- | |||
- | ## Clean target | ||
- | .PHONY: clean | ||
- | clean: | ||
- | -rm -rf $(OBJECTS) Radiopulseshaper.elf dep/* Radiopulseshaper.hex Radiopulseshaper.eep Radiopulseshaper.lss Radiopulseshaper.map table.h | ||
- | |||
- | |||
- | ## Other dependencies | ||
- | -include $(shell mkdir dep 2>/ | ||
- | </ |
communication/avr.1198448677.txt.gz · Last modified: 2008/07/19 23:30 (external edit)