標(biāo)題: 基于單片機(jī)的藍(lán)牙調(diào)光制作源碼 [打印本頁]

作者: 295735223    時(shí)間: 2018-5-11 00:46
標(biāo)題: 基于單片機(jī)的藍(lán)牙調(diào)光制作源碼
這是我以前做的一個(gè)課程設(shè)計(jì),通過藍(lán)牙APP發(fā)送指令,能夠調(diào)節(jié)燈帶亮度。燈帶通過晶閘管控制的?刂撇糠郑

硬件整體圖:


單片機(jī)源程序如下:
  1. #include <STC15.h>
  2. #include "OLED_Init.h"
  3. #include "UART.h"
  4. #include "pwm.h"

  5. void main()
  6. {
  7.     Init_OLED();
  8.     UartInit();
  9.     Init_T0_PWM();
  10.    
  11.     EA = 1;
  12.    
  13.     while(1)
  14.     {
  15.         writeTime();
  16.         RH();       //讀取溫度
  17.         OLED_ShowString(48,2,timeData,16);      //顯示當(dāng)前時(shí)間
  18.         OLED_ShowNum(48,4,Temperature,2,16);    //顯示溫濕度
  19.         OLED_ShowNum(48,6,Humidity,2,16);
  20.     }
  21. }
復(fù)制代碼

  1. /*------------------------------------------------------------------*/
  2. /* If you want to use the program or the program referenced in the  */
  3. /* article, please specify in which data and procedures from STC    */
  4. /*------------------------------------------------------------------*/

  5. #include <stc15.h>
  6. #include "PWM.h"

  7. /*************        功能說明        **************

  8. 本程序演示使用定時(shí)器做軟件PWM。

  9. 定時(shí)器0做16位自動(dòng)重裝,中斷,從T0CLKO高速輸出PWM。

  10. 本例程是使用STC15F/L系列MCU的定時(shí)器T0做模擬PWM的例程。

  11. PWM可以是任意的量程。但是由于軟件重裝需要一點(diǎn)時(shí)間,所以PWM占空比最小為32T/周期,最大為(周期-32T)/周期, T為時(shí)鐘周期。

  12. PWM頻率為周期的倒數(shù)。假如周期為6000, 使用24MHZ的主頻,則PWM頻率為4000HZ。

  13. ******************************************/

  14. #define ZQ  10      //ZD:周期=ZD*100US
  15. #define ZB  
  16. #define                PWM_HIGH_MIN        0                                //限制PWM輸出的最小占空比。用戶請勿修改。
  17. #define                PWM_HIGH_MAX        10        //20級光強(qiáng)度調(diào)節(jié)

  18. sbit        PWM_OUT = P0^0;                //定義PWM輸出引腳。

  19. typedef         unsigned char        u8;
  20. typedef         unsigned int        u16;
  21. typedef         unsigned long        u32;

  22. unsigned int PWM_Temp=0,PWM_ZB=1;


  23. /****************定時(shí)器1初始化****************/
  24. void Init_T0_PWM(void)                //100微秒@22.1184MHz
  25. {
  26.         AUXR |= 0x80;                //定時(shí)器時(shí)鐘1T模式
  27.         TMOD &= 0xF0;                //設(shè)置定時(shí)器模式
  28.         TMOD |= 0x01;                //設(shè)置定時(shí)器模式
  29.         TL0 = 0x5C;                //設(shè)置定時(shí)初值
  30.         TH0 = 0xF7;                //設(shè)置定時(shí)初值
  31.         TF0 = 0;                //清除TF0標(biāo)志
  32.         TR0 = 1;                //定時(shí)器0開始計(jì)時(shí)
  33.     ET0 = 1;
  34. }



  35. /********************* Timer0中斷函數(shù)************************/
  36. void timer0_int (void) interrupt 1 using 1
  37. {
  38.         TL0 = 0x5C;                //設(shè)置定時(shí)初值
  39.         TH0 = 0xF7;                //設(shè)置定時(shí)初值
  40.    
  41.     if(PWM_Temp==ZQ) PWM_Temp=1;
  42.    
  43.     if(PWM_ZB >= PWM_HIGH_MAX)                PWM_ZB = PWM_HIGH_MAX;        //如果寫入大于最大占空比數(shù)據(jù),則強(qiáng)制為最大占空比。
  44.         if(PWM_ZB <= PWM_HIGH_MIN)                PWM_ZB = PWM_HIGH_MIN;        //如果寫入小于最小占空比數(shù)據(jù),則強(qiáng)制為最小占空比。   
  45.    
  46.     if(PWM_ZB>=PWM_Temp) {PWM_OUT=1;}else{PWM_OUT=0;}
  47.     PWM_Temp++;  
  48. }

復(fù)制代碼



所有資料51hei提供下載:
藍(lán)牙調(diào)光程序.rar (85.94 KB, 下載次數(shù): 29)









歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1