|
下面是STC单片机推挽输出驱动数码管的proteus仿真原理图(工程文件可到本帖附件中下载):
单片机源程序如下:
- //#include <reg51.h>
- #include <STC12C5A60S2.h>
- #define u8 unsigned char
- #define u16 unsigned int
- #define u32 unsigned long int
- #define vtime 3000 //定时3ms,一帧8*3=24ms,频率=40Hz
- #define jz12 //jz132 :晶振选择132.710400MH//jz12 :12MH
- #ifdef jz12
- //正常12MH
- #define t8s_n 90
- #define psv 4
- #else
- //132.7014MH
- #define t8s_n 1000
- #define psv 900
- #endif
- void delay(void);
- //共阴数码管段码表
- u8 code distable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
- //位选码表
- u8 code numi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- //显示缓存
- u8 V_ram[]={0,0,3,4,5,6,7,8};
- u8 Vm[]={0,0,3,4,5,6,7,8};
- u8 wi=0,mn=0,nt=0,wbs=0; //位选循环变量
- u8 t_l,t_h,bs=0;
- u32 adv=0,adsum=0;
- main() //m1:m0 00=标准; 01=推挽; 10=输入; 11=开漏输出
- {
- u16 i=0;
- u16 temp=0;
- //u32 temp0=0,temp1,temp2;
- //m1:m0 00=标准; 01=推挽; 10=输入; 11=开漏输出
- //P0M1 = 0X00;
- // P0M0 = 0Xff;
- // P2M1 = 0X00;
- // P2M0 = 0Xff; //设定P0,P2推挽输出
- //P3M1 = 0Xff;
- //P3M0 = 0X00;
- P1M1 = 0x01;
- P1M0 = 0x08;
- TMOD = 0X50; //设定定时器0为16位计数方式
- //TH0 = (65536-vtime )/256;
- //TL0 = (65536-vtime )%256; //赋定时器0初值
- AUXR=0x80; //选择1T指令系统
- //ET0 = 1; //开定时器0中断
- AUXR1=AUXR1|0x04;
- P1ASF=0x01;
- ADC_CONTR=0x80;
- //TR0=1;
- //PX1=1;
- //PX0=1;
- //PT1=1;
- //IT0=1;
- //IT1=1;
- CCON = 0;
- CL = 0;
- CH = 0;
- CMOD = 0X0F;
- CCAP0H = 0X80;
- CCAP0L = 0X80;
- CCAPM0 = 0X42;
- CCAPM1 = 0X00;
- PCA_PWM0 = 0;
- CR = 1;
- EA = 1; //开总中断
- while(1)
- {
- if(ADC_CONTR&&0x10)
- {
- adv=((u32)ADC_RES<<8)+ADC_RESL;
- ADC_CONTR=0x88;//ADC_CONTR & 0xef;
- adsum=adsum+adv;
- i++;
- if(i==10000)
- {
- i=0;
- adsum=adsum/10000;
- if(adsum>1023)adsum=1023;
- V_ram[0]=adsum/1000;
- temp=adsum%1000;
- V_ram[1]=temp/100;
- temp=temp%100;
- V_ram[2]=temp/10;
- temp=temp%10;
- V_ram[3]=temp/1;
- //V_ram[4]=temp%10;
- mn=~(adsum>>2);
- adsum=0;
- }
- }
- delay();
- }
- }
- void delay()
- {
- u16 x,y,z;
- for(x=3;x>0;x--)
- for(y=3;y>0;y--)
- for(z=3;z>0;z--);
- }
- void t0_isp() interrupt 1 using 2 //13位计时中断
- {
- u8 dm,wx;
- dm=distable[V_ram[wi]]; //取显示段码
- //if(wi==wbs)dm=dm|0x80;
- wx=numi[wi]; //取位选码
- P2=0x00; //关显示
- P0=dm; //段码赋给P0口
- P2=wx; //点亮位选的那个数码管
- wi++;
- if(wi==4)wi=0;
- }
- void pca_isp() interrupt 7 //using 1 //计时开始中断
- {
- CF = 0;
- CCAP0H=mn;
- //CCAP0H++;
- }
-
- void int0_isp() interrupt 0 using 1 //计时开始中断
- {
- }
- void int1_isp() interrupt 2 using 3 //计时结束中断
- {
- }
- void t1_isp() interrupt 3 //using 1 //计时开始中断
复制代码
给大家分享一些我们老师给的一些经典的单片机程序源码, 一共有十多个.都有详细的注释,然大家快速的理解每一行代码的意思。而且有proteus仿真原理图。大家可以直接验证程序的对错.
本系列所有源码打包下载地址(含proteus仿真工程文件和源程序):
http://www.51hei.com/bbs/dpj-82474-1.html
本例程下载:
PWM.rar
(79.19 KB, 下载次数: 21)
|
|