hasanabd

Untitled

Apr 8th, 2018
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. /* PIC18F4550 Configuration Bit Settings*/
  2.  
  3. /* 'C' source line config statements*/
  4.  
  5. #include <xc.h>
  6. // CONFIG1L
  7. #pragma config FOSC=HS
  8. #pragma config FCMEN=ON
  9. #pragma config WDT=OFF
  10. #pragma config IESO=ON
  11. #pragma config XINST=OFF
  12. #pragma config LVP=OFF
  13. #pragma config PBADEN = OFF
  14.  
  15. ////////////////////////////////////////////////////////////////////////////////////////
  16.  
  17. //USART_header_file
  18.  
  19. // This is a guard condition so that contents of this file are not included
  20. // more than once.
  21. #ifndef USART_HEADER_FILE_H
  22. #define USART_HEADER_FILE_H
  23. #include <xc.h>
  24. #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
  25. #include <pic18f4550.h>
  26.  
  27. void USART_Init(long);
  28. void USART_TransmitChar(char);
  29. void USART_SendString(const char *);
  30. void MSdelay(unsigned int val);
  31. char USART_ReceiveChar();
  32.  
  33. #define F_CPU 10000000/64
  34. //#define Baud_value(baud_rate) (((float)(F_CPU)/(float)baud_rate)-1)
  35. #define Baud_value (((float)(F_CPU)/(float)baud_rate)-1)
  36.  
  37. #endif /* USART_HEADER_FILE_H */
  38.  
  39.  
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41.  
  42. /*
  43. * File: main.c
  44. * Author: Hasan
  45. *
  46. * Created on March 30, 2018, 1:05 PM
  47. */
  48.  
  49. #include "Configuration_Header_File.h"
  50. #include <xc.h>
  51. #include "USART_Header_File.h"
  52. #include <stdlib.h>
  53. #include <stdint.h>
  54. #include <stdio.h>
  55.  
  56.  
  57. #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
  58. #define RS LATA0 /*PORTD 0 pin is used for Register Select*/
  59. #define EN LATA1 /*PORTD 1 pin is used for Enable*/
  60. #define ldata LATD /*PORTB is used for transmitting data to LCD*/
  61. #define LCD_Port TRISD /*define macros for PORTB Direction Register*/
  62. #define LCD_Control TRISA /*define macros for PORTD Direction Register*/
  63. #define IR_TEMP_SENSOR LATEbits.LATE0 // Name the RA0 bit to "LED"
  64.  
  65. void LCD_Init();
  66. void LCD_Command(char);
  67. void LCD_Char(char x);
  68. void LCD_String(const char *);
  69. void LCD_String_xy(char, char, const char*);
  70. void I2C_Init(void);
  71. char I2C_Start(char slave_write_address);
  72. void I2C_Ready();
  73. char I2C_Write(unsigned char data);
  74. char I2C_Stop();
  75. char I2C_Read(char flag);
  76. void I2C_Ack();
  77. void I2C_Nack();
  78.  
  79. /****************************Functions********************************/
  80.  
  81. void LCD_Init() {
  82. __delay_ms(15); /*15ms,16x2 LCD Power on delay*/
  83. LCD_Port = 0x00; /*Set PORTB as output PORT for LCD data(D0-D7) pins*/
  84. LCD_Control = 0x00; /*Set PORTD as output PORT LCD Control(RS,EN) Pins*/
  85. LCD_Command(0x38); /*uses 2 line and initialize 5*7 matrix of LCD*/
  86. LCD_Command(0x01); /*clear display screen*/
  87. LCD_Command(0x0c); /*display on cursor off*/
  88. LCD_Command(0x06); /*increment cursor (shift cursor to right)*/
  89. }
  90.  
  91. void LCD_Clear() {
  92. LCD_Command(0x01); /*clear display screen*/
  93. }
  94.  
  95. void LCD_Command(char cmd) {
  96. ldata = cmd; /*Send data to PORT as a command for LCD*/
  97. RS = 0; /*Command Register is selected*/
  98. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  99. NOP();
  100. EN = 0;
  101. __delay_ms(3);
  102. }
  103.  
  104. void LCD_Char(char dat) {
  105. ldata = dat; /*Send data to LCD*/
  106. RS = 1; /*Data Register is selected*/
  107. EN = 1; /*High-to-Low pulse on Enable pin to latch data*/
  108. NOP();
  109. EN = 0;
  110. __delay_ms(1);
  111. }
  112.  
  113. void LCD_String(const char *msg) {
  114. while ((*msg) != 0) {
  115. LCD_Char(*msg);
  116. msg++;
  117. }
  118. }
  119.  
  120. void LCD_String_xy(char row, char pos, const char *msg) {
  121. char location = 0;
  122. if (row <= 1) {
  123. location = (0x80) | ((pos) & 0x0f); /*Print message on 1st row and desired location*/
  124. LCD_Command(location);
  125. } else {
  126. location = (0xC0) | ((pos) & 0x0f); /*Print message on 2nd row and desired location*/
  127. LCD_Command(location);
  128. }
  129. LCD_String(msg);
  130. }
  131.  
  132. void I2C_Init(void) {
  133.  
  134. TRISBbits.TRISB1 = 1;
  135. TRISBbits.TRISB0 = 1;
  136.  
  137. PIE1bits.SSPIE = 0;
  138.  
  139. SSPSTAT = 0xC0; /* Slew rate disabled, other bits are cleared */
  140. SSPCON1 = 0x28; /* Enable SSP port for I2C Master mode,
  141. clock = FOSC / (4 * (SSPADD+1))*/
  142. SSPCON2 = 0;
  143. SSPADD = 0x18; /* Clock 100 kHz */
  144. SSPIE = 1; /* Enable SSPIF interrupt */
  145. SSPIF = 0;
  146. }
  147.  
  148. char I2C_Start(char slave_write_address) {
  149. SSPCON2bits.SEN = 1; /* Send start pulse */
  150. /* Wait for completion of start pulse */
  151.  
  152. // if (!SSPSTATbits.S) { /* Check whether START detected last */
  153. // return 0; /* Return 0 to indicate start failed */
  154. // }
  155.  
  156. while (SSPCON2bits.SEN == 1);
  157. SSPIF = 0;
  158. if (!SSPSTATbits.S) { /* Check whether START detected last */
  159. //LCD_Char('0'); /* Return 0 to indicate start failed */
  160. } else {
  161. // LCD_Char('1');
  162. }
  163. return (I2C_Write(slave_write_address)); /* Write slave device address
  164. with write to communicate */
  165. }
  166.  
  167. void I2C_Ready() {
  168. while (BCLIF); /* Wait if bit collision interrupt flag is set*/
  169.  
  170. /* Wait for Buffer full and read write flag*/
  171. while (SSPSTATbits.BF || (SSPSTATbits.R_nW));
  172. SSPIF = 0; /* Clear SSPIF interrupt flag*/
  173. }
  174.  
  175. char I2C_Write(unsigned char data) {
  176. SSPBUF = data; /* Write data to SSPBUF*/
  177. I2C_Ready();
  178. if (ACKSTAT) {/* Check for acknowledge bit*/
  179. return 1;
  180. } else {
  181. return 2;
  182. }
  183. }
  184.  
  185. char I2C_Stop() {
  186. I2C_Ready();
  187. PEN = 1; /* Stop communication*/
  188. while (PEN); /* Wait for end of stop pulse*/
  189. SSPIF = 0;
  190. if (!SSPSTATbits.P); /* Check whether STOP is detected last */
  191. return 0; /* If not return 0 to indicate start failed*/
  192. }
  193.  
  194. /* Read data from location and send ack or nack depending upon parameter*/
  195.  
  196. char I2C_Read(char flag) {
  197. int buffer = 0;
  198. RCEN = 1; /* Enable receive */
  199.  
  200. /* Wait for buffer full flag which when complete byte received */
  201. while (!SSPSTATbits.BF);
  202. buffer = SSPBUF; /* Copy SSPBUF to buffer */
  203.  
  204. /* Send acknowledgment or negative acknowledgment after read to
  205. continue or stop reading */
  206. if (flag == 0)
  207. I2C_Ack();
  208. else
  209. I2C_Nack();
  210. I2C_Ready();
  211. return (buffer);
  212. }
  213.  
  214. void I2C_Ack() {
  215. ACKDT = 0; /* Acknowledge data 1:NACK,0:ACK */
  216. ACKEN = 1; /* Enable ACK to send */
  217. while (ACKEN);
  218. }
  219.  
  220. void I2C_Nack() {
  221. ACKDT = 1; /* Acknowledge data 1:NACK,0:ACK */
  222. ACKEN = 1; /* Enable ACK to send */
  223. while (ACKEN);
  224. }
  225.  
  226. /*****************************Main Program*******************************/
  227.  
  228. void main(void) {
  229.  
  230.  
  231. USART_Init(9600);
  232. char data_in;
  233. I2C_Init();
  234. TRISEbits.TRISE0 = 0;
  235. IR_TEMP_SENSOR = 0;
  236. __delay_ms(2000);
  237. IR_TEMP_SENSOR = 1;
  238. LCD_Init(); /*Initialize 16x2 LCD*/
  239. LCD_String_xy(1, 0, "Dig. Thermometer"); /*Display string at location(row,location).
  240. * This function passes string to display*/
  241. LCD_Command(0xc0);
  242. int Tadata[3] = {0, 0, 0};
  243. unsigned int TaValue = 0;
  244. __delay_ms(1000);
  245.  
  246. while (1) { // continually read temp from RAM
  247. data_in = USART_ReceiveChar();
  248. char output[6];
  249. float values[5];
  250. float total= 0 ;
  251. if (data_in == '1') {
  252. float Ta = 0;
  253. int i;
  254. for (i = 0; i < 5; i++) {
  255. char ack = I2C_Start(0xB4); // send START condition
  256. char ack2 = I2C_Write(0x07);
  257. SSPCON2bits.RSEN = 1; //Repeated start condition
  258. while (SSPCON2bits.RSEN == 1);
  259. while (PIR1bits.SSPIF == 0);
  260. PIR1bits.SSPIF = 0;
  261. char ack3 = I2C_Write(0xB5);
  262. Tadata[0] = I2C_Read(0); // Read 1st byte of data (low byte))
  263. Tadata[1] = I2C_Read(0); // Read 2nd byte (high byte))
  264. Tadata[2] = I2C_Read(1); // Read last byte (PEC)
  265. char ack4 = I2C_Stop(); // Hang up, send STOP condition
  266.  
  267. TaValue = (Tadata[1] << 8) | Tadata[0];
  268. Ta = (((float) TaValue * 0.02) - 273.15); // Compute actual ambient temp
  269. values[i] = Ta ;
  270. __delay_ms(1000);
  271. }
  272. int j;
  273. for (j = 0; j < 5; j++) {
  274. total = total + values[j];
  275. }
  276. sprintf(output, "%f", total/5);
  277. LCD_String_xy(2, 2, output);
  278. LCD_Char(0xDF);
  279. LCD_Char('C');
  280. USART_SendString(output);
  281.  
  282. }
  283. }
  284. }
  285.  
  286. /////////////////////////////////////////////////////////////////////////////////////
  287.  
  288. //USART FILE
  289.  
  290. #include "USART_Header_File.h"
  291. #include <xc.h>
  292. #define _XTAL_FREQ 10000000 //Set Oscillator Frequency
  293.  
  294. /*****************************USART Initialization*******************************/
  295. void USART_Init(long baud_rate)
  296. {
  297. float temp;
  298. TRISC6=0; /*Make Tx pin as output*/
  299. TRISC7=1; /*Make Rx pin as input*/
  300. temp=Baud_value;
  301. SPBRG= 15; /*baud rate=9600, SPBRG = (F_CPU /(64*9600))-1*/
  302. TXSTA=0x20; /*Transmit Enable(TX) enable*/
  303. RCSTA=0x90; /*Receive Enable(RX) enable and serial port enable */
  304. }
  305. /******************TRANSMIT FUNCTION*****************************************/
  306. void USART_TransmitChar(char out)
  307. {
  308. while(TXIF==0); /*wait for transmit interrupt flag*/
  309. TXREG=out; /*wait for transmit interrupt flag to set which indicates TXREG is ready
  310. for another transmission*/
  311. }
  312. /*******************RECEIVE FUNCTION*****************************************/
  313. char USART_ReceiveChar()
  314. {
  315.  
  316. while(RCIF==0); /*wait for receive interrupt flag*/
  317. return(RCREG); /*receive data is stored in RCREG register and return to main program */
  318. }
  319.  
  320. void USART_SendString(const char *out)
  321. {
  322. while(*out!='\0')
  323. {
  324. USART_TransmitChar(*out);
  325. out++;
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment