Guest User

Untitled

a guest
Oct 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 KB | None | 0 0
  1. #include <pic.h>
  2.  #include "Delay.h"
  3.  #include "math.h"
  4.  #include "string.h"
  5.  
  6.  __CONFIG(WDTDIS & INTCLK & PWRTDIS & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);
  7.  
  8.  //=============CONSTANTS===============================================
  9.  #define LCD_DATA PORTC
  10.  #define LCD_RS RA0
  11.  #define LCD_RW RA1
  12.  #define LCD_EN RA2
  13.  #define TEMP_PIN1 RA4
  14.  #define Vin 4.95
  15.  #define CHAR__ 0x20
  16.  #define CHAR_T 0x54
  17.  #define CHAR_H 0x48
  18.  #define CHAR_L 0x4C
  19.  #define CHAR_C 0x43
  20.  #define CHAR_I 0x49
  21.  #define CHAR_n 0x6E
  22.  #define CHAR_O 0x4F
  23.  #define CHAR_u 0x75
  24.  #define CHAR_t 0x74
  25.  #define CHAR_S1 0x3A
  26.  #define CHAR_S2 0xDF
  27.  #define CHAR_S3 0x2C
  28.  #define CHAR_1 0x31
  29.  #define CHAR_2 0x32
  30.  //=============MACROS==================================================
  31.  #define LCD_STROBE() ((LCD_EN = 1), DelayMs(1), (LCD_EN=0), DelayMs(1))
  32.  //=============PROCEDURES==============================================
  33.  void lcd_write_char(unsigned char);
  34.  void lcd_put_cursor(unsigned char);
  35.  void LCD_INIT(void);
  36.  void LCD_BACKGROUND(void);
  37.  void ADC_INIT(void);
  38.  void adc_read_temps(void);
  39.  //=====================================================================
  40.  
  41.  unsigned char LCD_BUFFER[17];  //lcd display buffer, 16 char long
  42.  const unsigned char str0[]="ADRESH=       "; //make sure that it doesn't overflow
  43.  
  44.  
  45.  void lcd_write_char(unsigned char c)
  46.  {LCD_RS=1;
  47.   LCD_EN=0;
  48.   LCD_DATA=c;
  49.   LCD_STROBE();
  50.   LCD_RS=0;
  51.  }
  52.  
  53.  int int_to_ASCII (int x)
  54.  {return (x + 48);}
  55.  /*
  56.  void lcd_write_int(int i)
  57.  {double edited_value=i;
  58.   int digits[8] = {0,0,0,0,0,0,0,0};
  59.   int write=0;
  60.  
  61.   for (int ctr=7;((edited_value>0) & (ctr>=0));ctr--)
  62.     {edited_value = edited_value/10.0;
  63.      digits[ctr]= (int)((fmod(edited_value,1)) * 10.0);
  64.     }
  65.   for (int ctr=0; ctr < 8; ctr++)
  66.     {if ((digits[ctr] != 0) | (write == 1))
  67.         {lcd_write_char(int_to_ASCII (digits[ctr]));
  68.          write = 1;}
  69.     }
  70.  }
  71. */
  72.  
  73. //display a string on lcd
  74. void lcd_write_str(unsigned char * str) {
  75.   while (*str) lcd_write_char(*str++);
  76. }
  77.  
  78. void ultoa(unsigned char * str, unsigned long ul, unsigned char length) {
  79.   do {
  80.     str[--length]=(ul % 10) + '0';  //convert the lowest digit to char
  81.     ul = ul / 10;  //move to the next digit
  82.   } while (length); //with leading errors
  83.   //} while (ul); //with leading blanks
  84. }
  85.  
  86.  
  87.  
  88. //===========================================================
  89. //00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-OF-10-11-12-13
  90. //40-41-42-43-44-45-46-47-48-49-4A-4B-4C-4D-4E-4F-50-51-52-53
  91. //14-15-16-17-18-19-1A-1B-1C-1D-1E-1F-20-21-22-23-24-25-26-27
  92. //54-55-56-57-58-59-5A-5B-5C-5D-5E-5F-60-61-62-63-64-65-66-67
  93. //===========================================================
  94.  void lcd_put_cursor(unsigned char c)
  95.  {LCD_DATA=c+128;       //add 10000000 to c
  96.   LCD_STROBE();
  97.  }
  98.  
  99.  void LCD_BACKGROUND(void)
  100.  {lcd_write_char(CHAR_H);
  101.   lcd_write_char(CHAR_S1);
  102.   lcd_put_cursor(0x08);
  103.   lcd_write_char(CHAR_S2);
  104.   lcd_write_char(CHAR_C);
  105.   lcd_put_cursor(0x0B);
  106.   lcd_write_char(CHAR_L);
  107.   lcd_write_char(CHAR_S1);
  108.   lcd_put_cursor(0x12);
  109.   lcd_write_char(CHAR_S2);
  110.   lcd_write_char(CHAR_C);
  111.  
  112.   lcd_put_cursor(0x40);
  113.   lcd_write_char(CHAR_I);
  114.   lcd_write_char(CHAR_n);
  115.   lcd_write_char(CHAR_1);
  116.   lcd_write_char(CHAR_S1);
  117.   lcd_put_cursor(0x48);
  118.   lcd_write_char(CHAR_S2);
  119.   lcd_write_char(CHAR_C);
  120.  
  121.   lcd_put_cursor(0x14);
  122.   lcd_write_char(CHAR_I);
  123.   lcd_write_char(CHAR_n);
  124.   lcd_write_char(CHAR_2);
  125.   lcd_write_char(CHAR_S1);
  126.   lcd_put_cursor(0x1C);
  127.   lcd_write_char(CHAR_S2);
  128.   lcd_write_char(CHAR_C);
  129.  
  130.   lcd_put_cursor(0x54);
  131.   lcd_write_char(CHAR_O);
  132.   lcd_write_char(CHAR_u);
  133.   lcd_write_char(CHAR_t);
  134.   lcd_write_char(CHAR_S1);
  135.   lcd_put_cursor(0x5C);
  136.   lcd_write_char(CHAR_S2);
  137.   lcd_write_char(CHAR_C);
  138.  }
  139.  
  140.  
  141. void ADC_INIT(void) {
  142.  
  143. ADON=0;  //turn the adc off
  144.  
  145. //Set up RA4 as the only analog input  
  146.     ANSEL |= 0b11111000;     //0b11111000;  
  147.     //ANSELH = 0;  
  148.     ANS3 = 1;            //AN3 (RA4) analog  
  149.     // Turn on ADC and use Fosc/8 as the conversion clock  
  150.     // (Fosc = 4MHz)  
  151.     //ADCON1 = 0b01010000;  
  152.     ADCS2=0, ADCS1=0, ADCS0=1;   //adc runs at Fosc/16
  153.     //Set channel for conversion to AN3 (on RA4)  
  154.     //ADCON0 = 0b00001101;  
  155.     ADFM = 0;    //left adjusted
  156.     VCFG = 0;    //Vdd as Vref
  157.     CHS3=0, CHS2=0, CHS1=1, CHS0=1; //0b0011 -> ANS3 as input
  158.     ADON = 1;    //turn the adc back on
  159.     DelayUs(10);  
  160. }  
  161.    
  162.  
  163. /*
  164. int bin2dec(char *bin)  
  165. {int b, k, m, n;
  166.  int len, sum = 0;
  167.  
  168.   len = strlen(bin) - 1;
  169.   for(k = 0; k <= len; k++)
  170.   {
  171.     n = (bin[k] - '0'); // char to numeric value
  172.     if ((n > 1) || (n < 0))
  173.     {return (0);}
  174.     for(b = 1, m = len; m > k; m--)
  175.     {
  176.       // 1 2 4 8 16 32 64 ... place-values, reversed here
  177.       b *= 2;
  178.     }
  179.     // sum it up
  180.     sum = sum + n * b;
  181.     //printf("%d*%d + ",n,b);  // uncomment to show the way this works
  182.   }
  183.   return(sum);  
  184. }
  185. */
  186.    
  187. unsigned long adc_read(void)
  188.    {GODONE = 1;  
  189.     while(GODONE) continue;  
  190.    
  191.     //return ((((int) (ADRESH & 0x03)) * 256) + ((int) ADRESL));
  192.     ADRESH = 0b11111111;
  193.     return ADRESH;
  194.     }
  195.  
  196.  
  197.  
  198.  
  199.  void LCD_INIT(void)
  200.  {//ADCON0=0; //A2D Off
  201.   TRISC=0;
  202.   TRISA=0b11111000; //Set RA0-RA2 as outputs and RA4 input
  203.   LCD_RS=0;
  204.   LCD_RW=0; //Permanantly Grounded
  205.   LCD_EN=0;
  206.  
  207.   DelayMs(60); //Wait for display to pwr up
  208.   LCD_DATA = 0b00110000; //Initial Value
  209.   LCD_STROBE();
  210.   DelayMs(10);
  211.   LCD_STROBE();
  212.   DelayUs(125);
  213.   LCD_STROBE();
  214.   DelayMs(10);
  215.  
  216.   LCD_DATA = 0b00111000;
  217.   LCD_STROBE();
  218.  
  219.   lcd_write_char(0b00001100);
  220.   LCD_STROBE();
  221.   DelayUs(125);
  222.   lcd_write_char(0b00000001);
  223.   LCD_STROBE();
  224.   DelayMs(5);
  225.   lcd_write_char(0b00000110);
  226.   LCD_STROBE();
  227.   DelayUs(125);
  228.  
  229.   ANSEL = 0;
  230.  }
  231.  
  232.  main(void) {
  233.  LCD_INIT();
  234.  LCD_BACKGROUND();
  235.  ADC_INIT();
  236.  for (;;)
  237.     {DelayMs(1000);
  238.      DelayMs(1000);
  239.      lcd_put_cursor(0x4B);
  240.      strcpy(LCD_BUFFER, str0); //copy str0 to _lcd_buffer
  241.      ultoa(&LCD_BUFFER[7], adc_read(), 8);  //convert the variable to the asci string
  242.      lcd_write_str(LCD_BUFFER);
  243.      }
  244.  }
Add Comment
Please, Sign In to add comment