51单片机频率采集+lcd1602+按键控制PWM
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #include "lcd1602.h"
- #define uchar unsigned char//宏定义
- #define uint unsigned int//宏定义
- sbit ji=P2^1;
- sbit jia_key=P3^2;
- sbit jian_key=P3^3;
- uchar sw,w,q,b,s,g,num=0,gao_num=1;di_num=3;
- void timer_init ();
- void delay (unsigned int a);
- unsigned long fre; //频率
- unsigned char time; //时间
- unsigned long count=0; //计数
- void delay(uint z) //延时子程序
- {
- uint x,y;
- for(x = z;x > 0;x--)
- for(y = 125;y > 0;y--);
- }
- /***********************************************************/
- /**************************************************/
- void timer_init (void) //定时器初始化
- {
- TMOD = 0x06; //计数器0工作 工作方式2,自动重装初值
- TH0 = 0; //计数器初值为0
- TL0 = 0;
- TR0 = 1; //计数器开始计数
- ET0 = 1; //打开计数器0中断
- RCAP2H = (65536 - 62500) / 256; //在程序初始化的时候给RCAP2H和RCAP2L赋值
- RCAP2L = (65536 - 62500) % 256; //TH2与TL2将会在中断产生时断使TH2 = RCAP2H,TL2 = RCAP2L.
- TH2 = RCAP2H; //12M晶振喜爱每次中断 62.5ms
- TL2 = RCAP2L;
- ET2 = 1; //打开定时器2中断
- TR2 = 1; //定时器2开始计时
- EA = 1; //打开总中断
-
- }
- void timer2 (void) interrupt 5 //定时器2中断62.5ms 服务函
- {
- time++;
- TF2 = 0; //定时器2的中断标志位TF2不能由硬件清零,所有要在中断服务程序中将其清零
- if (time == 16) //定时1s时间到
- {
- time = 0; //计时清零
- EA = 0; //关闭中断
- fre =((long)count*256 + TL0); //count*256强制转换为long型,否则将不产生进位
- TL0 = 0; //清零计数器0计数
- TH0 = 0;
- count = 0; //清零计数器计数
- EA = 1; //打开中断
-
- }
- }
- /****************************************************/
- void timer0 (void) interrupt 1 //计数器0中断
- {
-
- count++;
- }
- /*****************************************************/
- void display_frequency(unsigned long fre)
- {
-
- unsigned char bai,shi,ge,xiao,xiaoo;
-
- bai=fre/10000;
- shi=fre%10000/1000;
- ge=fre%1000/100;
- xiao=fre%100/10;
- xiaoo=fre%10;
- LCD1602_write_com(0x80+7); //??????????2??9? ???????1??+8??
- LCD1602_write_data(0x30+bai); //bai????2??8? ???????,
- LCD1602_write_com(0x80+8);
- LCD1602_write_data(0x30+shi); //??????,?????1 ??shi?????
- LCD1602_write_com(0x80+9);
- LCD1602_write_data(0x30+ge); //ge???shi?
- LCD1602_write_com(0x80+10);
- LCD1602_write_data(0x30+xiao);
- LCD1602_write_com(0x80+11);
- LCD1602_write_data(0x30+xiaoo);
- LCD1602_write_com(0x80+12);
- LCD1602_write_word("hz");
- }
- void key()
- {
- if(jia_key==0)
- {
- delay(5);
- if(jia_key==0)
- {
- num++;
- if(num==4)
- num=3;
- while(jia_key==0);
- }
- }
- if(jian_key==0)
- {
- delay(5);
- if(jian_key==0)
- {
- if(num!=0)
- num--;
- else
- num=0;
- while(jian_key==0);
- }
- }
- }
- void dispose()
- {
- switch(num)
- {
- case 0:gao_num=1;di_num=3;break;
- case 1:gao_num=2;di_num=2;break;
- case 2:gao_num=3;di_num=1;break;
- case 3:gao_num=4;di_num=0;break;
- }
- }
- void qudong()
- {
- uchar i;
- if(di_num!=0)
- {
- for(i=0;i<di_num;i++)
- {
- ji=0;
- display_frequency(fre);
- // delay(2);
- }
- }
- for(i=0;i<gao_num;i++)
- {
- ji=1;
- display_frequency(fre);
- // delay(2);
- }
- }
-
- void main ()
- {
- timer_init ();//定时/计数器初始化
- Init_LCD1602();
-
- while (1)
- {
- // ji=0;
- // display_frequency(fre);
- key();
- dispose();
- qudong();
- }
-
- }
复制代码
所有资料51hei提供下载:
51单片机.rar
(866.53 KB, 下载次数: 22)
|