Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. //Displaying "HELLO" on LCD for Dragon12+ Trainer Board
  2. //with HCS12 Serial Monitor Program installed. This code is for CodeWarrior IDE
  3. //Modified from Mazidi's book with contribution from Travis Chandler
  4. //On Dragon12+ LCD data pins of D7-D4 are connected to Pk5-Pk2, En=Pk1,and RS=Pk0,
  5.  
  6.  
  7. #include <hidef.h> /* common defines and macros */
  8. #include "mc9s12dg256.h" /* derivative-specific definitions */
  9. #include <string.h>
  10.  
  11. #define LCD_DATA PORTK
  12. #define LCD_CTRL PORTK
  13. #define RS 0x01
  14. #define EN 0x02
  15.  
  16. #define MAX 100
  17.  
  18. void COMWRT4(unsigned char);
  19. void DATWRT4(unsigned char);
  20. void MSDelay(unsigned int);
  21. void afisString(char* p);
  22.  
  23. char* p;
  24.  
  25. char expr[MAX];
  26.  
  27. int contor;
  28.  
  29. void main(void)
  30. {
  31. char* p=expr;
  32. DDRK = 0xFF;
  33.  
  34.  
  35. DDRA = 0x0F; //testam liniile(butoanele) 4,5,6,7 ->inputs
  36.  
  37. // PORTA=PORTA|0x01; //pentru testare buton 1 setam PA0 pe 1 si PA1-PA3-0
  38.  
  39.  
  40. COMWRT4(0x33); //reset sequence provided by data sheet
  41. MSDelay(1);
  42. COMWRT4(0x32); //reset sequence provided by data sheet
  43. MSDelay(1);
  44. COMWRT4(0x28); //Function set to four bit data length
  45. //2 line, 5 x 7 dot format
  46. MSDelay(1);
  47. COMWRT4(0x06); //entry mode set, increment, no shift
  48. MSDelay(1);
  49. COMWRT4(0x0E); //Display set, disp on, cursor on, blink off
  50. MSDelay(1);
  51. COMWRT4(0x01); //Clear display
  52. MSDelay(1);
  53. COMWRT4(0x80); //set start posistion, home position
  54. /* MSDelay(1);
  55. DATWRT4('H');
  56. MSDelay(1);
  57. DATWRT4('E');
  58. MSDelay(1);
  59. DATWRT4('L');
  60. MSDelay(1);
  61. DATWRT4('L');
  62. MSDelay(1);
  63. DATWRT4('O');
  64. MSDelay(1);
  65.  
  66. DATWRT4('V'); */
  67. // afisString("Numere");
  68.  
  69.  
  70.  
  71.  
  72. for(;;){
  73.  
  74. /* COLOANA 4 */
  75. PORTA=PORTA|0x08;
  76.  
  77. if( (PORTA & (1<<4) & (1<<3))!=0) {
  78.  
  79. while( (PORTA & (1<<4)& (1<<3))!=0);
  80. // afisString("+");
  81. strcat(p,"+");
  82. //MSDelay(1);
  83.  
  84. }
  85.  
  86. if( (PORTA & (1<<5) & (1<<3))!=0) {
  87.  
  88. while( (PORTA & (1<<5) & (1<<3))!=0);
  89. //afisString("-");
  90. strcat(p,"-");
  91. // MSDelay(1);
  92.  
  93. }
  94.  
  95. if( (PORTA & (1<<6) & (1<<3))!=0) {
  96.  
  97. while( (PORTA & (1<<6) & (1<<3) )!=0);
  98. //afisString("*");
  99. strcat(p,"*");
  100. //SDelay(1);
  101.  
  102. }
  103.  
  104. if( (PORTA & (1<<7) & (1<<3))!=0) {
  105.  
  106. while( (PORTA & (1<<7) & (1<<3))!=0);
  107. // afisString("/");
  108. strcat(p,"/");
  109. //Delay(1);
  110.  
  111.  
  112. }
  113.  
  114. PORTA=0;
  115.  
  116.  
  117. /* COLOANA 3 */
  118. PORTA=PORTA|0x04;
  119.  
  120. if( (PORTA & (1<<4))!=0 & (1<<2)) {
  121.  
  122. while( (PORTA & (1<<4)& (1<<2))!=0);
  123. // afisString("3");
  124. strcat(p,"3");
  125. //MSDelay(1);
  126.  
  127.  
  128. }
  129.  
  130. if( (PORTA & (1<<5) & (1<<2))!=0) {
  131.  
  132. while( (PORTA & (1<<5)& (1<<2))!=0);
  133. // afisString("6");
  134. strcat(p,"6");
  135. //MSDelay(1);
  136.  
  137.  
  138. }
  139.  
  140. if( (PORTA & (1<<6)& (1<<2))!=0) {
  141.  
  142. while( (PORTA & (1<<6)& (1<<2))!=0);
  143. //afisString("9");
  144. strcat(p,"9");
  145. //MSDelay(1);
  146.  
  147.  
  148. }
  149.  
  150. if( (PORTA & (1<<7)& (1<<2))!=0) {
  151.  
  152. while( (PORTA & (1<<7)&(1<<2))!=0);
  153. // afisString("=");
  154. //MSDelay(1);
  155.  
  156. afisString(p);
  157. }
  158.  
  159. PORTA=0;
  160.  
  161.  
  162.  
  163. /* COLOANA 2 */
  164. PORTA=PORTA|0x02;
  165.  
  166. if( (PORTA & (1<<4)&(1<<1))!=0) {
  167.  
  168. while( (PORTA & (1<<4)&(1<<1))!=0);
  169. //afisString("2");
  170. strcat(p,"2");
  171. //MSDelay(1);
  172.  
  173.  
  174. }
  175.  
  176. if( (PORTA & (1<<5)&(1<<1))!=0) {
  177.  
  178. while( (PORTA & (1<<5)&(1<<1))!=0);
  179. //afisString("5");
  180. strcat(p,"5");
  181. //MSDelay(1);
  182.  
  183.  
  184. }
  185.  
  186. if( (PORTA & (1<<6)&(1<<1))!=0) {
  187.  
  188. while( (PORTA & (1<<6)&(1<<1))!=0);
  189. //afisString("8");
  190. strcat(p,"8");
  191. // MSDelay(1);
  192.  
  193.  
  194. }
  195.  
  196. if( (PORTA & (1<<7)&(1<<1))!=0) {
  197.  
  198. while( (PORTA & (1<<7)&(1<<1))!=0);
  199. //afisString("0");
  200. strcat(p,"0");
  201. //MSDelay(1);
  202.  
  203.  
  204. }
  205.  
  206. //PORTA&=0xF0;
  207. PORTA=0;
  208.  
  209.  
  210.  
  211.  
  212. /* COLOANA 1 */
  213.  
  214. //while(PORTA&0xF0==0xF0);
  215.  
  216.  
  217. PORTA=PORTA|0x01;
  218.  
  219. //testare buton
  220. if( (PORTA & (1<<4) &1)!=0) {
  221.  
  222. while( (PORTA & (1<<4) &1)!=0);
  223. //afisString("1");
  224. strcat(p,"1");
  225. //MSDelay(1);
  226. }
  227.  
  228. if( (PORTA & (1<<5) &1)!=0) {
  229.  
  230. while( (PORTA & (1<<5) &1)!=0);
  231. //afisString("4");
  232. strcat(p,"4");
  233. // MSDelay(1);
  234.  
  235. }
  236.  
  237. if( (PORTA & (1<<6) &1)!=0) {
  238.  
  239. while( (PORTA & (1<<6) &1)!=0);
  240. //afisString("7");
  241. strcat(p,"7");
  242. // MSDelay(1);
  243.  
  244. }
  245.  
  246. if( (PORTA & (1<<7) &1)!=0) {
  247.  
  248. while( (PORTA & (1<<7) &1)!=0);
  249. //afisString("*");
  250.  
  251. strcat(p,"z");
  252. //MSDelay(1);
  253.  
  254. }
  255.  
  256. PORTA=0;
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267. }//stay here
  268.  
  269.  
  270.  
  271.  
  272.  
  273. }
  274. void COMWRT4(unsigned char command)
  275. {
  276. unsigned char x;
  277.  
  278. x = (command & 0xF0) >> 2; //shift high nibble to center of byte for Pk5-Pk2
  279. LCD_DATA =LCD_DATA & ~0x3C; //clear bits Pk5-Pk2
  280. LCD_DATA = LCD_DATA | x; //sends high nibble to PORTK
  281. MSDelay(1);
  282. LCD_CTRL = LCD_CTRL & ~RS; //set RS to command (RS=0)
  283. MSDelay(1);
  284. LCD_CTRL = LCD_CTRL | EN; //rais enable
  285. MSDelay(5);
  286. LCD_CTRL = LCD_CTRL & ~EN; //Drop enable to capture command
  287. MSDelay(15); //wait
  288. x = (command & 0x0F)<< 2; // shift low nibble to center of byte for Pk5-Pk2
  289. LCD_DATA =LCD_DATA & ~0x3C; //clear bits Pk5-Pk2
  290. LCD_DATA =LCD_DATA | x; //send low nibble to PORTK
  291. LCD_CTRL = LCD_CTRL | EN; //rais enable
  292. MSDelay(5);
  293. LCD_CTRL = LCD_CTRL & ~EN; //drop enable to capture command
  294. MSDelay(15);
  295. }
  296.  
  297. void DATWRT4(unsigned char data)
  298. {
  299. unsigned char x;
  300.  
  301.  
  302.  
  303. x = (data & 0xF0) >> 2;
  304. LCD_DATA =LCD_DATA & ~0x3C;
  305. LCD_DATA = LCD_DATA | x;
  306. MSDelay(1);
  307. LCD_CTRL = LCD_CTRL | RS;
  308. MSDelay(1);
  309. LCD_CTRL = LCD_CTRL | EN;
  310. MSDelay(1);
  311. LCD_CTRL = LCD_CTRL & ~EN;
  312. MSDelay(5);
  313.  
  314. x = (data & 0x0F)<< 2;
  315. LCD_DATA =LCD_DATA & ~0x3C;
  316. LCD_DATA = LCD_DATA | x;
  317. LCD_CTRL = LCD_CTRL | EN;
  318. MSDelay(1);
  319. LCD_CTRL = LCD_CTRL & ~EN;
  320. MSDelay(15);
  321. }
  322.  
  323.  
  324.  
  325. void afisString(char* string){
  326.  
  327. while(*string!='\0'){
  328. DATWRT4(*string);
  329. MSDelay(1);
  330. string++;
  331. }
  332.  
  333.  
  334. }
  335.  
  336.  
  337.  
  338. void MSDelay(unsigned int itime)
  339. {
  340. unsigned int i; unsigned int j;
  341. for(i=0;i<itime;i++)
  342. for(j=0;j<4000;j++);
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement