/* ????????12C5A60S2???AD???????·??????????1602?? */
/* ??????????????8·?????????P1ASF ???????????? */
#include "stdio.h"
//#include "STC12C5A60S2.H"
#include <reg51.h>
#include "lcd1602.h "
#include<intrins.h>
#define uchar unsigned char;
#define uint unsigned int;
#define ADC_OFF() ADC_CONTR = 0
#define ADC_ON (1 << 7)
#define ADC_90T (3 << 5)
#define ADC_180T (2 << 5)
#define ADC_360T (1 << 5)
#define ADC_540T 0
#define ADC_FLAG (1 << 4) //?????t??0
#define ADC_START (1 << 3) //????????0
#define ADC_CH0 0
#define ADC_CH1 1
#define ADC_CH2 2
#define ADC_CH3 3
#define ADC_CH4 4
#define ADC_CH5 5
#define ADC_CH6 6
#define ADC_CH7 7
sfr P1ASF = 0x9D; //12C5204AD/S2????D????a??????(AD???LVD)????
sfr ADC_CONTR = 0xBC; //AD???????
sfr ADC_RES = 0xBD; //??8λ
sfr ADC_RESL = 0xBE; //??2λ
sbit EADC = IE^5; //AD ?ж?
//pwm ????
//#define FOSC 18432000L
sfr CCON=0xD8;
sbit CCF0=CCON^0;
sbit CCF1=CCON^1;
sbit CR=CCON^6;
sbit CF=CCON^7;
sfr CMOD=0xD9;
sfr CL=0XE9;
sfr CH=0XF9;
sfr CCAPM0=0XDA;
sfr CCAP0L=0XEA;
sfr CCAP0H=0XFA;
sfr CCAPM1=0XDB;
sfr CCAP1L=0XEB;
sfr CCAP1H=0XFB;
sfr PCAPWM0=0XF2; //p1.3
sfr PCAPWM1=0XF3; //p1.4
sbit PWM1=P3^0;
sbit PWM2=P3^1;
unsigned char num=0,num1=0;
unsigned code table1[]={101.5,102,102.8,103}; //3.01 2.99 2.97
unsigned code table2[]={204,100,252,53};
//uchar b,i,ge,shi,bai;
uchar da1=0,da2=0,da3=0; da4=0;
double Data,c;
char a[5]="";
uchar ADC_Chanul_Turn=0;
/*------------------------------------------------
ADC????
------------------------------------------------*/
void InitADC()//???AD?????
{
P1ASF=0X27; //0xff????P1??????ADC???,0x07=0000,1111 ?????P10(??????5),P11,P12 p15???????
ADC_RES=0X00; //?????8λ????????
if(ADC_Chanul_Turn%5==0)//????????
{
ADC_CONTR=0xF0; //AD???????????,1111,0000 P10??
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xE8; //1110,1000??????Flag????λStart??
}
if(ADC_Chanul_Turn%5==1)
{
ADC_CONTR=0xF1; // 1111,0001 P11??
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xE9; //1110,1001
}
if(ADC_Chanul_Turn%5==2)
{
ADC_CONTR=0xF2; //1111,0010 P12??
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xEA; //1110,1010
}
if(ADC_Chanul_Turn%5==4)
{
ADC_CONTR=0xF5; //1111,0101 P15??
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xED; //1110,1101
}
}
void timer0() interrupt 1 //interrupt 1: ?????0??interrupt3???????3
{
TH0=(65536-20000)/256; //???λ??????????Xms??????????????65536-X????65536,????16λ????????λ??
TL0=(65536-20000)%256; //???λ
InitADC();
}
void adc_isr() interrupt 5 //FLAG???λ??λ?????ж??????????????????????????????0????
{
//V_5REF=V_1REF*256/da_ref;
if(ADC_Chanul_Turn%5==0) //????????
{
da1=ADC_RES; //?????????
Data=((double)da1/256)*5; //???λ?????????Data,
c =Data;
}
if(ADC_Chanul_Turn%5==1)
{
da2=ADC_RES; //?????????
Data=((double)da2/256)*5; //???λ????????Data,
c =Data;
}
if(ADC_Chanul_Turn%5==2)
{
da3=ADC_RES; //?????????
Data=((double)da3/256)*5; //???λ????????Data,
c =Data;
}
if(ADC_Chanul_Turn%5==4)
{
da4=ADC_RES; //?????????
Data=((double)da4/256)*5; //???λ????????Data,
c =Data;
}
a[0]=((int)c%10+0x30);//??λ?????<5,???и??? //0x30: ASCAI????????0??,?????????????????????????????????1602??????????
a[1]=0x2e; //С????
a[2]=((int)(c*10)%10+0x30); // ???λ
a[3]=((int)(c*100)%10+0x30);// ???λ
//a[4]=((int)(c*1000)%10+0x30);
a[4]='\0'; // ?????β?????????????
//if(ADC_Chanul_Turn%5==0) LCD_Write_String(0,0,a);
if(ADC_Chanul_Turn%5==1) LCD_Write_String(2,0,a);
if(ADC_Chanul_Turn%5==2) LCD_Write_String(7,0,a);
if(ADC_Chanul_Turn%5==4) LCD_Write_String(12,0,a);
ADC_CONTR&=0xEF; //???λ????
ADC_Chanul_Turn++;
if(ADC_Chanul_Turn==255)
ADC_Chanul_Turn=0;
}
//PWM ????
void init_PWM()
{
CCON=0;
CL=0;
CH=0;
CR=1;
CMOD=0X00; //??????PWM??????
//CCAP0H=CCAP0L=0X1E;
PCAPWM0=0X00;
//CCAPM0=0X42; //?????PWM
// CCAP1H=CCAP1L=0X80;
PCAPWM1=0X00;
// CCAPM1=0X42;CR=1; //????????????
}
void key_Contral_PWM(void);
void delay(int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
void main()
{
LCD_Init();
LCD_Clear(); //????
init_PWM();
InitADC();
// LCD_Write_Char(2,1,"??");
DelayMs(255);
TH0=(65536-20000)/256; //???????0
TL0=(65536-20000)%256;
EA=1; //??????ж?
ET0=1; //???????????ж?
EADC=1; //????ADC?ж?
TR0=1;
while (1)
{
CCAP0H = CCAP0L =table1[num];
CCAPM0 = 0x42;
CCAP1H = CCAP1L = table2[num1];
CCAPM1 = 0x42;
key_Contral_PWM();
if(num==4)
num=0;
if(num1==4)
num1=0;
}
}
void key_Contral_PWM(void)
{
if(PWM1==0)
{
delay(20);
if(PWM1==0)
{
num++;
while(!PWM1);
}
}
if(PWM2==0)
{
delay(20);
if(PWM2==0)
{
num1++;
while(!PWM2);
}
}
}
AD数据现在只显示了两路,而且还无法及时刷新。p10口做基准电压,P11 12 15 口做三路AD。
两路PWM控制DA输出
主程序不知道怎么改。求助
|