الاخوه الكرام
الكود التالي يعمل على pic16f877 و ir sharp distance sensor على المنفذ :PORTA0 واحتاج لاستخدام سنسورين 2 sensors على PORTA0 & PORTA1
ارجوا المساعده في الكود ليعمل على السنسورين 2 sensors
وجزاكم الله كل خير
// LCD module connections;
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections;
unsigned int cm,cm10;
unsigned int math;
char *text;
char *digit = "00";
void calc_distance(void)
{
// from the datasheet the analog voltage is
// the inverse of distance, so distance can be calculated
// d = (1 / volts) then just scaled to suit the datasheet
// load ADC value in 16bit math variable.
math = ADC_Read(0);
math = ADRESH;
math = (math * 255);
math += ADRESL;
// now invert it; (1 / volts) use (6050 / volts) for scaling, 6050 is a constant;
math = (6050 / math);
if(math >= 2) math -= 2; // fix linear error (-2)
if(math < 10) math = 10; // min limit at 10cm
if(math > 80) math = 80; // max limit at 80cm
// convert from 0-99 to 2 decimal digits, 0-99cm
cm10=0;
while(math >= 10)
{
cm10++;
math -= 10;
}
cm = math;
}
void main(void)
{
ADCON0 = 0X01; // RA0 as Analog Input;
ADCON1 = 0X00; // Reference VDD and VSS, with left justified;
TRISA0_bit = 1; // Make RA0 as input;
PORTA = 0x00; // Initial value of PORTA;
LCD_Init(); // Initialize the LCD;
LCD_Cmd(_LCD_CURSOR_OFF); // Cursor mode off;
LCD_Cmd(_LCD_CLEAR); // Clear the entire LCD;
text = "front";
LCD_Out(1,3,text);
text = "=";
LCD_Out(1,8,text);
text = "cm";
LCD_Out(1,13,text);
text = "back";
LCD_Out(2,3,text);
text = "=";
LCD_Out(2,8,text);
text = "cm";
LCD_Out(2,13,text);
while(0)
{
calc_distance(); // Call the "calc_distance" function;
digit[0] = cm10 + 48; // tens digit;
digit[1] = cm +48; // unit digit;
Lcd_Out(1,10,digit); // display on LCD from column 2, from character 10;
delay_ms(100);
}