Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: newmain.c
- * Author: Hasan
- *
- * Created on April 13, 2018, 9:45 PM
- */
- #pragma config FOSC=HS
- #pragma config FCMEN=ON
- #pragma config WDT=OFF
- #pragma config IESO=ON
- #pragma config XINST=OFF
- #pragma config LVP=OFF
- #pragma config PBADEN = OFF
- #include <xc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "USART_Header_File.h"
- /*
- *
- */
- #define ADDO_PORT PORTBbits.RB0 /* If ADC Data Input is connected to RD5 */
- #define ADDO_SetHigh() LATBbits.LATB0 = 1
- #define ADDO_SetLow() LATBbits.LATB0 = 0
- #define ADDO_GetValue() PORTBbits.RB0
- #define ADDO_TRIS TRISBbits.TRISB0
- #define ADSCK_LAT LATBbits.LATB1 /* If ADC Clock signal is connected to RD0 */
- #define ADSCK_TRIS TRISBbits.TRISB1
- #define ADSCK_SetHigh() LATBbits.LATB1 = 1
- #define ADSCK_SetLow() LATBbits.LATB1 = 0
- #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
- #define RS LATA0 /*PORTD 0 pin is used for Register Select*/
- #define EN LATA1 /*PORTD 1 pin is used for Enable*/
- #define ldata LATD /*PORTB is used for transmitting data to LCD*/
- #define LCD_Port TRISD /*define macros for PORTB Direction Register*/
- #define LCD_Control TRISA /*define macros for PORTD Direction Register*/
- void LCD_Init();
- void LCD_Command(char);
- void LCD_Char(char x);
- void LCD_String(const char *);
- void LCD_String_xy(char, char, const char*);
- /****************************Functions********************************/
- void LCD_Init() {
- __delay_ms(15); /*15ms,16x2 LCD Power on delay*/
- LCD_Port = 0x00; /*Set PORTB as output PORT for LCD data(D0-D7) pins*/
- LCD_Control = 0x00; /*Set PORTD as output PORT LCD Control(RS,EN) Pins*/
- LCD_Command(0x38); /*uses 2 line and initialize 5*7 matrix of LCD*/
- LCD_Command(0x01); /*clear display screen*/
- LCD_Command(0x0c); /*display on cursor off*/
- LCD_Command(0x06); /*increment cursor (shift cursor to right)*/
- }
- void LCD_Clear() {
- LCD_Command(0x01); /*clear display screen*/
- }
- void LCD_Command(char cmd) {
- ldata = cmd; /*Send data to PORT as a command for LCD*/
- RS = 0; /*Command Register is selected*/
- EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
- NOP();
- EN = 0;
- __delay_ms(3);
- }
- void LCD_Char(char dat) {
- ldata = dat; /*Send data to LCD*/
- RS = 1; /*Data Register is selected*/
- EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
- NOP();
- EN = 0;
- __delay_ms(1);
- }
- void LCD_String(const char *msg) {
- while ((*msg) != 0) {
- LCD_Char(*msg);
- msg++;
- }
- }
- void LCD_String_xy(char row, char pos, const char *msg) {
- char location = 0;
- if (row <= 1) {
- location = (0x80) | ((pos) & 0x0f); /*Print message on 1st row and desired location*/
- LCD_Command(location);
- } else {
- location = (0xC0) | ((pos) & 0x0f); /*Print message on 2nd row and desired location*/
- LCD_Command(location);
- }
- LCD_String(msg);
- }
- signed long ReadCount(void) /* Function to read measurement from HX711 */ {
- signed short long Count;
- unsigned char i;
- ADDO_TRIS = 1; /* Data line is Input to PIC */
- ADSCK_TRIS = 0; /* Clock line to Output from PIC */
- ADSCK_LAT = 0; /* Clear Clock line to start conversion */
- Count = 0;
- while (ADDO_GetValue()); /* Wait for measurement conversion. */
- for (i = 0; i < 24; i++) {
- ADSCK_SetHigh(); /* Clock signal High */
- Count = Count << 1; /* Shift value left */
- ADSCK_SetLow(); /* Clock signal Low */
- Count = Count | ADDO_GetValue(); /* Read result bit from HX711 */
- }
- for (i = 0; i < 3; i++) /* Resolution bits to HX711 */ {
- ADSCK_SetHigh();
- NOP();
- ADSCK_SetLow();
- NOP();
- }
- return (signed long) Count;
- }
- void main(void) {
- __delay_ms(1000);
- LCD_Init(); /*Initialize 16x2 LCD*/
- LCD_String_xy(1, 0, "Dig. Weightscale"); /*Display string at location(row,location).
- // * This function passes string to display*/
- LCD_Command(0xc0);
- USART_Init(9600);
- char output[5];
- char data_in;
- while (1) {
- signed long count = ReadCount();
- sprintf(output, "%f", (count / 12500.0)-2
- );
- USART_SendString(output);
- LCD_String_xy(2, 0, " ");
- LCD_String_xy(2, 2, output);
- LCD_Char('K');
- LCD_Char('g');
- __delay_ms(1000);
- }
- while (1) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment