//GCC M8
#include<avr/io.h>
#include<avr/pgmspace.h>
#include<avr/delay.h>
#include<avr/interrupt.h>
#include<avr/signal.h>
#define setbit(sfr,bit) (sfr|=(1<<bit))
#define clrbit(sfr,bit) (sfr&=~(1<<bit))
#define DATA PORTB
#define RW 1
#define DI 5
#define CS1 2
#define CS2 3
#define E 4
#define R PORTC
const unsigned char ling[16]PROGMEM={0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00};/*"0",0*/
const unsigned char yi[16]PROGMEM={0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00};
const unsigned char er[16]PROGMEM={0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00};/*"2",0*/
const unsigned char san[16]PROGMEM={0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00};/*"3",0*/
const unsigned char si[16]PROGMEM={0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00};/*"4",0*/
const unsigned char wu[16]PROGMEM={0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00};/*"5",0*/
const unsigned char liu[16]PROGMEM={0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00};/*"6",0*/
const unsigned char qi[16]PROGMEM={0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00};/*"7",0*/
const unsigned char ba[16]PROGMEM={0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00};/*"8",0*/
const unsigned char jiu[16]PROGMEM={0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00};/*"9",0*/
const unsigned char zhuan[32]PROGMEM={
0x08,0xC8,0xB8,0x8F,0xE8,0x88,0x40,0x48,
0xC8,0x7F,0x48,0x48,0x4C,0x68,0x40,0x00,
0x08,0x18,0x08,0x04,0xFF,0x04,0x00,0x02,
0x13,0x22,0x62,0x9A,0x06,0x02,0x00,0x00
};
const unsigned char huan[32]PROGMEM={
0x10,0x10,0x10,0xFF,0x90,0x90,0xE8,0x24,
0x27,0xF4,0x2C,0x24,0xF0,0x20,0x00,0x00,
0x02,0x42,0x81,0x7F,0x84,0x84,0x47,0x24,
0x14,0x0F,0x14,0x24,0x47,0xC4,0x44,0x00
};
const unsigned char jie[32]PROGMEM={
0x00,0x30,0x28,0xA4,0x63,0x10,0x08,0x48,
0x48,0x48,0x7F,0x48,0x48,0x4C,0x08,0x00,
0x00,0x22,0x63,0x22,0x12,0x12,0x00,0xFE,
0x42,0x42,0x42,0x42,0x42,0xFF,0x02,0x00
};
const unsigned char guo[32]PROGMEM={
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,
0x92,0x92,0x92,0xFF,0x02,0x00,0x00,0x00,
0x02,0x42,0x42,0x22,0x12,0x0A,0x06,0xFF,
0x06,0x0A,0x12,0x22,0x62,0x23,0x02,0x00
};
const prog_uchar *sr;
volatile unsigned int Result=0;
unsigned char DispBuf[4];
//初始化IO口
void Init_IO(void)
{
DDRC|=0xfe;
PORTC=0xff;
DDRD=0xff;
PORTD=0xff;
DDRB=0xff;
PORTB=0xff;
}
//初始化ADC
void Init_AD(void)
{
ADMUX|=(1<<REFS1)|(1<<REFS0);
ADCSRA|=(1<<ADIE);
}
//ADC中断服务程序
SIGNAL(SIG_ADC)
{
Result=ADCL;
Result|=(unsigned int)(ADCH<<8);
}
//检查LCD是否空闲
unsigned char getstate(void)
{
PORTB=0xff;
DDRB=0x00;
clrbit(R,CS1);
clrbit(R,CS2);
clrbit(R,DI);
setbit(R,RW);
setbit(R,E);
clrbit(R,E);
return PINB;
}
unsigned char lcdbusy(void)
{
unsigned char state=0;
state=getstate();
if(state&0x80)
return 1;
else
return 0;
}
//写命令
void lcdcommand(unsigned char command)
{
DDRB=0xff;
clrbit(R,CS1);
clrbit(R,CS2);
clrbit(R,RW);
clrbit(R,DI);
DATA=command;
setbit(R,E);
clrbit(R,E);
}
//开lcd
void lcdopen(void)
{
while(lcdbusy());
lcdcommand(0x3f);
}
//设置开始行
void lcdsetline(unsigned char line)
{
line|=0xc0;
while(lcdbusy());
lcdcommand(line);
}
//设置开始页
void lcdsetpage(unsigned char page)
{
page&=0xbf;
page|=0xb8;
while(lcdbusy());
lcdcommand(page);
}
//设置列地址
void lcdsetrow(unsigned char row)
{
row&=0x7f;
row|=0x40;
while(lcdbusy());
lcdcommand(row);
}
//写数据
void lcdwrite(unsigned char data,unsigned char cs)
{
while(lcdbusy());
DDRB=0xff;
if(cs==1)
{
clrbit(R,CS1);
setbit(R,CS2);
}
else
{
setbit(R,CS1);
clrbit(R,CS2);
}
clrbit(R,RW);
setbit(R,DI);
DATA=(data);
setbit(R,E);
clrbit(R,E);
}
//写一个数字
void lcd_nu(unsigned char cs,unsigned char page,unsigned char row,unsigned char line)
{
unsigned char i;
lcdsetline(line);
lcdsetpage(page);
lcdsetrow(row);
for(i=0;i<8;i++)
{
lcdwrite(pgm_read_byte(sr++),cs);
}
lcdsetline(line);
lcdsetpage(page+1);
lcdsetrow(row);
for(i=8;i<16;i++)
{
lcdwrite(pgm_read_byte(sr++),cs);
}
}
//写一个汉字
void lcd_cn(const prog_uchar *src,unsigned char cs,unsigned char page,unsigned char row,unsigned char line)
{
unsigned char i;
lcdsetline(line);
lcdsetpage(page);
lcdsetrow(row);
for(i=0;i<16;i++)
{
lcdwrite(pgm_read_byte(src+i),cs);
}
lcdsetline(line);
lcdsetpage(page+1);
lcdsetrow(row);
for(i=16;i<32;i++)
{
lcdwrite(pgm_read_byte(src+i),cs);
}
}
//结果装入显示缓冲区及显示服务程序
void Showlcd(void)
{
unsigned char r;
DispBuf[3]=Result%10;
DispBuf[2]=(Result%100)/10;
DispBuf[1]=(Result/100)%10;
DispBuf[0]=(Result/1000);
for(r=0;r<4;r++)
{
if(DispBuf[r]==0)
sr=ling;
else if(DispBuf[r]==1)
sr=yi;
else if(DispBuf[r]==2)
sr=er;
else if(DispBuf[r]==3)
sr=san;
else if(DispBuf[r]==4)
sr=si;
else if(DispBuf[r]==5)
sr=wu;
else if(DispBuf[r]==6)
sr=liu;
else if(DispBuf[r]==7)
sr=qi;
else if(DispBuf[r]==8)
sr=ba;
else if(DispBuf[r]==9)
sr=jiu;
else
sr=ling;
lcd_nu(2,3,r*8,0);
}
}
//主程序
int main(void)
{
Init_IO();
Init_AD();
lcdopen();
ADCSRA|=(1<<ADEN);
sei();
lcd_cn(zhuan,1,3,0,0);
lcd_cn(huan,1,3,16,0);
lcd_cn(jie,1,3,32,0);
lcd_cn(guo,1,3,48,0);
while(1)
{
ADCSRA|=(1<<ADSC);
Showlcd();
_delay_loop_2(10000);
}
} |