- #include "595.h"
- #include "gpio.h"
- #include "stm32f1xx_hal.h"
- /*引脚定义*/
- #define DS_H() HAL_GPIO_WritePin(GPIOB, DS_Pin, GPIO_PIN_SET);//高电平
- #define DS_L() HAL_GPIO_WritePin(GPIOB, DS_Pin, GPIO_PIN_RESET);//低电平
- #define SHCP_H() HAL_GPIO_WritePin(GPIOB, SHCP_Pin, GPIO_PIN_SET);//高电平
- #define SHCP_L() HAL_GPIO_WritePin(GPIOB, SHCP_Pin, GPIO_PIN_RESET);//低电平
- #define STCP_H() HAL_GPIO_WritePin(STCP_GPIO_Port, STCP_Pin, GPIO_PIN_SET);//高电平
- #define STCP_L() HAL_GPIO_WritePin(STCP_GPIO_Port, STCP_Pin, GPIO_PIN_RESET);//低电平
- /*
- 16 VCC 电源 2V-6V
- 15 QA 并行输出 QB QC....QH
- 14 SI/DS DATA 串行数据输入
- 13 OE_ 输出使能 PWM控制亮度
- 12 RCK/STCP CS 输出到锁存器时钟
- 11 SCK/SHCP CLK DATA输入时钟
- 10 SCLR/MR 低电平清零 接VCC
- 9 SQH/Q7S 串行数据输出 接下一个595
- */
-
- void for_delay_us(uint32_t nus);
- void hc595_init(); //595初始化
- void hc595_out_enable(); //595使能输出
- void hc595_send_byte(uint8_t outdata); //595发送一个字节数据
- void hc595_send_multi_byte(uint8_t *ddata,uint16_t len); //595发送多个数据,ddata为发送数据指针,len为长度
- //========================================================================
- // 描述: 实现延时us.
- // 参数: none.
- // 返回: none.
- //========================================================================
- void for_delay_us(uint32_t nus)
- {
- uint32_t Delay = nus * 168/4;
- do
- {
- // __NOP();
- }
- while (Delay --);
- }
- //========================================================================
- // 描述: 595初始化引脚.
- // 参数: none.
- // 返回: none.
- //========================================================================
- void hc595_init()
- {
- //引脚初始化
- DS_L();
- STCP_L();
- SHCP_L();
- }
- //========================================================================
- // 描述: 595使能输出.
- // 参数: none.
- // 返回: none.
- //========================================================================
- void hc595_out_enable()
- {
- STCP_L();
- for_delay_us(5); // 适当延时
- STCP_H();
- for_delay_us(5); // 适当延时
- STCP_L();
- }
- //========================================================================
- // 描述: 595发送8位数据.
- // 参数: 8位数据.
- // 返回: none.
- //========================================================================
- void hc595_send_byte(uint8_t outdata)
- {
- uint8_t i;
- for(i=0;i<8;i++) //将8位数据按位发送,先发高字节再发低字节
- {
- if((outdata&0x80)==0x80)
- {
- DS_H();
- }
- else
- {
- DS_L();
- }
- SHCP_L(); //时钟线低电平
- SHCP_H(); //时钟线高电平
- outdata = outdata<<1;
- }
- }
- //========================================================================
- // 描述: 595发送数组.
- // 参数: 数组地址,数据长度.
- // 返回: none.
- //========================================================================
- void hc595_send_multi_byte(uint8_t *ddata,uint16_t len)
- {
- uint16_t i;
- for(i=0;i<len;i++)
- {
- hc595_send_byte(ddata[i]);
- }
- // hc595_out_enable();
- }
- //#endif
- /*
- #define unchar unsigned char
- #define unint unsigned int
- unsigned char table[10]; //定义10个595的变量缓存
- void datout(unsigned char add,dat) //595输出函数,一次输出16位 add595选择,dat数据
- {
- unsigned char i,j,temp;
- table[add]=dat;
- for(j=0;j<10;j++) //一次输出10个值
- {
- temp=table[j];
- for(i=0;i<8;i++)
- {
- temp=temp>>1;
- sh_cp=0;
- ds=CY;
- sh_cp=1;
- }
- }
- st_cp=0;
- st_cp=1;
- }
- void main()
- {
- P2=0xff;
- while(1)
- {
- run();
- datout(0,0x00);//第十个595 输出8
- datout(1,0xFF);//第九个595 输出7
- datout(2,0xfe);//第八个595 输出6
- datout(3,0xfd);//第七个595 输出5
- datout(4,0xfb);//第六个595 输出4
-
- datout(5,0xf7);//第五个595 输出8
- datout(6,0xef);//第四个595 输出7
- datout(7,0xdf);//第三个595 输出6
- datout(8,0xd9);//第二个595 联级第二个其余以此类推,输出11111101
- datout(9,0x80);//第一个595 单片机连接的第一个,输出11111110
- */
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdio.h"
- #include "595.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- //#define DS(n) (n?HAL_GPIO_WritePin(GPIOB, DS_Pin, GPIO_PIN_SET):HAL_GPIO_WritePin(GPIOB, DS_Pin, GPIO_PIN_RESET));
- #define LED_H() HAL_GPIO_WritePin(GPIOB, LED_Pin, GPIO_PIN_SET);//高电平
- #define LED_L() HAL_GPIO_WritePin(GPIOB, LED_Pin, GPIO_PIN_RESET);//低电平
- /*
- #define DS_H() HAL_GPIO_WritePin(GPIOB, DS_Pin, GPIO_PIN_SET);//高电平
- #define DS_L() HAL_GPIO_WritePin(GPIOB, DS_Pin, GPIO_PIN_RESET);//低电平
- #define SHCP_H() HAL_GPIO_WritePin(GPIOB, SHCP_Pin, GPIO_PIN_SET);//高电平
- #define SHCP_L() HAL_GPIO_WritePin(GPIOB, SHCP_Pin, GPIO_PIN_RESET);//低电平
- #define STCP_H() HAL_GPIO_WritePin(STCP_GPIO_Port, STCP_Pin, GPIO_PIN_SET);//高电平
- #define STCP_L() HAL_GPIO_WritePin(STCP_GPIO_Port, STCP_Pin, GPIO_PIN_RESET);//低电平
- */
- #define EN_H() HAL_GPIO_WritePin(GPIOB, EN_Pin, GPIO_PIN_SET);//高电平
- #define EN_L() HAL_GPIO_WritePin(GPIOB, EN_Pin, GPIO_PIN_RESET);//低电平
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /** 595数组输出 **/
- uint8_t BUF[10]={
- 0xfe, //1路11111110
- 0xfd, //2路11111101
- 0xfb, //3路11111011
- 0x7f, //4路11110111
- 0x03, //5路11101111
- 0xf7, //6路11011111
- 0x10, //7路10111111
- 0xdd, //8路01111111
- 0x02, //9路
- 0x08 //10路
- };
- void EN() //控制595端口使能输出
- {
- HAL_Delay(1000);
- EN_L();
- }
- void out()//控制LED输出
- {
- LED_H();
- HAL_Delay(10);
- LED_L();
- HAL_Delay(50);
- }
-
- void setup()
- {
- //控制595端口输出
- //595 OE引脚接地使能,DS,STCP,SHCP根据实际情况连接对应引脚
- //595 支持级联,数组第一个字节最先发送
- hc595_init();//HC595初始化
- hc595_send_multi_byte(BUF,10);//595发送多个数据(级联第一个595在连接多个时会变成最后一个)
- hc595_out_enable();//595使能输出
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- 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 */
- /* 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();
- /* USER CODE BEGIN 2 */
-
- // EN();//控制595输出端口使能
- //setup();
-
- printf("初始化成功...\r\n");
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- printf("运行正常...\r\n");
- EN_L(); //打开595输出
- setup(); //发送数据
- out(); //led灯闪烁
-
-
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses 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_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
复制代码
仿真程序:
595fz.7z
(5.18 MB, 下载次数: 8)
|