這個程式裡面搭配了火焰檢測子產品,使用的是ADC0,AD轉換,顯示的電壓值可以根據光譜亮度進行變動。網上的1602字庫,大多用彙編語言編寫,可移植性太低。我這個是用C語言編寫的。1602的字庫,你們可以看到,顯示了中文“月”與“工”。
#include "iom16v.h"
#include "macros.h"
#include "led.h"
#define uint unsigned int
#define uchar unsigned char
#define D0 PORTD=PORTD&0xbf;
#define D1 PORTD=PORTD|0x40;
unsigned char const lcd1602[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char const lcd1602_yue[]={0x0e,0x0a,0x0e,0x0a,0x0e,0x0a,0x12,0x00};
unsigned char const lcd1602_gong[]={0x1f,0x04,0x04,0x04,0x04,0x04,0x1f,0x00};
uint constant1=0,a1=0,a2=0,a3=0,a4=0;
void add_gong() //自造字庫“工”
{
int a,b;
for (a=0x48;a<=0x4f;a++)
{
PORTB=a;
enable_lcd();
b=a&0x07;
PORTB=lcd1602_gong[b];
data();
}
}
void add_yue() //自造字庫“月”
{
int a=0x40,b;
for (a=0x40;a<0x48;a++)
{
PORTB=a;
enable_lcd();
b=a&0x0f;
PORTB=lcd1602_yue[b];
data();
}
}
void delay(unsigned long a)
{
while(a--);
}
void enable_lcd()
{
PORTD=PORTD&0xcf;
D0;
delay(400);
D1;
}
void data()
{
PORTD=PORTD&0xdf;
PORTD|=0x10;
D0;
delay(400);
D1;
}
void initial()
{
DDRA&=0xfe;//PA0設定為輸入 ADC0輸入部分
DDRA|=0x40;
PORTA|=0x40;
DDRD|=0x70;//PD4-6設定為輸出 1602顯示部分 不許改動
D1;
DDRB=0xff;//PB0-7設定為輸出 1602顯示部分
}
void displayinitial()
{
PORTB=0x00;//清顯示
enable_lcd();
PORTB=0x06;//光标右移
enable_lcd();
PORTB=0x10;;//移動光标
enable_lcd();
PORTB=0x38;//4位總線,雙行顯示,5X7點陣字元
enable_lcd();
PORTB=0x80;//顯示從第一行第二個開始
enable_lcd();
PORTB=0x0C;//開顯示
enable_lcd();
}
void display1()
{
PORTB=0x80;
enable_lcd();
PORTB='V';
data();
PORTB='o';
data();
PORTB='l';
data();
PORTB='t';
data();
PORTB='a';
data();
PORTB='g';
data();
PORTB='e';
data();
PORTB=':';
data();
PORTB=lcd1602[a1];
data();
PORTB='.';
data();
PORTB=lcd1602[a2];
data();
PORTB=lcd1602[a3];
data();
PORTB=lcd1602[a4];
data();
PORTB='V';
data();
PORTB=0x00;
data();
PORTB=0x01;
data();
}
unsigned int adc()
{
uint a,b;
ADMUX=0x40;//内部REF關閉,直接使用外接的REF,右對齊,選擇ADC0
ADCSRA=0xd8;//ADC使能,ADC開始轉換,ADIF置位1,ADIE使能,2分頻
do
{
;
}
while(ADCSRA&0x10!=0);
a=ADCL;
b=ADCH&0x03;
a=a+b*256;
return a;
}
void main()
{
unsigned int a;
initial();
displayinitial();
add_yue();
add_gong();
display1();
display(0);
strobe(5);
while(1)
{
a=adc();
constant1=a*4.98/1024*1000;
a1=constant1/1000;
a2=constant1%1000/100;
a3=constant1%1000%100/10;
a4=constant1%10;
display1();
delay(1000);
display(a2);
if (constant1<2460) PORTA&=0xbf;
else PORTA|=0x40;
}
}
自造字庫,是安插在程式中,感興趣的朋友,可以研究下。有不明白的地方,歡迎加我QQ:271715999
小編導語:這是剛在深圳認識的一個朋友,做硬體研發的,沒想到他也是一個發燒型的DIY。