Guest User

Untitled

a guest
Oct 21st, 2019
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define WDTCL *((short *)0x120)
  2. #define WDTHOLD 0x0080
  3. #define WDTPW 0x5a00
  4.  
  5. #define LCD_Data P2OUT
  6. #define _100us 100 //
  7. #define _10us 10 //
  8. #define E 3 //P2.3
  9. #define RS 2 //P2.2
  10.  
  11. #define CLR_DISP 0x01 // clear display
  12. #define CUR_HOME 0x02 // return home
  13. #define ENTRY_INC 0x06 // entry mode increment
  14. #define ENTRY_INC_ROL 0x07 // entry mode increment with rol data
  15. #define ENTRY_DEC 0x04 // entry mode decrement
  16. #define ENTRY_DEC_ROL 0x05 // entry mode decrement witch rol dat
  17. #define DISP_OFF 0x08 // all display off
  18. #define DISP_ON 0x0c // all display on
  19.  
  20. #define DATA_ROL_LEFT 0x18 // rol data left
  21. #define DATA_ROL_RIGHT 0x1c // rol data right
  22. #define CUR_SHIFT_LEFT 0x10 // shift coursor left
  23. #define CUR_SHIFT_RIGHT 0x14 // shift coursor right
  24.  
  25. #define DD_RAM_ADDR 0x80 // set DD_RAM
  26. #define DD_RAM_ADDR2 0xc0 // set CG_RAM
  27. #define DD_RAM_ADDR3 0x28 //
  28. #define CG_RAM_ADDR 0x40 //
  29.  
  30. #include "msp430x14x.h"
  31.  
  32. #define bitset(var,bitno) ((var) |= 1 << (bitno))
  33. #define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))
  34.  
  35. void Delay (unsigned int a);
  36. void Delayx100us(unsigned char b);
  37. void _E(void);
  38.  
  39. void SEND_CMD (unsigned char e) {
  40. int temp;
  41. Delayx100us(10); //10ms
  42. temp = e & 0xf0; //get upper nibble
  43. LCD_Data &= 0x0f;
  44. LCD_Data |= temp; //send CMD to LCD
  45. bitclr(P2OUT,RS); //set LCD to CMD mode
  46. _E(); //toggle E for LCD
  47. temp = e & 0x0f;
  48. temp = temp << 4; //get down nibble
  49. LCD_Data &= 0x0f;
  50. LCD_Data |= temp;
  51. bitclr(P2OUT,RS); //set LCD to CMD mode
  52. _E(); //toggle E for LCD
  53. }
  54.  
  55. void clearDisplay() {
  56. SEND_CMD(CLR_DISP);
  57. Delayx100us(10);
  58. }
  59.  
  60. void Delay (unsigned int a) {
  61. int k;
  62. for (k=0 ; k != a; ++k) {
  63. _NOP();
  64. _NOP();
  65. _NOP();
  66. _NOP();
  67. }
  68. }
  69.  
  70. void Delayx100us(unsigned char b) {
  71. int j;
  72. for (j=0; j!=b; ++j) Delay (_100us);
  73. }
  74.  
  75.  
  76. void _E(void) {
  77. bitset(P2OUT,E); //toggle E for LCD
  78. Delay(_10us);
  79. bitclr(P2OUT,E);
  80. }
  81.  
  82.  
  83. void SEND_CHAR (unsigned char d) {
  84. int temp;
  85. Delayx100us(5); //.5ms
  86. temp = d & 0xf0; //get upper nibble
  87. LCD_Data &= 0x0f;
  88. LCD_Data |= temp;
  89. bitset(P2OUT,RS); //set LCD to data mode
  90. _E(); //toggle E for LCD
  91. temp = d & 0x0f;
  92. temp = temp << 4; //get down nibble
  93. LCD_Data &= 0x0f;
  94. LCD_Data |= temp;
  95. bitset(P2OUT,RS); //set LCD to data mode
  96. _E(); //toggle E for LCD
  97. }
  98.  
  99.  
  100. void InitPortsLcd(void) {
  101. P2SEL = 0;
  102. P2OUT = 0;
  103. P2DIR = ~BIT0; //only P2.0 is input
  104. }
  105.  
  106. void InitLCD(void) {
  107. bitclr(P2OUT,RS);
  108. Delayx100us(250); //Delay 100ms
  109. Delayx100us(250);
  110. Delayx100us(250);
  111. Delayx100us(250);
  112. LCD_Data |= BIT4 | BIT5; //D7-D4 = 0011
  113. LCD_Data &= ~BIT6 & ~BIT7;
  114. _E(); //toggle E for LCD
  115. Delayx100us(100); //10ms
  116. _E(); //toggle E for LCD
  117. Delayx100us(100); //10ms
  118. _E(); //toggle E for LCD
  119. Delayx100us(100); //10ms
  120. LCD_Data &= ~BIT4;
  121. _E(); //toggle E for LCD
  122.  
  123. SEND_CMD(DISP_ON);
  124. SEND_CMD(CLR_DISP);
  125. Delayx100us(250);
  126. Delayx100us(250);
  127. Delayx100us(250);
  128. Delayx100us(250);
  129. }
  130.  
  131. void write(char *s, int n) {
  132. if(n == 2) SEND_CMD(DD_RAM_ADDR2 + 0x01);
  133. for(int i=0; i<strlen(s); i++) {
  134. SEND_CHAR(s[i]);
  135. }
  136. }
  137.  
  138. void roll(char dir, int n) {
  139. for(int i=0; i<n; i++) {
  140. SEND_CMD( dir == 'L' ? DATA_ROL_LEFT : DATA_ROL_RIGHT );
  141. }
  142. }
  143.  
  144. void writewrap(char *s, int shift) {
  145. SEND_CMD(CLR_DISP);
  146. SEND_CMD(DD_RAM_ADDR + shift);
  147. if(shift > 15) SEND_CMD(DD_RAM_ADDR2+shift-16);
  148. for(int i=0; i<strlen(s); i++) {
  149. if(i == 16-shift) SEND_CMD(DD_RAM_ADDR2);
  150. SEND_CHAR(s[i]);
  151. }
  152. }
  153.  
  154. void writeint(int n) {
  155. char s[10];
  156. int i=0;
  157. int minus = n<0;
  158.  
  159. if(minus) n = -n;
  160.  
  161. do {
  162. s[i++] = (n % 10) + 48;
  163. n /= 10;
  164. } while(n);
  165.  
  166. if(minus) {
  167. s[i] = '-';
  168. s[i+1] = 0;
  169. } else {
  170. s[i] = 0;
  171. }
  172.  
  173. char s2[10];
  174. for(int i=0; i<strlen(s); i++) {
  175. s2[strlen(s)-i-1] = s[i];
  176. }
  177.  
  178. s2[strlen(s)] = 0;
  179.  
  180. writewrap(s2, 0);
  181. }
  182.  
  183. int main(void) {
  184.  
  185. WDTCL = WDTPW + WDTHOLD;
  186.  
  187. InitPortsLcd();
  188. InitLCD();
  189.  
  190. /*for(;;) {
  191. for(int i=0; i<37; i++) {
  192. writewrap("0123456789", i);
  193. Delayx100us(1000);
  194. }
  195. }*/
  196.  
  197. writeint(0);
  198.  
  199.  
  200. return 0;
  201. }
RAW Paste Data