Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pic.h>
- #include "Delay.h"
- #include "math.h"
- #include "string.h"
- __CONFIG(WDTDIS & INTCLK & PWRTDIS & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);
- //=============CONSTANTS===============================================
- #define LCD_DATA PORTC
- #define LCD_RS RA0
- #define LCD_RW RA1
- #define LCD_EN RA2
- #define TEMP_PIN1 RA4
- #define Vin 4.95
- #define CHAR__ 0x20
- #define CHAR_T 0x54
- #define CHAR_H 0x48
- #define CHAR_L 0x4C
- #define CHAR_C 0x43
- #define CHAR_I 0x49
- #define CHAR_n 0x6E
- #define CHAR_O 0x4F
- #define CHAR_u 0x75
- #define CHAR_t 0x74
- #define CHAR_S1 0x3A
- #define CHAR_S2 0xDF
- #define CHAR_S3 0x2C
- #define CHAR_1 0x31
- #define CHAR_2 0x32
- //=============MACROS==================================================
- #define LCD_STROBE() ((LCD_EN = 1), DelayMs(1), (LCD_EN=0), DelayMs(1))
- //=============PROCEDURES==============================================
- void lcd_write_char(unsigned char);
- void lcd_put_cursor(unsigned char);
- void LCD_INIT(void);
- void LCD_BACKGROUND(void);
- void ADC_INIT(void);
- void adc_read_temps(void);
- //=====================================================================
- unsigned char LCD_BUFFER[17]; //lcd display buffer, 16 char long
- const unsigned char str0[]="ADRESH= "; //make sure that it doesn't overflow
- void lcd_write_char(unsigned char c)
- {LCD_RS=1;
- LCD_EN=0;
- LCD_DATA=c;
- LCD_STROBE();
- LCD_RS=0;
- }
- int int_to_ASCII (int x)
- {return (x + 48);}
- /*
- void lcd_write_int(int i)
- {double edited_value=i;
- int digits[8] = {0,0,0,0,0,0,0,0};
- int write=0;
- for (int ctr=7;((edited_value>0) & (ctr>=0));ctr--)
- {edited_value = edited_value/10.0;
- digits[ctr]= (int)((fmod(edited_value,1)) * 10.0);
- }
- for (int ctr=0; ctr < 8; ctr++)
- {if ((digits[ctr] != 0) | (write == 1))
- {lcd_write_char(int_to_ASCII (digits[ctr]));
- write = 1;}
- }
- }
- */
- //display a string on lcd
- void lcd_write_str(unsigned char * str) {
- while (*str) lcd_write_char(*str++);
- }
- void ultoa(unsigned char * str, unsigned long ul, unsigned char length) {
- do {
- str[--length]=(ul % 10) + '0'; //convert the lowest digit to char
- ul = ul / 10; //move to the next digit
- } while (length); //with leading errors
- //} while (ul); //with leading blanks
- }
- //===========================================================
- //00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-OF-10-11-12-13
- //40-41-42-43-44-45-46-47-48-49-4A-4B-4C-4D-4E-4F-50-51-52-53
- //14-15-16-17-18-19-1A-1B-1C-1D-1E-1F-20-21-22-23-24-25-26-27
- //54-55-56-57-58-59-5A-5B-5C-5D-5E-5F-60-61-62-63-64-65-66-67
- //===========================================================
- void lcd_put_cursor(unsigned char c)
- {LCD_DATA=c+128; //add 10000000 to c
- LCD_STROBE();
- }
- void LCD_BACKGROUND(void)
- {lcd_write_char(CHAR_H);
- lcd_write_char(CHAR_S1);
- lcd_put_cursor(0x08);
- lcd_write_char(CHAR_S2);
- lcd_write_char(CHAR_C);
- lcd_put_cursor(0x0B);
- lcd_write_char(CHAR_L);
- lcd_write_char(CHAR_S1);
- lcd_put_cursor(0x12);
- lcd_write_char(CHAR_S2);
- lcd_write_char(CHAR_C);
- lcd_put_cursor(0x40);
- lcd_write_char(CHAR_I);
- lcd_write_char(CHAR_n);
- lcd_write_char(CHAR_1);
- lcd_write_char(CHAR_S1);
- lcd_put_cursor(0x48);
- lcd_write_char(CHAR_S2);
- lcd_write_char(CHAR_C);
- lcd_put_cursor(0x14);
- lcd_write_char(CHAR_I);
- lcd_write_char(CHAR_n);
- lcd_write_char(CHAR_2);
- lcd_write_char(CHAR_S1);
- lcd_put_cursor(0x1C);
- lcd_write_char(CHAR_S2);
- lcd_write_char(CHAR_C);
- lcd_put_cursor(0x54);
- lcd_write_char(CHAR_O);
- lcd_write_char(CHAR_u);
- lcd_write_char(CHAR_t);
- lcd_write_char(CHAR_S1);
- lcd_put_cursor(0x5C);
- lcd_write_char(CHAR_S2);
- lcd_write_char(CHAR_C);
- }
- void ADC_INIT(void) {
- ADON=0; //turn the adc off
- //Set up RA4 as the only analog input
- ANSEL |= 0b11111000; //0b11111000;
- //ANSELH = 0;
- ANS3 = 1; //AN3 (RA4) analog
- // Turn on ADC and use Fosc/8 as the conversion clock
- // (Fosc = 4MHz)
- //ADCON1 = 0b01010000;
- ADCS2=0, ADCS1=0, ADCS0=1; //adc runs at Fosc/16
- //Set channel for conversion to AN3 (on RA4)
- //ADCON0 = 0b00001101;
- ADFM = 0; //left adjusted
- VCFG = 0; //Vdd as Vref
- CHS3=0, CHS2=0, CHS1=1, CHS0=1; //0b0011 -> ANS3 as input
- ADON = 1; //turn the adc back on
- DelayUs(10);
- }
- /*
- int bin2dec(char *bin)
- {int b, k, m, n;
- int len, sum = 0;
- len = strlen(bin) - 1;
- for(k = 0; k <= len; k++)
- {
- n = (bin[k] - '0'); // char to numeric value
- if ((n > 1) || (n < 0))
- {return (0);}
- for(b = 1, m = len; m > k; m--)
- {
- // 1 2 4 8 16 32 64 ... place-values, reversed here
- b *= 2;
- }
- // sum it up
- sum = sum + n * b;
- //printf("%d*%d + ",n,b); // uncomment to show the way this works
- }
- return(sum);
- }
- */
- unsigned long adc_read(void)
- {GODONE = 1;
- while(GODONE) continue;
- //return ((((int) (ADRESH & 0x03)) * 256) + ((int) ADRESL));
- ADRESH = 0b11111111;
- return ADRESH;
- }
- void LCD_INIT(void)
- {//ADCON0=0; //A2D Off
- TRISC=0;
- TRISA=0b11111000; //Set RA0-RA2 as outputs and RA4 input
- LCD_RS=0;
- LCD_RW=0; //Permanantly Grounded
- LCD_EN=0;
- DelayMs(60); //Wait for display to pwr up
- LCD_DATA = 0b00110000; //Initial Value
- LCD_STROBE();
- DelayMs(10);
- LCD_STROBE();
- DelayUs(125);
- LCD_STROBE();
- DelayMs(10);
- LCD_DATA = 0b00111000;
- LCD_STROBE();
- lcd_write_char(0b00001100);
- LCD_STROBE();
- DelayUs(125);
- lcd_write_char(0b00000001);
- LCD_STROBE();
- DelayMs(5);
- lcd_write_char(0b00000110);
- LCD_STROBE();
- DelayUs(125);
- ANSEL = 0;
- }
- main(void) {
- LCD_INIT();
- LCD_BACKGROUND();
- ADC_INIT();
- for (;;)
- {DelayMs(1000);
- DelayMs(1000);
- lcd_put_cursor(0x4B);
- strcpy(LCD_BUFFER, str0); //copy str0 to _lcd_buffer
- ultoa(&LCD_BUFFER[7], adc_read(), 8); //convert the variable to the asci string
- lcd_write_str(LCD_BUFFER);
- }
- }
Add Comment
Please, Sign In to add comment