|
包括C語言程序和proteus仿真
具有顯示時間 檢測溫度 重量檢測 聲光報警 以及電機(jī)控制等功能
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.gif (119.52 KB, 下載次數(shù): 30)
下載附件
2022-8-3 03:37 上傳
0.png (100.2 KB, 下載次數(shù): 26)
下載附件
2022-8-3 03:34 上傳
單片機(jī)源程序如下:
- #include "reg52.h"
- #include "ds1302.h"
- #include "lcd.h"
- #include "temp.h"
- #include "intrins.h"
- /*對數(shù)據(jù)類型進(jìn)行聲明定義*/
- typedef unsigned int u16;
- typedef unsigned char u8;
- #define ulong unsigned long
- #define GPIO_MOTOR P1
- /****I/O口定義****/
- sbit KEY1 = P3^0;
- sbit KEY2 = P3^1;
- sbit KEY3 = P3^2;
- sbit BEEP = P2^7;
- sbit LED1 = P3^3;
- sbit DOUT=P3^4; //定義2543數(shù)據(jù)讀取口
- sbit DIN =P3^5; //定義2543數(shù)據(jù)寫入口
- sbit CS =P3^6; //定義2543片選信號口
- sbit CLK =P3^7; //定義時鐘信號口
- /****變量定義****/
- bit adjust;//調(diào)節(jié)標(biāo)志位 0-1
- u8 num;//鬧鐘調(diào)節(jié)位
- u8 hour_shi, hour_ge, minute_shi, minute_ge;//鬧鐘調(diào)節(jié)
- ulong Volt;//測量的電壓值
- ulong AD_Volt;//測量的電壓值
- ulong quality;//檢測重量
- ulong quality_set=2000;//設(shè)置的重量閾值200克
- unsigned char code FFW_X[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //X反轉(zhuǎn)順序
- unsigned char code FFZ_X[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正轉(zhuǎn)順序
- unsigned char Direction,stop=0;
- void delay30ms(void) //誤差 0us
- {
- unsigned char a,b;
- for(b=101;b>0;b--)
- for(a=147;a>0;a--);
- }
- void Delay(unsigned int t)
- {
- unsigned int k;
- while(t--)
- {
- for(k=0; k<80; k++)
- { }
- }
- }
- void Motor()
- {
- unsigned char a=30;
- unsigned char i;
- for(i=0;i<8;i++)
- {
- if(Direction==1&&stop==0) //正轉(zhuǎn)電機(jī)&電機(jī)沒按下停止
- GPIO_MOTOR = FFW_X[i]&0x0F;//取數(shù)據(jù)
- if(Direction==2&&stop==0) //反轉(zhuǎn)&X電機(jī)&電機(jī)沒按下停止
- GPIO_MOTOR = FFZ_X[i]&0x0F;
-
- Delay(a);
- }
- }
- void Read2543(uchar addr)
- {
- uint ad=0;
- uchar i;
- CLK=0;
- CS=0;//片選段,啟動2543
- addr<<=4;//對地址位預(yù)處理
- for(i=0;i<12;i++) //12個時鐘走完,完成一次讀取測量
- {
- if(DOUT==1)
- ad=ad|0x01;//單片機(jī)讀取ad數(shù)據(jù)
- DIN=addr&0x80;//2543讀取測量地址位
- CLK=1;
- ;;;//很短的延時
- CLK=0;//產(chǎn)生下降沿,產(chǎn)生時鐘信號
- ;;;
- addr<<=1;
- ad<<=1;//將數(shù)據(jù)移位準(zhǔn)備下一位的讀寫
- }
- CS=1;//關(guān)2543
- ad>>=1;
- Volt=ad;//取走轉(zhuǎn)換結(jié)果
- //Volt=Volt*1221;//例子的滿量程為5V,轉(zhuǎn)換分辯率為12位(2的12次方=4096) 。即最大值是255,5/4096=1221mV,即例子中的1V代表實(shí)際1221mV
-
- Volt=Volt*5000/4095;
- quality=(Volt/5.12+40)*10/4;//計算壓力
- //Pressure=(Volt/5.41+40)*10/4;
-
- }
- void KEY_Control()//按鍵調(diào)節(jié)
- {
- if(KEY1==0)
- {
- delay30ms();
- if(KEY1==0)
- {
- stop=0;
- Direction=1;
- }
- }
- if(KEY2==0)
- {
- delay30ms();
- if(KEY2==0)
- {
- stop=0;
- Direction=2;
- }
- }
- if(KEY3==0)
- {
- delay30ms();
- if(KEY3==0)
- {
-
- stop=1;
- }
- }
-
- }
- void show_quality(void)//顯示重量
- {
- quality=quality-30;
- DisplayOneChar(11,0,(char)(quality/1000+'0'));
- DisplayOneChar(12,0,(char)(quality%1000/100+'0'));
- DisplayOneChar(13,0,(char)((quality%100)/10+'0'));
-
- if( quality <= quality_set )
- {
- stop=0;
- Direction=1;
- BEEP=0;
- LED1=0;
- Delay(200);
- BEEP=1;
- LED1=1;
- Delay(300);
- }
-
- if( quality > quality_set )
- {
- stop=1;
-
- BEEP=1;
- LED1=1;
-
- }
- }
- void main()//主函數(shù)
- {
-
- LcdInit();
- DisplayOneChar(0,0,'2'); DisplayOneChar(1,0,'0'); DisplayOneChar(4,0,'-'); DisplayOneChar(7,0,'-');//2022-05-05 6
- DisplayOneChar(2,1,':'); DisplayOneChar(5,1,':'); DisplayOneChar(2,1,':');//13:39:40
- DisplayListChar(9,1,"T:"); DisplayOneChar(13,1,'.'); DisplayOneChar(15,1,'C');//T:23.5C
- DisplayOneChar(15,0,'g');
- while(1)
- {
-
- KEY_Control();//按鍵控制
- Motor();//步進(jìn)電機(jī)控制
-
- Read2543(0);//檢測重量
- show_quality();//顯示重量
-
-
-
- ReadTemperature();//讀取溫度
- DisplayTemp();//顯示溫度
-
-
- Ds1302ReadTime();//讀取時間
- DisplayTime();//顯示時間
-
-
- }
- }
復(fù)制代碼
Keil代碼與Proteus8.8仿真下載:
寵物投食系統(tǒng).zip
(201 KB, 下載次數(shù): 88)
2022-8-2 16:54 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|