EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / code is being executed in proteus successfully but not with hardware

code is being executed in proteus successfully but not with hardware

|

Microcontroller › PIC › code is being executed in proteus successfully but not with hardware

  • This topic has 3 replies, 4 voices, and was last updated 10 years, 6 months ago by neha.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • October 4, 2015 at 2:51 pm #4032
    BHARATH.S.HEGDE
    Participant

    I have done program for voltage measurement system to send the measured voltage to PC using ADC and serial communication, The code is being exected in the proteus but not in the hardware.

     

    #include<htc.h>
    #include<PIC16F877A.h>
    #define rs PORTDbits.RD6
    #define rw PORTBbits.RB1
    #define en PORTDbits.RD7
     
    #define lcd_data PORTB
     
    typedef unsigned char uc;
    typedef unsigned int ui;
     
    uc digits[10]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
     
    void lcd_init();
    void lcd_cmd(uc cmd);
    void lcd_display(uc ldata);
    void delay(unsigned int time);
    void UART_init();
    void UART_transmit(uc tx_data);
    void ADC_init();
    long ADC_value();
     
    long hex_2_dec(long hex);
    long calculate_voltage(long dec_value);
    void extract_voltage_digits(long volt_value);
     
     
    int main()
    {
      char addr;
      char *config2h;
      ui n;
      uc port_value;
      long decimal;
      long hexadecimal=0x0000;
      long dec=0;
      long voltage=0;
      
      config2h=(char*)0x2007;
      *config2h=0x3F3A;
      
      lcd_init();
      
       TRISD=0x00;
       TRISB=0x00;
       TRISC=0x00;
       port_value=0x01;
     
       for(n=0;n<7;n++)
       {
          PORTC=port_value;   
         
          if((port_value!=0x40) ||(port_value!=0x80) )
          {
             ADC_init();
             hexadecimal=ADC_value();
             decimal=hex_2_dec(hexadecimal);
             voltage=calculate_voltage(decimal);
             UART_init();
             extract_voltage_digits(voltage);      
          }
     
      
          port_value=(port_value<<1);
     
        }
        
             port_value=0x01;
     
        for(n=0;n<=5;n++)
        {
           PORTD=port_value;
             ADC_init();
             hexadecimal=ADC_value();
             decimal=hex_2_dec(hexadecimal);
             voltage=calculate_voltage(decimal);
             UART_init();
             extract_voltage_digits(voltage);
             port_value=(port_value<<1);
        }     
     
     
       while(1);
         
            
        
      /*   for(n=6;n<=0;n–)
         {
            UART_transmit(digits[n]); 
              lcd_display('w');
            //UART_transmit('n');
         } */
     
       
         
      
    }
     
     
     
    //lcd initialisation //
    void lcd_init()
    {
    lcd_cmd(0x0e); //lcd on, cursor on
    delay(1);
    lcd_cmd(0x38); //8 bit initialize, 5×7 character font, 16×2 display
    delay(1);
    lcd_cmd(0x06); //right shift cursor automatically after each character is displayed
    delay(1);
    lcd_cmd(0x01); //clear lcd
    delay(1);
    lcd_cmd(0x80);
    }
     
     
    // sending commands to the lcd//
     
    void lcd_cmd(unsigned char cmd)
    {
    en=1;            //set enable pin
    rw=0;       
    rs=0;          //clear register select pin
    lcd_data=cmd;    //load 8 bit data
    delay(1);         //delay 2 milliseconds
    en=0;             //clear enable pin
    }                
     
     
    void lcd_display(uc ldata)
    {
         en=1;
         rs=1; 
             lcd_data=ldata;
             delay(1);
             en=0;
    }
     
    // UART initialization //
    void UART_init()
    {
        RCSTAbits.SPEN=1;
        TXSTA=0X20;
    SPBRG=15;
        PIE1bits.TXIE==0;
        PIR1bits.TXIF=0;
    }
     
    void UART_transmit(uc tx_data)
    {
            TXREG=tx_data;
            while(PIR1bits.TXIF==0);
            PIR1bits.TXIF=0;   
    }
     
    // ADC initialization //
     
    void ADC_init()
    {
       TRISAbits.TRISA0=1;
       ADCON0=0x81;
       ADCON1=0xC0;
    }
     
    // ADC process //
     
    long ADC_value()
    {
       long msb, lsb, adc_value;
     
        PIR1bits.ADIF=0;
        PIE1bits.ADIE=1;
           
           delay(30);
           ADCON0bits.GO=1;
           while(ADCON0bits.GO==1);
           
        msb=ADRESH; 
        msb=(msb<<8); 
        lsb=ADRESL;
        delay(2);
     
        adc_value=(msb | lsb);
        
        return(adc_value);
    }
     
    //hex to decimal conversion//
     
    long  hex_2_dec(long hex)
    {
      long  d0,d1,d2,d3, dec;
      
     
        d0=hex%10;
                    //d0=d0+0x30;
      hex=hex/10;
     
    d1=hex%10;
                    //d1=d1+0x30;
        d1=d1*10;
     
    hex=hex/10;
     
    d2=hex%10;
                    //d2=d2+0x30;
    d2=d2*100;
        
        hex=hex/10;
     
        d3=hex;
                   //d3=d3+0x30;
        d3=d3*1000;
    dec=d0+d1+d2+d3;
     
    return(dec);
    }
     
    // voltage calculation //
     
    long calculate_voltage(long dec_value)
    {
      long volt;
      
         volt=(dec_value*488);
         
       return(volt);  
    }
     
    // extraction of digits //
    void extract_voltage_digits(long volt_value)
    {
      uc d00,d01,d02,d03,d04,d05;
      ui i=0;
     
      if(volt_value<100000)
         {
                d00=volt_value%10;
           d00=d00+0x30;
           digits=d00;
           i++;
           
                 volt_value=volt_value/10;
            d01=volt_value%10;
            d01=d01+0x30;        
            digits=d01;
            i++;
     
                  volt_value=volt_value/10;
             d02=volt_value%10;
             d02=d02+0x30;         
             digits=d02;
             i++;                
     
                  volt_value=volt_value/10;
             d03=volt_value%10;
             d03=d03+0x30;   
             digits=d03;
             i++;
             
                  volt_value=volt_value/10;
              d04=volt_value;
                d04=d04+0x30;
             digits=d04;
             i++;
                  
                  digits[5]='.';
                  digits[6]=0x30;
                 
                 UART_transmit(0x30);
                 UART_transmit('.');
    UART_transmit(d04);
    UART_transmit(d03);
                UART_transmit(d02);
            UART_transmit(d01);
                 UART_transmit(d00);
                UART_transmit(0x0D);
                 
           } 
           
           else
      {
                 d00=volt_value%10;
             d00=d00+0x30;
            digits=d00;
            i++;
                    
                 volt_value=volt_value/10;
            d01=volt_value%10;
            d01=d01+0x30;
           digits=d01;
           i++;         
                     
                  volt_value=volt_value/10;
             d02=volt_value%10;
             d02=d02+0x30;
            digits=d02;
             i++;                    
                 
                 volt_value=volt_value/10;
             d03=volt_value%10;
             d03=d03+0x30;
            digits=d03;
            i++;          
                       
                  volt_value=volt_value/10;
              d04=volt_value%10;
               d04=d04+0x30;
             digits=d04;
             i++;
               
                  digits='.';
             i++;
     
                  volt_value=volt_value/10;
      d05=volt_value;
      d05=d05+0x30;
             digits=d05;
             i++;   
     
     
                UART_transmit(d05);
                 UART_transmit('.');
    UART_transmit(d04);
    UART_transmit(d03);
                UART_transmit(d02);
            UART_transmit(d01);
                 UART_transmit(d00);
                 UART_transmit(0x0D);
       }   
    }
     
     
    //delay function//
    void delay(unsigned int time)
    {
      int i,j;
       for(i=0;i<time;i++)
         for(j=0;j<1275;j++);
    }          
    October 5, 2015 at 12:13 pm #13354
    Prabakaran P M
    Participant

    Hi,

    Projects working in proteus will work in hardware surely, check the hardware connections, Crystal configuration in config bits.

    October 30, 2015 at 11:26 pm #13435
    Ashutosh Bhatt
    Participant

    u hv to set same baud rate at both side. 

    may be u hv to do some settings in your PC

    October 31, 2015 at 6:11 pm #13439
    neha
    Participant

    use PORTB for RS/RW/E

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Phone Charger 5v to 12v May 14, 2026
  • Difference between TTL, RS232 and RS485 May 14, 2026
  • Wall Radiator Heating: Efficiency and Placement Questions May 14, 2026
  • Assistance locating a 'trail' camera gadget, please ? May 14, 2026
  • Projector focus circuit May 14, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise