Double - Dabble - 16 - Bit NBCD - Ones NBCD - Tens NBCD - Hundreds: Void Char Char Char Char

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1 /////////////////////////////////////////////////////////////////////////////////////////

2 // Barometric Pressure Sensor (BMP180)


3 //
4 // - Hardware
5 // -----------------------
6 // Processor: PIC16F877A
7 // Compiler: XC8, C99 Mode
8 // Oscillator Frequency (FOSC): 8 MHZ
9 // Clock Cycle (TOSC): 0.25 uS
10 // Instruction Cycle (TCM) : 4*TOSC = 1 uS
11 //
12 // - Description
13 // Code to display the Temperature and Barometric Pressure sent by the BMP180
14 // sensor on an LCD
15 //
16 //////////////////////////////////////////////////////////////////////////////////////////
17
18 // Including C Standard Libraries
19 #include <stdint.h>
20 // Including XC8 Compiler Library
21 #include <xc.h>
22 // Including User's Libraries
23 //#include "bmp_180.h"
24 #include "lcd.h"
25 #include "i2c.h"
26
27 // Defining Oscillator Frequency
28 #define _XTAL_FREQ 8000000
29
30 // Setting Configuration Bits
31 #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
32 #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
33 #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
34 #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
35 #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable
bit (RB3 is digital I/O, HV on MCLR must be used for programming)
36 #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code
protection off)
37 #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off;
all program memory may be written to by EECON control)
38 #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
39
40 // Defining Registers Names
41 #define module_address_write 0xEE // Module Address Register (Write Mode)
42 #define module_address_read 0xEF // Module Address Register (Read Mode)
43
44 #define DATA_XLSB 0xF8 // Data Register XLSB
45 #define DATA_LSB 0xF7 // Data Register LSB
46 #define DATA_MSB 0xF6 // Data Register MSB
47
48 #define ctrl_meas 0xF4 // ctrl Register ( Measurment Control <4:0>, Sco <5>, Oss <7:6> )
49 // oss = 00:Single 01:2 Times 10:4 Times 11: 8 Times
50 #define soft 0xEO // Soft Reset Register
51 #define id 0xD0 // Chip ID Register
52
53 #define calib_AC1_MSB 0xAA // Calibration Register AC1 MSB
54 #define calib_AC1_LSB 0xAB // Calibration Register AC1 LSB
55 #define calib_AC2_MSB 0xAC // Calibration Register AC2 MSB
56 #define calib_AC2_LSB 0xAD // Calibration Register AC2 LSB
57 #define calib_AC3_MSB 0xAE // Calibration Register AC3 MSB
58 #define calib_AC3_LSB 0xAF // Calibration Register AC3 LSB
59 #define calib_AC4_MSB 0xB0 // Calibration Register AC4 MSB
60 #define calib_AC4_LSB 0xB1 // Calibration Register AC4 LSB
61 #define calib_AC5_MSB 0xB2 // Calibration Register AC5 MSB
62 #define calib_AC5_LSB 0xB3 // Calibration Register AC5 LSB
63 #define calib_AC6_MSB 0xB4 // Calibration Register AC6 MSB
64 #define calib_AC6_LSB 0xB5 // Calibration Register AC6 LSB
65 #define calib_B1_MSB 0xB6 // Calibration Register B1 MSB
66 #define calib_B1_LSB 0xB7 // Calibration Register B1 LSB
67 #define calib_B2_MSB 0xB8 // Calibration Register B2 MSB
68 #define calib_B2_LSB 0xB9 // Calibration Register B2 LSB
69 #define calib_MB_MSB 0xBA // Calibration Register MB MSB
70 #define calib_MB_LSB 0xBB // Calibration Register MB LSB
71 #define calib_MC_MSB 0xBC // Calibration Register MC MSB
72 #define calib_MC_LSB 0xBD // Calibration Register MC LSB
73 #define calib_MD_MSB 0xBE // Calibration Register MD MSB
74 #define calib_MD_LSB 0xBF // Calibration Register MD LSB
75
76 void double_dabble_16_bit(char *NBCD_Ones, char *NBCD_Tens, char *NBCD_Hundreds, char
*NBCD_Thousands, char *NBCD_Tens_Thousands, uint16_t binary ) // Range: 0-65,535
77 {
78
79 uint8_t Ones = 0;
80 uint8_t Tens = 0;
81 uint8_t Hundreds = 0;
82 uint8_t Thousands = 0;
83 uint8_t Tens_Thousands = 0;
84
85 uint8_t i;
86
87
88 for(i=0; i<16; i++) { // Dabble Double Algorithm to Convert Binary into BCD
89 Ones <<= 1;
90 Tens <<= 1;
91 Hundreds <<= 1;
92 Thousands <<= 1;
93 Tens_Thousands <<= 1;
94
95 if (binary & 0x8000) { // xxxx xxxx xxxx xxxx & 1000 0000 0000 0000 = x000 0000 0000 0000
96 Ones++;
97 }
98 if (Ones >= 10) {
99 Ones -= 10;
100 Tens++;
101 }
102 if (Tens >= 10) {
103 Tens -= 10;
104 Hundreds++;
105 }
106 if (Hundreds >= 10) {
107 Hundreds -= 10;
108 Thousands++;
109 }
110 if (Thousands >= 10) {
111 Thousands -= 10;
112 Tens_Thousands++;
113 }
114 binary <<= 1;
115 }
116
117 *NBCD_Ones = Ones; // Assigning BCD Values to Sensor Reading Values
118 *NBCD_Tens = Tens;
119 *NBCD_Hundreds = Hundreds;
120 *NBCD_Thousands = Thousands;
121 *NBCD_Tens_Thousands = Tens_Thousands;
122
123 *NBCD_Ones += '0'; // Convert to ASCII
124 *NBCD_Tens += '0';
125 *NBCD_Hundreds += '0';
126 *NBCD_Thousands += '0';
127 *NBCD_Tens_Thousands += '0';
128
129 }
130
131 void main(void)
132 {
133
134 // Sensor Initiation ///////////////////////////////////////////////////////////
135
136 // Declaring Global Variables (Sensor Output)
137
138 char Temp_Decim;
139 char Temp_Ones;
140 char Temp_Tens;
141
142 char Pressure_Centi;
143 char Pressure_Decim;
144 char Pressure_Ones;
145 char Pressure_Tens;
146 char Pressure_Hundreds;
147
148 // LCD Initiation //////////////////////////////////////////////////////////////
149 TRISD = 0x00;
150 Lcd_Four_Bit_Init();
151
152 Lcd_Clear();
153
154 Lcd_Set_Cursor(1,1);
155 Lcd_Write_String("BMP180 ");
156
157 Lcd_Set_Cursor(1,2);
158 Lcd_Write_String("Pressure Sensor ");
159
160 __delay_ms(2000);
161 Lcd_Clear();
162
163 // Main Function ///////////////////////////////////////////////////////////////////
164 while(1)
165 {
166
167
168 // Calibrating Factors (Registers used: AC5H, AC5L, AC6H, AC6L, MCH, MCL, MDH, MDL)
169 int8_t AC1H, AC2H;
170 uint8_t AC1L, AC2L;
171 int16_t AC1, AC2;
172
173
174 Lcd_Set_Cursor(1,1);
175 Lcd_Write_String("Val:");
176
177 char ones = '0';
178 char tens = '0';
179 char hundreds = '0';
180 char thousands = '0';
181 char tens_thousands = '0';
182
183 // I2C Initiation //////////////////////////////////////////////////////////////////
184 I2C_Master_Init(High_Speed_Mode); // I2C Master with clock in Standard Mode (100 kbps)
185
186 // BMP180 Calibration Data Reading
187
188 // Reading AC5
189 I2C_Master_Begin(); // Start condition
190 I2C_Master_Write(module_address_write); // 7 bit address + 0 for Write
191 //I2C_Master_End();
192
193 //I2C_Master_Begin(); // Start condition
194 I2C_Master_Write(calib_AC1_MSB); // Register Address
195 //I2C_Master_End();
196
197 //I2C_Master_Begin(); // Start condition
198 I2C_Master_Write(module_address_read); // 7 bit address + 1 for Read
199 AC1H = I2C_Master_Read(1); // Read + Acknowledge
200 AC1L = I2C_Master_Read(0); // Read + Not Acknowledge
201 I2C_Master_End();
202
203 AC1 = (AC1H << 8) + AC1L;
204 //AC1 = AC1H ;
205
206 if(AC1 < 0) // xxxx xxxx & 1000 0000 = x000 0000
207 {
208 Lcd_Write_Char('-');
209 AC1 = -AC1;
210
211 double_dabble_16_bit(&ones, &tens, &hundreds, &thousands, &tens_thousands, AC1);
212
213 Lcd_Write_Char(tens_thousands);
214 Lcd_Write_Char(thousands);
215 Lcd_Write_Char(hundreds);
216 Lcd_Write_Char(tens);
217 Lcd_Write_Char(ones);
218
219 }
220 else
221 {
222 double_dabble_16_bit(&ones, &tens, &hundreds, &thousands, &tens_thousands, AC1);
223
224 Lcd_Write_Char(tens_thousands);
225 Lcd_Write_Char(thousands);
226 Lcd_Write_Char(hundreds);
227 Lcd_Write_Char(tens);
228 Lcd_Write_Char(ones);
229
230 }
231
232
233
234 /*
235 bmp180_compute_temp(&Temp_Decim, &Temp_Ones, &Temp_Tens);
236
237
238 Lcd_Set_Cursor(1,1);
239 Lcd_Write_String("T:");
240
241 Lcd_Set_Cursor(3,1);
242 Lcd_Write_Char(Temp_Tens);
243 Lcd_Write_Char(Temp_Ones);
244 Lcd_Write_Char('.');
245 Lcd_Write_Char(Temp_Decim);
246
247 Lcd_Set_Cursor(7,1);
248 Lcd_Write_String("C ");
249
250
251
252 //Lcd_Set_Cursor(1,2);
253 //Lcd_Write_String("P:");
254
255 //Lcd_Set_Cursor(3,2);
256 //Lcd_Write_Char(Sen_P_Tens);
257 //Lcd_Write_Char(Sen_P_Ones);
258 //Lcd_Write_Char('.');
259 //Lcd_Write_Char(Sen_P_Decim);
260
261 //Lcd_Set_Cursor(9,2);
262 //Lcd_Write_String("hPa ");
263 */
264
265 __delay_ms(3000);
266 }
267 }
268

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy