Ben Gordon
/
Muscle_Controlled_Servo
ELEC240 Group Coursework ---------------------------------- C-Code for muscle controlled servo
LCD.cpp@10:6b9c7857d57c, 2018-06-01 (annotated)
- Committer:
- BenRJG
- Date:
- Fri Jun 01 13:39:12 2018 +0000
- Revision:
- 10:6b9c7857d57c
- Parent:
- 8:462ce856429b
fixed adc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BenRJG | 5:11489c0bd020 | 1 | #include "LCD.h" |
BenRJG | 5:11489c0bd020 | 2 | |
BenRJG | 5:11489c0bd020 | 3 | void LCD_INIT(void) |
BenRJG | 5:11489c0bd020 | 4 | { |
BenRJG | 5:11489c0bd020 | 5 | |
BenRJG | 5:11489c0bd020 | 6 | // Preload some arrays |
BenRJG | 5:11489c0bd020 | 7 | // char hello_world[] ="Hello World"; |
BenRJG | 8:462ce856429b | 8 | char splash_screen1[]="Group O"; |
BenRJG | 5:11489c0bd020 | 9 | char splash_screen2[]="Plymouth UNI"; |
BenRJG | 5:11489c0bd020 | 10 | char DVM[] ="Voltage="; |
BenRJG | 5:11489c0bd020 | 11 | |
BenRJG | 8:462ce856429b | 12 | LCD_CLR(); |
BenRJG | 8:462ce856429b | 13 | writeToLCD(splash_screen1,LINE1,1); //Credit line 1 |
BenRJG | 8:462ce856429b | 14 | writeToLCD(splash_screen2,LINE2,2); //Credit line 2 |
BenRJG | 5:11489c0bd020 | 15 | wait(2); |
BenRJG | 8:462ce856429b | 16 | LCD_CLR(); |
BenRJG | 8:462ce856429b | 17 | writeToLCD(DVM,LINE1,0); //Type Voltage display |
BenRJG | 8:462ce856429b | 18 | writeToLCD("V",LINE1,13); //Units display |
BenRJG | 5:11489c0bd020 | 19 | } |
BenRJG | 5:11489c0bd020 | 20 | |
BenRJG | 8:462ce856429b | 21 | void writeToLCD(char* str, int line, int pos) |
BenRJG | 8:462ce856429b | 22 | { |
BenRJG | 8:462ce856429b | 23 | unsigned int length = strlen(str); |
BenRJG | 8:462ce856429b | 24 | cmdLCD(line|pos); |
BenRJG | 5:11489c0bd020 | 25 | |
BenRJG | 8:462ce856429b | 26 | if(pos + length <= 16) |
BenRJG | 8:462ce856429b | 27 | { |
BenRJG | 8:462ce856429b | 28 | unsigned int i; |
BenRJG | 8:462ce856429b | 29 | |
BenRJG | 8:462ce856429b | 30 | for(i=0; i<length; i++) |
BenRJG | 8:462ce856429b | 31 | { |
BenRJG | 8:462ce856429b | 32 | putLCD(str[i]); |
BenRJG | 8:462ce856429b | 33 | } |
BenRJG | 8:462ce856429b | 34 | } |
BenRJG | 8:462ce856429b | 35 | else |
BenRJG | 8:462ce856429b | 36 | { |
BenRJG | 8:462ce856429b | 37 | unsigned int line1; |
BenRJG | 8:462ce856429b | 38 | unsigned int line2; |
BenRJG | 8:462ce856429b | 39 | |
BenRJG | 8:462ce856429b | 40 | line1 = findSpace(str); |
BenRJG | 8:462ce856429b | 41 | |
BenRJG | 8:462ce856429b | 42 | line2 = length - line1; |
BenRJG | 8:462ce856429b | 43 | |
BenRJG | 8:462ce856429b | 44 | unsigned int i; |
BenRJG | 8:462ce856429b | 45 | for(i=0; i<line1; i++) |
BenRJG | 8:462ce856429b | 46 | { |
BenRJG | 8:462ce856429b | 47 | putLCD(str[i]); |
BenRJG | 5:11489c0bd020 | 48 | } |
BenRJG | 8:462ce856429b | 49 | |
BenRJG | 8:462ce856429b | 50 | cmdLCD(LINE2); |
BenRJG | 8:462ce856429b | 51 | |
BenRJG | 8:462ce856429b | 52 | if((line2 <= 16)&&(line != 2)) |
BenRJG | 8:462ce856429b | 53 | { |
BenRJG | 8:462ce856429b | 54 | for(i=0; i<line2; i++) |
BenRJG | 8:462ce856429b | 55 | { |
BenRJG | 8:462ce856429b | 56 | putLCD(str[line1 + i]); |
BenRJG | 8:462ce856429b | 57 | } |
BenRJG | 8:462ce856429b | 58 | } |
BenRJG | 8:462ce856429b | 59 | else |
BenRJG | 8:462ce856429b | 60 | { |
BenRJG | 8:462ce856429b | 61 | for(i=0; i<(13); i++) |
BenRJG | 8:462ce856429b | 62 | { |
BenRJG | 8:462ce856429b | 63 | putLCD(str[line1 + i]); |
BenRJG | 8:462ce856429b | 64 | } |
BenRJG | 8:462ce856429b | 65 | |
BenRJG | 8:462ce856429b | 66 | for(i=0; i<3; i++) |
BenRJG | 8:462ce856429b | 67 | { |
BenRJG | 8:462ce856429b | 68 | putLCD('.'); |
BenRJG | 8:462ce856429b | 69 | } |
BenRJG | 8:462ce856429b | 70 | } |
BenRJG | 5:11489c0bd020 | 71 | } |
BenRJG | 5:11489c0bd020 | 72 | } |
BenRJG | 5:11489c0bd020 | 73 | |
BenRJG | 8:462ce856429b | 74 | void putLCD(unsigned char put) //sends a char to the LCD display |
BenRJG | 8:462ce856429b | 75 | { |
BenRJG | 8:462ce856429b | 76 | unsigned int data = WRITE|TEXT|put; //RS (text), RW (write), data (put) |
BenRJG | 8:462ce856429b | 77 | spi_write_data(CS_LCD, data); |
BenRJG | 8:462ce856429b | 78 | //spi_write_data(CS_LCD, (1<<8)+put); |
BenRJG | 8:462ce856429b | 79 | |
BenRJG | 8:462ce856429b | 80 | |
BenRJG | 8:462ce856429b | 81 | wait_us(25); |
BenRJG | 8:462ce856429b | 82 | } |
BenRJG | 8:462ce856429b | 83 | |
BenRJG | 8:462ce856429b | 84 | void cmdLCD(unsigned char cmd) //sends a byte to the LCD control register |
BenRJG | 8:462ce856429b | 85 | { |
BenRJG | 8:462ce856429b | 86 | unsigned int data = WRITE|COMMAND|cmd; //RS (command), RW (write), data (cmd) |
BenRJG | 8:462ce856429b | 87 | spi_write_data(CS_LCD, data); |
BenRJG | 8:462ce856429b | 88 | wait_us(2000); |
BenRJG | 8:462ce856429b | 89 | } |
BenRJG | 8:462ce856429b | 90 | |
BenRJG | 8:462ce856429b | 91 | unsigned int findSpace(char* str) |
BenRJG | 8:462ce856429b | 92 | { |
BenRJG | 8:462ce856429b | 93 | unsigned int space = 0; |
BenRJG | 8:462ce856429b | 94 | int n = 16; |
BenRJG | 8:462ce856429b | 95 | while(!space & (n != -1)) |
BenRJG | 5:11489c0bd020 | 96 | { |
BenRJG | 8:462ce856429b | 97 | n--; |
BenRJG | 8:462ce856429b | 98 | switch(str[n]) |
BenRJG | 8:462ce856429b | 99 | { |
BenRJG | 8:462ce856429b | 100 | case ' ': |
BenRJG | 8:462ce856429b | 101 | case '.': |
BenRJG | 8:462ce856429b | 102 | case ',': |
BenRJG | 8:462ce856429b | 103 | case ':': |
BenRJG | 8:462ce856429b | 104 | case ';': |
BenRJG | 8:462ce856429b | 105 | case '-': |
BenRJG | 8:462ce856429b | 106 | case '/': |
BenRJG | 8:462ce856429b | 107 | space = 1; |
BenRJG | 8:462ce856429b | 108 | break; |
BenRJG | 8:462ce856429b | 109 | } |
BenRJG | 5:11489c0bd020 | 110 | } |
BenRJG | 8:462ce856429b | 111 | if(n == -1){ return 16; } |
BenRJG | 8:462ce856429b | 112 | else{ return n+1; } |
BenRJG | 5:11489c0bd020 | 113 | } |