Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: Hasan
- *
- *
- */
- #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();
- 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; /* Slew rate disabled, other bits are cleared */
- SSPCON1 = 0x28; /* Enable SSP port for I2C Master mode,
- clock = FOSC / (4 * (SSPADD+1))*/
- SSPCON2 = 0;
- SSPADD = 24; /* Clock 100 kHz */
- SSPIE = 1; /* Enable SSPIF interrupt */
- SSPIF = 0;
- }
- char I2C_Start(char slave_write_address) {
- 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 */
- return 0; /* Return 0 to indicate start failed */
- }
- 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) {
- int temp1 = 0;
- float Temperature = 42;
- USART_Init(9600);
- char data_in;
- I2C_Init();
- TRISEbits.TRISE0 = 0;
- IR_TEMP_SENSOR = 0;
- __delay_ms(2000);
- IR_TEMP_SENSOR = 1;
- 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);
- int Tadata[3] = {0, 0, 0};
- unsigned int TaValue = 0;
- float Ta = 0;
- __delay_ms(1000);
- while (1) { // continually read temp from RAM
- char output[10];
- I2C_Ready(); // Wait until the bus is idle
- char ack = I2C_Start(0xB4); // send START condition
- LCD_Char(ack);
- char ack2 = I2C_Write(0x07);
- LCD_Char(ack2);
- SSPCON2bits.RSEN = 1; //Repeated start condition
- while (SSPCON2bits.RSEN == 1);
- while (PIR1bits.SSPIF == 0);
- PIR1bits.SSPIF = 0;
- char ack3 = I2C_Write(0xB5);
- LCD_Char(ack3);
- 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];
- // Calculate Ta from RAM reg value
- //Tadata[0] = Tadata[0] << 8; // left-shift lowe byte
- //TaValue = Tadata[0] + Tadata[1]; // Compute reg value
- //Ta = 0.02 * TaValue; // Compute actual ambient temp
- //LCD_Char(TaValue);
- //sprintf(output, 10, "%f", Ta);
- //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