電路原理圖如下:
電路圖
設(shè)計(jì)要求
(1)利用光電開(kāi)關(guān)計(jì)數(shù)系統(tǒng),可在0-999任意設(shè)定計(jì)數(shù)值,超過(guò)設(shè)定值時(shí),步進(jìn)電機(jī)停止。計(jì)數(shù)時(shí)步進(jìn)電機(jī)以某一恒定速度轉(zhuǎn)動(dòng)。 (2)超過(guò)設(shè)定值后利用220V報(bào)警燈提示。 (3)數(shù)碼管顯示當(dāng)前計(jì)數(shù)值和設(shè)定值。(按鍵選擇是出于設(shè)定還是計(jì)數(shù)狀態(tài))
單片機(jī)程序如下: - #include <reg51.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit p1=P2^0;
- sbit p2=P2^1;
- sbit p3=P2^2;
- sbit p4=P2^3;
- sbit q1=P2^4;
- sbit q2=P2^5;
- sbit q3=P2^6;
- uchar zongshu,szzongshu;
- uchar baiwei,shiwei,gewei;
- uchar buff,times,j;
- uchar code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar idata value[8];
- uchar key,a1,a2,a3;
- void fs_chushihua (void)
- {
- SCON=0x50; //串行控制寄存器01010000,工作方式1,允許接收
- TMOD=0x20; //工作方式控制寄存器00100000,定時(shí)器1,工作方式2
- TH1=0xfd;
- TL1=0xfd; //設(shè)置波特率9600
- TI=0; //發(fā)送中斷標(biāo)識(shí)位,=1表示發(fā)送結(jié)束
- TR1=1; //打開(kāi)定時(shí)器T1
- }
- void delay (void)
- {
- uint i;
- for (i=0;i<230;i++);
- }
- void delay1ms (void) //開(kāi)機(jī)延時(shí)1ms
- {
- uchar i;
- for (i=200;i>0;i--);
- }
- void delay5ms (void) //防止抖動(dòng),延時(shí)5ms
- {
- unsigned char i,j;
- for (i=5;i>0;i--)
- for (j=230;j>0;j--);
- }
- void xianshi (void)
- {
- switch (value[times-1])
- {
- case 0 : P0=dispcode[value[times-1]];break;
- case 1 : P0=dispcode[value[times-1]];break;
- case 2 : P0=dispcode[value[times-1]];break;
- case 3 : P0=dispcode[value[times-1]];break;
- case 4 : P0=dispcode[value[times-1]];break;
- case 5 : P0=dispcode[value[times-1]];break;
- case 6 : P0=dispcode[value[times-1]];break;
- case 7 : P0=dispcode[value[times-1]];break;
- case 8 : P0=dispcode[value[times-1]];break;
- case 9 : P0=dispcode[value[times-1]];break;
- }
- }
- void key_scan (void) //鍵盤(pán)輸入掃描函數(shù)
- {
- uchar hang,lie;
- P1=0xf0;
- if ((P1&0xf0)!=0xf0) //行碼為0,列碼為1
- {
- delay1ms();
- if((P1&0xf0)!=0xf0) //有鍵按下,列碼變?yōu)?
- {
- hang=0xfe; //逐行掃描
- times++;
- if (times==9)
- times=1;
- while ((hang&0x10)!=0) //掃描完4行后跳出
- {
- P1=hang;
- if ((P1&0xf0)!=0xf0) //本行有鍵按下
- {
- lie=(P1&0xf0)|0x0f;
- buff=((~hang)+(~lie));
- switch (buff) //buff等于哪個(gè),就執(zhí)行哪個(gè)。
復(fù)制代碼
更多請(qǐng)下載附件��!
全部資料51hei下載地址:
光電計(jì)數(shù).rar
(279.5 KB, 下載次數(shù): 115)
2019-3-13 10:39 上傳
點(diǎn)擊文件名下載附件
|