我试图修改我在互联网上找到的程序,我在这里找到了:
使用 PIC 微控制器连接超声波距离传感器 ASCII 输出
我需要对距离进行一些计算,然后才能将其输出到 LCD 显示器。我成功地将字符串转换为浮点数。它的代码在这里:
// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections
void main()
{
int i,temp;
char dist[] = "000.0";
float v,h,l=25,b=25;
Lcd_Init(); // Initialize LCD
UART1_Init(9600); //Initialize the UART module
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"Distance= cm");
do
{
if(UART1_Data_Ready()) //if data ready
{
if(UART1_Read() == 0x0D) //check for new line character
{
for(i=0;i<5;)
{
if(UART1_Data_Ready()) // if data ready
{
dist[i] = UART1_Read(); // read data
i++;
}
}
}
}
h=(100*(dist[0]-48))+(10*(dist[1]-48))+(dist[2]-48)+(0.1*(dist[4]-48));
v=l*b*h*0.001;
//sprintf(dist,"%f",v);
Lcd_Out(1,10,dist);
}while(1);
}
如果我添加 stdio,sprintf() 会起作用吗?还是我必须从头开始编写逻辑?或者我可以使用其他一些库函数吗?