久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6381|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

用51單片機(jī)實(shí)現(xiàn)紅外遙控風(fēng)扇程序 舵機(jī)搖頭

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:543246 發(fā)表于 2019-5-20 21:35 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
舵機(jī)信號(hào)線連接 P3.5
電機(jī)連接 uln2003 A腳
ch-         停止舵機(jī)轉(zhuǎn)動(dòng)
ch        開始舵機(jī)轉(zhuǎn)動(dòng)
prev         停止風(fēng)扇轉(zhuǎn)動(dòng)
next         開始風(fēng)扇轉(zhuǎn)動(dòng)
-            風(fēng)扇減速
+            風(fēng)扇加速

具體代碼見附件
一、元件清單
1、9G舵機(jī)                            1個(gè)
2、直流電機(jī)                     1個(gè)
3、電機(jī)支架                          1組
4、51主板                     1個(gè)
5、杜邦線                            若干
6、螺絲、螺帽            若干
二、軟件
              1、Keil4軟件
              2、程序燒錄軟件
三、硬件安裝
1、舵機(jī)
2、直流電機(jī)
四、硬件接線
1.舵機(jī)接線說明
舵機(jī)
51開發(fā)板
紅色
VCC
棕色
GND
橙色
P35

2.直流電機(jī)接線說明
一個(gè)腳接步進(jìn)電機(jī)模塊的+,另一個(gè)腳接步進(jìn)電機(jī)模塊的A 。



3.實(shí)物接線圖
3.1602直插即可。
五、實(shí)驗(yàn)操作說明
用紅外遙控器對(duì)準(zhǔn)紅外接收頭,操作即可。

單片機(jī)源程序如下:
  1. /*******************************************************************************
  2. * 實(shí)驗(yàn)名                           : 1602顯示紅外線值實(shí)驗(yàn)
  3. * 使用的IO             : 電機(jī)用P1口,鍵盤使用P3.0、P3.1、P3.2、P3.3
  4. * 實(shí)驗(yàn)效果       : LCD1602顯示出讀取到的紅外線的值
  5. *        注意                                         :
  6. *******************************************************************************/
  7. #include<reg51.h>
  8. #include"lcd.h"
  9. #include <stdio.h>
  10. //#include "delay.h"


  11. sfr T2CON  = 0xC8;          //timer2 control register
  12. sfr RCAP2L = 0xCA;
  13. sfr RCAP2H = 0xCB;
  14. sfr TL2    = 0xCC;
  15. sfr TH2    = 0xCD;

  16. typedef unsigned char BYTE;
  17. typedef unsigned int WORD;

  18. #define FOSC 12000000L      //System frequency
  19. #define BAUD 9600       //UART baudrate

  20. /*Define UART parity mode*/
  21. #define NONE_PARITY     0   //None parity
  22. #define ODD_PARITY      1   //Odd parity
  23. #define EVEN_PARITY     2   //Even parity
  24. #define MARK_PARITY     3   //Mark parity
  25. #define SPACE_PARITY    4   //Space parity

  26. #define PARITYBIT NONE_PARITY   //Testing even parity

  27. sbit bit9 = P2^2;           //P2.2 show UART data bit9
  28. bit busy;

  29. sbit Sevro_moto_pwm = P3^5;           //接舵機(jī)信號(hào)端輸入PWM信號(hào)調(diào)節(jié)速度

  30. sbit FanPin=P1^0;

  31. unsigned int pwm_val_left  = 0;//變量定義
  32. unsigned char push_val_left =14;//舵機(jī)歸中,產(chǎn)生約,1.5MS 信號(hào)
  33. unsigned int  timer=0;                        //延時(shí)基準(zhǔn)變量
  34. unsigned int  Servo_Ctr=0;

  35. unsigned int pwm_val_Fan=0;
  36. unsigned char Fan_speed=199;

  37. int flag=0;
  38. sbit IRIN=P3^2;

  39. unsigned char code CDIS1[13]={" Red Control "};
  40. unsigned char code CDIS2[13]={" IR-CODE:--H "};
  41. unsigned char code Test_OK[2]={"OK"};
  42. unsigned char IrValue[6];
  43. unsigned char Time;
  44. unsigned char IrOK=0;
  45. /*--------------------------------------------------------------
  46.                            函數(shù)聲明
  47. --------------------------------------------------------------*/
  48. void InitUART(void);
  49. char putchar(char c);
  50. void IrInit();
  51. void DelayMs(unsigned int );
  52. void SendString(char *s);
  53. void SendData(BYTE dat);
  54. void pwm_Servomoto_angle(unsigned int angle,unsigned int Servo_time);
  55. void pwm_Servomoto(void);
  56. void test_servo(void);
  57. void Motor_Ctr(void);

  58. void Pwm_Motor(void);
  59. /*******************************************************************************
  60. * 函數(shù)名         : main
  61. * 函數(shù)功能                   : 主函數(shù)
  62. * 輸入           : 無
  63. * 輸出                  : 無
  64. *******************************************************************************/

  65. void main()
  66. {
  67.         unsigned char i;
  68.             //串口初始化
  69.    

  70. //     printf("\r\n\n\r\n");
  71.    
  72.                 TMOD=0X11;
  73.         TH1=(65536-100)/256;          //100US定時(shí)
  74.         TL1=(65536-100)%256;
  75.         TH0=0;
  76.         TL0=0;  
  77.         TR1= 1;
  78.         ET1= 1;
  79.         ET0= 1;
  80.         EA = 1;

  81.         IrInit();        
  82.         LcdInit();
  83.         InitUART();

  84.         LcdWriteCom(0x80);
  85.         
  86.         for(i=0;i<13;i++)
  87.         {
  88.                 LcdWriteData(CDIS1[i]);        
  89.         }
  90.         LcdWriteCom(0x80+0x40);
  91.         for(i=0;i<13;i++)
  92.         {
  93.                 LcdWriteData(CDIS2[i]);        
  94.         }
  95.         while(1)
  96.         {
  97.             test_servo();
  98.                 IrValue[4]=IrValue[2]>>4;                          //高位
  99.                 IrValue[5]=IrValue[2]&0x0f;                //低位        
  100.                 if(IrValue[4]>9)
  101.                 {
  102.                         LcdWriteCom(0xc0+0x09);                        //設(shè)置顯示位置
  103.                         LcdWriteData(0x37+IrValue[4]);        //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  104.                 }
  105.                 else
  106.                 {
  107.                         LcdWriteCom(0xc0+0x09);
  108.                         LcdWriteData(IrValue[4]+0x30);        //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  109.                 }        
  110.                 if(IrValue[5]>9)
  111.                 {
  112.                         LcdWriteCom(0xc0+0x0a);
  113.                         LcdWriteData(IrValue[5]+0x37);                //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  114.                 }
  115.                 else
  116.                 {
  117.                         LcdWriteCom(0xc0+0x0a);
  118.                         LcdWriteData(IrValue[5]+0x30);                //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  119.                 }        
  120.                 DelayMs(50);
  121.         //        printf("\r\n SZ-51 UART printftest OK if you see these words! c%c%",        IrValue[2],        IrValue[3]);
  122.                  if(IrOK==1)                           //如果處理好后進(jìn)行紅外處理
  123.         {
  124. //                printf("\r\n SZ-51 UART printftest OK if you see these words!");
  125.                 SendString(Test_OK);
  126.                         if(IrValue[4]>9)
  127.                 {
  128.                         LcdWriteCom(0x80+0x0d);                        //設(shè)置顯示位置
  129.                         LcdWriteData(0x37+IrValue[4]);        //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  130.                 }
  131.                 else
  132.                 {
  133.                         LcdWriteCom(0x80+0x0d);
  134.                         LcdWriteData(IrValue[4]+0x30);        //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  135.                 }        
  136.                 if(IrValue[5]>9)
  137.                 {
  138.                         LcdWriteCom(0x80+0x0e);
  139.                         LcdWriteData(IrValue[5]+0x37);                //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  140.                 }
  141.                 else
  142.                 {
  143.                         LcdWriteCom(0x80+0x0e);
  144.                         LcdWriteData(IrValue[5]+0x30);                //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  145.                 }
  146.                    switch(IrValue[2])
  147.                       {
  148.                                     case 0x18:  
  149.                                                 LcdWriteCom(0x80+0x0f);
  150.                                                 LcdWriteData(CDIS1[3]);                //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  151.                         break;
  152.                                                 case 0x52:  
  153.                                                 LcdWriteCom(0x80+0x0f);
  154.                                                 LcdWriteData(CDIS1[2]);                //將數(shù)值轉(zhuǎn)換為該顯示的ASCII碼
  155.                         break;
  156.                                  default:break;
  157.                        }

  158.                    IrOK=0;
  159.         }
  160.                 else
  161.                 {
  162.                 //        printf("\r\n SZ-51 UART printftest OK if you see these words!");
  163.                 }
  164.                 DelayMs(50);
  165.         }
  166. }                                                                        
  167. /*******************************************************************************
  168. * 函數(shù)名         : DelayMs()
  169. * 函數(shù)功能                   : 延時(shí)
  170. * 輸入           : x
  171. * 輸出                  : 無
  172. *******************************************************************************/

  173. void DelayMs(unsigned int x)   //0.14ms誤差 0us
  174. {
  175. unsigned char i;
  176.   while(x--)
  177. {
  178.   for (i = 0; i<13; i++)
  179. {}
  180. }
  181. }
  182. /*******************************************************************************
  183. * 函數(shù)名         : IrInit()
  184. * 函數(shù)功能                   : 初始化紅外線接收
  185. * 輸入           : 無
  186. * 輸出                  : 無
  187. *******************************************************************************/

  188. void IrInit()
  189. {
  190.         IT0=1;//下降沿觸發(fā)
  191.         EX0=1;//打開中斷0允許
  192.         EA=1;        //打開總中斷

  193.         IRIN=1;//初始化端口
  194. }
  195. /*******************************************************************************
  196. * 函數(shù)名         : ReadIr()
  197. * 函數(shù)功能                   : 讀取紅外數(shù)值的中斷函數(shù)
  198. * 輸入           : 無
  199. * 輸出                  : 無
  200. *******************************************************************************/

  201. void ReadIr() interrupt 0
  202. {
  203.         unsigned char j,k;
  204.         unsigned int err;
  205.         Time=0;                                         
  206.         DelayMs(70);

  207.         if(IRIN==0)                //確認(rèn)是否真的接收到正確的信號(hào)
  208.         {         
  209.                
  210.                 err=1000;                                //1000*10us=10ms,超過說明接收到錯(cuò)誤的信號(hào)
  211.                 /*當(dāng)兩個(gè)條件都為真是循環(huán),如果有一個(gè)條件為假的時(shí)候跳出循環(huán),免得程序出錯(cuò)的時(shí)
  212.                 侯,程序死在這里*/        
  213.                 while((IRIN==0)&&(err>0))        //等待前面9ms的低電平過去                  
  214.                 {                        
  215.                         DelayMs(1);
  216.                         err--;
  217.                 }
  218.                 if(IRIN==1)                        //如果正確等到9ms低電平
  219.                 {
  220.                         err=500;
  221.                         while((IRIN==1)&&(err>0))                 //等待4.5ms的起始高電平過去
  222.                         {
  223.                                 DelayMs(1);
  224.                                 err--;
  225.                         }
  226.                         for(k=0;k<4;k++)                //共有4組數(shù)據(jù)
  227.                         {                                
  228.                                 for(j=0;j<8;j++)        //接收一組數(shù)據(jù)
  229.                                 {

  230.                                         err=60;               
  231.                                         while((IRIN==0)&&(err>0))//等待信號(hào)前面的560us低電平過去
  232. //                                        while (!IRIN)
  233.                                         {
  234.                                                 DelayMs(1);
  235.                                                 err--;
  236.                                         }
  237.                                         err=500;
  238.                                         while((IRIN==1)&&(err>0))         //計(jì)算高電平的時(shí)間長度。
  239.                                         {
  240.                                                 DelayMs(1);//0.14ms
  241.                                                 Time++;
  242.                                                 err--;
  243.                                                 if(Time>30)
  244.                                                 {
  245.                                                         EX0=1;
  246.                                                         return;
  247.                                                 }
  248.                                         }
  249.                                         IrValue[k]>>=1;         //k表示第幾組數(shù)據(jù)
  250.                                         if(Time>=8)                        //如果高電平出現(xiàn)大于565us,那么是1
  251.                                         {
  252.                                                 IrValue[k]|=0x80;
  253.                                         }
  254.                                         Time=0;                //用完時(shí)間要重新賦值                                                        
  255.                                 }
  256.                         }
  257.                 }
  258.                 if(IrValue[2]==~IrValue[3])
  259.                 {
  260.                         
  261.                         IrOK=1;

  262.                         DelayMs(5);
  263.                         return;
  264.                 }
  265.                 else
  266.                 {
  267.                         IrOK=0;
  268.                 }

  269.         }
  270.                
  271. }
  272. /*--------------------------------------------------------------
  273.                             串口初始化
  274. --------------------------------------------------------------*/
  275. void InitUART(void)
  276. {
  277. #if (PARITYBIT == NONE_PARITY)
  278.     SCON = 0x50;            //8-bit variable UART
  279. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  280.     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
  281. #elif (PARITYBIT == SPACE_PARITY)
  282.     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
  283. #endif

  284.     TL2 = RCAP2L = (65536-(FOSC/32/BAUD)); //Set auto-reload vaule
  285.     TH2 = RCAP2H = (65536-(FOSC/32/BAUD)) >> 8;
  286.     T2CON = 0x34;           //Timer2 start run
  287.     ES = 1;                 //Enable UART interrupt
  288.     EA = 1;
  289. }     

  290. /*--------------------------------------------------------------
  291.              printf調(diào)用的底層發(fā)送一個(gè)字節(jié)函數(shù)
  292. --------------------------------------------------------------*/
  293. char putchar(char c)
  294. {
  295.     SBUF = c;
  296.     while(!TI);
  297.     TI = 0;

  298.     return c;
  299. }


  300. /*----------------------------
  301. UART interrupt service routine
  302. ----------------------------*/
  303. void Uart_Isr() interrupt 4 using 1
  304. {
  305.     if (RI)
  306.     {
  307.         RI = 0;             //Clear receive interrupt flag
  308.         P0 = SBUF;          //P0 show UART data
  309.         bit9 = RB8;         //P2.2 show parity bit
  310.     }
  311.     if (TI)
  312.     {
  313.         TI = 0;             //Clear transmit interrupt flag
  314.         busy = 0;           //Clear transmit busy flag
  315.     }
  316. }

  317. /*----------------------------
  318. Send a byte data to UART
  319. Input: dat (data to be sent)
  320. Output:None
  321. ----------------------------*/
  322. void SendData(BYTE dat)
  323. {
  324.     while (busy);           //Wait for the completion of the previous data is sent
  325.     ACC = dat;              //Calculate the even parity bit P (PSW.0)
  326.     if (P)                  //Set the parity bit according to P
  327.     {
  328. #if (PARITYBIT == ODD_PARITY)
  329.         TB8 = 0;            //Set parity bit to 0
  330. #elif (PARITYBIT == EVEN_PARITY)
  331.         TB8 = 1;            //Set parity bit to 1
  332. #endif
  333.     }
  334.     else
  335.     {
  336. #if (PARITYBIT == ODD_PARITY)
  337.         TB8 = 1;            //Set parity bit to 1
  338. #elif (PARITYBIT == EVEN_PARITY)
  339.         TB8 = 0;            //Set parity bit to 0
  340. #endif
  341.     }
  342.     busy = 1;
  343.     SBUF = ACC;             //Send data to UART buffer
  344. }

  345. /*----------------------------
  346. Send a string to UART
  347. Input: s (address of string)
  348. Output:None
  349. ----------------------------*/
  350. void SendString(char *s)
  351. {
  352.     while (*s)              //Check the end of the string
  353.     {
  354.         SendData(*s++);     //Send current char and increment string ptr
  355.     }
  356. }


  357. /************************************************************************/
  358. /*                    PWM調(diào)制舵機(jī)電機(jī)轉(zhuǎn)速                                   */
  359. /************************************************************************/
  360. /*                    舵機(jī)電機(jī)調(diào)速                                        */
  361. /*調(diào)節(jié)push_val_left的值改變舵機(jī)電機(jī)轉(zhuǎn)速,占空比            */
  362.                 void pwm_Servomoto(void)
  363. {  

  364.     if(pwm_val_left<=push_val_left)
  365.                Sevro_moto_pwm=1;
  366.         else
  367.                Sevro_moto_pwm=0;
  368.         if(pwm_val_left>=200)
  369.         pwm_val_left=0;

  370. }

  371. void Pwm_Motor(void)
  372. {
  373.         if(flag==1)
  374.         {
  375.          FanPin=0;
  376.         }
  377.         else
  378.         {
  379.          if(pwm_val_Fan<=Fan_speed)
  380.                FanPin=1;
  381.         else
  382.                FanPin=0;
  383.         if(pwm_val_Fan>=200)
  384.         pwm_val_Fan=0;
  385.         }
  386. }
  387. /***************************************************/
  388. ///*TIMER1中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
  389.          void time1()interrupt 3   using 2
  390. {        
  391.      TH1=(65536-100)/256;          //100US定時(shí)
  392.          TL1=(65536-100)%256;
  393.          timer++;                                  //定時(shí)器100US為準(zhǔn)。在這個(gè)基礎(chǔ)上延時(shí)
  394.          pwm_val_left++;
  395.          pwm_val_Fan++;
  396.          pwm_Servomoto();
  397.          Pwm_Motor();
  398.          
  399. }

  400. void pwm_Servomoto_angle(unsigned int angle,unsigned int Servo_time)
  401. {
  402.                   push_val_left=5+angle*20/180;          //舵機(jī)向左轉(zhuǎn)90度
  403.                   timer=0;
  404.                   while(timer<=Servo_time); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置                 4000
  405. }


  406. void test_servo(void)
  407. {
  408. int pos;
  409.         for(pos=0;pos<180;pos+=3)
  410.         {
  411.                 Motor_Ctr();
  412. //                pwm_Fan_speed();
  413.                 while(Servo_Ctr==0)
  414.                 {
  415.                 Motor_Ctr();
  416.                
  417.                 }
  418.         
  419.                 pwm_Servomoto_angle(pos,100) ;
  420.                 DelayMs(10);
  421.         }
  422.         for(pos = 180; pos>=0; pos-=3)     // goes from 180 degrees to 0 degrees
  423.         {
  424.                 Motor_Ctr();

  425.                 while(Servo_Ctr==0)
  426. ……………………

  427. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
紅外遙控風(fēng)扇-lcd1602顯示鍵值.zip (2.51 MB, 下載次數(shù): 145)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏6 分享淘帖 頂2 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:735105 發(fā)表于 2020-4-22 12:42 | 只看該作者
我是一個(gè)非本專業(yè)的學(xué)生,出于好奇心和興趣,也想嘗試這個(gè)小設(shè)計(jì),在那個(gè)寶上買了元器件,雖然仔細(xì)查閱了資料,但還是沒能做出來,咨詢那個(gè)寶客服,它們的售后服務(wù)很差,沒法兒解決我的問題,一方面我不甘心放棄,另一方面實(shí)在沒找到突破口,就在山重水復(fù)疑無路之時(shí),在網(wǎng)上找到樓主這篇帖子,尤其其中有關(guān)鍵思路的備注,還有一些問題的解決辦法,我又重新燃起了希望,最終做了出來,能遇到這樣的平臺(tái),真的太幸運(yùn)了。
回復(fù)

使用道具 舉報(bào)

板凳
ID:735105 發(fā)表于 2020-4-22 19:07 | 只看該作者
請(qǐng)問樓主在軟件上仿真模擬過嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产精品视频二区三区 | 欧美国产亚洲一区二区 | 国产精品亚洲一区二区三区在线 | 欧美一区免费 | 久久婷婷国产麻豆91 | 精品美女视频在线观看免费软件 | 精品国产一区二区三区免费 | 中文字幕在线观看日韩 | 久久久国产精品一区 | 国产免费麻豆视频 | 又黄又色 | 91麻豆精品国产91久久久更新资源速度超快 | 国产欧美日韩综合精品一区二区 | 亚洲欧美日韩在线 | 亚洲激情综合 | 日韩欧美中文字幕在线观看 | 欧美精品久久久久 | 欧美日韩综合一区 | 亚洲va欧美va人人爽午夜 | 国产视频欧美 | 日韩欧美成人精品 | 日韩免费视频一区二区 | 天天看天天摸天天操 | 亚洲三区在线 | 欧美日韩不卡 | 不卡一区二区在线观看 | 精品亚洲永久免费精品 | 观看av | 国产激情一区二区三区 | 欧美日韩精品在线免费观看 | 久久中文字幕一区 | 91看片网| 一级免费看片 | 日本精品一区二区三区在线观看视频 | 欧美成年视频 | 亚洲精品欧美精品 | 国产精品一卡 | 久久狠狠 | 丁香一区二区 | 三级av网址 | 中文字幕乱码一区二区三区 |