Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include "LCD/lcd44780.h"
- #define BAUD 19200
- #define MYUBRR F_CPU/16/BAUD-1
- #define MENU_GL_SIZE 2
- #define MAX_SUBMENU_SIZE 3
- volatile uint8_t p=0;
- char data;
- const char id[10]={26,1,1,0x05,0x3c,0xf5,0x40,0x2d,165,27};
- enum
- {
- MENU_GLOWNE=0,
- MENU_1,
- MENU_2,
- MENU_3,
- TRYB_RAY,
- TRYB_ODBIOR
- }menu=MENU_GLOWNE;
- const char *menu_str[MENU_GL_SIZE+1][MAX_SUBMENU_SIZE] =\
- {{"nabial", "starcraft"},
- {"ser", "kefir"},
- {"Terran", "Protoss", "Zerg"}};
- const uint8_t menu_size[MENU_GL_SIZE+1] = {MENU_GL_SIZE, 2, 2};
- inline void USART_Init( unsigned int baud)
- {
- /* Set baud rate */
- UBRRH = (unsigned char)(baud>>8);
- UBRRL = (unsigned char)baud;
- /* Enable receiver and transmitter */
- UCSRB = (1<<RXEN)|(1<<TXEN);
- /* Set frame format: 8data, 2stop bit */
- UCSRC = (1<<USBS)|(3<<UCSZ0);
- }
- inline void USART_Transmit( unsigned char data )
- {
- while ( !( UCSRA & (1<<UDRE)) )
- ;
- UDR = data;
- }
- void ray(void) //nie nie, wcale nie gram w starcrafta
- {
- for (uint8_t ti=0; ti<10;ti++)
- USART_Transmit(id[ti]);
- for (uint8_t ti=3;ti<8;ti++)
- {
- lcd_hex(id[ti]);
- if(ti<7)lcd_str(":");
- }
- }
- void PiszMenu(uint8_t pos)
- {
- char numer[3]; // pomocnicza
- lcd_cls();
- lcd_locate(0, 0);
- lcd_str(">");
- // piszemy numer pozycji menu
- numer[0] = pos+0x31; // jak itoa()
- numer[1] = '.';
- numer[2] = '\0';
- lcd_str(numer);
- // teraz tekst pozycji
- lcd_str((char*)menu_str[menu][pos]);
- if((pos+1) < menu_size[menu]) // nastepna linijka tylko, jesli jest
- {
- lcd_locate(0, 1);
- numer[0]++;
- lcd_str(numer);
- // teraz tekst
- lcd_str((char*)menu_str[menu][pos]);
- }
- }
- void debounce(void)
- {
- _delay_ms(20);
- p=0;
- GIMSK |= ((1<<INT0)|(1<<INT1));
- }
- int main(void)
- {
- uint8_t w=0;
- uint8_t odb=1; // teraz odb bedzie znaczyc, czy pierwszy raz
- // wykonuje sie kod jakiegos trybu(zeby zrobic
- // wyswietlenie napisow tylko raz)
- uint8_t menu_pos=0;
- USART_Init(MYUBRR);
- DDRD|=(1<<PD6);
- DDRD&=~((1<<PD2)|(1<<PD3));
- PORTD|=((1<<PD2)|(1<<PD3));
- MCUCR |= ((1<<ISC01)|(1<<ISC11)); // przerwanie na opadajacym zboczu
- GIMSK |= ((1<<INT0)|(1<<INT1));
- lcd_init();
- lcd_cls();
- lcd_str("Elo elo");
- _delay_ms(100);
- lcd_cls();
- sei();
- for(;;)
- {
- UCSRB|=(1<<RXCIE);
- switch(menu)
- {
- case TRYB_RAY:
- if((p==1)||odb) // wyslij
- {
- lcd_cls();
- lcd_locate(0,0);
- lcd_str("Wyslano karte 1");
- lcd_locate(1,0);
- ray();
- odb=0;
- debounce();
- }
- if(p==2) // wroc
- {
- menu=MENU_1;
- odb=1;
- debounce();
- }
- break;
- case TRYB_ODBIOR:
- if(data) // cos przyszlo z USARTu
- {
- lcd_str(":");
- if (w==4)
- {
- lcd_locate(1,1);
- w=0;
- }
- lcd_hex(data);
- w++;
- data=0;
- }
- if(odb)
- {
- lcd_cls();
- lcd_locate(0,0);
- lcd_str("Odebrano dane: ");
- lcd_locate(1,0);
- w=0;
- odb=0;
- }
- if(p==2) // wroc
- {
- menu=MENU_1;
- odb=1;
- debounce();
- }
- break;
- default: // znaczy ze ma byc menu, a nie jakis tryb
- if(p==1) // OK
- {
- if(!(PIND&(1<<PD3))) // wroc
- menu=MENU_GLOWNE;
- if(menu==MENU_GLOWNE)
- menu=menu_pos+1;
- else
- menu = ((menu-1)*MAX_SUBMENU_SIZE)+4+menu_pos;
- // dziwny wzor, ale moze bedzie dzialal...
- menu_pos=0;
- PiszMenu(menu_pos);
- }
- if(p==2) // wybor menu(tylko w dol)
- {
- if(++menu_pos == menu_size[menu]) // jesli na ostatniej pozycji
- menu_pos=0;
- PiszMenu(menu_pos);
- debounce();
- }
- }
- }
- }
- ISR(INT0_vect)
- {
- p=1;
- GIMSK &= ~(1<<INT0);
- }
- ISR(INT1_vect)
- {
- p=2;
- GIMSK &= ~(1<<INT1);
- }
- ISR(USART_RX_vect)
- {
- data = UDR;
- UCSRB&=~(1<<RXCIE);
- }
Add Comment
Please, Sign In to add comment