hasanabd

Untitled

Apr 7th, 2018
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. /*
  2. * File: main.c
  3. * Author: Hasan
  4. *
  5. *
  6. */
  7.  
  8. #include "Configuration_Header_File.h"
  9. #include <xc.h>
  10. #include "USART_Header_File.h"
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14.  
  15.  
  16.  
  17.  
  18. #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
  19. #define RS LATA0 /*PORTD 0 pin is used for Register Select*/
  20. #define EN LATA1 /*PORTD 1 pin is used for Enable*/
  21. #define ldata LATD /*PORTB is used for transmitting data to LCD*/
  22. #define LCD_Port TRISD /*define macros for PORTB Direction Register*/
  23. #define LCD_Control TRISA /*define macros for PORTD Direction Register*/
  24. #define IR_TEMP_SENSOR LATEbits.LATE0 // Name the RA0 bit to "LED"
  25.  
  26. void LCD_Init();
  27. void LCD_Command(char);
  28. void LCD_Char(char x);
  29. void LCD_String(const char *);
  30. void LCD_String_xy(char, char, const char*);
  31. void I2C_Init(void);
  32. char I2C_Start(char slave_write_address);
  33. void I2C_Ready();
  34. char I2C_Write(unsigned char data);
  35. char I2C_Stop();
  36. char I2C_Read(char flag);
  37. void I2C_Ack();
  38. void I2C_Nack();
  39.  
  40.  
  41. unsigned int IR_temp_read(unsigned char command);
  42.  
  43. //extern unsigned int IR_temp_read(unsigned char);
  44.  
  45. /****************************Functions********************************/
  46.  
  47. void LCD_Init() {
  48. __delay_ms(15); /*15ms,16x2 LCD Power on delay*/
  49. LCD_Port = 0x00; /*Set PORTB as output PORT for LCD data(D0-D7) pins*/
  50. LCD_Control = 0x00; /*Set PORTD as output PORT LCD Control(RS,EN) Pins*/
  51. LCD_Command(0x38); /*uses 2 line and initialize 5*7 matrix of LCD*/
  52. LCD_Command(0x01); /*clear display screen*/
  53. LCD_Command(0x0c); /*display on cursor off*/
  54. LCD_Command(0x06); /*increment cursor (shift cursor to right)*/
  55. }
  56.  
  57. void LCD_Clear() {
  58. LCD_Command(0x01); /*clear display screen*/
  59. }
  60.  
  61. void LCD_Command(char cmd) {
  62. ldata = cmd; /*Send data to PORT as a command for LCD*/
  63. RS = 0; /*Command Register is selected*/
  64. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  65. NOP();
  66. EN = 0;
  67. __delay_ms(3);
  68. }
  69.  
  70. void LCD_Char(char dat) {
  71. ldata = dat; /*Send data to LCD*/
  72. RS = 1; /*Data Register is selected*/
  73. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  74. NOP();
  75. EN = 0;
  76. __delay_ms(1);
  77. }
  78.  
  79. void LCD_String(const char *msg) {
  80. while ((*msg) != 0) {
  81. LCD_Char(*msg);
  82. msg++;
  83. }
  84. }
  85.  
  86. void LCD_String_xy(char row, char pos, const char *msg) {
  87. char location = 0;
  88. if (row <= 1) {
  89. location = (0x80) | ((pos) & 0x0f); /*Print message on 1st row and desired location*/
  90. LCD_Command(location);
  91. } else {
  92. location = (0xC0) | ((pos) & 0x0f); /*Print message on 2nd row and desired location*/
  93. LCD_Command(location);
  94. }
  95. LCD_String(msg);
  96. }
  97.  
  98. void I2C_Init(void) {
  99.  
  100. TRISBbits.TRISB1 = 1;
  101. TRISBbits.TRISB0 = 1;
  102.  
  103. PIE1bits.SSPIE = 0;
  104.  
  105. SSPSTAT = 0xC0; /* Slew rate disabled, other bits are cleared */
  106. SSPCON1 = 0x28; /* Enable SSP port for I2C Master mode,
  107. clock = FOSC / (4 * (SSPADD+1))*/
  108. SSPCON2 = 0;
  109. SSPADD = 24; /* Clock 100 kHz */
  110. SSPIE = 1; /* Enable SSPIF interrupt */
  111. SSPIF = 0;
  112. }
  113.  
  114. char I2C_Start(char slave_write_address) {
  115. SSPCON2bits.SEN = 1; /* Send start pulse */
  116. while (SSPCON2bits.SEN); /* Wait for completion of start pulse */
  117. SSPIF = 0;
  118. if (!SSPSTATbits.S) { /* Check whether START detected last */
  119. return 0; /* Return 0 to indicate start failed */
  120. }
  121. return (I2C_Write(slave_write_address)); /* Write slave device address
  122. with write to communicate */
  123. }
  124.  
  125. void I2C_Ready() {
  126. while (BCLIF); /* Wait if bit collision interrupt flag is set*/
  127.  
  128. /* Wait for Buffer full and read write flag*/
  129. while (SSPSTATbits.BF || (SSPSTATbits.R_nW));
  130. SSPIF = 0; /* Clear SSPIF interrupt flag*/
  131. }
  132.  
  133. char I2C_Write(unsigned char data) {
  134. SSPBUF = data; /* Write data to SSPBUF*/
  135. I2C_Ready();
  136. if (ACKSTAT) {/* Check for acknowledge bit*/
  137. return 1;
  138. } else {
  139. return 2;
  140. }
  141. }
  142.  
  143. char I2C_Stop() {
  144. I2C_Ready();
  145. PEN = 1; /* Stop communication*/
  146. while (PEN); /* Wait for end of stop pulse*/
  147. SSPIF = 0;
  148. if (!SSPSTATbits.P); /* Check whether STOP is detected last */
  149. return 0; /* If not return 0 to indicate start failed*/
  150. }
  151.  
  152. /* Read data from location and send ack or nack depending upon parameter*/
  153.  
  154. char I2C_Read(char flag) {
  155. int buffer = 0;
  156. RCEN = 1; /* Enable receive */
  157.  
  158. /* Wait for buffer full flag which when complete byte received */
  159. while (!SSPSTATbits.BF);
  160. buffer = SSPBUF; /* Copy SSPBUF to buffer */
  161.  
  162. /* Send acknowledgment or negative acknowledgment after read to
  163. continue or stop reading */
  164. if (flag == 0)
  165. I2C_Ack();
  166. else
  167. I2C_Nack();
  168. I2C_Ready();
  169. return (buffer);
  170. }
  171.  
  172. void I2C_Ack() {
  173. ACKDT = 0; /* Acknowledge data 1:NACK,0:ACK */
  174. ACKEN = 1; /* Enable ACK to send */
  175. while (ACKEN);
  176. }
  177.  
  178. void I2C_Nack() {
  179. ACKDT = 1; /* Acknowledge data 1:NACK,0:ACK */
  180. ACKEN = 1; /* Enable ACK to send */
  181. while (ACKEN);
  182. }
  183.  
  184. /*****************************Main Program*******************************/
  185.  
  186. void main(void) {
  187.  
  188. int temp1 = 0;
  189. float Temperature = 42;
  190. USART_Init(9600);
  191.  
  192. char data_in;
  193. I2C_Init();
  194. TRISEbits.TRISE0 = 0;
  195. IR_TEMP_SENSOR = 0;
  196. __delay_ms(2000);
  197. IR_TEMP_SENSOR = 1;
  198.  
  199. int temps;
  200. float Temperature;
  201. LCD_Init(); /*Initialize 16x2 LCD*/
  202. LCD_String_xy(1, 0, "Dig. Thermometer"); /*Display string at location(row,location).
  203. // * This function passes string to display*/
  204. LCD_Command(0xc0);
  205.  
  206. int Tadata[3] = {0, 0, 0};
  207. unsigned int TaValue = 0;
  208. float Ta = 0;
  209.  
  210. __delay_ms(1000);
  211. while (1) { // continually read temp from RAM
  212. char output[10];
  213.  
  214. I2C_Ready(); // Wait until the bus is idle
  215. char ack = I2C_Start(0xB4); // send START condition
  216. LCD_Char(ack);
  217.  
  218. char ack2 = I2C_Write(0x07);
  219. LCD_Char(ack2);
  220.  
  221. SSPCON2bits.RSEN = 1; //Repeated start condition
  222. while (SSPCON2bits.RSEN == 1);
  223. while (PIR1bits.SSPIF == 0);
  224. PIR1bits.SSPIF = 0;
  225.  
  226.  
  227. char ack3 = I2C_Write(0xB5);
  228. LCD_Char(ack3);
  229.  
  230. Tadata[0] = I2C_Read(0); // Read 1st byte of data (low byte))
  231.  
  232. Tadata[1] = I2C_Read(0); // Read 2nd byte (high byte))
  233.  
  234. Tadata[2] = I2C_Read(1); // Read last byte (PEC)
  235.  
  236. char ack4 = I2C_Stop(); // Hang up, send STOP condition
  237.  
  238.  
  239. //TaValue = (Tadata[1]<<8) | Tadata[0];
  240. // Calculate Ta from RAM reg value
  241. //Tadata[0] = Tadata[0] << 8; // left-shift lowe byte
  242. //TaValue = Tadata[0] + Tadata[1]; // Compute reg value
  243. //Ta = 0.02 * TaValue; // Compute actual ambient temp
  244. //LCD_Char(TaValue);
  245. //sprintf(output, 10, "%f", Ta);
  246. //LCD_String(output);
  247. //USART_SendString(output);
  248. __delay_ms(5000);
  249. }
  250.  
  251.  
  252. //char flag = I2C_Start(0x5A);
  253. //LCD_Char(flag);
  254.  
  255.  
  256.  
  257. }
Advertisement
Add Comment
Please, Sign In to add comment