【LPC4330用户手册】外设库中有个SSP1的例程,学习一下能否用于驱动OLED:
初始化SSP1的Pin:
如下图所示,SSP1的4个引脚均引出到J1连接器。
- oid OLED_WR_Byte(u8 dat,u8 cmd)
- {
- // u8 i;
- if(cmd)
- OLED_DC_Set();
- else
- OLED_DC_Clr();
- OLED_CS_Clr();
- //SPI0_ByteExchange(dat); /* AVR64DD32 Exchange data */
- LPC_SSP1->DR = dat; //Chip_SSP_SendFrame(LPC_SSP1, dat);
- while ((Chip_SSP_GetStatus(LPC_SSP1, SSP_STAT_RNE) != SET)){
- ;
- }
- //DEBUGOUT("\r\nWrite 1 Byte : %x Done\n\r", dat);
- Chip_SSP_ReceiveFrame(LPC_SSP1);
- OLED_CS_Set();
- OLED_DC_Set();
- }
复制代码
关于RES和DC引脚: RES:P6-5 即GPIO3[4] DC: P6-1 即GPIO3[0]
OLED屏幕没有任何反应,于是考虑是否RES,DC等引脚并没有起作用。通过简单的翻转IO引脚也无法测试出电平变换,有点问题。
另外,也顺便测试了一下J2连接器上的P1_6, P1_9,P1_12以及P1_10引脚的GPIO功能,测试结果正常,梅林雀可以观测到方波。
#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,速度还是可以的!
附主程序代码: - /*
- * @brief Blinky example using sysTick
- *
- * @note
- * Copyright(C) NXP Semiconductors, 2013
- * All rights reserved.
- *
- * @par
- * Software that is described herein is for illustrative purposes only
- * which provides customers with programming information regarding the
- * LPC products. This software is supplied "AS IS" without any warranties of
- * any kind, and NXP Semiconductors and its licensor disclaim any and
- * all warranties, express or implied, including all implied warranties of
- * merchantability, fitness for a particular purpose and non-infringement of
- * intellectual property rights. NXP Semiconductors assumes no responsibility
- * or liability for the use of the software, conveys no license or rights under any
- * patent, copyright, mask work right, or any other intellectual property rights in
- * or to any products. NXP Semiconductors reserves the right to make changes
- * in the software without notification. NXP Semiconductors also makes no
- * representation or warranty that such application will be suitable for the
- * specified use without further testing or modification.
- *
- * @par
- * Permission to use, copy, modify, and distribute this software and its
- * documentation is hereby granted, under NXP Semiconductors' and its
- * licensor's relevant copyrights in the software, without fee, provided that it
- * is used in conjunction with NXP Semiconductors microcontrollers. This
- * copyright, permission, and disclaimer notice must appear in all copies of
- * this code.
- */
- #include "board.h"
- #include "oled/oled.h"
- /*****************************************************************************
- * Private types/enumerations/variables
- ****************************************************************************/
- #define TICKRATE_HZ (1000) /* 1000 ticks per second */
- /*****************************************************************************
- * Public types/enumerations/variables
- ****************************************************************************/
- #define LPC_SSP LPC_SSP1
- #define BUFFER_SIZE (0x100)
- #define SSP_DATA_BITS (SSP_BITS_8)
- #define SSP_DATA_BIT_NUM(databits) (databits+1)
- #define SSP_DATA_BYTES(databits) (((databits) > SSP_BITS_8) ? 2:1)
- #define SSP_LO_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? 0xFF:(0xFF>>(8-SSP_DATA_BIT_NUM(databits))))
- #define SSP_HI_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? (0xFF>>(16-SSP_DATA_BIT_NUM(databits))):0)
- #define SSP_MODE_SEL (0x31)
- #define SSP_TRANSFER_MODE_SEL (0x32)
- #define SSP_MASTER_MODE_SEL (0x31)
- #define SSP_SLAVE_MODE_SEL (0x32)
- #define SSP_POLLING_SEL (0x31)
- #define SSP_INTERRUPT_SEL (0x32)
- #define SSP_DMA_SEL (0x33)
- static SSP_ConfigFormat ssp_format;
- /*****************************************************************************
- * Private functions
- ****************************************************************************/
- /*****************************************************************************
- * Public functions
- ****************************************************************************/
- /**
- * @brief Handle interrupt from SysTick timer
- * @return Nothing
- */
- static volatile uint32_t tick_ct = 0;
- static uint8_t RGB_ON_OFF = 0;
- void SysTick_Handler(void)
- {
- tick_ct += 1;
- if ((tick_ct % 1000) == 0) {
- //Board_LED_Toggle(0);
- //Chip_GPIO_SetPinToggle(LPC_GPIO_PORT, 3, 5);
- switch(RGB_ON_OFF%3){
- case 0:
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
- break;
- case 1:
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
- break;
- case 2:
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 5);
- break;
- default:
- break;
- }//end--> switch-case
- RGB_ON_OFF++;
- }//end--> if statement
- }
- void Delay_ms(uint32_t N_ms){
- uint32_t start_cnt = tick_ct;
- while((tick_ct - start_cnt) <= N_ms){
- ;
- }
- }
- void RGB_LED_Blinky_with_Delay(uint32_t Nms_Delay){
- /* P6.11 GPIO3[7] : LED green as output */
- /* P2_7 GPIO0[7] : LED green as output */
- /* P6.9 GPIO3[5] : LED blue as output */
- //Red On, Green & Blue Off
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
- Delay_ms(Nms_Delay);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
- Delay_ms(Nms_Delay);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 5);
- Delay_ms(Nms_Delay);
- //Turn off R,G,B LEDs
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 7);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 5);
- Delay_ms(Nms_Delay);
- }
- /**
- * @brief main routine for blinky example
- * @return Function should not exit.
- */
- int main(void)
- {
- SystemCoreClockUpdate();
- Board_Init();
- /* Enable and setup SysTick Timer at a periodic rate */
- SysTick_Config(SystemCoreClock / TICKRATE_HZ);
- RGB_LED_Blinky_with_Delay(500);
- /* SSP initialization */
- Board_SSP_Init(LPC_SSP);
- Chip_SSP_Init(LPC_SSP);
- DEBUGOUT("\r\nBoard_SSP and Chip_SSP Init Done\n\r");
- ssp_format.frameFormat = SSP_FRAMEFORMAT_SPI;
- ssp_format.bits = SSP_DATA_BITS;
- ssp_format.clockMode = SSP_CLOCK_MODE0;
- Chip_SSP_SetFormat(LPC_SSP, ssp_format.bits, ssp_format.frameFormat, ssp_format.clockMode);
- Chip_SSP_Enable(LPC_SSP);
- Chip_SSP_SetMaster(LPC_SSP, 1);
- DEBUGOUT("\r\nMaster Mode\n\r");
- /* oled Initialization*/
- OLED_Init();
- DEBUGOUT("\r\nOLED Init Done\n\r");
- while (1) {
- //__WFI();
- OLED_BMP_TEST();
- }
- }
- void GPIO_TEST(void){
- #if 0
- Chip_SCU_ClockPinMuxSet(0, (SCU_PINIO_FAST | SCU_MODE_FUNC1)); /* CLK0 connected to CLK SCU_MODE_FUNC6=SSP1 CLK1 */
- //J1 P1_3 MISO
- Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 0, 10);
- Chip_GPIO_SetPinState(LPC_GPIO_PORT, 0, 10, (bool) TRUE );
- //J1 P1_4 MOSI
- Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 0, 11);
- Chip_GPIO_SetPinState(LPC_GPIO_PORT, 0, 11, (bool) TRUE );
- //J1 P1_5 SSEL
- Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 1, 8);
- Chip_GPIO_SetPinState(LPC_GPIO_PORT, 1, 8, (bool) TRUE );
- //J1 P6_5 Simulate RES Pin
- Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 3, 4);
- Chip_GPIO_SetPinState(LPC_GPIO_PORT, 3, 4, (bool) TRUE );
- //J2 P1_6
- Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 1, 9);
- Chip_GPIO_SetPinState(LPC_GPIO_PORT, 1, 9, (bool) TRUE );
- //J2 P1_9
- 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, 0, 10);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, 11);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 1, 8);
- Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 3, 4); //RES
- 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, 0, 10);
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, 11);
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 1, 8);
- Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 3, 4); //RES
- 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
- }
复制代码完整代码: |