I am working on making a digital thermometer using MPLAB and XC8 compiler and PI
ID: 2082474 • Letter: I
Question
I am working on making a digital thermometer using MPLAB and XC8 compiler and PIC16f1829 microprocessor
and LM35 temp sensor. So far I have the developed the code below. I need help with the rest of the code for the ADC
anlogue to digital converter and display the result on a 16x2 LCD display thanks.
/*
* File:
* Author:
* Comments:
* Revision history:
*/
// This is a guard condition so that contents of this file are not included
// more than once.
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
// include processor files - each processor file is guarded.
/*
* File:
* Author:
* Comments:
* Revision history:
*/
// This is a guard condition so that contents of this file are not included
// more than once.
// include processor files - each processor file is guarded. * Author: MUHIA
// * Created on March 26, 2017, 2:51 AM
//* This is a header file to support parallel 2x16 LCD display
//* November 1, 2016
//* Port B for Data lines 4 bit mode or port C data lines for 8 bit
//* RA5 for E, RA4 for RS RA2 or R/W if used
// This is a guard condition so that contents of this file are not included
// more than once
#include <xc.h>
#include<pic16f1829.h>
//include processor files - each processor file is guarded.
// TODO Insert appropriate #include < >
#define _XTAL_FREQ 4000000
#define RS RC0
#define RW RC6
#define E RC3
#define LCD_Data PORTB//B4, 5, 6, 7 R, i. //be sure and set TRIS reg correctly //LCD Function Prototypes
#define LCD_COMMAND PORTC
void Init_LCD (void);
void PutCMD_LCD (char);
void PutDATA_LCD (char) ;
void Busy_LCD (void);
void Delay_S (int);
void Init_LCD (void) {
int Dum;
RS=0;
RW= 0 ;
E = 0;
__delay_us (10); //First send 3 8 bit bytes of OX3X to ensure proper power up
PORTB = (0x30); // One. . . . .
__delay_us (10);
E = 1;
__delay_us (10);
E = 0;
__delay_us (10);
PORTB = (0x30) ; //2 . . . . .
__delay_us (10);
E = 1;
__delay_us (10);
E = 0;
__delay_us (10);
PORTB = (0x30) ; //3 . . . . .
__delay_us (10);
E = 1;
__delay_us (10);
E = 0;
__delay_us (10);
PORTB = (0x20); // Put in 4 bit mode
__delay_us (10);
E = 1;
__delay_us (10);
E = 0;
//Then turn on display with blinking Cursor
PutCMD_LCD (0x0F);
// Put in 2 line mode 5x7 chars
PutCMD_LCD (0x28);
// Clear display
PutCMD_LCD (0x01);
__delay_ms (10);
//clear display
PutCMD_LCD (0x02);
__delay_ms(10);
return;
}
//
void PutCMD_LCD(char Command) {
RS= 0;
RW= 0;
E = 1;
__delay_us (10); //short delay Dummy not used otherwise
PORTB =(Command); //send the high 4 bits first to RB4, 5, 6, 7
__delay_us (10);
E = 0;
__delay_us (10); //short delay Dummy not used otherwise
E = 1;
__delay_us (10); //short delay Dummy not used otherwise
PORTB =(Command << 4) ; //Send the low nibble to RB 4, 5, 6, 7
__delay_us (10); //short delay Dummy not used otherwise
E = 0;
__delay_ms (10); // May need a long delay for clear Command
return;
}
//
void PutDATA_LCD (char Data) {
__delay_us (10);
RS = 1;
RW= 0 ;
E = 1;
__delay_us (10);
PORTB = (Data); //send the high 4 bits first to RB4, 5, 6, 7
__delay_us (10);
E = 0;
__delay_us (10);
E = 1;
__delay_us (10);
PORTB = (Data << 4) ; //Send the low nibble to RB 4, 5, 6, 7
__delay_us (10);
E = 0;
__delay_us (10);
return;
}
void Busy_LCD (void) {
return;
}
void Delay_S (int q) {
int p;
for (p=0;
p>q;
p++) continue;
return; }
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
// TODO If C++ is being used, regular C code needs function names to have C
// linkage so the functions can be used by the c code.
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* XC_HEADER_TEMPLATE_H */
// TODO Insert appropriate #include <>
// TODO Insert C++ class definitions if appropriate
// TODO Insert declarations
// Comment a function and leverage automatic documentation with slash star star
/**
<p><b>Function prototype:</b></p>
<p
// TODO Insert declarations or function prototypes (right here) to leverage
// live documentation
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
// TODO If C++ is being used, regular C code needs function names to have C
// linkage so the functions can be used by the c code.
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* XC_HEADER_TEMPLATE_H */
and also
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC =INTOSC // Oscillator Selection (EXTRC oscillator: External RC circuit connected to CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is enabled on the CLKOUT pin)
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF // Low-Voltage Programming Enable (Low-voltage programming enabled)
#define _XTAL_FREQ 4000000
#include <string.h>
#include <stdio.h>
#include <pic16f1829.h>
#include<pic.h>
#include <stdint.h>
#include <xc.h>
# include "kichwa.h"
//be sure and set TRIS reg correctly
//LCD Function Prototypes
void Init_LCD (void);
void PutCMD_LCD (char) ;
void PutDATA_LCD (char);
void Busy_LCD (void);
void Delay_S (int);
int main (int argc, char** argv) {
char Letter;
__delay_ms (30); // Set up for Basic Clock
ANSELA = 0;
TRISA = 0 ;
ANSELB = 0;
TRISB = 0 ;
ANSELC = 0 ;
TRISC = 0;
OSCCON = 0X6A;
RA5 ^=1;
Init_LCD () ;
while (1) {
__delay_ms (400);
PutCMD_LCD (0xC0); //set to lower line on LCD display
Delay_S (20000);
Letter = 'M' ;
PutDATA_LCD (Letter);
Letter = 'U' ;
PutDATA_LCD (Letter) ;
Letter = 'H' ;
PutDATA_LCD (Letter);
Letter = 'I';
PutDATA_LCD (Letter);
Letter = 'A';
PutDATA_LCD (Letter);
Delay_S ( 60000);
}
return ;
}
Explanation / Answer
#include #include #pragma config OSC = HS #pragma config WDT = OFF #pragma config LVP = OFF void SendLCD(unsigned char Byte, unsigned char type); //LCD display function #define LCD LATD //LCD Latch PORT #define LCD_RS LATEbits.LATE0 //Register Select (0: Instruction/ 1: Data) #define LCD_E LATEbits.LATE2 //Enable (Starts data read/write) #define LED LATCbits.LATC3 //power up LED unsigned char minus = 0, //-ve flag...for -ve sign activation bin = 0, //temp buffer variable for ADC value for BCD conversion process //BCD coversion variables d0 = 0, d1 = 0, d2 = 0; void main(void) { //Configure ADC ADCON1 = 0b00001110 ; //VREF- = VSS, //VREF+ = VDD, //Enable AN0 only...rest all Digital ADCON0 = 0b00000000; //Select channel 0 (AN0), //ADC Idle, //ADC Dissabled ADCON2 = 0b00001100; //Left Justified for 8bit ADC scaling //ADC Acquisition Time 2TAD, //ADC Conversion Clock FOSC/4 PORTA = 0; //initiallize PORTA TRISAbits.TRISA0 = 1; // Analog Channel 0 (AN0 input) PORTB = 0; //intialize PORTB TRISBbits.TRISB3 = 1; //swtich 1 input TRISBbits.TRISB4 = 1; //switch 2 input TRISBbits.TRISB5 = 1; //switch 3 input PORTC = 0; //intialize PORTC TRISCbits.TRISC3 = 0; //LED PORTD = 0; //initiallize PORTD TRISD = 0x00; //LCD output PORTE = 0; //initiallize PORTE TRISEbits.TRISE0 = 0; //LCD Register Select (to LCD Pin 4) TRISEbits.TRISE2 = 0; //LCD Enable (to LCD Pin 6) INTCON = 0; //dissable all interrupts Delay10KTCYx(10); //20ms wait for LCD power up //Initialize the LCD LCD = 0x00; //clear LCD PORTD pins Delay1KTCYx(25); //delay 5mS SendLCD(0x03,0); //Initialization command Delay1KTCYx(25); //delay 5mS SendLCD(0x03,0); //Initialization command Delay100TCYx(8); //delay 160uS SendLCD(0x03,0); //Initialization command Delay100TCYx(8); //delay 160uS SendLCD(0x3C,0); //Interface lenght is 8 bits long, 2-lines, 5x10 dots Delay100TCYx(8); //delay 160uS SendLCD(0x10,0); //Turn off LCD Delay100TCYx(8); //delay 160uS SendLCD(0x01,0); //Clear LCD Delay1KTCYx(25); //delay 5mS SendLCD(0x06,0); //Increment the cursor after each byte written Delay100TCYx(8); //delay 160uS SendLCD(0x0C,0); //Turn on LCD, cursor off, cursor blinking off Delay100TCYx(8); //delay 160uS //Power up flashing LED LED = 1; Delay10KTCYx(125); //250ms delay LED = 0; Delay10KTCYx(125); //250ms delay LED = 1; Delay10KTCYx(125); //250ms delay LED = 0; ADCON0bits.ADON = 1; //Enable ADC while(1) //loop forever { ADCON0bits.GO_DONE = 1; //Start ADC Conversion while (ADCON0bits.GO_DONE == 1); //Wait until Conversion finishes bin = ADRESH; // reading ADC High byte only for 8bit ADC scale if(bin < 20) //below 0 deg C ... (0 deg C @ 400mV) minus = 1; //activate -ve sign bin = bin - 20; //offset adjustment ... (0 deg C @ 400mV) //convert ADC value to BCD d1 = bin % 10; d0 = (bin / 10) % 10; //convert BCD digit numbers to ASCII text characters for LCD d0 += '0'; d1 += '0'; SendLCD(0x80,0); //activate LCD line 1 //if temperature is in -ve ... then print -ve sign on LCD if(minus == 1) { SendLCD('-',1); minus = 0; //reset minus sign flag } else //temperature is in +ve so erase minus sign (blank space print) SendLCD(' ',1); //print the temperature on LCD SendLCD(d0,1); SendLCD(d1,1); SendLCD('C',1); Delay10KTCYx(150); //300ms delay }// end of main while loop } //end of main() void SendLCD(unsigned char Byte, unsigned char type) { LCD_RS = type; //Set whether it is a Command (0) or Data/Char (1) Delay100TCYx(8); //delay 160uS LCD = Byte; //assign the new data to the LCD PORTD Delay100TCYx(8); //delay 160uS LCD_E = 1; //Set Enable High Delay100TCYx(8); //delay 160uS LCD_E = 0; //Set Enable Low }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.