此为程序,求大神帮助
#include<c8051F410.h>
#include<stdio.h>
#define SYSCLK 24500000
#define uchar unsigned char
#define uint unsigned int
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 ADC0 = 0xbd; // ADC0 result
sbit rs=P1^5;
sbit wr=P1^6;
sbit lcden=P1^7;
uchar shuzi[] = {"0123456789."};
uint i,j; // LED='1' means ON
void SYSCLK_Init (void)
{
OSCICN = 0x87; // configure internal oscillator for
// 24.5MHz
RSTSRC = 0x04; // enable missing clock detector
}
void PORT_Init (void)
{
XBR0 = 0x01; // Enable UART0
XBR1 = 0x40; // Enable crossbar and weak pull-ups
//P0MDOUT |= 0x10; // Set TX pin to push-pull
P1MDOUT = 0xE0; // enable LED as a push-pull output
P1MDIN &= 0xFc; // set P1.1 as an analog input
P1SKIP |= 0x03; // skip P1.1 pin
P2MDOUT =0xFF;
}
void ADC0_Init (void)
{
ADC0CN = 0x03; // ADC0 disabled, normal tracking,
// conversion triggered on TMR2 overflow
REF0CN = 0x13; // Enable on-chip VREF = 2.2v and buffer
ADC0MX = 0x08; // Set P1.1 as positive input
ADC0CF = ((SYSCLK/3000000)-1)<<3; // set SAR clock to 3MHz
ADC0CF |= 0x00; // right-justify results
EIE1 |= 0x08; // enable ADC0 conversion complete int.
AD0EN = 1; // enable ADC0
}
void delay(uint x)
{
for(i=x;i>0;i--)
{
for(j=120;j>0;j--);
}
}
void write_com(uchar com)
{
rs=0;
wr=0;
lcden=0;
P2=com;
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar dat)
{
rs=1;
wr=0;
lcden=0;
P2=dat;
lcden=1;
delay(5);
lcden=0;
}
void init()
{
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0f);
}
void address(uchar x,uchar y)
{
if(x%2==1)
write_com(0x80+(y%16)-1);
else
write_com(0x80+0x40+(y%16)-1);
}
void main()
{
unsigned long mv=0;
uint num[4]; init();
SYSCLK_Init ();
PORT_Init ();
ADC0_Init();
while(1)
{
AD0INT=0;
AD0BUSY=1;
while(AD0INT==0)
{
mv=ADC0;
}
mv= (mv * 220)/4095;
num[0]=mv/100;
mv=mv-num[0]*100;
num[2]=mv/10;
num[3]=mv-(num[2]*10);
num[1]=10;
address(2,1);
for(i=0;i<4;i++)
{
write_data(shuzi[num
]);
}
}
while(1);
}
此帖出自小平头技术问答
一周热门 更多>