yash_01
Newbie
- Joined
- Dec 26, 2024
- Messages
- 4
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 30
Missing CODE or SYNTAX Tags: ADDED BY MODERATOR
i wanted to create a project using at89s52 for distance of an object measurement using ultrasonic sensor. so i wrote a code using keil and tried the simulation using proteus, i am getting "distance" as the output but the distance value isnt shown. what is wrong in the code?? my code is:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 #include <reg51.h> #define lcd_data P2 sbit rs=P1^0; //reg select sbit rw=P1^1; //rewrite sbit en=P1^2; //enable sbit trig=P1^3; sbit echo=P1^4; char distance; char time; int i=0,j=0; char str[4]; void lcd_init(); void cmd(unsigned char a); void dat(unsigned char b); void show(unsigned char *s); void lcd_delay(); void lcd_init() { cmd(0x38); //defining is first, the peripherals and the line cmd(0x0e); cmd(0x01); cmd(0x06); //entry mode - after refreshing we're ready to use it cmd(0x0c); //defining cursor cmd(0x80); // start from the first row first column } void cmd(unsigned char a) { lcd_data=a; rs=0; //registers select rw=0; // write mode en=1; //enabling writing to the registers, power should be on when in use lcd_delay(); en=0; } void dat(unsigned char b) { lcd_data=b; rs=1; rw=0; en=1; lcd_delay(); en=0; } void show(unsigned char *s) { while(*s) { dat(*s++); } } void lcd_delay(){ int i; for (i=0;i<10000;i++){ } } void send_pulse(){ trig=1; TMOD = 0x01; TH0 = 0xff; TL0 = 0xf6; TR0 = 1; while (TF0 == 0); TR0 = 0; TF0 = 0; trig=0; } unsigned char ultrasonic(){ //11.3m TMOD = 0x01; TH0 = 0x00; TL0 = 0x00; TR0=1; send_pulse(); while (!echo); while (echo); TR0=0; time=(TH0 << 8) | TL0; return time/58; } void int_to_string(unsigned int num, unsigned char *buf) { int i = 0, j; unsigned char temp[4]; // Convert each digit to a character if (num == 0) { buf[i++] = '0'; } else { while (num > 0) { temp[i++] = (num % 10) + '0'; num /= 10; } } // Reverse the order for (j = 0; j < i; j++) { buf[j] = temp[i - j - 1]; } buf[I] = '\0'; // Null-terminate the string } void main(){ lcd_init(); cmd(0x80); show("Distance"); while(1){ distance=ultrasonic(); int_to_string(distance, str); cmd(0xc0); show (str); lcd_delay(); cmd(0x01); lcd_delay(); } }[/I]
Last edited by a moderator: