[DemoCode下载] M451红外通信NEC格式

[复制链接]
687|2
 楼主| zhuomuniao110 发表于 2019-4-30 21:36 | 显示全部楼层 |阅读模式
IR_NEC.rar (13.98 KB, 下载次数: 6)
 楼主| zhuomuniao110 发表于 2019-4-30 21:36 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 3 $
  5. * $Date: 15/09/02 10:03a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrates how to set PWM pin analog IR emitters,
  7.                          the information is sent to the PWM capture pin, analyze the data received and displayed.
  8. * @note
  9. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include "stdio.h"
  13. #include "M451Series.h"
  14. #include "NuEdu-Basic01.h"

  15. #define PLL_CLOCK       72000000


  16. #define SPI_LCD_PORT  SPI2

  17. #define ILI9341_RESET   PB15
  18. #define ILI9341_DC      PB11
  19. #define ILI9341_LED     PB5

  20. extern uint8_t Font8x16[];

  21. #define        White           0xFFFF
  22. #define Black           0x0000
  23. #define Blue            0x001F
  24. #define Blue2           0x051F
  25. #define Red             0xF800
  26. #define Magenta         0xF81F
  27. #define Green           0x07E0
  28. #define Cyan            0x7FFF
  29. #define Yellow          0xFFE0

  30. extern void Initial_LB_Key_Input(void);
  31. extern unsigned char Get_LB_Key_Input(void);

  32. void ILI9341_LCD_PutString(uint16_t x, uint16_t y,uint8_t *s, uint32_t fColor, uint32_t bColor);

  33. uint8_t LCD_ReadReg(uint8_t u8Comm)
  34. {
  35.     SPI_ClearRxFIFO(SPI_LCD_PORT);

  36.     ILI9341_DC = 0;

  37.     SPI_WRITE_TX(SPI_LCD_PORT, u8Comm);
  38.     SPI_WRITE_TX(SPI_LCD_PORT, 0x00);

  39.     // wait tx finish
  40.     while(SPI_IS_BUSY(SPI_LCD_PORT));

  41.     SPI_READ_RX(SPI_LCD_PORT);

  42.     return (SPI_READ_RX(SPI_LCD_PORT));
  43. }

  44. void LCD_WriteCommand(uint8_t u8Comm)
  45. {
  46.     ILI9341_DC = 0;

  47.     SPI_WRITE_TX(SPI_LCD_PORT, u8Comm);

  48.     // wait tx finish
  49.     while(SPI_IS_BUSY(SPI_LCD_PORT));
  50. }

  51. void LCD_WriteData(uint8_t u8Data)
  52. {
  53.     ILI9341_DC = 1;

  54.     SPI_WRITE_TX(SPI_LCD_PORT, u8Data);

  55.     // wait tx finish
  56.     while(SPI_IS_BUSY(SPI_LCD_PORT));
  57. }


  58. void ILI9341_LCD_SetAddress(uint32_t x1,uint32_t x2,uint32_t y1,uint32_t y2)
  59. {
  60.     if(x1 >= 240)
  61.         x1 = 239;
  62.     if(x2 >= 240)
  63.         x2 = 239;  
  64.     if(y1 >= 320)
  65.         y1 = 319;  
  66.     if(y2 >= 320)
  67.         y2 = 319;      
  68.    
  69.     LCD_WriteCommand(0x2a);
  70.     LCD_WriteData(x1>>8);
  71.     LCD_WriteData(x1);
  72.     LCD_WriteData(x2>>8);
  73.     LCD_WriteData(x2);

  74.     LCD_WriteCommand(0x2b);
  75.     LCD_WriteData(y1>>8);
  76.     LCD_WriteData(y1);
  77.     LCD_WriteData(y2>>8);
  78.     LCD_WriteData(y2);
  79. }

  80. void ILI9341_LCD_PutChar8x16(uint16_t x, uint16_t y, uint8_t c, uint32_t fColor, uint32_t bColor)
  81. {
  82.         uint32_t i,j;
  83.         for(i=0;i<16;i++){
  84.         uint8_t m=Font8x16[c*16+i];
  85.         ILI9341_LCD_SetAddress(x+i,x+i,y,y+7);
  86.         LCD_WriteCommand(0x2c);        
  87.         
  88.                 for(j=0;j<8;j++){
  89.                         if((m&0x01)==0x01){
  90.                 LCD_WriteData(fColor>>8);
  91.                                 LCD_WriteData(fColor);
  92.                         }
  93.                         else{
  94.                 LCD_WriteData(bColor>>8);
  95.                                 LCD_WriteData(bColor);
  96.                         }
  97.                         m>>=1;
  98.                 }
  99.         }
  100. }

  101. void ILI9341_LCD_PutString(uint16_t x, uint16_t y,uint8_t *s, uint32_t fColor, uint32_t bColor)
  102. {
  103.     uint8_t l=0;
  104.     while(*s){
  105.         if(*s<0x80){
  106.             ILI9341_LCD_PutChar8x16(x,312-y-l*8,*s,fColor,bColor);
  107.             s++;
  108.             l++;
  109.                 }
  110.         }       
  111. }


  112. void ILI9341_LCD_Init(void)
  113. {
  114.     /* Configure DC/RESET/LED pins */
  115.     ILI9341_DC =0;
  116.     ILI9341_RESET=0;
  117.     ILI9341_LED=0;

  118.     GPIO_SetMode(PB, BIT5, GPIO_MODE_OUTPUT);
  119.     GPIO_SetMode(PB, BIT11, GPIO_MODE_OUTPUT);
  120.     GPIO_SetMode(PB, BIT15, GPIO_MODE_OUTPUT);

  121.     /* Configure LCD */
  122.     ILI9341_DC = 1;

  123.     ILI9341_RESET = 0;
  124.     TIMER_Delay(TIMER0, 20000);

  125.     ILI9341_RESET = 1;
  126.     TIMER_Delay(TIMER0, 40000);

  127.     LCD_WriteCommand(0xCB);
  128.     LCD_WriteData(0x39);
  129.     LCD_WriteData(0x2C);
  130.     LCD_WriteData(0x00);
  131.     LCD_WriteData(0x34);
  132.     LCD_WriteData(0x02);

  133.     LCD_WriteCommand(0xCF);
  134.     LCD_WriteData(0x00);
  135.     LCD_WriteData(0xC1);
  136.     LCD_WriteData(0x30);

  137.     LCD_WriteCommand(0xE8);
  138.     LCD_WriteData(0x85);
  139.     LCD_WriteData(0x00);
  140.     LCD_WriteData(0x78);

  141.     LCD_WriteCommand(0xEA);
  142.     LCD_WriteData(0x00);
  143.     LCD_WriteData(0x00);

  144.     LCD_WriteCommand(0xED);
  145.     LCD_WriteData(0x64);
  146.     LCD_WriteData(0x03);
  147.     LCD_WriteData(0x12);
  148.     LCD_WriteData(0x81);

  149.     LCD_WriteCommand(0xF7);
  150.     LCD_WriteData(0x20);

  151.     LCD_WriteCommand(0xC0);
  152.     LCD_WriteData(0x23);

  153.     LCD_WriteCommand(0xC1);
  154.     LCD_WriteData(0x10);

  155.     LCD_WriteCommand(0xC5);
  156.     LCD_WriteData(0x3e);
  157.     LCD_WriteData(0x28);

  158.     LCD_WriteCommand(0xC7);
  159.     LCD_WriteData(0x86);

  160.     LCD_WriteCommand(0x36);
  161.     LCD_WriteData(0x48);

  162.     LCD_WriteCommand(0x3A);
  163.     LCD_WriteData(0x55);

  164.     LCD_WriteCommand(0xB1);
  165.     LCD_WriteData(0x00);
  166.     LCD_WriteData(0x18);

  167.     LCD_WriteCommand(0xB6);
  168.     LCD_WriteData(0x08);
  169.     LCD_WriteData(0x82);
  170.     LCD_WriteData(0x27);

  171.     LCD_WriteCommand(0xF2);
  172.     LCD_WriteData(0x00);

  173.     LCD_WriteCommand(0x26);
  174.     LCD_WriteData(0x01);

  175.     LCD_WriteCommand(0xE0);
  176.     LCD_WriteData(0x0F);
  177.     LCD_WriteData(0x31);
  178.     LCD_WriteData(0x2B);
  179.     LCD_WriteData(0x0C);
  180.     LCD_WriteData(0x0E);
  181.     LCD_WriteData(0x08);
  182.     LCD_WriteData(0x4E);
  183.     LCD_WriteData(0xF1);
  184.     LCD_WriteData(0x37);
  185.     LCD_WriteData(0x07);
  186.     LCD_WriteData(0x10);
  187.     LCD_WriteData(0x03);
  188.     LCD_WriteData(0x0E);
  189.     LCD_WriteData(0x09);
  190.     LCD_WriteData(0x00);

  191.     LCD_WriteCommand(0xE1);
  192.     LCD_WriteData(0x00);
  193.     LCD_WriteData(0x0E);
  194.     LCD_WriteData(0x14);
  195.     LCD_WriteData(0x03);
  196.     LCD_WriteData(0x11);
  197.     LCD_WriteData(0x07);
  198.     LCD_WriteData(0x31);
  199.     LCD_WriteData(0xC1);
  200.     LCD_WriteData(0x48);
  201.     LCD_WriteData(0x08);
  202.     LCD_WriteData(0x0F);
  203.     LCD_WriteData(0x0C);
  204.     LCD_WriteData(0x31);
  205.     LCD_WriteData(0x36);
  206.     LCD_WriteData(0x0F);

  207.     LCD_WriteCommand(0x11);
  208.     TIMER_Delay(TIMER0, 60000);

  209.     LCD_WriteCommand(0x29);    //Display on

  210.     ILI9341_LED = 1;
  211. }




  212. void SYS_Init(void)
  213. {
  214.     /*---------------------------------------------------------------------------------------------------------*/
  215.     /* Init System Clock                                                                                       */
  216.     /*---------------------------------------------------------------------------------------------------------*/

  217.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  218.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  219.     /* Wait for HIRC clock ready */
  220.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  221.     /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
  222.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  223.     /* Enable HXT clock (external XTAL 12MHz) */
  224.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  225.     /* Wait for HXT clock ready */
  226.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  227.     /* Set core clock as PLL_CLOCK from PLL */
  228.     CLK_SetCoreClock(PLL_CLOCK);

  229.     /* Enable IP clock */
  230.     CLK_EnableModuleClock(UART0_MODULE);
  231.     CLK_EnableModuleClock(TMR0_MODULE);
  232.     CLK_EnableModuleClock(SPI2_MODULE);

  233.     /* Select IP clock source */
  234.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
  235.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);
  236.     CLK_SetModuleClock(SPI2_MODULE, CLK_CLKSEL2_SPI2SEL_PLL, 0);
  237.        
  238.         /* Get Core Clock Frequency      */
  239.     SystemCoreClockUpdate();
  240.        
  241.     /*---------------------------------------------------------------------------------------------------------*/
  242.     /* Init I/O Multi-function                                                                                 */
  243.     /*---------------------------------------------------------------------------------------------------------*/

  244.     /* Set PD multi-function pins for UART0 RXD(PD.6) and TXD(PD.1) */
  245.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD6MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  246.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD6MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  247.     /* SPI2: GPD12=SS, GPD15=CLK, GPD14=MISO, GPD13=MOSI */
  248.     SYS->GPD_MFPH &= ~(SYS_GPD_MFPH_PD12MFP_Msk | SYS_GPD_MFPH_PD13MFP_Msk | SYS_GPD_MFPH_PD14MFP_Msk | SYS_GPD_MFPH_PD15MFP_Msk);
  249.     SYS->GPD_MFPH |= (SYS_GPD_MFPH_PD12MFP_SPI2_SS | SYS_GPD_MFPH_PD13MFP_SPI2_MOSI | SYS_GPD_MFPH_PD14MFP_SPI2_MISO | SYS_GPD_MFPH_PD15MFP_SPI2_CLK);

  250. }

  251. void UART0_Init()
  252. {
  253.     /*---------------------------------------------------------------------------------------------------------*/
  254.     /* Init UART                                                                                               */
  255.     /*---------------------------------------------------------------------------------------------------------*/
  256.     /* Reset UART module */
  257.     SYS_ResetModule(UART0_RST);

  258.     /* Configure UART0 and set UART0 baud rate */
  259.     UART_Open(UART0, 115200);
  260. }

  261. /*---------------------------------------------------------------------------------------------------------*/
  262. /*  Main Function                                                                                          */
  263. /*---------------------------------------------------------------------------------------------------------*/
  264. int32_t main(void)
  265. {
  266.     uint8_t au8IR_CODE[4];

  267.     /* Unlock protected registers */
  268.     SYS_UnlockReg();

  269.     /* Init System, peripheral clock and multi-function I/O */
  270.     SYS_Init();

  271.     /* Lock protected registers */
  272.     SYS_LockReg();

  273.     /* Init UART0 for printf */
  274.     UART0_Init();
  275.        
  276.     /* Configure SPI3 as a master, MSB first, 8-bit transaction, SPI Mode-0 timing, clock is 4MHz */
  277.     SPI_Open(SPI_LCD_PORT, SPI_MASTER, SPI_MODE_0, 8, 4000000);
  278.        
  279.         /* Configure SPI1 as a low level active device. */
  280.     SPI_EnableAutoSS(SPI_LCD_PORT, SPI_SS, SPI_SS_ACTIVE_LOW);

  281.     /* Start SPI */
  282.     SPI_ENABLE(SPI_LCD_PORT);

  283.     /* Init LCD */
  284.     ILI9341_LCD_Init();
  285.        
  286.         /* Button Init */
  287.         Initial_LB_Key_Input();
  288.     IrDA_NEC_TxRx_Init();

  289.     /* Show the String on the screen */                                                                                                              // Long= 40 , Width= 15
  290.         ILI9341_LCD_PutString(0,0,"******************************************",Red,Yellow);
  291.     ILI9341_LCD_PutString(15,0,"*Demonstrates how to set PWM pin analog*",Red,Yellow);
  292.     ILI9341_LCD_PutString(30,0,"*IR emitters, the information is sent  *",Red,Yellow);
  293.         ILI9341_LCD_PutString(45,0,"*to the PWM capture pin, analyze the   *",Red,Yellow);
  294.         ILI9341_LCD_PutString(60,0,"*data received and displayed.          *",Red,Yellow);
  295.         ILI9341_LCD_PutString(75,0,"*Please touch the KEY 1-2              *",Red,Yellow);
  296.         ILI9341_LCD_PutString(90,0,"******************************************",Red,Yellow);
  297.        
  298.     au8IR_CODE[0] = 0x00;
  299.     au8IR_CODE[1] = ~au8IR_CODE[0];

  300.     while(1)
  301.     {
  302.         /* Detect Key status */
  303.         if(Get_LB_Key_Input() == 0x01)
  304.         {
  305.             au8IR_CODE[2] = 0x10;
  306.             au8IR_CODE[3] = ~au8IR_CODE[2];
  307.             SendNEC(au8IR_CODE);
  308.             CLK_SysTickDelay(100000);
  309.         }
  310.         if(Get_LB_Key_Input() == 0x02)
  311.         {
  312.             au8IR_CODE[2] = 0x14;
  313.             au8IR_CODE[3] = ~au8IR_CODE[2];
  314.             SendNEC(au8IR_CODE);
  315.             CLK_SysTickDelay(100000);
  316.         }       
  317.     }
  318. }
 楼主| zhuomuniao110 发表于 2019-4-30 21:36 | 显示全部楼层
压缩包的工程比较完整。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

233

主题

3529

帖子

11

粉丝
快速回复 在线客服 返回列表 返回顶部