|
仿真时,经常出现target fail to respond的错误,多试几次可能又可以运行。lcd显示时存在很多杂点噪声,仿真时按键也不是很灵敏。
#include "C8051F410.h"
#include
#define uchar unsigned char
#define uint unsigned int
sbit RS=P1^3;
sbit RW=P1^4;
sbit EN=P1^5;
sbit PSB=P1^6;
sbit RET=P0^7;
sbit P04=P0^4;
sbit P03=P0^3;
sbit P02=P0^2;
sfr16 IDA0=0x96;
sfr16 ADC0 =0xbd;
int q=0;
unsigned char m=0,n=0,l=0,k=0,i=0,j=0,sum=0;
unsigned char z0=0,z1=0,z2=0,z3=0,z4=10,p=0;
unsigned long acc;
float mv;
uchar code key1[]={"步进="};
uchar code key2[]={"增益="};
uchar code key3[]={"电压="};
uchar code key4[]={"mV"};
uchar code key5[]={"dB"};
uchar code key[]= {"0123456789+-"};
void PCA_Init()
{
PCA0MD &= ~0x40;
PCA0MD = 0x00;
}
void Timer_Init()
{
CKCON = 0x10;
TMR2CN = 0x04;
TMR2RLL = 0xFF;
TMR2RLH = 0xFF;
TMR2H = 0xC0;
TMR3CN = 0x04;
TMR3RLL = 0xb1;
TMR3RLH = 0xc1;
TMR3L = 0x31;
TMR3H = 0x21;
}
void ADC_Init()
{
ADC0MX = 0x0F;
ADC0CF = 0x38;
ADC0CN = 0x81;
}
void DAC_Init()
{
IDA0CN = 0xA1;
}
void Voltage_Reference_Init()
{
REF0CN = 0x0A;
}
void Port_IO_Init()
{
P0MDIN = 0xFC;
P1MDIN = 0x7F;
P2MDOUT = 0xFF;
P0SKIP = 0x1F;
P1SKIP = 0x80;
P2SKIP = 0xFF;
XBR1 = 0x40;
}
void Oscillator_Init()
{
OSCICN = 0x83;
}
void Interrupts_Init()
{
EIE1 = 0x08;
IE = 0x80;
}
void delay_50us(uint t)
{
uchar i=2;
while(t--)
while(i--);
}
void delay_50ms(uint t)
{
uint i,j,k;
for (i=t;i>0;i--)
for (j=57;j>0;j--)
for(k=100;k>0;k--);
}
void write12864com(uchar com)
{
RS=0;
RW=0;
delay_50us(1);
P2=com;
EN=1;
delay_50us(1);
EN=0;
delay_50us(1);
}
void write12864dat(uchar dat)
{
RS=1;
RW=0;
delay_50us(1);
P2=dat;
EN=1;
delay_50us(1);
EN=0;
delay_50us(1);
}
void initinal12864(void)
{ PSB=1;
RET=1;
delay_50ms(2);
write12864com(0x30);
delay_50us(2);
write12864com(0x30);
delay_50us(2);
write12864com(0x0c);
delay_50us(1);
write12864com(0x01);
delay_50us(20);
write12864com(0x06);
delay_50us(2);
}
void Init_Device(void)
{
PCA_Init();
Timer_Init();
ADC_Init();
DAC_Init();
Voltage_Reference_Init();
Port_IO_Init();
Oscillator_Init();
Interrupts_Init();
initinal12864();
}
void keyscan(void)
{
uchar scan1;
P04=1;
P03=0;
P02=0;
scan1=P0;
if((scan1&0x10)!=0x10) //判键是否按下
{
delay_50us(50); //延时0.25ms
scan1=P0;
if((scan1&0x10)!=0x10) //二次判键是否按下
{
P04=0;
P03=1;
P02=1;
if(P02==0) //查表得键值
{
q=q+1;
}
if(P03==0) //查表得键值
{
q=q-1;
}
while((P0&0x0C)!=0x0C); //检查P00和P01口
}
}
}
void adc(void)
{
uchar i=1000;
sum++;
if(sum==100){
sum=0; //延时2ms
mv=mv*2500/4096;
acc=mv;
acc=acc+q;
m=acc/1000;
acc=acc%1000;
n=acc/100;
acc=acc%100;
l=acc/10;
acc=acc%10;
k=acc;
while(i--){
write12864com(0x83);
write12864dat(key[m]);
write12864dat(key[n]);
write12864com(0x84);
write12864dat(key[l]);
write12864dat(key[k]);
}
}
}
void bujin()
{
uchar i=1000;
z3++;
if(z3==100)
{
z3=0;
p=q;
if(q<0)
{
z4=11;
p=256+q;
p=256-p; //P为有符号数,与无符号数的负数运算较复杂,故间接求值
}
if(q==0)
{
z4=0;
}
if(q>0)
{
z4=10;
}
z0=p/100;
p=p%100;
z1=p/10;
p=p%10;
z2=p;
while(i--){
write12864com(0x8B);
write12864dat(key[z4]);
write12864dat(key[z0]);
write12864com(0x8C);
write12864dat(key[z1]);
write12864dat(key[z2]);
}
}
}
void main()
{
Init_Device();
IDA0=0xa440; //基准电压=1.5 ,a370
EA =1;
write12864com(0x80);
write12864dat(key3[0]);
write12864dat(key3[1]);
write12864dat(key3[2]);
write12864dat(key3[3]);
write12864dat(key3[4]);
write12864com(0x85);
write12864dat(key4[0]);
write12864dat(key4[1]);//第一行
write12864com(0x88);
write12864dat(key1[0]);
write12864dat(key1[1]);
write12864dat(key1[2]);
write12864dat(key1[3]);
write12864dat(key1[4]);
write12864com(0x8D);
write12864dat(key5[0]);
write12864dat(key5[1]);
while(1)
{
adc();
bujin();
keyscan();
}
}
void ADC_ISR(void)interrupt 10
{
AD0INT =0;
mv=ADC0;
}
|
|