Advertisement
Guest User

SI

a guest
May 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.25 KB | None | 0 0
  1. #include <hidef.h> /* common defines and macros */
  2. #include "mc9s12dg256.h" /* derivative-specific definitions */
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9.  
  10. #define LCD_DATA PORTK
  11. #define LCD_CTRL PORTK
  12. #define RS 0x01
  13. #define EN 0x02
  14.  
  15. #define MAX 100
  16.  
  17. void COMWRT4(unsigned char);
  18. void DATWRT4(unsigned char);
  19. void MSDelay(unsigned int);
  20.  
  21.  
  22. /* Functii principale*/
  23. void afisString(char* p);
  24. void evaluateExpr(char* p);
  25. void afisIntreg(int number);
  26. char* p;
  27.  
  28. char expr[MAX]; //buffer in care retinem expresia aritmetica
  29.  
  30. int contor;
  31.  
  32. void main(void)
  33. {
  34.  
  35. char* p=expr;
  36. DDRK = 0xFF; //setare port K ca si output
  37.  
  38.  
  39. DDRA = 0x0F; // testam liniile(butoanele) P4,P5,P6,P7 ->inputs
  40.  
  41.  
  42.  
  43. COMWRT4(0x33); //reset sequence provided by data sheet
  44. MSDelay(1);
  45. COMWRT4(0x32); //reset sequence provided by data sheet
  46. MSDelay(1);
  47. COMWRT4(0x28); //Function set to four bit data length
  48. //2 line, 5 x 7 dot format
  49. MSDelay(1);
  50. COMWRT4(0x06); //entry mode set, increment, no shift
  51. MSDelay(1);
  52. COMWRT4(0x0E); //Display set, disp on, cursor on, blink off
  53. MSDelay(1);
  54. COMWRT4(0x01); //Clear display
  55. MSDelay(1);
  56. COMWRT4(0x80); //set start posistion, home position
  57. MSDelay(1);
  58.  
  59.  
  60.  
  61. /* CITIRE TASTATURA */
  62.  
  63.  
  64. for(;;){
  65. PORTA=0; //dezactivare coloane
  66.  
  67. //COLOANA 4
  68. PORTA=PORTA|0x08;
  69.  
  70. if(PORTA == 0x18) { //testare linie 1 coloana 4
  71.  
  72. while((PORTA & (1<<4))!=0); //debouncing
  73. afisString("+");
  74. MSDelay(200);
  75. strcat(p,"+");
  76. //MSDelay(1);
  77.  
  78. }
  79.  
  80. if(PORTA == 0x28) { //testare linie 2 coloana 4
  81.  
  82. while((PORTA & (1<<5))!=0);
  83. afisString("-");
  84. strcat(p,"-");
  85. // MSDelay(1);
  86.  
  87. }
  88.  
  89. if(PORTA == 0x48) { //testare linie 3 coloana 4
  90.  
  91. while((PORTA & (1<<6))!=0);
  92. afisString("*");
  93. strcat(p,"*");
  94. //SDelay(1);
  95.  
  96. }
  97.  
  98. if(PORTA == 0x88) { //testare linie 4 coloana 4
  99.  
  100. while((PORTA & (1<<7))!=0);
  101. afisString("/");
  102. strcat(p,"/");
  103. //Delay(1);
  104.  
  105.  
  106. }
  107.  
  108. PORTA=0; //dezactivare coloane
  109.  
  110.  
  111. // COLOANA 3
  112. PORTA=PORTA|0x04;
  113.  
  114. if(PORTA == 0x14) { //testare linie 1 coloana 3
  115.  
  116. while((PORTA & (1<<4))!=0);
  117. afisString("3");
  118. MSDelay(200);
  119. strcat(p,"3");
  120. //MSDelay(1);
  121.  
  122.  
  123. }
  124.  
  125. if(PORTA == 0x24) { //testare linie 2 coloana 3
  126.  
  127. while((PORTA & (1<<5))!=0);
  128. afisString("6");
  129. strcat(p,"6");
  130. //MSDelay(1);
  131.  
  132.  
  133. }
  134.  
  135. if(PORTA == 0x44) { //testare linie 3 coloana 3
  136.  
  137. while((PORTA & (1<<6))!=0);
  138. afisString("9");
  139. strcat(p,"9");
  140. //MSDelay(1);
  141.  
  142.  
  143. }
  144.  
  145. if(PORTA == 0x84) { //testare linie 4 coloana 3
  146.  
  147. while((PORTA & (1<<7))!=0);
  148. ;
  149. //MSDelay(1);
  150.  
  151. //afisString(p);
  152. afisString("=");
  153. evaluateExpr(p);
  154. }
  155.  
  156. PORTA=0; //dezactivare coloane
  157.  
  158.  
  159.  
  160. // COLOANA 2
  161. PORTA=PORTA|0x02;
  162.  
  163. if(PORTA == 0x12) { //testare linie 1 coloana 2
  164.  
  165. while((PORTA & (1<<4))!=0);
  166. afisString("2");
  167. MSDelay(200);
  168. strcat(p,"2");
  169. //MSDelay(1);
  170.  
  171.  
  172. }
  173.  
  174. if(PORTA == 0x22) { //testare linie 2 coloana 2
  175.  
  176. while( (PORTA & (1<<5))!=0);
  177. afisString("5");
  178. strcat(p,"5");
  179. //MSDelay(1);
  180.  
  181.  
  182. }
  183.  
  184. if(PORTA == 0x42) { //testare linie 3 coloana 2
  185.  
  186. while( (PORTA & (1<<6))!=0);
  187. afisString("8");
  188. strcat(p,"8");
  189. // MSDelay(1);
  190.  
  191.  
  192. }
  193.  
  194. if(PORTA == 0x82) { //testare linie 4 coloana 2
  195.  
  196. while( (PORTA & (1<<7))!=0);
  197. afisString("0");
  198. strcat(p,"0");
  199. //MSDelay(1);
  200.  
  201.  
  202. }
  203.  
  204.  
  205. PORTA=0;//dezactivare coloane
  206.  
  207.  
  208.  
  209.  
  210. // COLOANA 1
  211.  
  212. PORTA=PORTA|0x01;
  213.  
  214. //testare buton
  215. if(PORTA == 0x11) { //testare linie 1 coloana 1
  216.  
  217. while( (PORTA & (1<<4) )!=0);
  218. afisString("1");
  219. MSDelay(200);
  220. strcat(p,"1");
  221. //MSDelay(1);
  222. }
  223.  
  224. if(PORTA == 0x21) { //testare linie 2 coloana 1
  225.  
  226. while( (PORTA & (1<<5) )!=0);
  227. afisString("4");
  228. strcat(p,"4");
  229. // MSDelay(1);
  230.  
  231. }
  232.  
  233. if(PORTA == 0x41) { //testare linie 3 coloana 1
  234.  
  235. while( (PORTA & (1<<6) )!=0);
  236. afisString("7");
  237. strcat(p,"7");
  238. // MSDelay(1);
  239.  
  240. }
  241.  
  242.  
  243. if( PORTA == 0x81) { //testare linie 4 coloana 1
  244.  
  245. while( (PORTA & (1<<7) )!=0);
  246. afisString(".");
  247. strcat(p,".");
  248. //MSDelay(1);
  249.  
  250. }
  251.  
  252. PORTA=0; //dezactivare coloana
  253.  
  254. }
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261. }
  262.  
  263.  
  264.  
  265. void COMWRT4(unsigned char command)
  266. {
  267. unsigned char x;
  268.  
  269. x = (command & 0xF0) >> 2; //shift high nibble to center of byte for Pk5-Pk2
  270. LCD_DATA =LCD_DATA & ~0x3C; //clear bits Pk5-Pk2
  271. LCD_DATA = LCD_DATA | x; //sends high nibble to PORTK
  272. MSDelay(1);
  273. LCD_CTRL = LCD_CTRL & ~RS; //set RS to command (RS=0)
  274. MSDelay(1);
  275. LCD_CTRL = LCD_CTRL | EN; //rais enable
  276. MSDelay(5);
  277. LCD_CTRL = LCD_CTRL & ~EN; //Drop enable to capture command
  278. MSDelay(15); //wait
  279. x = (command & 0x0F)<< 2; // shift low nibble to center of byte for Pk5-Pk2
  280. LCD_DATA =LCD_DATA & ~0x3C; //clear bits Pk5-Pk2
  281. LCD_DATA =LCD_DATA | x; //send low nibble to PORTK
  282. LCD_CTRL = LCD_CTRL | EN; //rais enable
  283. MSDelay(5);
  284. LCD_CTRL = LCD_CTRL & ~EN; //drop enable to capture command
  285. MSDelay(15);
  286. }
  287.  
  288. void DATWRT4(unsigned char data)
  289. {
  290. unsigned char x;
  291.  
  292.  
  293.  
  294. x = (data & 0xF0) >> 2;
  295. LCD_DATA =LCD_DATA & ~0x3C;
  296. LCD_DATA = LCD_DATA | x;
  297. MSDelay(1);
  298. LCD_CTRL = LCD_CTRL | RS;
  299. MSDelay(1);
  300. LCD_CTRL = LCD_CTRL | EN;
  301. MSDelay(1);
  302. LCD_CTRL = LCD_CTRL & ~EN;
  303. MSDelay(5);
  304.  
  305. x = (data & 0x0F)<< 2;
  306. LCD_DATA =LCD_DATA & ~0x3C;
  307. LCD_DATA = LCD_DATA | x;
  308. LCD_CTRL = LCD_CTRL | EN;
  309. MSDelay(1);
  310. LCD_CTRL = LCD_CTRL & ~EN;
  311. MSDelay(15);
  312. }
  313.  
  314.  
  315. //functie afisare String pe display
  316. void afisString(char* string){
  317.  
  318. while(*string!='\0'){
  319. DATWRT4(*string);
  320. MSDelay(1);
  321. string++;
  322. }
  323.  
  324.  
  325. }
  326.  
  327.  
  328.  
  329. void MSDelay(unsigned int itime)
  330. {
  331. unsigned int i; unsigned int j;
  332. for(i=0;i<itime;i++)
  333. for(j=0;j<4000;j++);
  334. }
  335.  
  336.  
  337. //functia de calcul a expresiei introduse la calculator
  338. void evaluateExpr(char* expr){ //expr este este expresia introdusa prin intermediul butoanelor
  339.  
  340.  
  341.  
  342. //maxim 3 operanzi
  343. char operatori[2] = {0}; //vector in care retinem operatorii aritmetici +,-,/,*
  344. float operanzi[3]; //vector in care retinem operanzii
  345. unsigned k = 0;
  346. unsigned nrop = 0; //contor pentru a stii numarul de operatii care trebuie efectuat
  347. float rez = 0; //variabila in care retinem rezultatul final
  348. float aux = 0; //variabila intermediara pentru calcul
  349. char* expresie = expr; //variabila intermediara
  350. char* p; //folosit pentru obtinere operanzi
  351.  
  352. //variabile folosite pentru scrierea pe display a rezultatului "rez"
  353. int intreg=0; //parte intreaga a numarului
  354. float afterv=0; //parte fractionare a rezultatului
  355. int virgula=0; //partea zecimala
  356. int flag=0;
  357.  
  358. //Caz particular in care primul numar este negativ
  359. if(*expresie == '-'){ //daca primul caracter este cu minus setam flagul pe 1
  360.  
  361. flag=1;
  362. strcpy(expresie,expresie+1); // eliminare
  363. expresie[strlen(expresie)]='\0'; //setare sfarsit de sir
  364. }
  365.  
  366.  
  367. //obtinere operatori
  368. while (*expresie != '\0'){ //parcugem caracter cu caracter si retinem operatorii in vector
  369.  
  370. if (!isdigit(*expresie) && *expresie !='.')
  371. operatori[nrop++] = *expresie; //DETERMINAM NUMARUL DE OPERATII
  372. ++expresie;
  373. }
  374.  
  375.  
  376.  
  377. p = strtok(expr, "/+-*");
  378.  
  379. //obtinere operanzi
  380. while (p!=NULL){ //separam fiecare operand
  381.  
  382. //operanzi[k]= strtod(p,NULL);
  383. operanzi[k]=atof(p); //convertire din char[] la float
  384. k++;
  385. p = strtok(NULL, "/+-*");
  386.  
  387. }
  388.  
  389.  
  390. //flag == 1 primul numar trebuie sa devina negativ
  391. if(flag==1){ //daca flagul este 1 atunci primul operand este negativ
  392. operanzi[0]*=(-1); //il inmultim cu -1
  393. }
  394.  
  395.  
  396.  
  397. //testare impartire la 0
  398. if(((operatori[0] =='/') && (operanzi[1]==0)) || ((operatori[1] =='/') && (operanzi[2]==0))){
  399.  
  400. COMWRT4(0x01); //clear
  401. MSDelay(1);
  402. COMWRT4(0x80); //set start posistion, home position
  403. MSDelay(1);
  404. afisString("Eroare /0");
  405. }
  406.  
  407.  
  408.  
  409.  
  410. /* Calcul expresie */
  411.  
  412. //daca avem un operator
  413. //avem 2 operanzi
  414. if (nrop == 1){
  415.  
  416. switch (operatori[0]){
  417. case '+':
  418. rez = operanzi[0] + operanzi[1];
  419. break;
  420.  
  421. case '-':
  422. rez = operanzi[0] - operanzi[1];
  423. break;
  424.  
  425. case '*':
  426. rez = operanzi[0] * operanzi[1];
  427. break;
  428.  
  429. case '/':
  430. rez = operanzi[0] / operanzi[1];
  431. break;
  432.  
  433. default:
  434. break;
  435.  
  436. }
  437.  
  438. //2 operatori => 3 operanzi
  439. }else if ((operatori[1] == '*' || operatori[1] == '/' )&& (operatori[0] == '+' || operatori[0] == '-')){
  440. //daca al doilea operator este * sau / si primul operator este + sau - atunci trebuie sa se respecte ordinea operatiilor
  441. //Pentru a se respecta ordinea operatiilor in cazul mentionat mai sus se calculeaza mai intai operatia ditre operand 2 si operand 3
  442. switch (operatori[1]){
  443. case '*':
  444. aux = operanzi[1] * operanzi[2];
  445. break;
  446. case '/':
  447. aux = operanzi[1] / operanzi[2];
  448. break;
  449.  
  450. }
  451.  
  452. switch (operatori[0]){
  453. case '+':
  454. rez = aux + operanzi[0];
  455. break;
  456. case '-':
  457. rez = aux - operanzi[0];
  458. break;
  459.  
  460. }
  461.  
  462.  
  463.  
  464. }
  465. else{ //daca nu exista exceptie ordinea operatiilor se desfasoara in mod normal
  466. switch (operatori[0]){
  467. case '*':
  468. aux = operanzi[0] * operanzi[1];
  469. break;
  470. case '/':
  471. aux = operanzi[0] / operanzi[1];
  472. break;
  473. case '+':
  474. aux = operanzi[0] + operanzi[1];
  475. break;
  476. case '-':
  477. aux = operanzi[0] - operanzi[1];
  478. break;
  479.  
  480. }
  481.  
  482. switch (operatori[1]){
  483. case '+':
  484. rez = aux + operanzi[2];
  485. break;
  486. case '-':
  487. rez = aux - operanzi[2];
  488. break;
  489. case '*':
  490. rez = aux * operanzi[2];
  491. break;
  492. case '/':
  493. rez = aux / operanzi[2];
  494. break;
  495.  
  496. }
  497.  
  498.  
  499. }
  500.  
  501.  
  502. /* PRELUCRARE REZULTAT */
  503.  
  504. intreg=(int)rez; //retinem partea intreaga
  505.  
  506.  
  507. if(intreg<0){ //daca numarul este negativ
  508.  
  509. DATWRT4('-'); //afisam '-'
  510. intreg*=(-1);
  511. afisIntreg(intreg); //afisam partea intreaga
  512. rez*=(-1);
  513. afterv=(float)(rez - intreg);//extragem partea fractionara
  514. afterv=afterv*100;
  515. afterv=ceil(afterv);//aproxim primele 2 cifre din parte fractionara
  516.  
  517. if(afterv!=0){ //daca are parte fractionara
  518. DATWRT4('.'); //afisam '.'
  519. virgula=(int)afterv;//obtinem primele 2 cifre
  520. afisIntreg(virgula); //afisam primele 2 cifre din partea fractionara
  521. }
  522.  
  523. }
  524. else{ //daca numarul este pozitiv
  525.  
  526. afisIntreg(intreg); //afisam partea intreaga
  527. afterv=(float)(rez - intreg); //aflam partea fractionara
  528. afterv=afterv*100;//primele 2 cifre din partea fractionara
  529. afterv=ceil(afterv); //rotunjim superior partea fractionara
  530.  
  531. if(afterv!=0){ //daca exista parte fractionara
  532. DATWRT4('.'); //print '.'
  533. virgula=(int)afterv;
  534. afisIntreg(virgula); //afiasare primele 2 cifre din partea fractionara
  535. }
  536. }
  537.  
  538.  
  539.  
  540. }
  541.  
  542. //afisare numar intreg cifra cu cifra
  543. void afisIntreg(int nr){
  544. if(!nr)
  545. return;
  546. afisIntreg(nr/10);
  547. DATWRT4((unsigned char)(nr%10 + '0'));
  548.  
  549. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement