/* * LCD/DS1620 * Uses routines from delay.c * This code will interface to a standard LCD controller * like the Hitachi HD44780. It uses it in 4 bit mode, with * the hardware connected as follows (the standard 14 pin * LCD connector is used): * * LCD connections as follows: * R/W = GND * PORTB bit 4 = RS (output) * PORTB bit 5 = EN (output) * * PORTB bit 0 = D4 (output) * PORTB bit 1 = D5 (output) * PORTB bit 2 = D6 (output) * PORTB bit 3 = D7 (output) * * DS1620 connections as follows: * PORTC bit 4 = DQ * PORTC bit 5 = CLK * PORTC bit 6 = RST * * Humidity sensor connections as follows: * PORTA bit 3 * PORTC bit 2 * PORTC bit 3 dummy read * */ #include #include #include "lcd.h" #include "delay.h" #include "adc.h" static bit HUMIDITY @ ((unsigned)&PORTC*8+2); // Humidity select static bit DUMMY @ ((unsigned)&PORTC*8+3); // Dummy read static bit DS1620_DQ @ ((unsigned)&PORTC*8+4); // DS1620 DQ static bit DS1620_CLK @ ((unsigned)&PORTC*8+5); // DS1620 CLK static bit DS1620_RST @ ((unsigned)&PORTC*8+6); // DS1620 RST static bit LCD_RS @ ((unsigned)&PORTB*8+4); // Register select static bit LCD_EN @ ((unsigned)&PORTB*8+5); // Enable #define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0)) int tens(float); /* write a byte to the LCD in 4 bit mode */ void lcd_write(unsigned char c) { TRISB = 0x0; //Make PORT B output PORTB = (PORTB & 0xF0) | (c >> 4); LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); LCD_STROBE; DelayUs(40); } /* Clear and home the LCD */ void lcd_clear(void) { TRISB = 0x0; //Make bit 0 to 7 output LCD_RS = 0; lcd_write(0x1); DelayMs(2); } /* cursor at home */ void lcd_home(void) { TRISB = 0x0; //Make bit 0 to 7 output LCD_RS = 0; lcd_write(0x2); DelayMs(2); } /* write a string of chars to the LCD */ void lcd_puts(const char *s) { TRISB = 0x0; //Make bit 0 to 7 output LCD_RS = 1; // write characters while(*s) lcd_write(*s++); } /* write one character to the LCD */ void lcd_putch(char c) { TRISB = 0x0; //Make bit 0 to 7 output LCD_RS = 1; // write characters PORTB = (PORTB & 0xF0) | (c >> 4); LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); LCD_STROBE; DelayUs(40); } /* Go to the specified position */ void lcd_goto(unsigned char pos) { TRISB = 0x0; //Make bit 0 to 7 output LCD_RS = 0; lcd_write(0x80+pos); } /* initialise the LCD - put into 4 bit mode */ void lcd_init(void) { TRISB = 0x0; //Make bit 0 to 7 output LCD_RS = 0; // write control bytes DelayMs(15); // power on delay PORTB = 0x3; // display & cursor home LCD_STROBE; DelayMs(5); LCD_STROBE; DelayUs(100); LCD_STROBE; DelayMs(5); PORTB = 0x2; // set 4 bit mode LCD_STROBE; DelayUs(40); // lcd_write(0x2f); // 4 bit mode, 2 line mode, 5x10 font lcd_write(0x28); // 4 bit mode, 2 line mode, 5x8 font lcd_write(0x08); // display off // lcd_write(0x0F); // display on, underline on, blink curson on lcd_write(0x0C); // display on, underline off, blink curson off lcd_write(0x06); // entry mode } /************************************************************* /* Get Temp from DS1620 *************************************************************/ unsigned char Get1620byte(void) { unsigned char j,k=0,b=1; k=0; b=1; TRISC = 0x10; // Make bit 0 to 3 output bit 4 input bit 5 to 7 output for (j=0; j < 8; j++) { DS1620_CLK = 0; if (DS1620_DQ) k|=b; DS1620_CLK = 1; b = (b << 1); } return k; } /************************************************************* /* Put temp from DS1620 *************************************************************/ void Put1620byte(char m) { char k,b=1; TRISC = 0x0; // Make bit 0 to 7 output DS1620_RST = 1; for (k = 0; k < 8; k++) { DS1620_CLK = 0; if ((m & b) > 0) { DS1620_DQ = 1; } else { DS1620_DQ = 0; } DS1620_CLK = 1; b = (b<<1); } return; } void init_1620(void) { int k; /************************************************************* * Setup to get Temp *************************************************************/ DS1620_RST = 0; DS1620_RST = 1; Put1620byte(0xAC); // Read config register k = Get1620byte(); // Get config register data DS1620_RST = 0; if ((k & 0x18) != 0x08) { lcd_goto(0); DelayMs(15); lcd_puts("No DS1620 "); } } void adc_read_dummy(void) { int humidity_h, humidity_l; ADCON1=128; DUMMY = 1; adc_read(3); // read channel 3 humidity_h = ADRESH; humidity_l = ADRESL; DUMMY = 0; ADCON1=7; // Set all the input/output to digital } void adc_read_hummidity(void) { int humidity_h, humidity_l; ADCON1=128; HUMIDITY = 1; adc_read(3); // read channel 3 humidity_h = ADRESH; humidity_l = ADRESL; HUMIDITY = 0; ADCON1=7; // Set all the input/output to digital } int calculate_tens(float value) { int tens = 0; /************************************************************* /* Calculate tens *************************************************************/ do { if (value > 10) { value = value - 10; tens = tens + 1; } else { lcd_puts(""); } }while(value > 10); return tens; } int calculate_ones(float value) { int ones = 0; /************************************************************* /* Calculate ones from temp_c *************************************************************/ do { if (value > 1) { value = value - 1; ones = ones + 1; } else { lcd_puts(""); } }while(value > 1); return ones; } int calculate_one_tens(float value) { int one_tens = 0; /************************************************************* /* Calculate 1/10s from temp_c *************************************************************/ do { if (value > 0.10) { value = value - 0.10; one_tens = one_tens + 1; } else { lcd_puts(""); } }while(value > 0.10); return one_tens; } int calculate_one_hundreds(float value) { int one_hundreds = 0; /************************************************************* /* Calculate 1/100s from temp_c *************************************************************/ do { if (value > 0.01) { value = value - 0.01; one_hundreds = one_hundreds + 1; } else { lcd_puts(""); } }while(value > 0.01); return one_hundreds; } main() { unsigned char k, sign_bit; double temp_and_half_bit, humidity_value; float temp_c, temp_read; unsigned int count_remain, count_per_c, humidity; int t, humidity_l; ADCON1=7; // Set all the input/output to digital ADCON0=24; TRISA = 0x08; // Make bit 0 to 2 output bit 3 input 4 to 7 output TRISB = 0x0; // Make bit 0 to 7 output TRISC = 0x0; // Make bit 0 to 7 output PORTA=0; PORTB=0; PORTC=0; lcd_init(); //DelayMs(10); lcd_clear(); //DelayMs(10); lcd_puts("Kriss_Tall"); //for(k =1 ;k++;k<100){DelayMs(50);}; lcd_clear; DelayMs(10); lcd_goto(0); lcd_puts("Temp = "); lcd_goto(40); lcd_puts("Humidity = "); while(1){ adc_read_dummy(); adc_read_hummidity(); init_1620(); /************************************************************* * Set to CPU & 1 shot *************************************************************/ DS1620_RST = 1; Put1620byte(0x0C); Put1620byte(0x03); // Set to CPU an 1Shot DS1620_RST = 0; /************************************************************* * Stop convert (ie reset) *************************************************************/ DS1620_RST = 1; Put1620byte(0x22); // Stop convert DS1620_RST = 0; /************************************************************* * Start convert and wait to finish *************************************************************/ DelayMs(100); DS1620_RST = 1; Put1620byte(0xEE); // Start temp convert DS1620_RST = 0; do { DS1620_RST = 1; Put1620byte(0xAC); // Open status register k = Get1620byte(); // Get status byte DS1620_RST = 0; lcd_goto(0); DelayMs(15); //lcd_puts("Waiting "); }while ((k & 0x80) != 0x80); // changed 0x80 to 0x00 wait for Done bit /************************************************************* * Read Temp and sign bit *************************************************************/ DS1620_RST = 1; Put1620byte(0xAA); // Read temp command temp_and_half_bit = Get1620byte(); // Get 1st byte of temp sign_bit = Get1620byte(); // read 2nd byte of temp DS1620_RST = 0; /************************************************************* * Get count remain & count per C for .1 resolution *************************************************************/ DS1620_RST = 1; Put1620byte(0xA0); // Read count remaining count_remain = Get1620byte(); // Read 1st byte count_remain += Get1620byte() * 256; // read 2nd byte DS1620_RST = 0; DS1620_RST = 1; Put1620byte(0x41); // load slope into counter DS1620_RST = 0; DS1620_RST = 1; Put1620byte(0xA0); // Read slope as count/C count_per_c = Get1620byte(); // Read 1st byte count_per_c += Get1620byte() * 256; // Read 2nd byte DS1620_RST = 0; /************************************************************* * Calculate C *************************************************************/ if (count_per_c == 0) count_per_c = 1; temp_read = (temp_and_half_bit/2); if (sign_bit != 0) temp_read = (temp_read - 128); temp_c = (float)temp_read - 0.25 + ((count_per_c - count_remain) / (float)count_per_c); lcd_goto(8); if (sign_bit == 0) { lcd_puts("+"); } else { lcd_puts("-"); } t = calculate_tens(temp_c); temp_c = temp_c - (10 * t); lcd_write(t + 48); t = calculate_ones(temp_c); temp_c = temp_c - (1 * t); lcd_write(t + 48); lcd_puts(","); t = calculate_one_tens(temp_c); temp_c = temp_c - (0.10 * t); lcd_write(t + 48); t =calculate_one_hundreds(temp_c); temp_c = temp_c - (0.01 * t); lcd_write(t + 48); DelayMs(10); lcd_write(223); lcd_puts("C"); DelayMs(100); lcd_goto(51); humidity_l = ADRESL; humidity = calculate_tens(humidity_l); humidity_l = humidity_l - (10 * humidity); lcd_write(humidity + 48); humidity = calculate_ones(humidity_l); humidity_l = humidity_l - (1 * humidity); lcd_write(humidity + 48); humidity = calculate_one_tens(humidity_l); humidity_l = humidity_l - (0.10 * humidity); lcd_write(humidity + 48); humidity = calculate_one_hundreds(humidity_l); humidity_l = humidity_l - (0.01 * humidity); lcd_write(humidity + 48); } }