hasanabd

Untitled

Apr 7th, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. /*
  2. * File: main.c
  3. * Author: Hasan
  4. *
  5.  
  6.  
  7.  
  8. * Created on March 30, 2018, 1:05 PM
  9.  
  10.  
  11.  
  12. */
  13.  
  14. #include <pic18f4550.h>
  15. #include "Configuration_Header_File.h"
  16. #include <xc.h>
  17. #include "USART_Header_File.h"
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21.  
  22.  
  23. #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
  24. #define RS LATA0 /*PORTD 0 pin is used for Register Select*/
  25. #define EN LATA1 /*PORTD 1 pin is used for Enable*/
  26. #define ldata LATD /*PORTB is used for transmitting data to LCD*/
  27. #define LCD_Port TRISD /*define macros for PORTB Direction Register*/
  28. #define LCD_Control TRISA /*define macros for PORTD Direction Register*/
  29. #define IR_TEMP_SENSOR LATEbits.LATE0 // Name the RA0 bit to "LED"
  30.  
  31. void LCD_Init();
  32. void LCD_Command(char);
  33. void LCD_Char(char x);
  34. void LCD_String(const char *);
  35. void LCD_String_xy(char, char, const char*);
  36. void MSdelay(unsigned int);
  37. void i2c_Init(void);
  38. void i2c_wait(unsigned int cnt);
  39. unsigned int IR_temp_read(unsigned char command);
  40.  
  41. //extern unsigned int IR_temp_read(unsigned char);
  42.  
  43. /****************************Functions********************************/
  44.  
  45. void LCD_Init() {
  46. __delay_ms(15); /*15ms,16x2 LCD Power on delay*/
  47. LCD_Port = 0x00; /*Set PORTB as output PORT for LCD data(D0-D7) pins*/
  48. LCD_Control = 0x00; /*Set PORTD as output PORT LCD Control(RS,EN) Pins*/
  49. LCD_Command(0x38); /*uses 2 line and initialize 5*7 matrix of LCD*/
  50. LCD_Command(0x01); /*clear display screen*/
  51. LCD_Command(0x0c); /*display on cursor off*/
  52. LCD_Command(0x06); /*increment cursor (shift cursor to right)*/
  53. }
  54.  
  55. void LCD_Clear() {
  56. LCD_Command(0x01); /*clear display screen*/
  57. }
  58.  
  59. void LCD_Command(char cmd) {
  60. ldata = cmd; /*Send data to PORT as a command for LCD*/
  61. RS = 0; /*Command Register is selected*/
  62. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  63. NOP();
  64. EN = 0;
  65. __delay_ms(3);
  66. }
  67.  
  68. void LCD_Char(char dat) {
  69. ldata = dat; /*Send data to LCD*/
  70. RS = 1; /*Data Register is selected*/
  71. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  72. NOP();
  73. EN = 0;
  74. __delay_ms(1);
  75. }
  76.  
  77. void LCD_String(const char *msg) {
  78. while ((*msg) != 0) {
  79. LCD_Char(*msg);
  80. msg++;
  81. }
  82. }
  83.  
  84. void LCD_String_xy(char row, char pos, const char *msg) {
  85. char location = 0;
  86. if (row <= 1) {
  87. location = (0x80) | ((pos) & 0x0f); /*Print message on 1st row and desired location*/
  88. LCD_Command(location);
  89. } else {
  90. location = (0xC0) | ((pos) & 0x0f); /*Print message on 2nd row and desired location*/
  91. LCD_Command(location);
  92. }
  93. LCD_String(msg);
  94. }
  95.  
  96. void i2c_Init(void) {
  97.  
  98. TRISBbits.TRISB1 = 1;
  99. TRISBbits.TRISB0 = 1;
  100.  
  101. PIE1bits.SSPIE = 0;
  102.  
  103. SSPSTAT = 0xC0;
  104. SSPCON1 = 0x08; //Master Mode
  105. SSPCON2 = 0x00;
  106. SSPADD = 24;
  107. //SSPCON1bits.SSPEN = 1;
  108. SSPCON1 |= 0x20;
  109. i2c_wait(20);
  110. }
  111.  
  112. void i2c_wait(unsigned int cnt) {
  113. while (--cnt) {
  114. NOP();
  115. NOP();
  116. }
  117. }
  118.  
  119. unsigned int IR_temp_read(unsigned char command) {
  120. //b'0001 aaaa: Read of EEPROM
  121. //b'0010 aaaa: Read of RAM
  122. //aaaa is the memory address to be read
  123. //EEPROM Addresses
  124. //b'0000: Slave Address
  125. //b'0010: Config
  126. //b'0011: Emissivity
  127. //RAM Addresses
  128. //b'0101: Raw IR data
  129. //b'0110: T ambient
  130. //b'0111: T object
  131. unsigned char SlaveAddress = 0x5A;
  132. //unsigned char command;
  133. unsigned char i2cData[3];
  134. int i2cRX_Data[3];
  135. int DataSz;
  136. int Index = 0;
  137. unsigned int return_data;
  138. i2cRX_Data[0] = 0;
  139. i2cRX_Data[1] = 0;
  140. i2cRX_Data[2] = 0;
  141.  
  142. PIR1bits.SSPIF = 0;
  143.  
  144. i2cData[0] = ((SlaveAddress << 1) & 0xFE);
  145. i2cData[1] = command;
  146. //i2cData[2] = ((SlaveAddress << 1) | 0x01);
  147. DataSz = 2;
  148.  
  149. //START
  150. SSPCON2bits.SEN = 1; /* Send start pulse */
  151. while (SSPCON2bits.SEN); /* Wait for completion of start pulse */
  152. SSPIF = 0;
  153. if (!SSPSTATbits.S){ /* Check whether START detected last */
  154. LCD_Char('0'); /* Return 0 to indicate start failed */
  155. }else{
  156. LCD_Char('1');
  157. }
  158. //
  159.  
  160. Index = 0;
  161. i2c_wait(15);
  162. while (DataSz)
  163. {
  164. //Write
  165. SSPBUF = i2cData[Index++];
  166. while (PIR1bits.SSPIF == 0);
  167. i2c_wait(10);
  168. while (SSPCON2bits.ACKSTAT == 1);
  169. PIR1bits.SSPIF = 0;
  170. DataSz--;
  171. }
  172.  
  173. //Start read from IR temp sensor
  174. //T0CONbits.TMR0ON = 1; //Turn on the timer
  175. //INTCONbits.GIE = 0;
  176. TMR0 = 0;
  177. INTCONbits.TMR0IF = 0;
  178.  
  179. SSPCON2bits.RSEN = 1; //Repeated start condition
  180. while (SSPCON2bits.RSEN == 1);
  181. while (PIR1bits.SSPIF == 0);
  182. PIR1bits.SSPIF = 0;
  183.  
  184. i2c_wait(15); //JM was 50
  185.  
  186. SSPBUF = ((SlaveAddress << 1) | 1);
  187. while (SSPCON2bits.ACKSTAT == 1);
  188. while (PIR1bits.SSPIF == 0);
  189. PIR1bits.SSPIF = 0;
  190. i2c_wait(30);
  191.  
  192. //Receive First Byte
  193. SSPCON2bits.RCEN = 1;
  194. while (PIR1bits.SSPIF == 0);
  195. PIR1bits.SSPIF = 0;
  196. SSPCON2bits.ACKDT = 0;
  197. SSPCON2bits.ACKEN = 1;
  198. while ((PIR1bits.SSPIF == 0) || (INTCONbits.TMR0IF == 1));
  199. if (INTCONbits.TMR0IF == 0) {
  200. TMR0 = 0;
  201. INTCONbits.TMR0IF = 0;
  202. }
  203. PIR1bits.SSPIF = 0;
  204. i2cRX_Data[0] = SSPBUF;
  205. i2c_wait(30);
  206.  
  207. //Receive second byte
  208. SSPCON2bits.RCEN = 1;
  209. while (PIR1bits.SSPIF == 0);
  210. PIR1bits.SSPIF = 0;
  211. SSPCON2bits.ACKDT = 0;
  212. SSPCON2bits.ACKEN = 1;
  213. while ((PIR1bits.SSPIF == 0) || (INTCONbits.TMR0IF == 1));
  214. if (INTCONbits.TMR0IF == 0) {
  215. TMR0 = 0;
  216. INTCONbits.TMR0IF = 0;
  217. }
  218. PIR1bits.SSPIF = 0;
  219. i2cRX_Data[1] = SSPBUF;
  220. i2c_wait(30);
  221.  
  222. //Receive third byte
  223. SSPCON2bits.RCEN = 1;
  224. while (PIR1bits.SSPIF == 0);
  225. PIR1bits.SSPIF = 0;
  226. SSPCON2bits.ACKDT = 0;
  227. SSPCON2bits.ACKEN = 1;
  228. while ((PIR1bits.SSPIF == 0) || (INTCONbits.TMR0IF == 1));
  229. if (INTCONbits.TMR0IF == 0) {
  230. TMR0 = 0;
  231. INTCONbits.TMR0IF = 0;
  232. }
  233. PIR1bits.SSPIF = 0;
  234. i2cRX_Data[2] = SSPBUF;
  235.  
  236. NOP();
  237. NOP();
  238. NOP();
  239.  
  240. SSPCON2bits.PEN = 1;
  241. while (SSPCON2bits.PEN)
  242. while ((SSPCON2 & 0x1F) | (SSPSTATbits.R_nW))
  243. i2c_wait(100);
  244. return_data = 0x0000;
  245. if (INTCONbits.TMR0IF == 0)//timer didn't overflow so info received from temp sensor
  246. {
  247. return_data = (i2cRX_Data[1] << 8) | i2cRX_Data[0];
  248. }
  249.  
  250. return (return_data);
  251. } //end unsigned int IR_temp_read(unsigned char command)
  252.  
  253. /*****************************Main Program*******************************/
  254.  
  255. void main(void) {
  256.  
  257. int temp1 = 0;
  258. float Temperature = 42;
  259. // USART_Init(9600);
  260. i2c_Init();
  261. char data_in;
  262. TRISEbits.TRISE0 = 0;
  263. IR_TEMP_SENSOR = 0;
  264. __delay_ms(2000);
  265. IR_TEMP_SENSOR = 1;
  266. //__delay_ms(10000);
  267. int temps;
  268. float Temperature;
  269. LCD_Init(); /*Initialize 16x2 LCD*/
  270. LCD_String_xy(1, 0, "Dig. Thermometer"); /*Display string at location(row,location).
  271. // * This function passes string to display*/
  272. LCD_Command(0xc0);
  273.  
  274.  
  275. __delay_ms(1000);
  276. while (1) { // continually read temp from RAM
  277. char output[10];
  278. temp1 = IR_temp_read(0x07);
  279. LCD_Char('1');
  280. Temperature = (((float) temp1 * 0.02) - 273.15);
  281. sprintf(output, 10, "%f", Temperature);
  282. LCD_String(output);
  283. // USART_SendString(output);
  284. __delay_ms(5000);
  285. }
  286.  
  287.  
  288. //char flag = I2C_Start(0x5A);
  289. //LCD_Char(flag);
  290.  
  291. }
Advertisement
Add Comment
Please, Sign In to add comment