本帖最后由 疏雨梧桐 于 2014-7-29 14:33 编辑
我今天刚开始做自己设计的程序和电路,会有些粗糙。
先说原件:
10kΩ的电阻 一个
面包板一个
面包线 若干
arduino Uno 板一个
-思路:analogRead()函数可以读取一个值,0--1023对应0---5v
把5v电源,10K电阻,还有待测电阻接一个回路。
10k电阻一端接GND,然后读取另一段的电位值,可求出电流。然后电流乘以10K+待测的值=5
解未知量 可求出电阻的值。然后在串口通信中输出 ,外接的话可以采用稳压二极管来得到稳定电压。
A4端做为输入
程序
如下:
#define SENSOR 0
int i=0,x=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
i=analogRead(5);
x=(10240000/i)-10000;
Serial.print(x);
delay(1000);
}
图片上传不上去,只能这样,我想用lcd显示 但是不知道输出变量的函数是什么,求大神指点。
我写了这个 也贴上去,
[pre lang="arduino" line="1" file="LCD显示"]
//ceshidianzu
int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};//使用数组来定义总线需要的管脚
int q[5];
int Enable = 2;
int x=0,i=0,y=0;
void LcdCommandWrite(int value) {
// 定义所有引脚
int i = 0;
for (i=DB[0]; i <= DI; i++) //总线赋值
{
digitalWrite(i,value & 01);//因为1602液晶信号识别是D7-D0(不是D0-D7),这里是用来反转信号。
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1); // 延时1ms
digitalWrite(Enable,LOW);
delayMicroseconds(1); // 延时1ms
}
void LcdDataWrite(int value) {
// 定义所有引脚
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1); // 延时1ms
}
void setup(){
int i = 0;
for (i=Enable; i <= DI; i++) {
pinMode(i,OUTPUT);
}
delay(100);
// 短暂的停顿后初始化LCD
// 用于LCD控制需要
LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小
delay(64);
LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小
delay(50);
LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小
delay(20);
LcdCommandWrite(0x06); // 输入方式设定
// 自动增量,没有显示移位
delay(20);
LcdCommandWrite(0x0E); // 显示设置
// 开启显示屏,光标显示,无闪烁
delay(20);
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(100);
LcdCommandWrite(0x80); // 显示设置
// 开启显示屏,光标显示,无闪烁
delay(20);
}
void SHOW(int x)
{
switch(x)
{
case 0: LcdDataWrite('0');break;
case 1: LcdDataWrite('1');break;
case 2: LcdDataWrite('2');break;
case 3: LcdDataWrite('3');break;
case 4: LcdDataWrite('4');break;
case 5: LcdDataWrite('5');break;
case 6: LcdDataWrite('6');break;
case 7: LcdDataWrite('7');break;
case 8: LcdDataWrite('8');break;
case 9: LcdDataWrite('9');break;
}
}
void loop()
{
i=analogRead(5);
x=(10240000/i)-10000;
for(y=0;x>1;)
{
y++;
q[y]=x%10;
x/=10;
}
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(10);
LcdCommandWrite(0x80+3);
delay(10);
// 写入欢迎信息
LcdDataWrite('W');
LcdDataWrite('e');
LcdDataWrite('l');
LcdDataWrite('c');
LcdDataWrite('o');
LcdDataWrite('m');
LcdDataWrite('e');
LcdDataWrite(' ');
LcdDataWrite('t');
LcdDataWrite('o');
delay(10);
LcdCommandWrite(0xc0+1); // 定义光标位置为第二行第二个位置
delay(10);
LcdDataWrite('m');
LcdDataWrite('y');
LcdDataWrite(' ');
LcdDataWrite('D');
LcdDataWrite('I');
LcdDataWrite('Y');
LcdDataWrite(' ');
delay(5000);
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(10);
LcdDataWrite('I');
LcdDataWrite('h');
LcdDataWrite('e');
LcdDataWrite(' ');
LcdDataWrite('r');
LcdDataWrite('e');
LcdDataWrite('s');
LcdDataWrite('i');
LcdDataWrite('s');
LcdDataWrite('t');
LcdDataWrite('a');
LcdDataWrite('n');
LcdDataWrite('c');
LcdDataWrite('e');
delay(10);
LcdCommandWrite(0xc0+1); // 定义光标位置为第二行第si个位置
delay(10);
LcdDataWrite(' ');
LcdDataWrite('i');
LcdDataWrite('s');
LcdDataWrite(' ');
for(y;y>0;y--)
{
SHOW(q[y]);
}
delay(7000);
}[/code]
改进后版本:
稍后加上!
[pre lang="arduino" line="1" file="文件"]、
#include <LiquidCrystal.h> //调用arduino自带的LiquidCrystal库
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);//设置接口
int x,i;
void setup()
{
lcd.begin(16, 2); //初始化LCD
delay(1000); //延时1000ms
}
void loop ()
{
i = analogRead(5); //i变量为从5信号口读取到的数值
x=(10240000/i)-10000;
lcd.clear(); //清屏
lcd.setCursor(0, 0) ; //设置光标位置为第一行第一个位置
lcd.print("R="); //使屏幕显示文字R=
lcd.print(x);
delay(3000); //延时3秒,这里也就是刷新速度。
}
[/code]
求指点,菜鸟一枚,奋斗中…… |