hasanabd

Untitled

Apr 14th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. /*
  2. * File: newmain.c
  3. * Author: Hasan
  4. *
  5. * Created on April 13, 2018, 9:45 PM
  6. */
  7. #pragma config FOSC=HS
  8. #pragma config FCMEN=ON
  9. #pragma config WDT=OFF
  10. #pragma config IESO=ON
  11. #pragma config XINST=OFF
  12. #pragma config LVP=OFF
  13. #pragma config PBADEN = OFF
  14.  
  15. #include <xc.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "USART_Header_File.h"
  19.  
  20. /*
  21. *
  22. */
  23. #define ADDO_PORT PORTBbits.RB0 /* If ADC Data Input is connected to RD5 */
  24. #define ADDO_SetHigh() LATBbits.LATB0 = 1
  25. #define ADDO_SetLow() LATBbits.LATB0 = 0
  26. #define ADDO_GetValue() PORTBbits.RB0
  27. #define ADDO_TRIS TRISBbits.TRISB0
  28. #define ADSCK_LAT LATBbits.LATB1 /* If ADC Clock signal is connected to RD0 */
  29. #define ADSCK_TRIS TRISBbits.TRISB1
  30. #define ADSCK_SetHigh() LATBbits.LATB1 = 1
  31. #define ADSCK_SetLow() LATBbits.LATB1 = 0
  32.  
  33. #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
  34. #define RS LATA0 /*PORTD 0 pin is used for Register Select*/
  35. #define EN LATA1 /*PORTD 1 pin is used for Enable*/
  36. #define ldata LATD /*PORTB is used for transmitting data to LCD*/
  37. #define LCD_Port TRISD /*define macros for PORTB Direction Register*/
  38. #define LCD_Control TRISA /*define macros for PORTD Direction Register*/
  39.  
  40.  
  41. void LCD_Init();
  42. void LCD_Command(char);
  43. void LCD_Char(char x);
  44. void LCD_String(const char *);
  45. void LCD_String_xy(char, char, const char*);
  46.  
  47. /****************************Functions********************************/
  48.  
  49. void LCD_Init() {
  50. __delay_ms(15); /*15ms,16x2 LCD Power on delay*/
  51. LCD_Port = 0x00; /*Set PORTB as output PORT for LCD data(D0-D7) pins*/
  52. LCD_Control = 0x00; /*Set PORTD as output PORT LCD Control(RS,EN) Pins*/
  53. LCD_Command(0x38); /*uses 2 line and initialize 5*7 matrix of LCD*/
  54. LCD_Command(0x01); /*clear display screen*/
  55. LCD_Command(0x0c); /*display on cursor off*/
  56. LCD_Command(0x06); /*increment cursor (shift cursor to right)*/
  57. }
  58.  
  59. void LCD_Clear() {
  60. LCD_Command(0x01); /*clear display screen*/
  61. }
  62.  
  63. void LCD_Command(char cmd) {
  64. ldata = cmd; /*Send data to PORT as a command for LCD*/
  65. RS = 0; /*Command Register is selected*/
  66. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  67. NOP();
  68. EN = 0;
  69. __delay_ms(3);
  70. }
  71.  
  72. void LCD_Char(char dat) {
  73. ldata = dat; /*Send data to LCD*/
  74. RS = 1; /*Data Register is selected*/
  75. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  76. NOP();
  77. EN = 0;
  78. __delay_ms(1);
  79. }
  80.  
  81. void LCD_String(const char *msg) {
  82. while ((*msg) != 0) {
  83. LCD_Char(*msg);
  84. msg++;
  85. }
  86. }
  87.  
  88. void LCD_String_xy(char row, char pos, const char *msg) {
  89. char location = 0;
  90. if (row <= 1) {
  91. location = (0x80) | ((pos) & 0x0f); /*Print message on 1st row and desired location*/
  92. LCD_Command(location);
  93. } else {
  94. location = (0xC0) | ((pos) & 0x0f); /*Print message on 2nd row and desired location*/
  95. LCD_Command(location);
  96. }
  97. LCD_String(msg);
  98. }
  99.  
  100. signed long ReadCount(void) /* Function to read measurement from HX711 */ {
  101. signed short long Count;
  102. unsigned char i;
  103. ADDO_TRIS = 1; /* Data line is Input to PIC */
  104. ADSCK_TRIS = 0; /* Clock line to Output from PIC */
  105. ADSCK_LAT = 0; /* Clear Clock line to start conversion */
  106. Count = 0;
  107. while (ADDO_GetValue()); /* Wait for measurement conversion. */
  108. for (i = 0; i < 24; i++) {
  109. ADSCK_SetHigh(); /* Clock signal High */
  110. Count = Count << 1; /* Shift value left */
  111. ADSCK_SetLow(); /* Clock signal Low */
  112. Count = Count | ADDO_GetValue(); /* Read result bit from HX711 */
  113. }
  114. for (i = 0; i < 3; i++) /* Resolution bits to HX711 */ {
  115. ADSCK_SetHigh();
  116. NOP();
  117. ADSCK_SetLow();
  118. NOP();
  119. }
  120. return (signed long) Count;
  121. }
  122.  
  123. void main(void) {
  124. __delay_ms(1000);
  125. LCD_Init(); /*Initialize 16x2 LCD*/
  126. LCD_String_xy(1, 0, "Dig. Weightscale"); /*Display string at location(row,location).
  127. // * This function passes string to display*/
  128. LCD_Command(0xc0);
  129. USART_Init(9600);
  130. char output[5];
  131. char data_in;
  132.  
  133. while (1) {
  134. signed long count = ReadCount();
  135. sprintf(output, "%f", (count / 12500.0)-2
  136. );
  137. USART_SendString(output);
  138. LCD_String_xy(2, 0, " ");
  139. LCD_String_xy(2, 2, output);
  140. LCD_Char('K');
  141. LCD_Char('g');
  142. __delay_ms(1000);
  143. }
  144. while (1) {
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment