AVR单片机 电压显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AVR单片机 电压显示相关的知识,希望对你有一定的参考价值。

Atmetgal16 单片机 电压输入0到5V 自带10位AD 得到10位数字量,再通过数据处理得到电压直 取2位小数.显示在数码管上.程序如下
#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
uchar const table[]=
0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f;
void delay(uchar x)

uchar a,b;
for(a=x;a>0;a--)
for(b=60;b>0;b--);


uint ad()

uint dat;
ADMUX=0x43;
ADCSRA=0xc0;
while(!(ADCSRA&BIT(ADSC)));
dat=ADCL;
dat=ADCL+ADCH*256;
return dat;

void main()

DDRA=0x07;
DDRB=0xff;
DDRC=0x80;
PORTC=0x80;
while(1)

uint num,dianya;
num=ad();
dianya=((num*500)/1024);
PORTB=table[dianya/100]|0x80;
PORTA=0;
delay(5);
PORTB=table[(dianya%100)/10];
PORTA=1;
delay(5);
PORTB=table[(dianya%100)%10];
PORTA=2;
delay(5);


结果调节电位器 数码管只能在0到0.51之见显示 请教程序哪里出错 谢谢
如果我从AD采样得到的10位数字量0到 1024直接显示在数码管上可以,但是再转换成电压显示就出现只能在0到0.51之间的直了.希望高手给个调试好的程序给我参考下 51的也可以

参考技术A 在做油量检测模拟时写的小程序 希望对你有用~~~

程序用到的lcd.h 实在不会写的话或是看不懂的话再找我吧~

#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int

#define sound_port PD7 //报警端口
#define warm_charge PD6 //红灯亮,加油
#define warm_discharge PD5 //绿灯亮,正常
#define botton_port PORTD
#define botton_pin PIND //按键端口,PD 低四位

#include"lcd160204.h" //LCD 4线驱动
#include"lcd.h" //数据类型转换成字符串输出
//全局变量
float v,now_adc,warm_line=2.12,get_average,adc_val,get_val[20];

float adc_convert(void) //ADC转换值读取

int temp1,temp2;
float temp;
temp1=ADCL; //必须先读低位
temp2=ADCH;
temp2=(temp2<<8)+temp1;
temp=(float)temp2;
return(temp);


//20 次采样,求取平均值
float find_effectval(float *array)

uchar i;
float min_val,max_val,sum,average;
min_val=array[0];
max_val=array[0];
for(i=1;i<20;i++)
if(min_val>array[i]) min_val=array[i];

for(i=1;i<20;i++)
if(max_val<array[i]) max_val=array[i];

for(i=0;i<20;i++)
sum+=array[i];

average = (sum - min_val - max_val)/18.0;
return(average);


void sound(void) //警报声 100ms

uint a;
for(a=0;a<10;a++)

PORTD|=BIT(sound_port);
delay_1ms();
PORTD&=~BIT(sound_port);
delay_1ms();


//报警检测
void warm(void)

if(v<warm_line) //红灯

PORTD&=~BIT(warm_charge);
PORTD|=BIT(warm_discharge);
sound();

else //绿灯

PORTD&=~BIT(warm_discharge);
PORTD|=BIT(warm_charge);
//sound();


//报警值修改函数,做按键扫描
void change_warm(void)

delay_nms(30);
if((botton_pin&0x0f)!=0x0f)

delay_nms(50);
switch(botton_pin&0x0f)

case 0x0e: warm_line += 0.02;break;
case 0x0d: warm_line += 0.2;break;
case 0x0b: warm_line -= 0.02;break;
case 0x07: warm_line -= 0.2;break;
default :break;




void show(void)

locate(1,1);
lcd_str("adc:");
locate(1,5);
lcd_long_to_char(get_average);
lcd_str(" ");

locate(1,8);
lcd_str("val:");
locate(1,12);
lcd_float_to_char(v);
lcd_str(" ");

locate(2,1);
lcd_str("warm_line :");
locate(2,12);
lcd_float_to_char(warm_line);
lcd_str(" ");


void init_all(void)

DDRA = 0xfe; //设置A口 不带上拉输入;
PORTA = 0x00;
DDRD = 0xe0;
PORTD = 0x7f;
lcd_init();
cls();
//延时待系统稳定;
delay_nms(200);
locate(1,2);
lcd_str("loading system!");
locate(2,2);
lcd_str("please wait!");
delay_nms(2000);
cls();/**/


void get_adc_info(void)

uchar x;
for(x=0;x<20;x++)

ADMUX=0x40;
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADIF); //AD转换使能,启动ADC转换 ,二分频
while(!(ADCSRA&(1<<ADIF))); //等待AD转换结束
adc_val=adc_convert();
get_val[x]=adc_val;
delay_nms(30);
change_warm(); //报警值修改

get_average = find_effectval(get_val);
v = (get_average*5.0)/(1023*1.0);


void main(void)

//float adc_val,get_val[20];,get_average;
init_all();

while(1)

get_adc_info(); //获取信息并做处理
warm(); //报警检测
show(); //显示信息

参考技术B Code Vision AVR C1.25.5的程序,其中给出了ADC转换函数,调用一次就转换一次。

include <mega16.h>

#include <delay.h>

#define ADC_VREF_TYPE 0x00

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)

ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;


// Declare your global variables here

void main(void)

// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 57.600 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x86;

while (1)

// Place your code here

;
参考技术C 个人觉得是你送PORTA的事,要是让某个数码管点亮,不能直接送012啊 参考技术D 看你参考电压那接了没?接的是否和你程序设置的一样!

STC单片机LCD1602四线驱动模式显示ADC采样电压

STC单片机LCD1602四线驱动模式显示ADC采样电压