Microcontroller › PIC › code is being executed in proteus successfully but not with hardware
- This topic has 3 replies, 4 voices, and was last updated 8 years, 10 months ago by neha.
Viewing 4 posts - 1 through 4 (of 4 total)
-
AuthorPosts
-
October 4, 2015 at 2:51 pm #4032BHARATH.S.HEGDEParticipant
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 PORTBtypedef 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 ondelay(1);lcd_cmd(0x38); //8 bit initialize, 5×7 character font, 16×2 displaydelay(1);lcd_cmd(0x06); //right shift cursor automatically after each character is displayeddelay(1);lcd_cmd(0x01); //clear lcddelay(1);lcd_cmd(0x80);}// sending commands to the lcd//void lcd_cmd(unsigned char cmd){en=1; //set enable pinrw=0;rs=0; //clear register select pinlcd_data=cmd; //load 8 bit datadelay(1); //delay 2 millisecondsen=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<<;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 #13354Prabakaran P MParticipantHi,
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 #13435Ashutosh BhattParticipantu 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 #13439nehaParticipantuse PORTB for RS/RW/E
-
AuthorPosts
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.