Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PIC18F4550 Configuration Bit Settings*/
- /* 'C' source line config statements*/
- #include <xc.h>
- // CONFIG1L
- #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
- ////////////////////////////////////////////////////////////////////////////////////////
- //USART_header_file
- // This is a guard condition so that contents of this file are not included
- // more than once.
- #ifndef USART_HEADER_FILE_H
- #define USART_HEADER_FILE_H
- #include <xc.h>
- #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
- #include <pic18f4550.h>
- void USART_Init(long);
- void USART_TransmitChar(char);
- void USART_SendString(const char *);
- void MSdelay(unsigned int val);
- char USART_ReceiveChar();
- #define F_CPU 10000000/64
- //#define Baud_value(baud_rate) (((float)(F_CPU)/(float)baud_rate)-1)
- #define Baud_value (((float)(F_CPU)/(float)baud_rate)-1)
- #endif /* USART_HEADER_FILE_H */
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- * File: main.c
- * Author: Hasan
- *
- * Created on March 30, 2018, 1:05 PM
- */
- #include "Configuration_Header_File.h"
- #include <xc.h>
- #include "USART_Header_File.h"
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #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*/
- #define IR_TEMP_SENSOR LATEbits.LATE0 // Name the RA0 bit to "LED"
- 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*);
- void I2C_Init(void);
- char I2C_Start(char slave_write_address);
- void I2C_Ready();
- char I2C_Write(unsigned char data);
- char I2C_Stop();
- char I2C_Read(char flag);
- void I2C_Ack();
- void I2C_Nack();
- /****************************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);
- }
- void I2C_Init(void) {
- TRISBbits.TRISB1 = 1;
- TRISBbits.TRISB0 = 1;
- PIE1bits.SSPIE = 0;
- SSPSTAT = 0xC0; /* Slew rate disabled, other bits are cleared */
- SSPCON1 = 0x28; /* Enable SSP port for I2C Master mode,
- clock = FOSC / (4 * (SSPADD+1))*/
- SSPCON2 = 0;
- SSPADD = 0x18; /* Clock 100 kHz */
- SSPIE = 1; /* Enable SSPIF interrupt */
- SSPIF = 0;
- }
- char I2C_Start(char slave_write_address) {
- SSPCON2bits.SEN = 1; /* Send start pulse */
- /* Wait for completion of start pulse */
- // if (!SSPSTATbits.S) { /* Check whether START detected last */
- // return 0; /* Return 0 to indicate start failed */
- // }
- while (SSPCON2bits.SEN == 1);
- SSPIF = 0;
- if (!SSPSTATbits.S) { /* Check whether START detected last */
- //LCD_Char('0'); /* Return 0 to indicate start failed */
- } else {
- // LCD_Char('1');
- }
- return (I2C_Write(slave_write_address)); /* Write slave device address
- with write to communicate */
- }
- void I2C_Ready() {
- while (BCLIF); /* Wait if bit collision interrupt flag is set*/
- /* Wait for Buffer full and read write flag*/
- while (SSPSTATbits.BF || (SSPSTATbits.R_nW));
- SSPIF = 0; /* Clear SSPIF interrupt flag*/
- }
- char I2C_Write(unsigned char data) {
- SSPBUF = data; /* Write data to SSPBUF*/
- I2C_Ready();
- if (ACKSTAT) {/* Check for acknowledge bit*/
- return 1;
- } else {
- return 2;
- }
- }
- char I2C_Stop() {
- I2C_Ready();
- PEN = 1; /* Stop communication*/
- while (PEN); /* Wait for end of stop pulse*/
- SSPIF = 0;
- if (!SSPSTATbits.P); /* Check whether STOP is detected last */
- return 0; /* If not return 0 to indicate start failed*/
- }
- /* Read data from location and send ack or nack depending upon parameter*/
- char I2C_Read(char flag) {
- int buffer = 0;
- RCEN = 1; /* Enable receive */
- /* Wait for buffer full flag which when complete byte received */
- while (!SSPSTATbits.BF);
- buffer = SSPBUF; /* Copy SSPBUF to buffer */
- /* Send acknowledgment or negative acknowledgment after read to
- continue or stop reading */
- if (flag == 0)
- I2C_Ack();
- else
- I2C_Nack();
- I2C_Ready();
- return (buffer);
- }
- void I2C_Ack() {
- ACKDT = 0; /* Acknowledge data 1:NACK,0:ACK */
- ACKEN = 1; /* Enable ACK to send */
- while (ACKEN);
- }
- void I2C_Nack() {
- ACKDT = 1; /* Acknowledge data 1:NACK,0:ACK */
- ACKEN = 1; /* Enable ACK to send */
- while (ACKEN);
- }
- /*****************************Main Program*******************************/
- void main(void) {
- USART_Init(9600);
- char data_in;
- I2C_Init();
- TRISEbits.TRISE0 = 0;
- IR_TEMP_SENSOR = 0;
- __delay_ms(2000);
- IR_TEMP_SENSOR = 1;
- LCD_Init(); /*Initialize 16x2 LCD*/
- LCD_String_xy(1, 0, "Dig. Thermometer"); /*Display string at location(row,location).
- * This function passes string to display*/
- LCD_Command(0xc0);
- int Tadata[3] = {0, 0, 0};
- unsigned int TaValue = 0;
- __delay_ms(1000);
- while (1) { // continually read temp from RAM
- data_in = USART_ReceiveChar();
- char output[6];
- float values[5];
- float total= 0 ;
- if (data_in == '1') {
- float Ta = 0;
- int i;
- for (i = 0; i < 5; i++) {
- char ack = I2C_Start(0xB4); // send START condition
- char ack2 = I2C_Write(0x07);
- SSPCON2bits.RSEN = 1; //Repeated start condition
- while (SSPCON2bits.RSEN == 1);
- while (PIR1bits.SSPIF == 0);
- PIR1bits.SSPIF = 0;
- char ack3 = I2C_Write(0xB5);
- Tadata[0] = I2C_Read(0); // Read 1st byte of data (low byte))
- Tadata[1] = I2C_Read(0); // Read 2nd byte (high byte))
- Tadata[2] = I2C_Read(1); // Read last byte (PEC)
- char ack4 = I2C_Stop(); // Hang up, send STOP condition
- TaValue = (Tadata[1] << 8) | Tadata[0];
- Ta = (((float) TaValue * 0.02) - 273.15); // Compute actual ambient temp
- values[i] = Ta ;
- __delay_ms(1000);
- }
- int j;
- for (j = 0; j < 5; j++) {
- total = total + values[j];
- }
- sprintf(output, "%f", total/5);
- LCD_String_xy(2, 2, output);
- LCD_Char(0xDF);
- LCD_Char('C');
- USART_SendString(output);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////
- //USART FILE
- #include "USART_Header_File.h"
- #include <xc.h>
- #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
- /*****************************USART Initialization*******************************/
- void USART_Init(long baud_rate)
- {
- float temp;
- TRISC6=0; /*Make Tx pin as output*/
- TRISC7=1; /*Make Rx pin as input*/
- temp=Baud_value;
- SPBRG= 15; /*baud rate=9600, SPBRG = (F_CPU /(64*9600))-1*/
- TXSTA=0x20; /*Transmit Enable(TX) enable*/
- RCSTA=0x90; /*Receive Enable(RX) enable and serial port enable */
- }
- /******************TRANSMIT FUNCTION*****************************************/
- void USART_TransmitChar(char out)
- {
- while(TXIF==0); /*wait for transmit interrupt flag*/
- TXREG=out; /*wait for transmit interrupt flag to set which indicates TXREG is ready
- for another transmission*/
- }
- /*******************RECEIVE FUNCTION*****************************************/
- char USART_ReceiveChar()
- {
- while(RCIF==0); /*wait for receive interrupt flag*/
- return(RCREG); /*receive data is stored in RCREG register and return to main program */
- }
- void USART_SendString(const char *out)
- {
- while(*out!='\0')
- {
- USART_TransmitChar(*out);
- out++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment