本程使用MPX4250。程序运行时,传感器向单片机输入电压信号,经A/D转换后,根据技术手册提供的公式进行,最后显示在数码管上。
Proteus仿真截图:
Atmel Studi6.2 运行截图:
程序清单:
/*
* GccApplication38.c
*
* Created: 2014-12-23 20:48:39
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
const uint8_t SEG_CODE[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
uint8_t SEG_Display_Buffer[] = {0xF3,0,0,0};
void Show_PRESS_ON_DSY()
{
uint8_t i = 0;
for(i=0;i<4;i++)
{
PORTD = ~_BV(i);
PORTC = SEG_Display_Buffer[i];
_delay_ms(4);
}
}
int main(void)
{
int AD_Result,Pressure_Value;
DDRA = 0x7F; PORTA = 0xFF;
DDRC = 0xFF; PORTC = 0xFF;
DDRD = 0xFF; PORTD = 0xFF;
ADCSRA = 0xe6;
_delay_ms(2000);
ADMUX = 0x07;
while(1)
{
AD_Result = ADCL + (ADCH << 8);
Pressure_Value = (AD_Result * 5.0/1023.0/5.1 - 0.04)/0.00369 + 1.99;
SEG_Display_Buffer[1] = SEG_CODE[Pressure_Value/100];
SEG_Display_Buffer[2] = SEG_CODE[Pressure_Value/10 % 10];
SEG_Display_Buffer[3] = SEG_CODE[Pressure_Value%10];
if(SEG_Display_Buffer[1]==0x3F)
{
SEG_Display_Buffer[1] = 0x00;
if(SEG_Display_Buffer[2] == 0x3F)SEG_Display_Buffer[2] = 0x00;
}
Show_PRESS_ON_DSY();
}
}
|