Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. sbit SoftSpi_CLK at RA0_bit;
  2. sbit SoftSpi_SDI at RA1_bit;
  3. sbit SoftSpi_SDO at RA2_bit;
  4. sbit SoftSpi_CLK_Direction at TRISA0_bit;
  5. sbit SoftSpi_SDI_Direction at TRISA1_bit;
  6. sbit SoftSpi_SDO_Direction at TRISA2_bit;
  7.  
  8. sbit LCD_RS at RB4_bit;
  9. sbit LCD_EN at RB5_bit;
  10. sbit LCD_D4 at RB0_bit;
  11. sbit LCD_D5 at RB1_bit;
  12. sbit LCD_D6 at RB2_bit;
  13. sbit LCD_D7 at RB3_bit;
  14. sbit LCD_RS_Direction at TRISB4_bit;
  15. sbit LCD_EN_Direction at TRISB5_bit;
  16. sbit LCD_D4_Direction at TRISB0_bit;
  17. sbit LCD_D5_Direction at TRISB1_bit;
  18. sbit LCD_D6_Direction at TRISB2_bit;
  19. sbit LCD_D7_Direction at TRISB3_bit;
  20.  
  21. unsigned short value;
  22. unsigned short read;
  23. char uart_rd;
  24. char broj[10];
  25. char niza[16];
  26. int counter;
  27. int i;
  28. short shouldBreak = 0;
  29. short oneDollar = 0;
  30. int temp;
  31. int zbir;
  32.  
  33. void printOnScreen(){
  34. Lcd_Cmd(_LCD_CLEAR);
  35. temp = 16 - counter;
  36. for (i = 1; i <= temp;i++){
  37. Lcd_Chr(1,i,' ');
  38. }
  39. for(i = temp + 1; i <= 16; i++){
  40. Lcd_Chr(i,i,niza[i - temp - 1]);
  41. }
  42.  
  43. Delay_ms(1);
  44. }
  45. void sendBySPI(){
  46. char* zbirot;
  47.  
  48. IntToStr(zbir,broj);
  49.  
  50.  
  51.  
  52. zbirot = Ltrim(broj);
  53. Lcd_Cmd(_LCD_CLEAR);
  54. Lcd_Out(1,1,"Prakjam zbir");
  55. Lcd_Out(2,1,zbirot);
  56. Delay_ms(5);
  57.  
  58. if (zbir > 10){
  59. if (zbir > 99){
  60. Soft_SPI_Write(broj[7]);
  61. Soft_SPI_Write(broj[8]);
  62. Soft_SPI_Write(broj[9]);
  63. }
  64. else{
  65. Soft_SPI_Write(broj[8]);
  66. Soft_SPI_Write(broj[9]);
  67. }
  68. }else{
  69. Soft_SPI_Write(broj[9]);
  70. }
  71.  
  72.  
  73.  
  74. }
  75.  
  76. void printError(){
  77. Lcd_Cmd(_LCD_CLEAR);
  78. Lcd_Out(1,1,Ltrim("Error!"));
  79. Delay_ms(1);
  80. }
  81.  
  82. void printEnd(){
  83. Lcd_Cmd(_LCD_CLEAR);
  84. Lcd_Out(1,1,Ltrim("End"));
  85. }
  86.  
  87. void main() {
  88.  
  89. ANSEL = 0;
  90. ANSELH = 0;
  91.  
  92. C1ON_bit = 0;
  93. C2ON_bit = 0;
  94.  
  95. Soft_SPI_Init();
  96. UART1_Init(9600);
  97. Lcd_Init();
  98. Lcd_Cmd(_LCD_CURSOR_OFF);
  99. Lcd_Cmd(_LCD_CLEAR);
  100. Delay_ms(5);
  101.  
  102. counter = 0;
  103. zbir = 0;
  104. while(1){
  105.  
  106. if (UART1_Data_Ready()) {
  107. uart_rd = UART1_Read();
  108. Lcd_Cmd(_LCD_CLEAR);
  109. Lcd_Chr(1,1,uart_rd);
  110. Delay_ms(1);
  111.  
  112. if (uart_rd >= '0' && uart_rd <= '9'){
  113. if (counter == 16){
  114. printError();
  115. oneDollar = 0;
  116. counter = 0;
  117. for (i = 0; i < 16; i++){
  118. niza[i] = ' ';
  119. }
  120. }else{
  121. niza[counter] = uart_rd;
  122. zbir = zbir + (uart_rd - '0');
  123.  
  124. counter = counter + 1;
  125. intToStr(counter,broj);
  126. Lcd_Out(2,1,"C: ");
  127. Lcd_Out(2,4,broj);
  128. delay_ms(1);
  129. oneDollar = 0;
  130. }
  131. }else if (uart_rd == '$'){
  132. if (oneDollar){
  133. printEnd();
  134. shouldBreak = 1;
  135. }else{
  136. oneDollar = 1;
  137. printOnScreen();
  138. sendBySPI();
  139. counter = 0;
  140. zbir = 0;
  141.  
  142. for (i = 0; i < 16; i++){
  143. niza[i] = ' ';
  144. }
  145. }
  146. }else{
  147. printError();
  148. oneDollar = 0;
  149. counter = 0;
  150. zbir = 0;
  151. for (i = 0; i < 16; i++){
  152. niza[i] = ' ';
  153. }
  154. }
  155. }
  156.  
  157. if (shouldBreak){
  158. break;
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement