两片595级联,可控制16路灯,只用了十路。
灯的状态,有常亮,灭,快闪,慢闪
1、初始化IO
2、设定灯状态
- gIO[LEDALM].status = IO_SLOW;
- gIO[LED1].status = IO_SLOW;
- gIO[LED2].status = IO_FAST;
- gIO[LED2].status = IO_FAST;
- gIO[LED3].status = IO_FAST;
- gIO[LED4].status = IO_FAST;
- gIO[LED5].status = IO_FAST;
复制代码
3、定时调用
led_display()
- #ifndef _595_H
- #define _595_H
- //#include "stm32f0xx_hal.h"
- #include "stm32f10x.h"
- #define SLOW_SUM_TIME 30
- #define SLOW_ON_TIME 15
- #define FAST_SUM_TIME 4
- #define FAST_ON_TIME 1
- #define IONUM 11
- #define LED1MASK (0x0001<<11)
- #define LED2MASK (0x0001<<10)
- #define LED3MASK (0x0001<<9)
- #define LED4MASK (0x0001<<8)
- #define LED5MASK (0x0001<<15)
- #define LED6MASK (0x0001<<14)
- #define LED7MASK (0x0001<<13)
- #define LED8MASK (0x0001<<12)
- #define LEDPOWERMASK (0x0001<<0)
- #define LEDALMMASK (0x0001<<3)
- #define BEEPMASK (0x0001<<7)
- #define LED1 0
- #define LED2 1
- #define LED3 2
- #define LED4 3
- #define LED5 4
- #define LED6 5
- #define LED7 6
- #define LED8 7
- #define LEDPOWER 8
- #define LEDALM 9
- #define BEEP 10
- typedef enum
- {
- IO_OFF = 0,
- IO_ON,
- IO_FAST, //¿ìÉÁ
- IO_SLOW //ÂýÉÁ
- }IO_STUTAS;
- typedef struct
- {
- const uint16_t mask;
- IO_STUTAS status;
- uint8_t onCnt;
- }io_t;
- #define _595_RCLK_PORT GPIOC
- #define _595_RCLK_PIN GPIO_Pin_10 //´æ´¢¼Ä´æÆ÷ʱÖÓ
- #define _595_SRCLK_PORT GPIOC
- #define _595_SRCLK_PIN GPIO_Pin_11 //ÒÆλ¼Ä´æÆ÷ʱÖÓ
- #define _595_SER_PORT GPIOA
- #define _595_SER_PIN GPIO_Pin_11 //´®ÐÐÊý¾ÝÊäÈë
- //#define _595_SER_PORT GPIOA
- //#define _595_SER_PIN GPIO_Pin_0 //´®ÐÐÊý¾ÝÊäÈë
- //#define LED_Write(X) X>0? GPIO_WriteBit(LED_PORT,LED_PIN,GPIO_PIN_SET) : GPIO_WriteBit(LED_PORT,LED_PIN,GPIO_PIN_RESET)
- #define _595_RCLK_Write(X) X>0? GPIO_WriteBit(_595_RCLK_PORT,_595_RCLK_PIN,Bit_SET) : GPIO_WriteBit(_595_RCLK_PORT,_595_RCLK_PIN,Bit_RESET)
- #define _595_SRCLK_Write(X) X>0? GPIO_WriteBit(_595_SRCLK_PORT,_595_SRCLK_PIN,Bit_SET) : GPIO_WriteBit(_595_SRCLK_PORT,_595_SRCLK_PIN,Bit_RESET)
- #define _595_SER_Write(X) X>0? GPIO_WriteBit(_595_SER_PORT,_595_SER_PIN,Bit_SET) : GPIO_WriteBit(_595_SER_PORT,_595_SER_PIN,Bit_RESET)
- extern io_t gIO[IONUM];
- void _595_GPIO_Init(void);
- void _595_WriteByte(uint8_t byte);
- void _595_WriteMultiByte(uint16_t multibyte);
- void led_display(void);
- void led_runTogle();
- void delay(uint16_t t);
- #endif
复制代码
- #include "595.h"
- #define DELAY 10000
- io_t gIO[IONUM] =
- {
- {LED1MASK,IO_OFF,0},
- {LED2MASK,IO_OFF,0},
- {LED3MASK,IO_OFF,0},
- {LED4MASK,IO_OFF,0},
- {LED5MASK,IO_OFF,0},
- {LED6MASK,IO_OFF,0},
- {LED7MASK,IO_OFF,0},
- {LED8MASK,IO_OFF,0},
- {LEDPOWERMASK,IO_ON,0},
- {LEDALMMASK,IO_OFF,0},
- {BEEPMASK,IO_OFF,0}
- };
- void _595_GPIO_Init()
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //ʹÄÜPC¶Ë¿ÚʱÖÓ
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜPA¶Ë¿ÚʱÖÓ
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //ʹÄÜPA¶Ë¿ÚʱÖÓ
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
- GPIO_Init(GPIOC, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯
- GPIO_InitStructure.GPIO_Pin = _595_SER_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
- GPIO_Init(_595_SER_PORT, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
- GPIO_Init(GPIOE, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯
-
- GPIO_WriteBit(GPIOE,GPIO_Pin_0,Bit_SET);
-
- _595_SER_Write(0);
- _595_SRCLK_Write(0);
- _595_RCLK_Write(0);
- }
- void _595_WriteByte(uint8_t byte)
- {
- uint8_t i;
- for(i=0;i<8;i++)
- {
- if(byte&0x80)
- _595_SER_Write(1);
- else
- _595_SER_Write(0);
- _595_SRCLK_Write(0);
- delay(DELAY);
- _595_SRCLK_Write(1);
- delay(DELAY);
- byte <<= 1;
- }
- }
- void _595_WriteMultiByte(uint16_t multibyte)
- {
- uint16_t temp = 0;
- temp = multibyte&0x00ff;
- _595_WriteByte(temp);
- temp = (multibyte>>8)&0x00ff;
- _595_WriteByte(temp);
- _595_RCLK_Write(0);
- delay(DELAY);
- _595_RCLK_Write(1);
- delay(DELAY);
- }
- void led_display()
- {
- uint8_t i;
- uint16_t iobuff = 0;
-
- for(i = 0 ;i<IONUM;i++)
- {
- if(gIO[i].status == IO_ON)
- {
- iobuff &= (~gIO[i].mask);
- }
- else if(gIO[i].status == IO_OFF)
- {
- iobuff |= gIO[i].mask;
- }
- else if(gIO[i].status == IO_FAST)
- {
- if(gIO[i].onCnt < FAST_ON_TIME)
- {
- iobuff &= (~gIO[i].mask);
- }
- else
- {
- iobuff |= gIO[i].mask;
- }
- gIO[i].onCnt++;
- if(gIO[i].onCnt >= FAST_SUM_TIME)
- gIO[i].onCnt = 0;
- }
- else if(gIO[i].status == IO_SLOW)
- {
- if(gIO[i].onCnt < SLOW_ON_TIME)
- {
- iobuff &= (~gIO[i].mask);
- }
- else
- {
- iobuff |= gIO[i].mask;
- }
- gIO[i].onCnt++;
- if(gIO[i].onCnt >= SLOW_SUM_TIME)
- gIO[i].onCnt = 0;
- }
- }
- _595_WriteMultiByte(iobuff);
- }
- void led_runTogle()
- {
- uint8_t flag = 0;
-
- if(flag == 0)
- {
- flag = 1 ;
- GPIO_WriteBit(GPIOE,GPIO_Pin_0,Bit_RESET);
- }
- else
- {
- flag = 0 ;
- GPIO_WriteBit(GPIOE,GPIO_Pin_0,Bit_SET);
- }
- }
- void delay(uint16_t t)
- {
- for(;t!=0;t--);
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>