Created
March 19, 2013 13:09
-
-
Save bananu7/5195965 to your computer and use it in GitHub Desktop.
Text editor on PIC microcontroller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <p18f4520.h> | |
| #include <delays.h> | |
| #include <usart.h> | |
| #include "xlcd.h" | |
| #include<ctype.h> | |
| #pragma config WDT = OFF | |
| int j; | |
| char rb = '?'; | |
| void initXLCD(void); | |
| void txtXLCD(void); | |
| void initUSART(void); | |
| char reciveByteAndShow(void); | |
| void sendByte(char c); | |
| void initUSART(void) { | |
| // Inicjalizacja modułu USART | |
| TRISCbits.TRISC7 = 1; | |
| TRISCbits.TRISC6 = 1; | |
| //SPBRG = 103; | |
| SPBRG = 51; | |
| // TX - wysylanie | |
| // Sprawdzanie parzystosci | |
| TXSTAbits.TX9 = 1; | |
| // Wlaczenie transmisji (zawsze 1!) | |
| TXSTAbits.TXEN = 1; | |
| // Tryb synchroniczny/asynchroniczny | |
| TXSTAbits.SYNC = 0; | |
| // Tryb wyboru szybkosci | |
| TXSTAbits.BRGH = 1; | |
| RCSTAbits.SPEN = 1; | |
| // Sprawdzanie parzystosci | |
| RCSTAbits.RX9 = 1; | |
| RCSTAbits.CREN = 1; | |
| } | |
| const int start_position = 0x00; | |
| const int end_position = 0x0f; | |
| int position = 0x00; | |
| char reciveByteAndShow() { | |
| // Odebranie bajtu danych z komputera PC | |
| // i wyśietlenie go na wyświetlaczu LCD | |
| char parity; | |
| while (~PIR1bits.RCIF); | |
| rb = RCREG; | |
| parity = RCSTAbits.RX9D; | |
| /* if (parity) | |
| rb = 'X'; | |
| else | |
| rb = '0';*/ | |
| /* Uzupełnij kod */ | |
| if (rb == 0x8) { // backspace | |
| SetDDRamAddr(position); | |
| putcXLCD(' '); | |
| if (position > start_position) | |
| --position; | |
| } | |
| else if (rb == 0xd) // enter? | |
| { | |
| if (position < end_position) | |
| position = 0x40; | |
| } | |
| else { | |
| if (position < end_position) | |
| ++position; | |
| SetDDRamAddr(position); | |
| putcXLCD(rb); | |
| } | |
| return rb; | |
| } | |
| char convert(char c) { | |
| if (c >= 'a' && c <= 'z') | |
| return toupper(c); | |
| else if (c >= 'A' && c <= 'Z') | |
| return tolower(c); | |
| else | |
| return c; | |
| } | |
| char calc_parity (char c) { | |
| char p; | |
| for (int i = 0 ; i < 8; ++i) | |
| if (c & (1 << i)) | |
| p = !p; | |
| return c; | |
| } | |
| void sendByte(char c) { | |
| // Wysłanie danych do komputera PC | |
| TXSTA.TX9D = calc_parity(c); | |
| while(~PIR1bits.TXIF); | |
| TXREG = c; | |
| } | |
| void main() { | |
| initXLCD(); | |
| txtXLCD(); | |
| initUSART(); | |
| while(1) { | |
| rb = reciveByteAndShow(); | |
| rb = convert(rb); | |
| Delay1KTCYx(10); | |
| sendByte(rb); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment