查看: 1107|回复: 0

[分享] 【庆典三:项目速成设计】LPC4330硬件SPI驱动OLED

[复制链接]
  • TA的每日心情
    奋斗
    2024-1-4 09:08
  • 签到天数: 493 天

    连续签到: 1 天

    [LV.9]以坛为家II

    3

    主题

    764

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    1872
    最后登录
    2025-5-13
    发表于 2024-12-27 19:32:08 | 显示全部楼层 |阅读模式
    【LPC4330用户手册】外设库中有个SSP1的例程,学习一下能否用于驱动OLED:

    1.png
    初始化SSP1的Pin:

    2.png

    如下图所示,SSP1的4个引脚均引出到J1连接器。


    3.png

    4.png
    1. oid OLED_WR_Byte(u8 dat,u8 cmd)
    2. {       
    3. //        u8 i;                          
    4.         if(cmd)
    5.           OLED_DC_Set();
    6.         else
    7.           OLED_DC_Clr();                  
    8.         OLED_CS_Clr();
    9.     //SPI0_ByteExchange(dat);   /* AVR64DD32 Exchange data */
    10.         LPC_SSP1->DR = dat;        //Chip_SSP_SendFrame(LPC_SSP1, dat);
    11.         while ((Chip_SSP_GetStatus(LPC_SSP1, SSP_STAT_RNE) != SET)){
    12.                 ;
    13.         }
    14.         //DEBUGOUT("\r\nWrite 1 Byte : %x Done\n\r", dat);
    15.         Chip_SSP_ReceiveFrame(LPC_SSP1);


    16.         OLED_CS_Set();
    17.         OLED_DC_Set();             
    18. }
    复制代码


    关于RES和DC引脚:
    RES:P6-5 即GPIO3[4]
    DC:  P6-1 即GPIO3[0]

    5.png
    OLED屏幕没有任何反应,于是考虑是否RES,DC等引脚并没有起作用。通过简单的翻转IO引脚也无法测试出电平变换,有点问题。

    另外,也顺便测试了一下J2连接器上的P1_6, P1_9,P1_12以及P1_10引脚的GPIO功能,测试结果正常,梅林雀可以观测到方波。
    6.png

    #if 1

    Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT,1, 9);
    Chip_GPIO_SetPinState(LPC_GPIO_PORT,1, 9, (bool) TRUE );

    Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT,1, 2); //RES P6-5 : GPIO3[4]
    Chip_GPIO_SetPinState(LPC_GPIO_PORT,1, 2, (bool) TRUE );

    Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT,1, 5); //RES P6-5 : GPIO3[4]
    Chip_GPIO_SetPinState(LPC_GPIO_PORT,1, 5, (bool) TRUE );

    Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT,1, 3); //RES P6-5 : GPIO3[4]
    Chip_GPIO_SetPinState(LPC_GPIO_PORT,1, 3, (bool) TRUE );

    while(1){
    Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT,1, 9);
    Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT,1, 2);

    Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT,1, 5);
    Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT,1, 3);

    Delay_ms(50);

    Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT,1, 9);
    Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT,1, 2);

    Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT,1, 5);
    Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT,1, 3);
    Delay_ms(50);
    }
    #endif

    接下来神奇的事情发生了,把OLED的VCC跟GND连接到LPC4330Xpressso开发板的另一个连接器上,然后屏幕开始缓慢刷新了。速度非常慢,考虑可能是SSP的时钟只有100K,后来改成了1M,速度还是可以的!
    WeChat Image_20230303083213.jpg

    附主程序代码:
    1. /*
    2. * @brief Blinky example using sysTick
    3. *
    4. * @note
    5. * Copyright(C) NXP Semiconductors, 2013
    6. * All rights reserved.
    7. *
    8. * @par
    9. * Software that is described herein is for illustrative purposes only
    10. * which provides customers with programming information regarding the
    11. * LPC products.  This software is supplied "AS IS" without any warranties of
    12. * any kind, and NXP Semiconductors and its licensor disclaim any and
    13. * all warranties, express or implied, including all implied warranties of
    14. * merchantability, fitness for a particular purpose and non-infringement of
    15. * intellectual property rights.  NXP Semiconductors assumes no responsibility
    16. * or liability for the use of the software, conveys no license or rights under any
    17. * patent, copyright, mask work right, or any other intellectual property rights in
    18. * or to any products. NXP Semiconductors reserves the right to make changes
    19. * in the software without notification. NXP Semiconductors also makes no
    20. * representation or warranty that such application will be suitable for the
    21. * specified use without further testing or modification.
    22. *
    23. * @par
    24. * Permission to use, copy, modify, and distribute this software and its
    25. * documentation is hereby granted, under NXP Semiconductors' and its
    26. * licensor's relevant copyrights in the software, without fee, provided that it
    27. * is used in conjunction with NXP Semiconductors microcontrollers.  This
    28. * copyright, permission, and disclaimer notice must appear in all copies of
    29. * this code.
    30. */

    31. #include "board.h"
    32. #include "oled/oled.h"

    33. /*****************************************************************************
    34. * Private types/enumerations/variables
    35. ****************************************************************************/

    36. #define TICKRATE_HZ (1000)        /* 1000 ticks per second */

    37. /*****************************************************************************
    38. * Public types/enumerations/variables
    39. ****************************************************************************/
    40. #define LPC_SSP           LPC_SSP1

    41. #define BUFFER_SIZE                         (0x100)
    42. #define SSP_DATA_BITS                       (SSP_BITS_8)
    43. #define SSP_DATA_BIT_NUM(databits)          (databits+1)
    44. #define SSP_DATA_BYTES(databits)            (((databits) > SSP_BITS_8) ? 2:1)
    45. #define SSP_LO_BYTE_MSK(databits)           ((SSP_DATA_BYTES(databits) > 1) ? 0xFF:(0xFF>>(8-SSP_DATA_BIT_NUM(databits))))
    46. #define SSP_HI_BYTE_MSK(databits)           ((SSP_DATA_BYTES(databits) > 1) ? (0xFF>>(16-SSP_DATA_BIT_NUM(databits))):0)

    47. #define SSP_MODE_SEL                        (0x31)
    48. #define SSP_TRANSFER_MODE_SEL               (0x32)
    49. #define SSP_MASTER_MODE_SEL                 (0x31)
    50. #define SSP_SLAVE_MODE_SEL                  (0x32)
    51. #define SSP_POLLING_SEL                     (0x31)
    52. #define SSP_INTERRUPT_SEL                   (0x32)
    53. #define SSP_DMA_SEL                         (0x33)

    54. static SSP_ConfigFormat ssp_format;
    55. /*****************************************************************************
    56. * Private functions
    57. ****************************************************************************/

    58. /*****************************************************************************
    59. * Public functions
    60. ****************************************************************************/

    61. /**
    62. * @brief        Handle interrupt from SysTick timer
    63. * @return        Nothing
    64. */
    65. static volatile uint32_t tick_ct = 0;
    66. static uint8_t RGB_ON_OFF = 0;
    67. void SysTick_Handler(void)
    68. {
    69.         tick_ct += 1;

    70.         if ((tick_ct % 1000) == 0) {
    71.                 //Board_LED_Toggle(0);
    72.                 //Chip_GPIO_SetPinToggle(LPC_GPIO_PORT, 3, 5);
    73.                 switch(RGB_ON_OFF%3){
    74.                 case 0:
    75.                         Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 7);
    76.                         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
    77.                         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
    78.                         break;
    79.                 case 1:
    80.                         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
    81.                         Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 7);
    82.                         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
    83.                         break;
    84.                 case 2:
    85.                         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
    86.                         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
    87.                         Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 5);
    88.                         break;
    89.                 default:
    90.                         break;
    91.                 }//end--> switch-case

    92.                 RGB_ON_OFF++;
    93.         }//end--> if statement

    94. }

    95. void Delay_ms(uint32_t N_ms){
    96.         uint32_t start_cnt = tick_ct;
    97.         while((tick_ct - start_cnt) <= N_ms){
    98.                 ;
    99.         }
    100. }


    101. void RGB_LED_Blinky_with_Delay(uint32_t Nms_Delay){
    102.         /* P6.11 GPIO3[7] : LED green as output */
    103.         /* P2_7  GPIO0[7] : LED green as output */
    104.         /* P6.9  GPIO3[5] : LED blue as output */

    105.         //Red On, Green & Blue Off
    106.         Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 7);
    107.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
    108.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
    109.         Delay_ms(Nms_Delay);

    110.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
    111.         Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 7);
    112.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
    113.         Delay_ms(Nms_Delay);

    114.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
    115.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
    116.         Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 5);
    117.         Delay_ms(Nms_Delay);

    118.         //Turn off R,G,B LEDs
    119.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
    120.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
    121.         Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);

    122.         Delay_ms(Nms_Delay);
    123. }
    124. /**
    125. * @brief        main routine for blinky example
    126. * @return        Function should not exit.
    127. */
    128. int main(void)
    129. {

    130.         SystemCoreClockUpdate();
    131.         Board_Init();

    132.         /* Enable and setup SysTick Timer at a periodic rate */
    133.         SysTick_Config(SystemCoreClock / TICKRATE_HZ);
    134.         RGB_LED_Blinky_with_Delay(500);

    135.         /* SSP initialization */
    136.         Board_SSP_Init(LPC_SSP);
    137.         Chip_SSP_Init(LPC_SSP);
    138.         DEBUGOUT("\r\nBoard_SSP and Chip_SSP Init Done\n\r");

    139.         ssp_format.frameFormat = SSP_FRAMEFORMAT_SPI;
    140.         ssp_format.bits = SSP_DATA_BITS;
    141.         ssp_format.clockMode = SSP_CLOCK_MODE0;
    142.     Chip_SSP_SetFormat(LPC_SSP, ssp_format.bits, ssp_format.frameFormat, ssp_format.clockMode);
    143.         Chip_SSP_Enable(LPC_SSP);

    144.         Chip_SSP_SetMaster(LPC_SSP, 1);
    145.         DEBUGOUT("\r\nMaster Mode\n\r");

    146.         /* oled Initialization*/
    147.     OLED_Init();
    148.     DEBUGOUT("\r\nOLED Init Done\n\r");

    149.         while (1) {
    150.                 //__WFI();
    151.                 OLED_BMP_TEST();
    152.         }
    153. }

    154. void GPIO_TEST(void){
    155. #if 0
    156.         Chip_SCU_ClockPinMuxSet(0, (SCU_PINIO_FAST | SCU_MODE_FUNC1));                /* CLK0 connected to CLK        SCU_MODE_FUNC6=SSP1 CLK1  */

    157.         //J1 P1_3 MISO
    158.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 0, 10);
    159.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 0, 10, (bool) TRUE );

    160.         //J1 P1_4 MOSI
    161.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 0, 11);
    162.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 0, 11, (bool) TRUE );

    163.         //J1 P1_5 SSEL
    164.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 1, 8);
    165.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 1, 8, (bool) TRUE );

    166.         //J1 P6_5 Simulate RES Pin
    167.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 3, 4);
    168.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 3, 4, (bool) TRUE );

    169.         //J2 P1_6
    170.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 1, 9);
    171.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 1, 9, (bool) TRUE );

    172.         //J2 P1_9
    173.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 1, 2); //RES P6-5 : GPIO3[4]
    174.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 1, 2, (bool) TRUE );

    175.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 1, 5); //RES P6-5 : GPIO3[4]
    176.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 1, 5, (bool) TRUE );

    177.         Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 1, 3); //RES P6-5 : GPIO3[4]
    178.         Chip_GPIO_SetPinState(LPC_GPIO_PORT, 1, 3, (bool) TRUE );



    179.         while(1){
    180.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 10);
    181.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 11);
    182.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 1, 8);

    183.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 4);        //RES

    184.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 1, 9);
    185.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 1, 2);

    186.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 1, 5);
    187.                 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 1, 3);

    188.                 Delay_ms(50);
    189.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 10);
    190.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 11);
    191.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 1, 8);

    192.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 4);        //RES

    193.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 1, 9);
    194.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 1, 2);

    195.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 1, 5);
    196.                 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 1, 3);
    197.                 Delay_ms(50);
    198.         }
    199. #endif

    200. }






    复制代码
    完整代码:
    periph_blinky_spi_test_v_1.0.zip (435.49 KB, 下载次数: 0)
    加油!
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-8-24 04:13 , Processed in 0.074149 second(s), 19 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表