初级会员
- 积分
- 80
- 金钱
- 80
- 注册时间
- 2020-8-10
- 在线时间
- 20 小时
|
5金钱
我用的是赛元的SC92F7422单片机,设置了两个通道进行ADC读取,现在的问题是单独进行输入电压显示时可以正常读取并显示出数值,但是同时开的时候数值就以叠加的形式显示出来了
代码如下:
#include "Function_Init.H"
#include "SC92F742x_C.H"
#include "intrins.H"
#include<stdio.h>
unsigned long ADCValue;
unsigned short i,b,a=0,u=0;
bit AdcFlag = 0;
unsigned short J,K;
unsigned char times_delay = 0,adc_delay=0,s_delay=0;
//#define LED2 P25
sbit TL=P1^3;
sbit WP=P1^0;
#define N 1000
int TempAdd=0;
uint value=0,step=0;
unsigned int t1 = 0;
unsigned int t2 = 0;
unsigned int t3 = 0;
unsigned int t4 = 0;
unsigned int t5 = 0;
unsigned int t6 = 0;
unsigned int t7 = 0;
unsigned int t8 = 0;
unsigned int t9 = 0;
unsigned int t10 = 0;
unsigned int t11 = 0;
unsigned int s0 = 0;
unsigned int s1 = 0;
unsigned int s3 = 0;
unsigned int s4 = 0;
unsigned int cnt1;
unsigned int cnt2;
unsigned int cnt3;
unsigned int cnt4;
unsigned int cnt5;
unsigned int cnt6;
unsigned int cnt7;
unsigned int cnt8;
unsigned int cnt9;
unsigned int cnt10;
unsigned int cnt11;
unsigned long ADC_Value1,ADC_Value2;
unsigned long sum;
void Delay(uint time)
{
uint c = 0;
for(c=0; c<100; c++)
for(; time>0; time--)
;
}
void IO_Init(void)
{
P0CON=0X3f;//0011 1111
P0PH=0X00;
P1CON = 0x84; //设置P1.7为强推挽模式//1000 1000
P1PH = 0x00;//0000 0000
P2CON = 0xD3; //设置P2为强推挽模式
P2PH = 0x00;
}
void Timer_Init(void)
{
TMCON = 0X01; // ;Timer0选择时钟Fsys
//T0设置
TMOD |= 0x01; //0000 0001;Timer0设置工作方式1
TL0 = (65536 - 1200)%256; //溢出时间:时钟为Fsys,则12000*(1/Fsys)=1ms; 12MHz
TH0 = (65536 - 1200)/256;
TR0 = 0;
ET0 = 1;//定时器0允许
TR0 = 1;//打开定时器0
EA = 1;
}
//void ADC_Init(void)
//{
// ADCCON |= 0X80; //开启ADC,ADC采样频率为2M ,选择AIN9位ADC采样口
//// ADCVH=0;
//// ADCVL=0;//清除数据
////
// ADCCON&= ~(0X10); //清中断标志位
// //IE |= 0X40; //允许ADC产生中断
//// EA = 1;
//}
unsigned long Voltage()
{
unsigned long L,M;
if(adc_delay>2)
{
ADCCON|=0X40;
if(!(ADCCON&0x10)==0)
{
ADCCON&= ~(0X10); //清中断标志位
}
ADCValue =((ADCVH<<4)|(ADCVL>>4));//0000 1111 1111 1111
M=(M*N+ADCValue*(1024-N))/1024;
adc_delay=0;
}
L=M/3;
//if(L>99){L=99;}
return L;
}
void ADC_channel(unsigned char channel)
{
ADCCON = ADCCON&0x80 | channel; //ADC 输入选择为 ADCchannel 口
}
void ADC_Multichannel()
{ ADCCON |= 0X80;
ADCCFG0=0x20;
ADCCFG1=0X02;//ain9为adc输入,移除上拉电阻
ADC_channel(5); //ADC 入口切换至 AIN5 口
Delay(50);
ADC_Value1 = Voltage(); //启动 ADC 转换,获得转换值
//ADC_Value1=8;
ADC_channel(9); //ADC 入口切换至 AIN9 口
Delay(50);
ADC_Value2 =Voltage(); //启动 ADC 转换,获得转换值
}
unsigned int kaiguan(void)
{
if (TL==1)
{
value|=(1<<0);//1
}
else
{
value&=~(1<<0);//0
}
if (WP==1)
{
value|=(1<<1);//2
}
else
{
value&=~(1<<1);//0
}
return value;
}
void dlLCD(unsigned int data1,unsigned long data2)
{
unsigned short c=0;
if((data2==-1)||(data2==-2))
{
c=data2;
data2*=-1;
}
t6=1;
s0 = data1 &(1<<0);//1是off,0是on
s1 = data1 &(1<<1);
switch(s0) //激光
{ case 0:t1= 1;t5=3;t10=0;break;
case 1:t1=0;t5=1;t10=1;break;
}
switch(s1) //水保护
{
case 0:t11 = 1;break;
case 2:t11 = 2;t5=1;break;
}
s3=data2/10;//左边数字
s4=data2%10;//右边数字
if((c==-1)||(c==-2))
{
s3=10;
}
switch(s3)
{
case 0:t7=3;t8=2;t9=3;t10=2;break;
case 1:t7=1;t8=0;t9=1;break;
case 2:t7=3;t8=1;t9=2;t10=2;break;
case 3:t7=3;t8=1;t9=1;t10=2;break;
case 4:t7=1;t8=3;t9=1;break;
case 5:t7=2;t8=3;t9=1;t10=2;break;
case 6:t7=2;t8=3;t9=3;t10=2;break;
case 7:t7=3;t8=0;t9=1;break;
case 8:t7=3;t8=3;t9=3;t10=2;break;
case 9:t7=3;t8=3;t9=1;t10=2;break;
case 10:t7=0;t8=1;t9=0;break;
}
// if((s0==1)&(s3==0)==1){t10=3;}
if((s0==0)&(s3==0)|(s3==2)|(s3==3)|(s3==5)|(s3==6)|(s3==8)|(s3==9)==1){t10=2;}
if((s0==1)&(s3==0)==1){t10=3;}
if((s0==1)&(s3==2)==1){t10=3;}
if((s0==1)&(s3==3)==1){t10=3;}
if((s0==1)&(s3==5)==1){t10=3;}
if((s0==1)&(s3==6)==1){t10=3;}
if((s0==1)&(s3==8)==1){t10=3;}
if((s0==1)&(s3==9)==1){t10=3;}
if((s3==4)&(s0==0)==1){t10=0;}
if((s3==7)&(s0==0)==1){t10=0;}
if((s3==1)&(s0==0)==1){t10=0;}
switch(s4)
{
case 0:t2=3;t3=2;t4=3;t6=3;break;
case 1:t2=1;t3=0;t4=1;break;
case 2:t2=3;t3=1;t4=2;t6=3;break;
case 3:t2=3;t3=1;t4=1;t6=3;break;
case 4:t2=1;t3=3;t4=1;break;
case 5:t2=2;t3=3;t4=1;t6=3;break;
case 6:t2=2;t3=3;t4=3;t6=3;break;
case 7:t2=3;t3=0;t4=1;break;
case 8:t2=3;t3=3;t4=3;t6=3;break;
case 9:t2=3;t3=3;t4=1;t6=3;break;
}
switch(step)
{
case 0:
if (times_delay<45)
{
break;
}
times_delay=0;
/////////////////////////////////////////////////////////////
P01=1;
P0VO = 0X01;//P01(com1)先高电平,P0=1/2vdd
switch(t1)
{
case 002 = 1; cnt1 = 1;break; //
case 102 = 1; cnt1 = 2;break; //
case 202 = 0; cnt1 = 2;break; //
case 302 = 0; cnt1 = 1;break; //
default:break;
}
switch(t2)
{
case 003 = 1; cnt2 = 1;break; //
case 103 = 1; cnt2 = 2;break; //
case 203 = 0; cnt2 = 2;break; //
case 303 = 0; cnt2 = 1;break; //
default:break;
}
switch(t3)
{
case 004 = 1; cnt3 = 1;break; //
case 104 = 1; cnt3 = 2;break; //
case 2:P04 = 0; cnt3 = 2;break; //
case 3:P04 = 0; cnt3 = 1;break; //
default:break;
}
switch(t4)
{
case 0:P05 = 1; cnt4 = 1;break; //
case 1:P05 = 1; cnt4 = 2;break; //
case 2:P05 = 0; cnt4 = 2;break; //
case 3:P05 = 0; cnt4 = 1;break; //
default:break;
}
switch(t5)
{
case 0:P17 = 1; cnt5 = 1;break; //
case 1:P17 = 1; cnt5 = 2;break; //
case 2:P17 = 0; cnt5 = 2;break; //
case 3:P17 = 0; cnt5 = 1;break; //
default:break;
}
switch(t6)
{
case 0:P20 = 1; cnt6 = 1;break; //
case 1:P20 = 1; cnt6 = 2;break; //
case 2:P20 = 0; cnt6 = 2;break; //
case 3:P20 = 0; cnt6 = 1;break; //
default:break;
}
switch(t7)
{
case 0:P21 = 1; cnt7 = 1;break; //
case 1:P21 = 1; cnt7 = 2;break; //
case 2:P21 = 0; cnt7 = 2;break; //
case 3:P21 = 0; cnt7 = 1;break; //
default:break;
}
switch(t8)
{
case 0:P24 = 1; cnt8 = 1;break; //
case 1:P24 = 1; cnt8 = 2;break; //
case 2:P24 = 0; cnt8 = 2;break; //
case 3:P24 = 0; cnt8 = 1;break; //
default:break;
}
switch(t9)
{
case 0:P12 = 1; cnt9 = 1;break; //
case 1:P12 = 1; cnt9 = 2;break; //
case 2:P12 = 0; cnt9 = 2;break; //
case 3:P12 = 0; cnt9 = 1;break; //
default:break;
}
switch(t10)
{
case 0:P26 = 1; cnt10 = 1;break; //
case 1:P26 = 1; cnt10 = 2;break; //
case 2:P26 = 0; cnt10 = 2;break; //
case 3:P26 = 0; cnt10 = 1;break; //
default:break;
}
switch(t11)
{
case 0:P27 = 1; cnt11 = 1;break; //
case 1:P27 = 1; cnt11 = 2;break; //
case 2:P27 = 0; cnt11 = 2;break; //
case 3:P27 = 0; cnt11 = 1;break; //
default:break;
}
step=1;
break;
case 1:
if (times_delay<45)
{
break;
}
times_delay=0;
/////////////////////////////////////////////////////////////////
// times_delay = 0;
P00=0;//po低电平,P1=1/2vdd
P0VO = 0X02;
cnt1 -- ;if (cnt1 == 0){P02=~P02;cnt1 =2;}
cnt2 -- ;if (cnt2 == 0){P03=~P03;cnt2 =2;}
cnt3 -- ;if (cnt3 == 0){P04=~P04;cnt3 =2;}
cnt4 -- ;if (cnt4 == 0){P05=~P05;cnt4 =2;}
cnt5 -- ;if (cnt5 == 0){P17=~P17;cnt5 =2;}
cnt6 -- ;if (cnt6 == 0){P20=~P20;cnt6 =2;}
cnt7 -- ;if (cnt7 == 0){P21=~P21;cnt7 =2;}
cnt8 -- ;if (cnt8 == 0){P24=~P24;cnt8 =2;}
cnt9 -- ;if (cnt9 == 0){P12=~P12;cnt9 =2;}
cnt10 -- ;if (cnt10 == 0){P26=~P26;cnt10 =2;}
cnt11 -- ;if (cnt11 == 0){P27=~P27;cnt11 =2;}
step=2;
break;
/////////////////////////////////////////////////////////////////
case 2:
if (times_delay<45)
{
break;
}
times_delay=0;
P01=0;
P0VO = 0X01;//P0=1/2vdd,P1低电平
cnt1 -- ;if (cnt1 == 0){P02=~P02;cnt1 =2;}
cnt2 -- ;if (cnt2 == 0){P03=~P03;cnt2 =2;}
cnt3 -- ;if (cnt3 == 0){P04=~P04;cnt3 =2;}
cnt4 -- ;if (cnt4 == 0){P05=~P05;cnt4 =2;}
cnt5 -- ;if (cnt5 == 0){P17=~P17;cnt5 =2;}
cnt6 -- ;if (cnt6 == 0){P20=~P20;cnt6 =2;}
cnt7 -- ;if (cnt7 == 0){P21=~P21;cnt7 =2;}
cnt8 -- ;if (cnt8 == 0){P24=~P24;cnt8 =2;}
cnt9 -- ;if (cnt9 == 0){P12=~P12;cnt9 =2;}
cnt10 -- ;if (cnt10 == 0){P26=~P26;cnt10 =2;}
cnt11 -- ;if (cnt11 == 0){P27=~P27;cnt11 =2;}
step=3;
break;
////////////////////////////////////////////////////////////////////
case 3:
if (times_delay<45)
{
break;
}
times_delay = 0;
P00=1;//
P0VO = 0X02;//po高电平,P1=1/2vdd
cnt1 -- ;if (cnt1 == 0){P02=~P02;cnt1 =2;}
cnt2 -- ;if (cnt2 == 0){P03=~P03;cnt2 =2;}
cnt3 -- ;if (cnt3 == 0){P04=~P04;cnt3 =2;}
cnt4 -- ;if (cnt4 == 0){P05=~P05;cnt4 =2;}
cnt5 -- ;if (cnt5 == 0){P17=~P17;cnt5 =2;}
cnt6 -- ;if (cnt6 == 0){P20=~P20;cnt6 =2;}
cnt7 -- ;if (cnt7 == 0){P21=~P21;cnt7 =2;}
cnt8 -- ;if (cnt8 == 0){P24=~P24;cnt8 =2;}
cnt9 -- ;if (cnt9 == 0){P12=~P12;cnt9 =2;}
cnt10 -- ;if (cnt10 == 0){P26=~P26;cnt10 =2;}
cnt11 -- ;if (cnt11 == 0){P27=~P27;cnt11 =2;}
step=0;
break;
default:
break;
///////////////////////////////////////////////////////////////////////////
}
}
void main(void)
{
IO_Init();
Timer_Init();
// ADC_channel( );
while(1)
{
kaiguan();
Voltage();
ADC_Multichannel();
// ADC_Multichannel2();
if(u==0)
{
dlLCD(value,ADC_Value2);
u=1;
Delay(800);
}
else if(u==1)
{
dlLCD(value,ADC_Value1);
u=0;
Delay(800);
}
}
}
void timer0() interrupt 1
{
TL0 = (65536 - 1200)%256;
TH0 = (65536 - 1200)/256;
times_delay++;
adc_delay++;
s_delay++;
}
|
|