📄 程序.txt
字号:
#include<C8051F330.h>
#include<math.h>
sbit mode = P0^4; //mode
sbit SEND=P1^1; //模拟串口的输出口。
sbit CLOCK=P1^2; //模拟串口的时钟口。
sbit shuma1=P1^3;
sbit shuma2=P1^4;
sbit shuma3=P1^5;
//unsigned char uca_BitPosition[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned int code uca_LEDCode[] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xe6,0x02,0x01};
unsigned char state = 0;
void SysClkInit(void) //配置系统时钟
{
OSCICN |= 0x03; //22.11 MHz
OSCICL=OSCICL+22;
}
void SystemInit(void) //系统配置
{
PCA0MD &= ~0x40; //禁止看门狗定时器
EA = 1;
}
void PortInit(void) //端口配置
{
P0SKIP = 0x0d; //P0.2 P0.3 P0.0为输入。。作为模拟输入(ADC或比较器)或特殊功能(VREF输入、外部振荡器电路、
P0MDIN = 0xf3; //配置P0、P1口输入输出方式
P0MDOUT = 0xff;
XBR1 = 0x40; //交叉开关使能
P1MDOUT = 0xff;
ADC0CN = 0X00;
IE=0x81; //开外部中断
IT01CF=0x54; //设置P0.4外部中断0
ADC0CF = (22114800/2500000)<<3; //ADC转换时钟
ADC0CF &= ~0x07;
}
void delayms(int n) //延时n秒
{
unsigned int i;
for(;n>0;n--)
{
for(i=2211;i>0;i--);
}
}
void myoutput(int byte) //输出译码
{
int num,c;
if(byte<12)
{
num=uca_LEDCode[byte];
for(c=0;c<8;c++)
{
CLOCK=0;
SEND=num&0x01;
CLOCK=1;
num>>=1;
}
}
}
void Voltager1() ///////////////////////////////////////////////////////////////////////////////////1路电压测量
{
float dianya1;
long float wendu;
int shi1,ge1,fen1,n; //第一路由P0.2进入,第二路由P0.3进入
unsigned int Temp1;
while(state==0){
REF0CN = 0x0a;
AMX0P = 0x02;AMX0N = 0x11; //测量第一路的电压值;
AD0EN = 1;
delayms(2);
AD0BUSY = 1;
delayms(2);while(!AD0INT);
Temp1 = ADC0H;
Temp1 <<= 8;
Temp1 &= 0xff00;
Temp1 |= ADC0L;
dianya1 = 3.3/1024*Temp1;
wendu=(1330-43.5*dianya1);//
shi1 = (int)wendu/10;
ge1 = (int)wendu%10;
fen1 = (int)(wendu*10)%10;
AD0BUSY = 0;
for(n=0;n<100;n++){//shuma1=0;shuma2=0;shuma3=0;delayms(2);
myoutput(shi1);delayms(2);
shuma1=0;shuma2=1;shuma3=1;
myoutput(ge1);delayms(2);
shuma1=1;shuma2=0;shuma3=1;
myoutput(fen1);delayms(2);
shuma1=1;shuma2=1;shuma3=0;
}
}}
void Voltager2() ///////////////////////////////////////////////////////////////////////////////////2路电压测量
{
float dianya2;
int shi2,ge2,fen2,n;
long float wendu; //第一路由P0.2进入,第二路由P0.3进入
unsigned int Temp2;
while(state==1){
AMX0P = 0x03;AMX0N = 0x11;
REF0CN = 0x0a;
AD0EN = 1;
delayms(2);
AD0BUSY = 1;
delayms(2);
while(!AD0INT);
Temp2 = ADC0H;
Temp2 <<= 8;
Temp2 &= 0xff00;
Temp2 |= ADC0L;
dianya2 = 3.3/1024*Temp2;
wendu=(1370-454*dianya2); //
shi2 = (int)wendu/10;
ge2 = (int)wendu%10;
fen2 = (int)(wendu*10)%10;
AD0BUSY = 0;
for(n=0;n<100;n++){
myoutput(shi2);
shuma1=0;shuma2=1;shuma3=1;delayms(2);
myoutput(ge2);
shuma1=1;shuma2=0;shuma3=1;delayms(2);
myoutput(fen2);
shuma1=1;shuma2=1;shuma3=0;delayms(2);
}
}
}
void timer0(void) interrupt 0 using 1
{
delayms(20);
if(state < 1)
state++;
else
state = 0;
while(!mode);
delayms(20);
}
void main()
{ int n=0;
SystemInit(); //系统配置
SysClkInit(); //配置系统时钟
PortInit(); //端口配置
while(1)
{switch(state)
{
case 0:
myoutput(1);
for(n=0;n<100;n++)
{
myoutput(1);
shuma1=0;shuma2=1;shuma3=1;delayms(5);
myoutput(10);
shuma1=1;shuma2=0;shuma3=1;delayms(5);
myoutput(10);
shuma1=1;shuma2=1;shuma3=0;delayms(5);
}
Voltager1();
break;
case 1:
myoutput(2);
for(n=0;n<100;n++)
{
myoutput(2);
shuma1=0;shuma2=1;shuma3=1;delayms(5);
myoutput(10);
shuma1=1;shuma2=0;shuma3=1;delayms(5);
myoutput(10);
shuma1=1;shuma2=1;shuma3=0;delayms(5);
}
Voltager2();
break;
default:
state = 0;break;
}/*shuma1=0;shuma2=0;shuma3=0;
for(n=0;n<10;n++){myoutput(n);delayms(500);}*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -