找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3225|回复: 2
收起左侧

mcp23017程序包:树莓PI+STM32F429+arduino

[复制链接]
ID:473376 发表于 2019-1-27 15:59 | 显示全部楼层 |阅读模式
可以自己,只有程序包哦
0.png

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f4xx_hal.h"
  4. #include "i2c.h"
  5. #include "usart.h"
  6. #include "gpio.h"

  7. /* USER CODE BEGIN Includes */
  8. #include "MCP23017.h"
  9. /* USER CODE END Includes */

  10. /* Private variables ---------------------------------------------------------*/

  11. /* USER CODE BEGIN PV */
  12. /* Private variables ---------------------------------------------------------*/

  13. /* USER CODE END PV */

  14. /* Private function prototypes -----------------------------------------------*/
  15. void SystemClock_Config(void);

  16. /* USER CODE BEGIN PFP */
  17. /* Private function prototypes -----------------------------------------------*/

  18. /* USER CODE END PFP */

  19. /* USER CODE BEGIN 0 */

  20. /* USER CODE END 0 */

  21. /**
  22.   * @brief  The application entry point.
  23.   *
  24.   * @retval None
  25.   */
  26. int main(void)
  27. {
  28.   /* USER CODE BEGIN 1 */

  29.   /* USER CODE END 1 */

  30.   /* MCU Configuration----------------------------------------------------------*/

  31.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  32.   HAL_Init();

  33.   /* USER CODE BEGIN Init */
  34.   //the param can be 0 to 7,don't solder A0,A1,A2 result in default address 0x27,param is 7
  35.   //Addr(BIN)    Addr(hex)   param
  36.   //010 0111      0x27         7
  37.   //010 0110      0x26         6
  38.   //010 0101      0x25         5
  39.   //010 0100      0x24         4
  40.   //010 0011      0x23         3
  41.   //010 0010      0x22         2
  42.   //010 0001      0x21         1
  43.   //010 0000      0x20         0
  44.   begin(7);
  45.   /* USER CODE END Init */

  46.   /* Configure the system clock */
  47.   SystemClock_Config();

  48.   /* USER CODE BEGIN SysInit */

  49.   /* USER CODE END SysInit */

  50.   /* Initialize all configured peripherals */
  51.   MX_GPIO_Init();
  52.   MX_USART1_UART_Init();
  53.   MX_I2C1_Init();
  54.   /* USER CODE BEGIN 2 */
  55.   //Change this Macro definition to different demo test
  56.   #define debug 0
  57.   #define outputTest 0
  58.   #define inputTest 0
  59.   //For interrupt test, you need Connect PA7 and PB7 to a button(when the button pressed,it connect to GND)
  60.   #define interruptTest 1
  61.   /* USER CODE END 2 */

  62.   /* Infinite loop */
  63.   /* USER CODE BEGIN WHILE */
  64.     while (1)
  65.     {

  66.   /* USER CODE END WHILE */

  67.   /* USER CODE BEGIN 3 */

  68.         HAL_Delay(500);

  69.         #if(debug)
  70.         for(int addr = 0; addr< 22; addr++)
  71.         {
  72.           printf("addr:0x%02x,value:0x%02x\r\n",addr,readRegister(addr));
  73.         }
  74.         printf("------------------------------------------\r\n");
  75.         #endif

  76.         #if(outputTest)
  77.         for(int i = 0; i<16; i++)
  78.         {
  79.             pinMode(i, 0);
  80.         }

  81.         for(int i = 0; i<16; i++)
  82.         {
  83.             digitalWrite(i,1);
  84.         }

  85.         HAL_Delay(500);

  86.         for(int i = 0; i<16; i++)
  87.         {
  88.             digitalWrite(i,0);
  89.         }
  90.         #endif

  91.         #if(inputTest)
  92.         //Pin0 Output
  93.         pinMode(0, 0);
  94.         //Pin1 Input
  95.         pinMode(1, 1);
  96.         //Pin1 Pullup
  97.         pullUp(1,1);
  98.         digitalWrite(0, digitalRead(1));
  99.         #endif

  100.         #if(interruptTest)
  101.         //mirroring, don't openDrain, INT output pin will be low when interrupt occur
  102.         setupInterrupts(1,0,0);

  103.         //input
  104.         pinMode(7,1);
  105.         //pullup
  106.         pullUp(7,1);
  107.         //1 means compare against given value,and 1 means when differ from 1 occur the interrupt
  108.         setupInterruptPin(7,1);

  109.         //input
  110.         pinMode(15,1);
  111.         //pullup
  112.         pullUp(15,1);
  113.         //1 means compare against given value,and 1 means when differ from 1 occur the interrupt
  114.         setupInterruptPin(15,1);
  115.         #endif
  116.     }
  117.   /* USER CODE END 3 */

  118. }

  119. /**
  120.   * @brief System Clock Configuration
  121.   * @retval None
  122.   */
  123. void SystemClock_Config(void)
  124. {

  125.   RCC_OscInitTypeDef RCC_OscInitStruct;
  126.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  127.     /**Configure the main internal regulator output voltage
  128.     */
  129.   __HAL_RCC_PWR_CLK_ENABLE();

  130.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  131.     /**Initializes the CPU, AHB and APB busses clocks
  132.     */
  133.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  134.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  135.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  136.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  137.   RCC_OscInitStruct.PLL.PLLM = 4;
  138.   RCC_OscInitStruct.PLL.PLLN = 168;
  139.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  140.   RCC_OscInitStruct.PLL.PLLQ = 4;
  141.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  142.   {
  143.     _Error_Handler(__FILE__, __LINE__);
  144.   }

  145.     /**Initializes the CPU, AHB and APB busses clocks
  146.     */
  147.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  148.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  149.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  150.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  151.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  152.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  153.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  154.   {
  155.     _Error_Handler(__FILE__, __LINE__);
  156.   }

  157.     /**Configure the Systick interrupt time
  158.     */
  159.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  160.     /**Configure the Systick
  161.     */
  162.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  163.   /* SysTick_IRQn interrupt configuration */
  164.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  165. }

  166. /* USER CODE BEGIN 4 */
  167. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  168. {
  169.   if(GPIO_Pin == GPIO_PIN_0)
  170.   {
  171.       printf("_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\r\n");
  172.       printf("the key has been pressed!!!\r\n");
  173.       printf("the last interrupt Pin is: %d\r\n",getLastInterruptPin());
  174.       printf("the last interrupt value is: %d\r\n",getLastInterruptPinValue());
  175.       printf("_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\r\n");
  176.   }  
  177. }
  178. /* USER CODE END 4 */

  179. /**
  180.   * @brief  This function is executed in case of error occurrence.
  181.   * @param  file: The file name as string.
  182.   * @param  line: The line in file as a number.
  183.   * @retval None
  184.   */
  185. void _Error_Handler(char *file, int line)
  186. {
  187.   /* USER CODE BEGIN Error_Handler_Debug */
  188.   /* User can add his own implementation to report the HAL error return state */
  189.   while(1)
  190.   {
  191.   }
  192. ……………………

  193. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
MCP23017-IO-Expansion-Board-Demo-Code.7z (3.85 MB, 下载次数: 63)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:529070 发表于 2019-5-6 22:42 | 显示全部楼层
谢谢分享。。
回复

使用道具 举报

ID:364261 发表于 2020-3-4 09:38 | 显示全部楼层
谢谢楼主!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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