Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.33 KB | None | 0 0
  1. #include <p18F4550.h>
  2. #include <delays.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <i2c.h>
  6.  
  7. // Configuração do microcontrolador para execução de instruções
  8. #pragma config FOSC = HS /// EXTERN CLOCK 8MHZ
  9. #pragma config IESO = OFF /// INTERNAL/EXTERNAL OSCILATOR DISABLE
  10. #pragma config PWRT = OFF /// DISABLE POWER-UP TIMER
  11. #pragma config BORV = 3 /// BROWN-OUT RESET MINIMUM
  12. #pragma config WDT = OFF /// DISABLE WATCHDOG TIMER
  13. #pragma config WDTPS = 32768 /// WATCHDOG TIMER 32768s
  14. #pragma config MCLRE = OFF /// MASTER CLEAR PIN (RE3) DISBALE
  15. #pragma config LPT1OSC = OFF /// TIMER1 LOW POWER OPERATION
  16. #pragma config PBADEN = OFF /// PORTB.RB0,1,2,3,4 AS I/O DIGITAL
  17. #pragma config STVREN = ON /// STACK FULL/UNDERFLOW CAUSE RESET
  18. #pragma config LVP = OFF /// DISABLE LOW VOLTAGE PROGRAM (ICSP DISABLE)
  19.  
  20. /**************** PINAGEM DO LCD****************/
  21. #define LCD_RS PORTCbits.RC0
  22. #define LCD_EN PORTCbits.RC1
  23. #define LCD_DATA LATD
  24. #define LCD_CLEAR 0x01
  25. #define LCD_CURSOR_OFF 0x06
  26. #define FIRST_ROW 0x80
  27. #define SECOND_ROW 0xC0
  28. #define SDA PORTCbits.RC2
  29. #define SCL PORTCbits.RC4
  30.  
  31. /**************** PINAGEM DO TECLADO****************/
  32. #define Linha_1 LATBbits.LATB4
  33. #define Linha_2 LATBbits.LATB5
  34. #define Linha_3 LATBbits.LATB6
  35. #define Linha_4 LATBbits.LATB7
  36. #define Coluna_1 PORTBbits.RB3
  37. #define Coluna_2 PORTBbits.RB2
  38. #define Coluna_3 PORTBbits.RB1
  39.  
  40. //matriz de credenciais
  41. char usuarios[10][4] = {"1234", "5678"};
  42. char senhas[10][4] = {"1234", "5678"};
  43.  
  44. //matriz de logs
  45. char logs[10][8];
  46.  
  47. /******************************************************************************/
  48. //variáveis auxilixares
  49. unsigned char ram_msg[16];
  50. char funcao[6] = ">";
  51. char cred[6] = "N:";
  52. char credencial[4];
  53. char asteriscos[6] = "";
  54. unsigned char senha[4] = "0000";
  55. int seg;
  56. int min;
  57. int hr;
  58. /******************************************************************************/
  59. /*********************Protótipos das função do LCD ****************************/
  60. /******************************************************************************/
  61. void Delay_ms(unsigned int time); //função de delay
  62. void Lcd4_Init(void); //função que inicia LCD
  63. void Lcd4_Cmd(unsigned char value);
  64. void Lcd4_Write(unsigned char value);
  65. void Lcd4_Write_Text(unsigned char msg[]);
  66. void Lcd4_segundaLinha(unsigned char msg[], int flag);
  67. unsigned char * CopyConstToRAM(unsigned char *dest, const rom unsigned char * src);
  68. unsigned char * CopyToRAM(unsigned char *dest, unsigned char * src);
  69. void Clear_Destination_Buffer(void);
  70.  
  71. /******************************************************************************/
  72. /*************************** Protótipos dos Teclado ***************************/
  73. /******************************************************************************/
  74. unsigned char Varre_Teclado(unsigned char flag); //Método que varre o teclado
  75.  
  76. /******************************************************************************/
  77. /************************* Protótipos do Relógio ******************************/
  78. /******************************************************************************/
  79. char le_relogio();
  80. void inicializa_SCL_SDA();
  81. void start_SCL_SDA();
  82. void stop_SCL_SDA();
  83. void indica_leitura_relogio();
  84. void indica_leitura_segudos();
  85. void indica_leitura_minutos();
  86. void indica_leitura_horas();
  87. int le_segundos();
  88. int le_minutos();
  89. int le_horas();
  90. char convertNumero(int numero);
  91.  
  92. /******************************************************************************/
  93. /**********************Protótipos das Funções Lógicas *************************/
  94. /******************************************************************************/
  95. void selecionaFuncoes();
  96. void primeiraLinha(unsigned char var[]);
  97. void segundaLinha(unsigned char var[], int cont);
  98. void primeiraSegundaLinha(unsigned char var[], unsigned char var2[]);
  99. void cadastro();
  100. void acesso();
  101. void excluir();
  102. int verificaDados(char matricula[4], char senha[4]);
  103. void gravaLog(char matricula[4], char hora[8]);
  104. void metodoLogs();
  105. /******************************************************************************/
  106. /*************************** MÉTODO PRINCIPAL *********************************/
  107. /******************************************************************************/
  108.  
  109. void main() {
  110. TRISB = 0b00001111;
  111. TRISC = 0b11111100;
  112.  
  113. while (1) {
  114. acesso();
  115. }
  116. }
  117.  
  118. /******************************************************************************/
  119. /***********************DEFINIÇÕES DAS FUNÇÕES LÓGICAS*************************/
  120. /******************************************************************************/
  121. //método que identifica função selescionada
  122. int verificaDados(char matricula[4], char senha[4]) {
  123. int flag = 0;
  124. int i, j, cont = 0;
  125. char letra, pass;
  126.  
  127. //pecorre a matriz de credenciais
  128. for (i = 0; i < 10; i++) {
  129. for (j = 0; j < 4; j++) {
  130. letra = matricula[j];
  131. pass = senha[j];
  132.  
  133. if (usuarios[i][j] == letra && senhas[i][j] == pass) {
  134. cont++;
  135. }
  136.  
  137. if (cont == 4)
  138. i = 10;
  139.  
  140. matricula + 1;
  141. senha + 1;
  142. }
  143. }
  144.  
  145. if (cont == 4)
  146. flag = 1;
  147.  
  148. return flag;
  149. }
  150.  
  151. void gravaLog(char matricula[4], char hora[8]) {
  152.  
  153. int aux = 0;
  154. int i, j, cont = 0;
  155. char letra;
  156.  
  157. //pecorre a matriz de credenciais
  158. for (i = 0; i < 10; i++) {
  159. for (j = 0; j < 4; j++) {
  160. letra = matricula[j];
  161. if (usuarios[i][j] == letra) {
  162. cont++;
  163. }
  164.  
  165. //para laço
  166. if (cont == 4)
  167. {
  168. aux=i;
  169. i = 10;
  170. }
  171.  
  172. matricula + 1;
  173. }
  174. }
  175.  
  176. //grava horas no log
  177. if (cont == 4)
  178. {
  179. logs[aux][0] = hora[0];
  180. logs[aux][1] = hora[1];
  181. logs[aux][2] = ':';
  182. logs[aux][3] = hora[3];
  183. logs[aux][4] = hora[4];
  184. logs[aux][5] = ':';
  185. logs[aux][6] = hora[6];
  186. logs[aux][7] = hora[7];
  187. }
  188.  
  189. }
  190.  
  191. void selecionaFuncoes() {
  192. int cont = 0;
  193.  
  194. primeiraSegundaLinha("FUNCAO", ">");
  195.  
  196. //zerando cred, senha e funcao
  197. for (cont = 1; cont < 6; cont++) {
  198. funcao[cont] = ' ';
  199.  
  200. }
  201.  
  202. //digitos da função
  203. for (cont = 1; cont < 3; cont++) {
  204. funcao[cont] = Varre_Teclado(0);
  205. segundaLinha(funcao, 6);
  206. }
  207.  
  208. //cadastrar
  209. if (funcao[1] == '9' && funcao[2] == '9') {
  210. cadastro();
  211. }
  212.  
  213. //excluir
  214. if (funcao[1] == '9' && funcao[2] == '8') {
  215. excluir();
  216. }
  217.  
  218. //logs
  219. if (funcao[1] == '9' && funcao[2] == '7') {
  220. metodoLogs();
  221. }
  222. }
  223.  
  224. //método que busca log
  225. void metodoLogs()
  226. {
  227. int cont = 0;
  228. int parada = 0;
  229. int i, j, k;
  230. char l[8];
  231.  
  232. for (i = 2; i < 6; i++) {
  233. cred[i] = ' ';
  234. asteriscos[i - 2] = ' ';
  235. }
  236.  
  237. primeiraSegundaLinha("MATRICULA DO LOG", "N:");
  238.  
  239. for (i = 2; i < 6; i++) {
  240. if (cred[i - 1] == '#') {
  241. parada = 1;
  242. break;
  243. }
  244. cred[i] = Varre_Teclado(0);
  245. segundaLinha(cred, 6);
  246. }
  247.  
  248. if (parada == 0) {
  249. for (i = 0; i < 10; i++) {
  250. for (j = 0; j < 4; j++) {
  251. if (usuarios[i][j] == cred[j + 2]) {
  252. cont++;
  253. }
  254.  
  255. }
  256.  
  257. if(cont == 4)
  258. {
  259. cont = 0;
  260.  
  261. for(k=0; k<8; k++)
  262. {
  263. l[k] = logs[i][k];
  264. }
  265. }
  266.  
  267.  
  268. primeiraLinha("HORA DE ACESSO");
  269. segundaLinha(l, 8);
  270.  
  271. Delay10KTCYx(90);
  272. break;
  273. }
  274. cont = 0;
  275. }
  276.  
  277. }
  278. //método que imprime mensagem na primeira e segunda linha
  279.  
  280. void primeiraSegundaLinha(unsigned char var[], unsigned char var2[]) {
  281. Lcd4_Init(); //inicia display
  282. Lcd4_Cmd(LCD_CLEAR); //zera cursos do display
  283. Lcd4_Cmd(LCD_CURSOR_OFF); //desliga bit do cursor
  284. Lcd4_Cmd(LCD_CLEAR);
  285.  
  286. CopyConstToRAM(ram_msg, var); //método que copia string para memória do lcd
  287. Lcd4_Write_Text(ram_msg);
  288. Delay_ms(10);
  289.  
  290. Lcd4_Cmd(SECOND_ROW); //pula uma linha
  291. CopyConstToRAM(ram_msg, var2);
  292. Lcd4_Write_Text(ram_msg);
  293. }
  294.  
  295. //método que imprime mensagem na primeira linha
  296.  
  297. void primeiraLinha(unsigned char var[]) {
  298. Lcd4_Init(); //inicia display
  299. Lcd4_Cmd(LCD_CLEAR); //zera cursos do display
  300. Lcd4_Cmd(LCD_CURSOR_OFF); //desliga bit do cursor
  301. Lcd4_Cmd(LCD_CLEAR);
  302.  
  303. CopyConstToRAM(ram_msg, var); //método que copia string para memória do lcd
  304. Lcd4_Write_Text(ram_msg);
  305. Delay_ms(10);
  306. }
  307.  
  308. //método que imprime mensagem na segunda linha
  309.  
  310. void segundaLinha(unsigned char var[], int cont) {
  311. Lcd4_Cmd(SECOND_ROW); //pula uma linha
  312. CopyToRAM(ram_msg, var);
  313. Lcd4_segundaLinha(ram_msg, cont);
  314. }
  315.  
  316. //método que cadastra nova credencial
  317.  
  318. void cadastro() {
  319. int i, j;
  320. int cont;
  321. int parada = 0;
  322. ;
  323.  
  324. //zerando cred, senha
  325. for (cont = 2; cont < 6; cont++) {
  326. cred[cont] = ' ';
  327. asteriscos[cont - 2] = ' ';
  328. }
  329.  
  330. primeiraSegundaLinha("NOVA MATRICULA", "N:");
  331.  
  332. for (cont = 2; cont < 6; cont++) {
  333. if (cred[cont - 1] == '#') {
  334. parada = 1;
  335. break;
  336. }
  337. cred[cont] = Varre_Teclado(0);
  338. segundaLinha(cred, 6);
  339. }
  340.  
  341. //passando valores de cred para credencial
  342. for (i = 0; i < 4; i++)
  343. credencial[i] = cred[i + 2];
  344.  
  345. if (cont == 6) {
  346. primeiraLinha("SENHA:");
  347.  
  348. Lcd4_Cmd(SECOND_ROW); //pula uma linha
  349. CopyToRAM(ram_msg, asteriscos);
  350. Lcd4_Write_Text(ram_msg);
  351.  
  352. for (cont = 0; cont < 4; cont++) {
  353. if (senha[cont - 1] == '#') {
  354. parada = 1;
  355. break;
  356. }
  357.  
  358.  
  359. senha[cont] = Varre_Teclado(0);
  360. asteriscos[cont] = '*';
  361. segundaLinha(asteriscos, 6);
  362. }
  363. if (parada == 0) {
  364. //preenche o vetor usuarios com um usuário novo
  365. for (i = 0; i < 10; i++) {
  366. if (usuarios[i][0] == '\0') {
  367. for (j = 0; j < 4; j++) {
  368. usuarios[i][j] = credencial[j];
  369.  
  370. }
  371.  
  372. if (j == 4)
  373. i = 10;
  374. }
  375. }
  376.  
  377. //preenche o vetor senhas com uma senha nova
  378. for (i = 0; i < 10; i++) {
  379. if (senhas[i][0] == '\0') {
  380. for (j = 0; j < 4; j++) {
  381. senhas[i][j] = senha[j];
  382. }
  383.  
  384. if (j == 4)
  385. i = 10;
  386. }
  387. }
  388. }
  389. }
  390.  
  391. }
  392.  
  393. //método de exclusão
  394.  
  395. void excluir() {
  396. int cont = 0;
  397. int parada = 0;
  398. int i, j;
  399.  
  400. for (i = 2; i < 6; i++) {
  401. cred[i] = ' ';
  402. asteriscos[i - 2] = ' ';
  403. }
  404.  
  405. primeiraSegundaLinha("APAGAR MATRICULA", "N:");
  406.  
  407. for (i = 2; i < 6; i++) {
  408. if (cred[i - 1] == '#') {
  409. parada = 1;
  410. break;
  411. }
  412. cred[i] = Varre_Teclado(0);
  413. segundaLinha(cred, 6);
  414. }
  415.  
  416. if (parada == 0) {
  417. for (i = 0; i < 10; i++) {
  418. for (j = 0; j < 4; j++) {
  419. if (usuarios[i][j] == cred[j + 2]) {
  420. cont++;
  421. }
  422.  
  423. }
  424.  
  425. if (cont == 4) {
  426. for (j = 0; j < 4; j++) {
  427. usuarios[i][j] = '\0';
  428. senhas[i][j] = '\0';
  429. }
  430.  
  431. primeiraSegundaLinha("MATRICULA", "APAGADA...");
  432.  
  433. Delay10KTCYx(50);
  434. break;
  435. }
  436. cont = 0;
  437. }
  438. }
  439.  
  440. }
  441.  
  442. //método da função acesso
  443.  
  444. void acesso() {
  445. int cont;
  446. int flag = 0;
  447.  
  448. //zerando cred e senha
  449. for (cont = 2; cont < 6; cont++) {
  450. cred[cont] = ' ';
  451. asteriscos[cont - 2] = ' ';
  452. }
  453.  
  454. primeiraSegundaLinha("MATRICULA", "N:");
  455.  
  456. for (cont = 2; cont < 6; cont++) {
  457. if (cred[cont - 1] == '#') {
  458. selecionaFuncoes();
  459. break;
  460. }
  461.  
  462. cred[cont] = Varre_Teclado(0);
  463. segundaLinha(cred, 6);
  464. }
  465.  
  466. if (cont == 6) {
  467.  
  468. primeiraSegundaLinha("SENHA:", " ");
  469.  
  470. for (cont = 0; cont < 4; cont++) {
  471. if (senha[cont - 1] == '#') {
  472. cadastro();
  473. break;
  474. }
  475.  
  476. senha[cont] = Varre_Teclado(0);
  477. asteriscos[cont] = '*';
  478. segundaLinha(asteriscos, 6);
  479. }
  480.  
  481. //passando dados de cred para credencial
  482. for (cont = 2; cont < 6; cont++) {
  483. credencial[cont - 2] = cred[cont];
  484. }
  485.  
  486. //pecorre a matriz de credenciais
  487. if (verificaDados(credencial, senha) == 1) {
  488.  
  489. //ALANE
  490.  
  491. char hora[8];
  492. hora[0] = convertNumero(le_segundos()/10);
  493. hora[1] = convertNumero(le_segundos()%10);
  494. hora[2] = ':';
  495. hora[3] = convertNumero(le_minutos()/10);
  496. hora[4] = convertNumero(le_minutos()%10);
  497. hora[5] = ':';
  498. hora[6] = convertNumero(le_horas()/10);
  499. hora[7] = convertNumero(le_horas()%10);
  500. //ALANE
  501.  
  502. gravaLog(credencial, hora);
  503. flag = 1;
  504. }
  505.  
  506. if (flag == 1) {
  507. primeiraSegundaLinha("ACESSO", "PERMITIDO...");
  508. Delay10KTCYx(50);
  509.  
  510. } else {
  511. primeiraSegundaLinha("DADOS", "INCORRETOS...");
  512. Delay10KTCYx(50);
  513. }
  514.  
  515. }
  516.  
  517. }
  518.  
  519. /******************************************************************************/
  520. /*******************************DEFINIÇÕES DO RELÓGIO**************************/
  521. /******************************************************************************/
  522. void inicializa_SCL_SDA() {
  523. SDA = 1;
  524. SCL = 1;
  525. }
  526.  
  527. void start_SCL_SDA() {
  528. SDA = 0;
  529. SCL = 0;
  530. }
  531.  
  532. void stop_SCL_SDA() {
  533. SDA = 1;
  534. SCL = 1;
  535. }
  536.  
  537. void indica_leitura_relogio() {
  538. start_SCL_SDA();
  539. // DELAY
  540. SDA = 1; // BIT MSB
  541.  
  542. // CLOCK INICIO
  543. SCL = 1;
  544. // DELAY
  545. SCL = 0;
  546. // CLOCK FIM
  547.  
  548. SDA = 1; // BIT 6
  549.  
  550. // CLOCK INICIO
  551. SCL = 1;
  552. // DELAY
  553. SCL = 0;
  554. // CLOCK FIM
  555.  
  556. SDA = 0; // BIT 5
  557.  
  558. // CLOCK INICIO
  559. SCL = 1;
  560. // DELAY
  561. SCL = 0;
  562. // CLOCK FIM
  563.  
  564. SDA = 1; // BIT 4
  565.  
  566. // CLOCK INICIO
  567. SCL = 1;
  568. // DELAY
  569. SCL = 0;
  570. // CLOCK FIM
  571.  
  572. SDA = 0; // BIT 3
  573.  
  574. // CLOCK INICIO
  575. SCL = 1;
  576. // DELAY
  577. SCL = 0;
  578. // CLOCK FIM
  579.  
  580. SDA = 0; // BIT 2
  581.  
  582. // CLOCK INICIO
  583. SCL = 1;
  584. // DELAY
  585. SCL = 0;
  586. // CLOCK FIM
  587.  
  588. SDA = 0; // BIT 1
  589.  
  590. // CLOCK INICIO
  591. SCL = 1;
  592. // DELAY
  593. SCL = 0;
  594. // CLOCK FIM
  595.  
  596. SDA = 0; // BIT DE LEITURA
  597.  
  598. // CLOCK INICIO
  599. SCL = 1;
  600. // DELAY
  601. SCL = 0;
  602. // CLOCK FIM
  603.  
  604. SDA = 0; // PARADA
  605.  
  606. // CLOCK INICIO
  607. SCL = 1;
  608. // DELAY
  609. SCL = 0;
  610. // CLOCK FIM
  611.  
  612.  
  613. }
  614.  
  615. void indica_leitura_segudos() {
  616. // DELAY
  617. SDA = 0; // BIT MSB
  618.  
  619. // CLOCK INICIO
  620. SCL = 1;
  621. // DELAY
  622. SCL = 0;
  623. // CLOCK FIM
  624.  
  625. SDA = 0; // BIT 6
  626.  
  627. // CLOCK INICIO
  628. SCL = 1;
  629. // DELAY
  630. SCL = 0;
  631. // CLOCK FIM
  632.  
  633. SDA = 0; // BIT 5
  634.  
  635. // CLOCK INICIO
  636. SCL = 1;
  637. // DELAY
  638. SCL = 0;
  639. // CLOCK FIM
  640.  
  641. SDA = 0; // BIT 4
  642.  
  643. // CLOCK INICIO
  644. SCL = 1;
  645. // DELAY
  646. SCL = 0;
  647. // CLOCK FIM
  648.  
  649. SDA = 0; // BIT 3
  650.  
  651. // CLOCK INICIO
  652. SCL = 1;
  653. // DELAY
  654. SCL = 0;
  655. // CLOCK FIM
  656.  
  657. SDA = 0; // BIT 2
  658.  
  659. // CLOCK INICIO
  660. SCL = 1;
  661. // DELAY
  662. SCL = 0;
  663. // CLOCK FIM
  664.  
  665. SDA = 0; // BIT 1
  666.  
  667. // CLOCK INICIO
  668. SCL = 1;
  669. // DELAY
  670. SCL = 0;
  671. // CLOCK FIM
  672.  
  673. SDA = 0; // BIT DE LEITURA
  674.  
  675. // CLOCK INICIO
  676. SCL = 1;
  677. // DELAY
  678. SCL = 0;
  679. // CLOCK FIM
  680.  
  681. SDA = 0; // PARADA
  682.  
  683. // CLOCK INICIO
  684. SCL = 1;
  685. // DELAY
  686. SCL = 0;
  687. // CLOCK FIM
  688. }
  689.  
  690. int le_segundos() {
  691. int i;
  692. int segundos = 0;
  693. int aux = 0;
  694.  
  695. indica_leitura_segudos();
  696. for(i = 6; i >= 0; i++) {
  697. if(SDA == 1) {
  698. aux = (int)pow(2, i);
  699. segundos += aux;
  700. }
  701. }
  702. return segundos;
  703. }
  704.  
  705. void indica_leitura_minutos() {
  706. // DELAY
  707. SDA = 1; // BIT MSB
  708.  
  709. // CLOCK INICIO
  710. SCL = 1;
  711. // DELAY
  712. SCL = 0;
  713. // CLOCK FIM
  714.  
  715. SDA = 1; // BIT 6
  716.  
  717. // CLOCK INICIO
  718. SCL = 1;
  719. // DELAY
  720. SCL = 0;
  721. // CLOCK FIM
  722.  
  723. SDA = 1; // BIT 5
  724.  
  725. // CLOCK INICIO
  726. SCL = 1;
  727. // DELAY
  728. SCL = 0;
  729. // CLOCK FIM
  730.  
  731. SDA = 1; // BIT 4
  732.  
  733. // CLOCK INICIO
  734. SCL = 1;
  735. // DELAY
  736. SCL = 0;
  737. // CLOCK FIM
  738.  
  739. SDA = 1; // BIT 3
  740.  
  741. // CLOCK INICIO
  742. SCL = 1;
  743. // DELAY
  744. SCL = 0;
  745. // CLOCK FIM
  746.  
  747. SDA = 1; // BIT 2
  748.  
  749. // CLOCK INICIO
  750. SCL = 1;
  751. // DELAY
  752. SCL = 0;
  753. // CLOCK FIM
  754.  
  755. SDA = 1; // BIT 1
  756.  
  757. // CLOCK INICIO
  758. SCL = 1;
  759. // DELAY
  760. SCL = 0;
  761. // CLOCK FIM
  762.  
  763. SDA = 1; // BIT DE LEITURA
  764.  
  765. // CLOCK INICIO
  766. SCL = 1;
  767. // DELAY
  768. SCL = 0;
  769. // CLOCK FIM
  770.  
  771. SDA = 1; // PARADA
  772.  
  773. // CLOCK INICIO
  774. SCL = 1;
  775. // DELAY
  776. SCL = 0;
  777. // CLOCK FIM
  778.  
  779. }
  780.  
  781. int le_minutos() {
  782. int i;
  783. int minutos = 0;
  784. int aux = 0;
  785.  
  786. indica_leitura_minutos();
  787.  
  788. for(i = 6; i >= 0; i++) {
  789. if(SDA == 1) {
  790. aux = (int)pow(2, i);
  791. minutos += aux;
  792. }
  793. }
  794. return minutos;
  795. }
  796.  
  797. void indica_leitura_horas() {
  798. // DELAY
  799. SDA = 2; // BIT MSB
  800.  
  801. // CLOCK INICIO
  802. SCL = 1;
  803. // DELAY
  804. SCL = 0;
  805. // CLOCK FIM
  806.  
  807. SDA = 2; // BIT 6
  808.  
  809. // CLOCK INICIO
  810. SCL = 1;
  811. // DELAY
  812. SCL = 0;
  813. // CLOCK FIM
  814.  
  815. SDA = 2; // BIT 5
  816.  
  817. // CLOCK INICIO
  818. SCL = 1;
  819. // DELAY
  820. SCL = 0;
  821. // CLOCK FIM
  822.  
  823. SDA = 2; // BIT 4
  824.  
  825. // CLOCK INICIO
  826. SCL = 1;
  827. // DELAY
  828. SCL = 0;
  829. // CLOCK FIM
  830.  
  831. SDA = 2; // BIT 3
  832.  
  833. // CLOCK INICIO
  834. SCL = 1;
  835. // DELAY
  836. SCL = 0;
  837. // CLOCK FIM
  838.  
  839. SDA = 2; // BIT 2
  840.  
  841. // CLOCK INICIO
  842. SCL = 1;
  843. // DELAY
  844. SCL = 0;
  845. // CLOCK FIM
  846.  
  847. SDA = 2; // BIT 1
  848.  
  849. // CLOCK INICIO
  850. SCL = 1;
  851. // DELAY
  852. SCL = 0;
  853. // CLOCK FIM
  854.  
  855. SDA = 2; // BIT DE LEITURA
  856.  
  857. // CLOCK INICIO
  858. SCL = 1;
  859. // DELAY
  860. SCL = 0;
  861. // CLOCK FIM
  862.  
  863. SDA = 2; // PARADA
  864.  
  865. // CLOCK INICIO
  866. SCL = 1;
  867. // DELAY
  868. SCL = 0;
  869. // CLOCK FIM
  870. }
  871.  
  872. int le_horas() {
  873. int i;
  874. int horas = 0;
  875. int aux = 0;
  876.  
  877. indica_leitura_horas();
  878. for(i = 6; i >= 0; i++) {
  879. if(SDA == 1) {
  880. aux = (int)pow(2, i);
  881. horas += aux;
  882. }
  883. }
  884. return horas;
  885. }
  886.  
  887. char le_relogio() {
  888. indica_leitura_segudos();
  889. seg = le_segundos();
  890. indica_leitura_minutos();
  891. min = le_minutos();
  892. indica_leitura_horas();
  893. hr = le_horas();
  894. }
  895.  
  896. char convertNumero(int numero)
  897. {
  898. if(numero == 0)
  899. return '0';
  900. if(numero == 0)
  901. return '1';
  902. if(numero == 0)
  903. return '2';
  904. if(numero == 0)
  905. return '3';
  906. if(numero == 0)
  907. return '4';
  908. if(numero == 0)
  909. return '5';
  910. if(numero == 0)
  911. return '6';
  912. if(numero == 0)
  913. return '7';
  914. if(numero == 0)
  915. return '8';
  916. }
  917.  
  918. /******************************************************************************/
  919. /**************************DEFINIÇÕES DO TECLADO*******************************/
  920. /******************************************************************************/
  921. //método que varre o teclado
  922.  
  923. unsigned char Varre_Teclado(unsigned char flag) {
  924. do {
  925. Linha_1 = 1;
  926. Linha_2 = 0;
  927. Linha_3 = 0;
  928. Linha_4 = 0;
  929.  
  930. if (Coluna_1) {
  931. while (Coluna_1 == 1) {
  932. };
  933. return '1';
  934. }
  935. if (Coluna_2) {
  936. while (Coluna_2 == 1) {
  937. };
  938. return '2';
  939. }
  940. if (Coluna_3) {
  941. while (Coluna_3 == 1) {
  942. };
  943. return '3';
  944. }
  945.  
  946. Linha_1 = 0;
  947. Linha_2 = 1;
  948. Linha_3 = 0;
  949. Linha_4 = 0;
  950. if (Coluna_1) {
  951. while (Coluna_1 == 1) {
  952. };
  953. return '4';
  954. }
  955. if (Coluna_2) {
  956. while (Coluna_2 == 1) {
  957. };
  958. return '5';
  959. }
  960. if (Coluna_3) {
  961. while (Coluna_3 == 1) {
  962. };
  963. return '6';
  964. }
  965. Linha_1 = 0;
  966. Linha_2 = 0;
  967. Linha_3 = 1;
  968. Linha_4 = 0;
  969.  
  970. if (Coluna_1) {
  971. while (Coluna_1 == 1) {
  972. };
  973. return '7';
  974. }
  975. if (Coluna_2) {
  976. while (Coluna_2 == 1) {
  977. };
  978. return '8';
  979. }
  980. if (Coluna_3) {
  981. while (Coluna_3 == 1) {
  982. };
  983. return '9';
  984. }
  985.  
  986. Linha_1 = 0;
  987. Linha_2 = 0;
  988. Linha_3 = 0;
  989. Linha_4 = 1;
  990.  
  991. if (Coluna_1) {
  992. while (Coluna_1 == 1) {
  993. };
  994. return '*';
  995. }
  996. if (Coluna_2) {
  997. while (Coluna_2 == 1) {
  998. };
  999. return '0';
  1000. }
  1001. if (Coluna_3) {
  1002. while (Coluna_3 == 1) {
  1003. };
  1004. return '#';
  1005. }
  1006. } while (flag == 0);
  1007. return -1;
  1008. }
  1009.  
  1010. /******************************************************************************/
  1011. /******************************DEFINIÇÕES DO LCD*******************************/
  1012. /******************************************************************************/
  1013. //Delay
  1014.  
  1015. void Delay_ms(unsigned int time) {
  1016. unsigned int first;
  1017. unsigned int second;
  1018. for (first = 0; first < time; first++) {
  1019. for (second = 0; second < 650; second++) {
  1020. Delay1TCY();
  1021. }
  1022. }
  1023.  
  1024. }
  1025.  
  1026. //Controla comandos do display
  1027.  
  1028. void Lcd4_Cmd(unsigned char value) {
  1029. LCD_RS = 0;
  1030. LCD_DATA = (value & 0xF0);
  1031. LCD_EN = 1;
  1032. Delay_ms(1);
  1033. LCD_EN = 0;
  1034. LCD_DATA = ((value << 4) & 0xF0);
  1035. LCD_EN = 1;
  1036. Delay_ms(1);
  1037. LCD_EN = 0;
  1038. Delay_ms(1);
  1039. }
  1040.  
  1041. //Escreve uma letra no lcd
  1042.  
  1043. void Lcd4_Write(unsigned char value) {
  1044. LCD_RS = 1;
  1045. LCD_DATA = (value & 0xF0);
  1046. LCD_EN = 1;
  1047. Delay_ms(1);
  1048. LCD_EN = 0;
  1049. LCD_DATA = ((value << 4) & 0xF0);
  1050. LCD_EN = 1;
  1051. Delay_ms(1);
  1052. LCD_EN = 0;
  1053. Delay_ms(1);
  1054. }
  1055.  
  1056. //Escreve um texto no lcd
  1057.  
  1058. void Lcd4_Write_Text(unsigned char msg[]) {
  1059. while (*msg) {
  1060. Lcd4_Write(*msg);
  1061. msg++;
  1062. }
  1063. }
  1064.  
  1065. //Escreve um texto no lcd
  1066.  
  1067. void Lcd4_segundaLinha(unsigned char msg[], int flag) {
  1068. int cont = 0;
  1069.  
  1070. while (*msg) {
  1071. if (cont < flag) {
  1072. Lcd4_Write(*msg);
  1073. msg++;
  1074. cont++;
  1075. } else {
  1076. break;
  1077. }
  1078.  
  1079. }
  1080. }
  1081.  
  1082.  
  1083. //Inicia lcd
  1084.  
  1085. void Lcd4_Init(void) {
  1086. TRISD = 0x00;
  1087. TRISCbits.TRISC0 = 0;
  1088. TRISCbits.TRISC1 = 0;
  1089.  
  1090. LCD_RS = 0; //Command Register
  1091. LCD_EN = 0;
  1092.  
  1093. LCD_DATA = 0x30;
  1094.  
  1095. LCD_EN = 1;
  1096. Delay_ms(1);
  1097. LCD_EN = 0;
  1098. Delay_ms(1);
  1099.  
  1100. LCD_EN = 1;
  1101. Delay_ms(1);
  1102. LCD_EN = 0;
  1103. Delay_ms(1);
  1104.  
  1105. LCD_EN = 1;
  1106. Delay_ms(1);
  1107. LCD_EN = 0;
  1108. Delay_ms(1);
  1109.  
  1110. Delay_ms(1);
  1111. LCD_DATA = 0x20; // Four bit mode
  1112. LCD_EN = 1;
  1113. Delay_ms(1);
  1114. LCD_EN = 0;
  1115. Delay_ms(1);
  1116.  
  1117. Lcd4_Cmd(0x28);
  1118. Lcd4_Cmd(0x0C);
  1119. Lcd4_Cmd(0x01);
  1120. Lcd4_Cmd(0x06);
  1121. }
  1122.  
  1123. //Copia para a memória do lcd
  1124.  
  1125. unsigned char * CopyConstToRAM(unsigned char *dest, const rom unsigned char * src) {
  1126. Clear_Destination_Buffer();
  1127. for (; *dest++ = *src++;)
  1128. ;
  1129. return dest;
  1130. }
  1131.  
  1132. //Copia para a memória do lcd
  1133.  
  1134. unsigned char * CopyToRAM(unsigned char *dest, unsigned char * src) {
  1135. Clear_Destination_Buffer();
  1136. for (; *dest++ = *src++;)
  1137. ;
  1138. return dest;
  1139. }
  1140.  
  1141. //Apaga memória do lcd
  1142.  
  1143. void Clear_Destination_Buffer() {
  1144. unsigned char i;
  1145. for (i = 0; i < 16; i++) {
  1146. ram_msg[i] = 0;
  1147. }
  1148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement