代码在Arduino上可以运行, 但是移植到STM32上就不行了, 求高手赐教如何解决.
Arduino.h
- #include "Arduino.h"
- int _sda, _scl;
- void IO_Init(void);
- void Set_Pin(int pin, bool status);
- void IIC_without_ACK::IO_Init(void)
- {
- pinMode(_sda, OUTPUT);
- pinMode(_scl, OUTPUT);
- }
- void IIC_without_ACK::Set_Pin(int pin, bool status)
- {
- digitalWrite(pin, status);
- }
- IIC_without_ACK::IIC_without_ACK(int sda, int scl)
- {
- _sda = sda;
- _scl = scl;
- IO_Init();
- }
- void IIC_without_ACK::IIC_Start(void)
- {
- Set_Pin(_scl, HIGH);
- Set_Pin(_sda, HIGH);
- Set_Pin(_sda, LOW);
- Set_Pin(_scl, LOW);
- }
- void IIC_without_ACK::IIC_Stop(void)
- {
- Set_Pin(_scl, LOW);
- Set_Pin(_sda, LOW);
- Set_Pin(_scl, HIGH);
- Set_Pin(_sda, HIGH);
- }
- void IIC_without_ACK::Write_IIC_Byte(unsigned char IIC_Byte)
- {
- for(int i=0; i<8; i++)
- {
- if((IIC_Byte << i) & 0x80) Set_Pin(_sda, HIGH);
- else Set_Pin(_sda, LOW);
- Set_Pin(_scl, HIGH);
- Set_Pin(_scl, LOW);
- }
- Set_Pin(_sda, HIGH);
- Set_Pin(_scl, HIGH);
- Set_Pin(_scl, LOW);
- }
- void IIC_without_ACK::Write_IIC_Command(unsigned char IIC_Command)
- {
- IIC_Start();
- Write_IIC_Byte(0x78);
- Write_IIC_Byte(0x00);
- Write_IIC_Byte(IIC_Command);
- IIC_Stop();
- }
- void IIC_without_ACK::Begin_IIC_Data()
- {
- IIC_Start();
- Write_IIC_Byte(0x78);
- Write_IIC_Byte(0x40);
- }
- void IIC_without_ACK::Fill_Screen(unsigned char fill_Data)
- {
- unsigned char m,n;
- for(m=0; m<8; m++)
- {
- Write_IIC_Command(0xb0 + m);
- Write_IIC_Command(0x00);
- Write_IIC_Command(0x10);
- Begin_IIC_Data();
- for(n=0; n<128; n++) Write_IIC_Byte(fill_Data);
- IIC_Stop();
- }
- }
- void IIC_without_ACK::Initial()
- {
- unsigned char shellcode[] = {0xAE, 0x00, 0x10, 0x40, 0x81, 0xCF, 0xA1, 0xC8, 0xA6, 0xA8, 0x3F, 0xD3, 0x00, 0xD5, 0x80, 0xD9, 0xF1, 0xDA, 0x12, 0xDB, 0x40, 0x20, 0x02, 0x8D, 0x14, 0xA4, 0xA6, 0xAF};
- for (int i=0; i<28; i++) Write_IIC_Command(shellcode[i]);
- }
复制代码
STM32F4-main.c
- #include "main.h"
- static void SystemClock_Config(void);
- static void Error_Handler(void);
- static void EXTILine0_Config(void);
- void IIC_Start(void);
- void IIC_Stop(void);
- void Write_IIC_Byte(unsigned char IIC_Byte);
- void Write_IIC_Command(unsigned char IIC_Command);
- void Begin_IIC_Data(void);
- void Fill_Screen(unsigned char fill_Data);
- void Initial(void);
- int _sda = 0x8080, _scl = 0x8000;
- int HIGH = 0x7000, LOW = 0x7070;
- void IO_Init(void);
- void Set_Pin(int pin, int status);
- void IIC_Init(void);
- void IIC_Start(void);
- void IIC_Stop(void);
- void Write_IIC_Byte(unsigned char IIC_Byte);
- void Write_IIC_Command(unsigned char IIC_Command);
- void Begin_IIC_Data(void);
- void IIC_SetPos(unsigned char x, unsigned char y);
- void Fill_Screen(unsigned char fill_Data);
- void SSD1306_Init(void);
- int main(void)
- {
- HAL_Init();
-
- BSP_LED_Init(LED3);
- BSP_LED_Init(LED4);
-
- SystemClock_Config();
- EXTILine0_Config();
-
- IO_Init();
- HAL_Delay(10);
- Initial();
- HAL_Delay(10);
- Fill_Screen(0xff);
-
- while (1)
- {
- HAL_Delay(500);
- BSP_LED_Toggle(LED4);
- }
- }
- static void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_OscInitTypeDef RCC_OscInitStruct;
- __PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 360;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) Error_Handler();
- HAL_PWREx_ActivateOverDrive();
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) Error_Handler();
- }
- static void EXTILine0_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- __GPIOA_CLK_ENABLE();
- GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- GPIO_InitStructure.Pin = GPIO_PIN_0;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
- HAL_NVIC_EnableIRQ(EXTI0_IRQn);
- }
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- if(GPIO_Pin == KEY_BUTTON_PIN)
- {
- BSP_LED_Toggle(LED3);
- }
- }
- void IO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- __GPIOE_CLK_ENABLE();
- GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3;
- GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
- GPIO_InitStructure.Pull = GPIO_PULLDOWN;
- GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
-
- HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
- }
- void Set_Pin(int pin, int status)
- {
- if (pin == _sda) HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, status == HIGH ? GPIO_PIN_SET : GPIO_PIN_RESET);
- if (pin == _scl) HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, status == HIGH ? GPIO_PIN_SET : GPIO_PIN_RESET);
- }
- void IIC_Start(void)
- {
- Set_Pin(_scl, HIGH);
- Set_Pin(_sda, HIGH);
- Set_Pin(_sda, LOW);
- Set_Pin(_scl, LOW);
- }
- void IIC_Stop(void)
- {
- Set_Pin(_scl, LOW);
- Set_Pin(_sda, LOW);
- Set_Pin(_scl, HIGH);
- Set_Pin(_sda, HIGH);
- }
- void Write_IIC_Byte(unsigned char IIC_Byte)
- {
- for(int i=0; i<8; i++)
- {
- if((IIC_Byte << i) & 0x80) Set_Pin(_sda, HIGH);
- else Set_Pin(_sda, LOW);
- Set_Pin(_scl, HIGH);
- Set_Pin(_scl, LOW);
- }
- Set_Pin(_sda, HIGH);
- Set_Pin(_scl, HIGH);
- Set_Pin(_scl, LOW);
- }
- void Write_IIC_Command(unsigned char IIC_Command)
- {
- IIC_Start();
- Write_IIC_Byte(0x78);
- Write_IIC_Byte(0x00);
- Write_IIC_Byte(IIC_Command);
- IIC_Stop();
- }
- void Begin_IIC_Data()
- {
- IIC_Start();
- Write_IIC_Byte(0x78);
- Write_IIC_Byte(0x40);
- }
- void Fill_Screen(unsigned char fill_Data)
- {
- unsigned char m,n;
- for(m=0; m<8; m++)
- {
- Write_IIC_Command(0xb0 + m);
- Write_IIC_Command(0x00);
- Write_IIC_Command(0x10);
- Begin_IIC_Data();
- for(n=0; n<128; n++) Write_IIC_Byte(fill_Data);
- IIC_Stop();
- }
- }
- void Initial()
- {
- unsigned char shellcode[] = {0xAE, 0x00, 0x10, 0x40, 0x81, 0xCF, 0xA1, 0xC8, 0xA6, 0xA8, 0x3F, 0xD3, 0x00, 0xD5, 0x80, 0xD9, 0xF1, 0xDA, 0x12, 0xDB, 0x40, 0x20, 0x02, 0x8D, 0x14, 0xA4, 0xA6, 0xAF};
- for (int i=0; i<28; i++) Write_IIC_Command(shellcode[i]);
- }
- static void Error_Handler(void)
- {
- BSP_LED_On(LED4);
- while(1);
- }
- #ifdef USE_FULL_ASSERT
- void assert_failed(uint8_t* file, uint32_t line)
- {
- while (1);
- }
- #endif
复制代码
此帖出自
小平头技术问答
一周热门 更多>