#ifndef AT89CX051_HEADER_FILE
#define AT89CX051_HEADER_FILE 1
//Byte 寄存器
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr P1 = 0x90;
sfr SCON = 0x98;
sfr SBUF = 0x99;
sfr IE = 0xA8;
sfr P3 = 0xB0;
sfr IP = 0xB8;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
//PCON
#define IDL_ 0x01
#define STOP_ 0x02
#define EWT_ 0x04
#define EPFW_ 0x08
#define WTR_ 0x10
#define PFW_ 0x20
#define POR_ 0x40
#define SMOD_ 0x80
//TCON
sbit IT0 = 0x88;
sbit IE0 = 0x89;
sbit IT1 = 0x8A;
sbit IE1 = 0x8B;
sbit TR0 = 0x8C;
sbit TF0 = 0x8D;
sbit TR1 = 0x8E;
sbit TF1 = 0x8F;
//TMOD值
#define T0_M0_ 0x01
#define T0_M1_ 0x02
#define T0_CT_ 0x04
#define T0_GATE_ 0x08
#define T1_M0_ 0x10
#define T1_M1_ 0x20
#define T1_CT_ 0x40
#define T1_GATE_ 0x80
#define T1_MASK_ 0xF0
#define T0_MASK_ 0x0F
//P1
sbit P1_0 = 0x90;
sbit P1_1 = 0x91;
sbit P1_2 = 0x92;
sbit P1_3 = 0x93;
sbit P1_4 = 0x94;
sbit P1_5 = 0x95;
sbit P1_6 = 0x96;
sbit P1_7 = 0x97;
sbit AIN0 = 0x90; // + 输入
sbit AIN1 = 0x91; //- 输入
//SCON
sbit RI = 0x98;
sbit TI = 0x99;
sbit RB8 = 0x9A;
sbit TB8 = 0x9B;
sbit REN = 0x9C;
sbit SM2 = 0x9D;
sbit SM1 = 0x9E;
sbit SM0 = 0x9F;
//IE
sbit EX0 = 0xA8; // 开INT0
sbit ET0 = 0xA9; // 开ET0
sbit EX1 = 0xAA; // 开INT1
sbit ET1 = 0xAB; // Timer 1
sbit ES = 0xAC; // 开ES
sbit ET2 = 0xAD; // Timer 2
sbit EA = 0xAF; // 关中断
//P3
sbit P3_0 = 0xB0;
sbit P3_1 = 0xB1;
sbit P3_2 = 0xB2;
sbit P3_3 = 0xB3;
sbit P3_4 = 0xB4;
sbit P3_5 = 0xB5;
sbit P3_7 = 0xB7;
sbit RXD = 0xB0; // 串行收中断
sbit TXD = 0xB1; //串行发中断
sbit INT0 = 0xB2; // INT0
sbit INT1 = 0xB3; // INT11
sbit T0 = 0xB4; // Timer 0
sbit T1 = 0xB5; // Timer 1
sbit AOUT = 0xB6; // 模拟输出
//IP
sbit PX0 = 0xB8;
sbit PT0 = 0xB9;
sbit PX1 = 0xBA;
sbit PT1 = 0xBB;
sbit PS = 0xBC;
//PSW
sbit P = 0xD0;
sbit FL = 0xD1;
sbit OV = 0xD2;
sbit RS0 = 0xD3;
sbit RS1 = 0xD4;
sbit F0 = 0xD5;
sbit AC = 0xD6;
sbit CY = 0xD7;
//中断地址= (Number * 8) + 3
#define IE0_VECTOR 0 // 0x03 INT0
#define TF0_VECTOR 1 // 0x0B Timer 0
#define IE1_VECTOR 2 // 0x13 INT11
#define TF1_VECTOR 3 // 0x1B Timer 1
#define SIO_VECTOR 4 // 0x23 串行口
#endif
// io分配:
/* OUTPUT:
P1.0 ...... 模拟量输入
P1.1 ...... DA输入比较基准电压
P1.2~7..... R-2R DA电阻网络
P3.7 ...... LED 模拟亮度输出
CPU CLOCK EQU 6M */
//#pragma src
#include "AT89x051.h"
#include <stdlib.h>
#include<math.h>
#include<intrins.h>
//变量类型标识的宏定义
#define Uchar unsigned char
#define Uint unsigned int
#define Ledlight() (P3 &= 0x7f)
#define Leddark() (P3 |= 0x80)
sbit P36 = P3^6; // 比较器内部判断脚
sbit LED = P3^7; // 一个发光二级管观察亮度变化
// 内部标志位定义
bit less; // 比较是否大于 1.小于,0.大于
// 全局变量定义
Uchar timer1, // 通用延时计数器
timer2, // 按键蜂鸣器反应定时器
adcdata, // ad转换变量
pwm1; // PWM输出比例
// 函数列表
void DelayMs(unsigned int number); // 毫秒延时
void timers0(); // 在定时器中断中做数码管的扫描显示(ct0)
void Initall(void); // 系统初始化
void timers1(void); // TC1定时器中断用于扫描显示与键盘
Uchar adcread(void); // adc转换程序
void main(void) using 0
{
DelayMs(120);
Initall();
pwm1 = adcread();
LED=1;
while(1)
{
pwm1 = adcread();
timer2=10;
while (timer2);
}
}
// 毫秒延时
void DelayMs(unsigned int number)
{
unsigned char temp;
for(;number!=0;number--)
for(temp=112;temp!=0;temp--);
}
//在定时器中断中做LED的PWM输出
void timers0() interrupt 1 using 1
{
TH0 = 0xff;
TL0 = 0xd0;
timer1--;
if (timer1==pwm1)LED=0;
if (timer1==0){
LED=1;
timer1=0x40;
timer2--;
};
}
//6位 ADC转换
Uchar adcread(void)
{
Uchar i=0x3f,temp=0;
P36 =1;
P1 = 3; _nop_();_nop_(); //从零开始
while ((i--)&& (P36))
{
temp += 4;
P1 = temp|3;
_nop_();
}
temp >>= 2;
return temp;
}
//系统初始化
void Initall(void)
{
TMOD = 0x11; // 0001 0001 16进制计数器
IP = 0x8; // 0000 1000 t1优先
IE = 0x8A; // 1000 1010 t0,t1中断允许
TCON = 5; // 0000 0101 外部中断低电平触发
TR0 = 1; // 打开定时器中断,IE中已经打开,在明示一下
TR1 = 0;
ET0 = 1;
ET1 = 0;
P1 = 0xff;
}
// TC1定时器中断用于扫描显示与键盘(ct1)
void timers1(void) interrupt 3 using 2
{
_nop_(); //实验中没有启用
}