Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #pragmaconfig WDT = OFF
  2. #pragmaconfig OSC = HS
  3. #pragmaconfig FCMEN = OFF
  4. #pragmaconfig XINST = OFF
  5. #include <xc.h>
  6. #include<spi.h>
  7. #include<delays.h>
  8. #include <adc.h>
  9. #include <stdlib.h>
  10. #defineIODIRA_address 0x00
  11. #defineIODIRB_address 0x01
  12. #defineGPIOA_address 0x12
  13. #defineGPIOB_address 0x13
  14. #defineCS PORTAbits.RA2
  15. #define_XTAL_FREQ 4000000
  16. #defineLCD_clear 0b00000001
  17. #defineLCD_cursor_off 0b00001111
  18. #defineLCD_set 0b00111011
  19. #defineLCD_entry 0b00000110
  20. #definecoloana1_inc 128
  21. #definecoloana2_inc 192
  22. intvoltage=0;
  23. intvoltage2=0;
  24. floatvoltage1 =0.0;
  25. intconversie=0;
  26. unsigned charsir1[30],sir2[30];
  27. voidGPIO_set(charaddress,charvalue)
  28. {
  29. CS=0;
  30. WriteSPI1(0x40);
  31. WriteSPI1(address);
  32. WriteSPI1(value);
  33. CS=1;
  34. }
  35. voidIODIR_set(charaddress,chardir)
  36. {
  37. CS=0;
  38. WriteSPI1(0x40);
  39. WriteSPI1(address) ;
  40. WriteSPI1(dir);
  41. CS=1;
  42. }
  43. voidLCD_cmd(charc)
  44. {
  45. GPIO_set(GPIOA_address,0x00);
  46. __delay_ms(50);
  47. GPIO_set(GPIOB_address,c);
  48. __delay_ms(50);
  49. GPIO_set(GPIOA_address,0x40);
  50. __delay_ms(50);
  51. GPIO_set(GPIOA_address,0x00);
  52. __delay_ms(50);
  53. }
  54. voidLCD_init()
  55. {
  56. IODIR_set(IODIRA_address,0x00);
  57. IODIR_set(IODIRB_address,0x00);
  58. LCD_cmd(0b00111111);
  59. LCD_cmd(0b00001111);
  60. LCD_cmd(0b00000001);
  61. LCD_cmd(0b00000110);
  62. }
  63. voidLCD_poz(intcoloana, intlinie)
  64. {
  65. if(coloana==1)
  66. {
  67. LCD_cmd(coloana1_inc+linie);
  68. }
  69. if(coloana==2)
  70. {
  71. LCD_cmd(coloana2_inc+linie);
  72. }
  73. voidLCD_out(unsigned charlitera)
  74. {
  75. GPIO_set(GPIOA_address,0x80);
  76. __delay_ms(50);
  77. GPIO_set(GPIOB_address,litera);
  78. __delay_ms(50);
  79. GPIO_set(GPIOA_address,0xc0);
  80. __delay_ms(50);
  81. GPIO_set(GPIOA_address,0x00);
  82. __delay_ms(50);
  83. }
  84. voidLCD_out_text(unsigned char*s)
  85. {
  86. while(*s)
  87. LCD_out(*s++);
  88. }
  89. voidmain()
  90. {
  91. LATD = 0x00;
  92. TRISD = 0x00;
  93. while(1){
  94. for(inti=0;i<=20;i++)
  95. __delay_ms(20);
  96. TRISAbits.RA2=0;
  97. CS=1;
  98. OpenSPI1(SPI_FOSC_64,MODE_10,SMPEND);
  99. OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 &
  100. ADC_REF_VDD_VSS & ADC_INT_OFF, 1);
  101. ConvertADC();
  102. while(BusyADC());
  103. LATD=ReadADC();
  104. voltage = (ReadADC()*5)/1024;
  105. voltage1 = (ReadADC()*5.0)/1024;
  106. voltage2=voltage1*100-voltage*100;
  107. CloseADC();
  108. itoa(sir1,voltage,10);
  109. itoa(sir2,voltage2,10);
  110. LCD_init();
  111. LCD_poz(2,5);
  112. LCD_out_text(sir1);
  113. LCD_out('.');
  114. LCD_out_text(sir2);
  115. CloseSPI1();}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement