c - storing characters in EEPROM & echoing back -
am trying write program avr stk600 board in cycling through leds. when switch pressed, appropriate leds turns on (upto point, goes well). adding functionality board, trying use usart (rs232 serial communication).
-when user presses key, should echoed (on hyperterminal). -when crtl-z hit, data should recorded in eeprom (again hitting crtl-z should stop recording). -when crtl-y pressed, recorded data should played back.
the problem here getting usart (i did check hardware settings, did loopback test...everything working well) there should problem code. upto toggling leds goes after eeprom, usart not going well.
here code, using avr-gcc. not using "avrlib" because course taking doesn't allow so:
#include "header.h" #define base_addr 0x20 #define read_atmega(addr) *((p_char)(base_addr + ((addr) * addr_multiplier))) #define write_atmega(addr, data) *((p_char)(base_addr + ((addr) * addr_multiplier))) = data #define led_delay 10 #define f_cpu 8000000 #define baudrate 9600 #define baud_prescaler (((f_cpu/(baudrate * 16ul)))-1) volatile uint intrs, = 1, count, switch_pressed = 0, uiadd, uiaddend, flag_led_intr; volatile uint record, play_recorded_keys, rx, flag_serial_receiver, tx_complete; volatile unsigned char get_switch=0, data; extern void __vector_23 (void) __attribute__ ((interrupt)); extern void __vector_25 (void) __attribute__ ((signal)); extern void __vector_27 (void) __attribute__ ((interrupt)); void enable_ports(void); int get_switchpressed(void); void call_interrupthandler(void); void usart_init(void); //void usart_bounce(); void initialize(void); void readwriteserialport(void); void eeprom_write(unsigned int uiaddress, unsigned char ucdata); unsigned char eeprom_read(unsigned int uiaddress); void initialize(void) { cli(); //stop interrupts flag_led_intr = 0; record = 0; play_recorded_keys = 0; rx = 0; flag_serial_receiver = 0; uiadd = 0; uiaddend = 0; tx_complete = 0; enable_ports(); usart_init(); sei(); } void enable_ports() //enables portb, portd & timer { write_atmega(ddrb,0xff); //portb output leds write_atmega(portb,0xff); //initialize portb write_atmega(ddrd, 0x00); //portd input switches write_atmega(tccr0b, 0x05); //setting timer 1024 prescaler write_atmega(timsk0, 0x01); write_atmega(tcnt0, 256-led_delay); intrs = 0; } void usart_init(void) //enables usart { /* set baud rate */ write_atmega(ubrr0l, baud_prescaler); write_atmega(ubrr0h,(baud_prescaler>>8)); /* set frame format: 8 bit data + start bit + stop bit */ write_atmega(ucsr0c, 0x06); /* enable reciever , transmitter */ write_atmega(ucsr0b, 0xd8); } void __vector_23 (void) { call_interrupthandler(); } void __vector_25 (void) { rx = udr0; flag_serial_receiver = 1; sei(); } void __vector_27 (void) { tx_complete = 1; } void call_interrupthandler() { intrs++; if(intrs > led_delay) { write_atmega(portb, 0xff<<i); i++; if(i==10) { write_atmega(portb, 0xff); = 1; } } count = get_switchpressed(); if(count != 0) { write_atmega(portb, 0xff<<count); intrs = 0; } //sei(); } int get_switchpressed() { get_switch = read_atmega(pind); switch(get_switch) { case 0xfe: switch_pressed = 1; break; case 0xfd: switch_pressed = 2; break; case 0xfb: switch_pressed = 3; break; case 0xf7: switch_pressed = 4; break; case 0xef: switch_pressed = 5; break; case 0xdf: switch_pressed = 6; break; case 0xbf: switch_pressed = 7; break; case 0x7f: switch_pressed = 8; break; }; return (switch_pressed); } void readwriteserialport(void) { if(rx == 0x1a) { record = !record; play_recorded_keys = 0; } else if(rx == 0x19) { record = 0; play_recorded_keys = !play_recorded_keys; } if(record) { eeprom_write(uiadd++, rx); } if(uiadd == 4096) { record = 0; uiaddend = 4096; } else uiaddend = uiadd; } void eeprom_write(unsigned int uiaddress, unsigned char ucdata) { while(eecr & (1<<eepe)); /* wait completion of previous write */ write_atmega(eearh,(uiaddress>>8)); /* set address , data registers */ write_atmega(eearl, uiaddress); write_atmega(eedr, ucdata); write_atmega(eecr, (eecr |(1<<eempe))); /* write logical 1 eempe */ write_atmega(eecr, (eecr |(1<<eere))); /* start eeprom write setting eepe */ } unsigned char eeprom_read(unsigned int uiaddress) { while(eecr & (1<<eepe)); /* wait completion of previous write */ write_atmega(eearh,(uiaddress>>8)); /* set address register */ write_atmega(eearl,uiaddress); write_atmega(eecr, (eecr |(1<<eere))); /* start eeprom read writing eere */ return eedr; /* return data data register */ } int main(void) { initialize(); while(1) { if(flag_serial_receiver == 1) { flag_serial_receiver = 0; readwriteserialport(); } if(play_recorded_keys) { unsigned char tx = eeprom_read(uiadd++); if(uiadd == 4096 || uiadd >= uiaddend) { play_recorded_keys = 0; uiadd = 0; } while((ucsr0a & (1<<udre0)) == 0); write_atmega(udr0,tx); while(tx_complete == 0); tx_complete; } } return(0); }
here header.h file
#ifndef header_h #define header_h /*******ports******************/ #define pina 0x0 #define ddra 0x1 #define porta 0x2 #define pinb 0x3 #define ddrb 0x4 #define portb 0x5 #define pinc 0x6 #define ddrc 0x7 #define portc 0x8 #define pind 0x9 #define ddrd 0xa #define portd 0xb #define pine 0xc #define ddre 0xd #define porte 0xe #define pinf 0xf #define ddrf 0x10 #define portf 0x11 #define ping 0x12 #define ddrg 0x13 #define portg 0x14 #define pinh 0xe0 #define ddrh 0xe1 #define porth 0xe2 #define pinj 0xe3 #define ddrj 0xe4 #define portj 0xe5 #define pink 0xe6 #define ddrk 0xe7 #define portk 0xe8 #define pinl 0xe9 #define ddrl 0xea #define portl 0xeb /************timers ************/ #define tccr0a 0x24 #define tccr0b 0x25 #define tcnt0 0x26 #define ocr0a 0x27 #define ocr0b 0x28 #define timsk0 0x4e #define tifr0 0x15 #define com0a1 0x7 #define com0a0 0x6 #define com0b1 0x5 #define com0b0 0x4 #define wgm02 0x3 #define wgm01 0x1 #define wgm00 0x0 #define cs02 0x2 #define cs01 0x1 #define cs00 0x0 #define ocie0b 0x2 #define ocie0a 0x1 #define toie0 0x0 /****** interrupts *******/ #define eicra 0x49 #define eicrb 0x4a #define eimsk 0x1d #define pcicr 0x48 #define pcmsk0 0x4b #define pcmsk1 0x4c #define pcmsk2 0x4d /************eeprom************/ #define eecr 0x1f #define eedr 0x20 #define eearl 0x21 #define eearh 0x22 #define eear0 0 #define eear1 1 #define eear2 2 #define eear3 3 #define eear4 4 #define eear5 5 #define eear6 6 #define eear7 7 #define eear8 8 #define eear9 9 #define eear10 10 #define eear11 11 #define lsb 0 #define msb 7 #define eere 0 #define eepe 1 #define eempe 2 #define eerie 3 #define eepm0 4 #define eepm1 5 /************uart status control registers & data registers **************/ /** usart3 **/ #define ucsr3a 0x130 #define ucsr3b 0x131 #define ucsr3c 0x132 #define ubrr3l 0x134 #define ubrr3h 0x135 #define udr3 0x136 #define udr3_txb udr3 /* transmit data buffer register */ #define udr3_rxb udr3 /* receive data buffer register */ /** usart2 **/ #define udr2 0xd6 #define ubrr2h 0xd5 #define ubrr2l 0xd4 #define ucsr2c 0xd2 #define ucsr2b 0xd1 #define ucsr2a 0xd0 #define udr2_txb udr2 /* transmit data buffer register */ #define udr2_rxb udr2 /* receive data buffer register */ /** usart1 **/ #define udr1 0xce #define ubrr1h 0xcd #define ubrr1l 0xcc #define ucsr1c 0xca #define ucsr1b 0xc9 #define ucsr1a 0xc8 #define udr1_txb udr1 /* transmit data buffer register */ #define udr1_rxb udr1 /* receive data buffer register */ /** usart0 **/ #define udr0 0xc6 #define ubrr0h 0xc5 #define ubrr0l 0xc4 #define ucsr0c 0xc2 #define ucsr0b 0xc1 #define ucsr0a 0xc0 #define udr0_txb udr0 /* transmit data buffer register */ #define udr0_rxb udr0 /* receive data buffer register */ #define txb80 0 #define rxb80 1 #define ucsz02 2 #define txen0 3 #define rxen0 4 #define udrie0 5 #define txcie0 6 #define rxcie0 7 #define mpcm0 0 #define u2x0 1 #define upe0 2 #define dor0 3 #define fe0 4 #define udre0 5 #define txc0 6 #define rxc0 7 #define ucpol0 0 #define ucsz00 1 #define ucsz01 2 #define usbs0 3 #define upm00 4 #define upm01 5 #define umsel00 6 #define umsel01 7 /******************************/ # define sei() __asm__ __volatile__ ("sei" ::) # define cli() __asm__ __volatile__ ("cli" ::) /* vectors in avr hard cided function name */ extern void __vector_23 (void) __attribute__ ((interrupt)); /* interrupt disabled @ initilization */ extern void __vector_11 (void) __attribute__ ((interrupt)); /*******addr_multiplier********/ #define addr_multiplier 1 /*******type declaration*******/ #define uint unsigned int #define p_char volatile unsigned char * /***leds on-off declaration****/ #define led_on 0x00 #define led_off 0xff #endif
i highly appreciate if me solving puzzle badly drowning under it.
thanks in advance.
well, it's bit hard check entire code, it's long , of course there lot of bad things setted on avr...
i think should first remove led's code , try uart recorder alone, until works.
the thing saw do:
unsigned char tx = eeprom_read(uiadd++);
but not set uiadd start 0...
anyway, i'm not sure because it's hard follow program flow.
Comments
Post a Comment