📄 ad convert.c
字号:
#include<regx51.h>
#define TIMER0_COUNT 0xDC11
#define adc_in P0
sbit rs=P3^5;
sbit rw=P3^4;
sbit en=P3^3;
sbit adc_rd=P2^0;
sbit adc_wr=P2^1;
static unsigned timer0_tick;
const char int2char[]="0123456789";
void delay(void)
{
unsigned int i,j;
for(i=0;i<3;i++)
for(j=0;j<255;j++);
}
void write_command(unsigned command)
{
rw=0;
rs=0;
en=1;
P1=command;
en=0;
rs=1;
rw=1;
delay();
}
void write_data(unsigned lcddata)
{
rw=0;
rs=1;
en=1;
P1=lcddata;
en=0;
rs=0;
rw=1;
delay();
}
void set_lcd(void)
{
write_command(56);
write_command(12);
write_command(6);
}
void clear_lcd(void)
{
write_command(1);
write_command(2);
}
void display_string(char *p)
{
while(*p)
{
write_data(*p);
p++;
}
}
void gotoxy(unsigned x,unsigned y)
{
if(x==1)
write_command(128+y);
else
write_command(192+y);
}
void display_number(unsigned char number)
{
unsigned char x,y,i=2;
int z;
z=(int) number;
z=z<<1;
x=z/10;
y=z-x*10;
gotoxy(2,3);
write_data(int2char[y]);
z=x;
x=z/10;
y=z-x*10;
gotoxy(2,2);
write_data(int2char[y]);
gotoxy(2,1);
display_string(".");
z=x;
x=z/10;
y=z-x*10;
gotoxy(2,0);
write_data(int2char[y]);
gotoxy(2,4);
display_string("Volt");
}
static void timer0_isr(void) interrupt 1 using 1
{
TR0=0;
TL0=(TIMER0_COUNT & 0X00FF);
TH0=(TIMER0_COUNT >> 8);
TR0=1;
timer0_tick++;
if(timer0_tick==200){
adc_wr=0;
timer0_tick=0;
adc_wr=1;
}
}
static void int0_isr(void) interrupt 0 using 0
{
unsigned char voltage;
adc_in=0xff;
adc_rd=0;
voltage=adc_in;
voltage=voltage<<1;
adc_rd=1;
gotoxy(2,0);
display_number(voltage);
}
static void timer0_initialize(void)
{
EA=0;
timer0_tick=0;
TR0=0;
TMOD &=0xf0;
TMOD |=0x01;
TL0=( TIMER0_COUNT & 0x00FF);
TH0=( TIMER0_COUNT >> 8);
PT0=0;
ET0=1;
TR0=1;
EA=1;
}
void main(void){
set_lcd();
clear_lcd();
display_string("Input voltage:");
timer0_initialize();
IT0=1;
EX0=1;
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -