Advertisement
SwarupSaha

Digital Volt Meter

Feb 27th, 2016
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <htc.h>
  2. #define data PORTB
  3. #define RS RD0
  4. #define EN RD1
  5.  
  6. __CONFIG(HS & WDTDIS & PWRTEN & BORDIS & LVPDIS);
  7.  
  8. float valch0,valch1;
  9.  
  10. void delay_ms(int n )
  11. {
  12.     TMR1H=0xEC;
  13.  TMR1L=0x77;
  14.  T1CKPS1=0;
  15.  T1CKPS0=0;
  16.  TMR1CS=0;
  17.  TMR1IF=0;
  18.  TMR1ON=1;
  19.     while(n>0)
  20.     {
  21.   while(!TMR1IF);
  22.   TMR1IF=0;
  23.   TMR1H=0xEC;
  24.   TMR1L=0x77;
  25.   n--;
  26.  }
  27. }
  28.  
  29. float select_adc(unsigned char chnl){    // init_adc(channel name);
  30.     CHS2=CHS1=0;CHS0=chnl;
  31.     delay_ms(10);
  32.     ADON=1;
  33.     ADGO=1;
  34.     while(ADGO);
  35.     return (((ADRESH*256+ADRESL)*5.0/1023)*10);
  36. }
  37.  
  38.  
  39.  
  40.  
  41. void LCD_Write(unsigned char values,int rs){
  42.  data = values;
  43.  RS = rs;    // rs=0 command and rs=1 data
  44.  EN = 1;
  45.  delay_ms(1);
  46.  EN = 0;
  47. }
  48.  
  49. void LCD_clear(void){
  50.  LCD_Write(0x01,0);  //this clears LCD
  51. }
  52.  
  53. void LCD_goto(unsigned char row,unsigned char column){
  54.  if(row==1){
  55.   LCD_Write(0x80+column,0);
  56.  }
  57.  else if(row==2){
  58.   LCD_Write(0xC0+column,0);
  59.  }
  60. }
  61.  
  62. void LCD_num(int n){
  63.  
  64.   LCD_Write((n/100)+48,1);
  65.   LCD_Write(((n%100)/10)+48,1);
  66.   LCD_Write((n%10)+48,1);
  67. }
  68.  
  69. void initLCD(void){
  70.  
  71.  TRISD=0x00;//as output
  72.  TRISB = 0x00;
  73.  delay_ms(100);
  74.  
  75.  
  76.  LCD_Write(0x30, 0); //8 - bit display
  77.  EN=1; EN = 0;
  78.  LCD_Write(0x38,0);    //2 lines mode
  79.  
  80.  
  81.  
  82.  LCD_Write(0x0C,0);    //dispaly on , cursor blinking
  83.  delay_ms(1);
  84.  
  85.  LCD_clear();LCD_goto(1,0);delay_ms(1);
  86.  LCD_goto(1,7);
  87.  LCD_Write('D',1);delay_ms(100);
  88.  LCD_Write('V',1);delay_ms(100);
  89.  LCD_Write('M',1);delay_ms(100);
  90.  LCD_goto(2,12);
  91.  LCD_Write('v',1);delay_ms(100);
  92.  LCD_goto(2,7);
  93.  LCD_Write('.',1);delay_ms(100);
  94.  
  95. }
  96.  
  97. void main(void){
  98.     PCFG3=PCFG2=PCFG1=PCFG0=0;
  99.  ADFM=1;
  100.  ADCS1=1;
  101.  ADCS0=0;
  102.     TRISA=0xFF;
  103.  initLCD();
  104.  
  105.     while(1){
  106.  
  107.         valch0=select_adc(0);
  108.         valch1=select_adc(1);
  109.  
  110.  
  111.         if(valch0 <= 0.00 && valch1!=0){
  112.             valch0=valch1;
  113.             LCD_goto(2,1);
  114.             LCD_Write('-',1);
  115.             delay_ms(5);
  116.         }
  117.  
  118.         else{
  119.             LCD_goto(2,1);
  120.             LCD_Write('+',1);
  121.             delay_ms(5);
  122.         }
  123.  
  124.  
  125.         LCD_goto(2,4);
  126.         LCD_num(valch0);
  127.         LCD_goto(2,8);
  128.         LCD_num((valch0-(int)valch0)*1000);
  129.  
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement