Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: Hasan
- *
- * Created on March 30, 2018, 1:05 PM
- */
- #include <pic18f4550.h>
- #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 MSdelay(unsigned int);
- void i2c_Init(void);
- void i2c_wait(unsigned int cnt);
- unsigned int IR_temp_read(unsigned char command);
- //extern unsigned int IR_temp_read(unsigned 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);
- }
- void i2c_Init(void) {
- TRISBbits.TRISB1 = 1;
- TRISBbits.TRISB0 = 1;
- PIE1bits.SSPIE = 0;
- SSPSTAT = 0xC0;
- SSPCON1 = 0x08; //Master Mode
- SSPCON2 = 0x00;
- SSPADD = 24;
- //SSPCON1bits.SSPEN = 1;
- SSPCON1 |= 0x20;
- i2c_wait(20);
- }
- void i2c_wait(unsigned int cnt) {
- while (--cnt) {
- NOP();
- NOP();
- }
- }
- unsigned int IR_temp_read(unsigned char command) {
- //b'0001 aaaa: Read of EEPROM
- //b'0010 aaaa: Read of RAM
- //aaaa is the memory address to be read
- //EEPROM Addresses
- //b'0000: Slave Address
- //b'0010: Config
- //b'0011: Emissivity
- //RAM Addresses
- //b'0101: Raw IR data
- //b'0110: T ambient
- //b'0111: T object
- unsigned char SlaveAddress = 0x5A;
- //unsigned char command;
- unsigned char i2cData[3];
- int i2cRX_Data[3];
- int DataSz;
- int Index = 0;
- unsigned int return_data;
- i2cRX_Data[0] = 0;
- i2cRX_Data[1] = 0;
- i2cRX_Data[2] = 0;
- PIR1bits.SSPIF = 0;
- i2cData[0] = ((SlaveAddress << 1) & 0xFE);
- i2cData[1] = command;
- //i2cData[2] = ((SlaveAddress << 1) | 0x01);
- DataSz = 2;
- //START
- SSPCON2bits.SEN = 1; /* Send start pulse */
- while (SSPCON2bits.SEN); /* Wait for completion of start pulse */
- SSPIF = 0;
- if (!SSPSTATbits.S){ /* Check whether START detected last */
- LCD_Char('0'); /* Return 0 to indicate start failed */
- }else{
- LCD_Char('1');
- }
- //
- Index = 0;
- i2c_wait(15);
- while (DataSz)
- {
- //Write
- SSPBUF = i2cData[Index++];
- while (PIR1bits.SSPIF == 0);
- i2c_wait(10);
- while (SSPCON2bits.ACKSTAT == 1);
- PIR1bits.SSPIF = 0;
- DataSz--;
- }
- //Start read from IR temp sensor
- //T0CONbits.TMR0ON = 1; //Turn on the timer
- //INTCONbits.GIE = 0;
- TMR0 = 0;
- INTCONbits.TMR0IF = 0;
- SSPCON2bits.RSEN = 1; //Repeated start condition
- while (SSPCON2bits.RSEN == 1);
- while (PIR1bits.SSPIF == 0);
- PIR1bits.SSPIF = 0;
- i2c_wait(15); //JM was 50
- SSPBUF = ((SlaveAddress << 1) | 1);
- while (SSPCON2bits.ACKSTAT == 1);
- while (PIR1bits.SSPIF == 0);
- PIR1bits.SSPIF = 0;
- i2c_wait(30);
- //Receive First Byte
- SSPCON2bits.RCEN = 1;
- while (PIR1bits.SSPIF == 0);
- PIR1bits.SSPIF = 0;
- SSPCON2bits.ACKDT = 0;
- SSPCON2bits.ACKEN = 1;
- while ((PIR1bits.SSPIF == 0) || (INTCONbits.TMR0IF == 1));
- if (INTCONbits.TMR0IF == 0) {
- TMR0 = 0;
- INTCONbits.TMR0IF = 0;
- }
- PIR1bits.SSPIF = 0;
- i2cRX_Data[0] = SSPBUF;
- i2c_wait(30);
- //Receive second byte
- SSPCON2bits.RCEN = 1;
- while (PIR1bits.SSPIF == 0);
- PIR1bits.SSPIF = 0;
- SSPCON2bits.ACKDT = 0;
- SSPCON2bits.ACKEN = 1;
- while ((PIR1bits.SSPIF == 0) || (INTCONbits.TMR0IF == 1));
- if (INTCONbits.TMR0IF == 0) {
- TMR0 = 0;
- INTCONbits.TMR0IF = 0;
- }
- PIR1bits.SSPIF = 0;
- i2cRX_Data[1] = SSPBUF;
- i2c_wait(30);
- //Receive third byte
- SSPCON2bits.RCEN = 1;
- while (PIR1bits.SSPIF == 0);
- PIR1bits.SSPIF = 0;
- SSPCON2bits.ACKDT = 0;
- SSPCON2bits.ACKEN = 1;
- while ((PIR1bits.SSPIF == 0) || (INTCONbits.TMR0IF == 1));
- if (INTCONbits.TMR0IF == 0) {
- TMR0 = 0;
- INTCONbits.TMR0IF = 0;
- }
- PIR1bits.SSPIF = 0;
- i2cRX_Data[2] = SSPBUF;
- NOP();
- NOP();
- NOP();
- SSPCON2bits.PEN = 1;
- while (SSPCON2bits.PEN)
- while ((SSPCON2 & 0x1F) | (SSPSTATbits.R_nW))
- i2c_wait(100);
- return_data = 0x0000;
- if (INTCONbits.TMR0IF == 0)//timer didn't overflow so info received from temp sensor
- {
- return_data = (i2cRX_Data[1] << 8) | i2cRX_Data[0];
- }
- return (return_data);
- } //end unsigned int IR_temp_read(unsigned char command)
- /*****************************Main Program*******************************/
- void main(void) {
- int temp1 = 0;
- float Temperature = 42;
- // USART_Init(9600);
- i2c_Init();
- char data_in;
- TRISEbits.TRISE0 = 0;
- IR_TEMP_SENSOR = 0;
- __delay_ms(2000);
- IR_TEMP_SENSOR = 1;
- //__delay_ms(10000);
- int temps;
- float Temperature;
- 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);
- __delay_ms(1000);
- while (1) { // continually read temp from RAM
- char output[10];
- temp1 = IR_temp_read(0x07);
- LCD_Char('1');
- Temperature = (((float) temp1 * 0.02) - 273.15);
- sprintf(output, 10, "%f", Temperature);
- LCD_String(output);
- // USART_SendString(output);
- __delay_ms(5000);
- }
- //char flag = I2C_Start(0x5A);
- //LCD_Char(flag);
- }
Advertisement
Add Comment
Please, Sign In to add comment