可以自己,只有程序包哦
單片機源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f4xx_hal.h"
- #include "i2c.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- #include "MCP23017.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- //the param can be 0 to 7,don't solder A0,A1,A2 result in default address 0x27,param is 7
- //Addr(BIN) Addr(hex) param
- //010 0111 0x27 7
- //010 0110 0x26 6
- //010 0101 0x25 5
- //010 0100 0x24 4
- //010 0011 0x23 3
- //010 0010 0x22 2
- //010 0001 0x21 1
- //010 0000 0x20 0
- begin(7);
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_I2C1_Init();
- /* USER CODE BEGIN 2 */
- //Change this Macro definition to different demo test
- #define debug 0
- #define outputTest 0
- #define inputTest 0
- //For interrupt test, you need Connect PA7 and PB7 to a button(when the button pressed,it connect to GND)
- #define interruptTest 1
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- HAL_Delay(500);
- #if(debug)
- for(int addr = 0; addr< 22; addr++)
- {
- printf("addr:0x%02x,value:0x%02x\r\n",addr,readRegister(addr));
- }
- printf("------------------------------------------\r\n");
- #endif
- #if(outputTest)
- for(int i = 0; i<16; i++)
- {
- pinMode(i, 0);
- }
- for(int i = 0; i<16; i++)
- {
- digitalWrite(i,1);
- }
- HAL_Delay(500);
- for(int i = 0; i<16; i++)
- {
- digitalWrite(i,0);
- }
- #endif
- #if(inputTest)
- //Pin0 Output
- pinMode(0, 0);
- //Pin1 Input
- pinMode(1, 1);
- //Pin1 Pullup
- pullUp(1,1);
- digitalWrite(0, digitalRead(1));
- #endif
- #if(interruptTest)
- //mirroring, don't openDrain, INT output pin will be low when interrupt occur
- setupInterrupts(1,0,0);
- //input
- pinMode(7,1);
- //pullup
- pullUp(7,1);
- //1 means compare against given value,and 1 means when differ from 1 occur the interrupt
- setupInterruptPin(7,1);
- //input
- pinMode(15,1);
- //pullup
- pullUp(15,1);
- //1 means compare against given value,and 1 means when differ from 1 occur the interrupt
- setupInterruptPin(15,1);
- #endif
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- 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 = 4;
- RCC_OscInitStruct.PLL.PLLN = 168;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |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(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- if(GPIO_Pin == GPIO_PIN_0)
- {
- printf("_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\r\n");
- printf("the key has been pressed!!!\r\n");
- printf("the last interrupt Pin is: %d\r\n",getLastInterruptPin());
- printf("the last interrupt value is: %d\r\n",getLastInterruptPinValue());
- printf("_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\r\n");
- }
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
MCP23017-IO-Expansion-Board-Demo-Code.7z
(3.85 MB, 下載次數: 65)
2019-1-27 15:59 上傳
點擊文件名下載附件
|