/*
STM8L-discovery
采用官方库的595驱动LED(共阳数码管)测试
2014.07.09 masonson
*/
#include "stm8l15x.h"
/*
#define RCK GPIOE->ODR | 0X20 //595 12 RCK锁存器
#define SRCK GPIOE->ODR | 0X40 //595 11 SCK寄存器
#define SER GPIOE->ODR | 0X80 //595 14 SDA输入数据
*/
#define RCK_H() (GPIO_SetBits (GPIOD,GPIO_Pin_2))
#define RCK_L() (GPIO_ResetBits (GPIOD,GPIO_Pin_2))
#define SRCK_H() (GPIO_SetBits (GPIOD,GPIO_Pin_0))
#define SRCK_L() (GPIO_ResetBits (GPIOD,GPIO_Pin_0))
#define SER_H() (GPIO_SetBits (GPIOD,GPIO_Pin_3))
#define SER_L() (GPIO_ResetBits (GPIOD,GPIO_Pin_3))
#define OE1_L() (GPIO_ResetBits (GPIOD,GPIO_Pin_4))
#define OE2_L() (GPIO_ResetBits (GPIOD,GPIO_Pin_5))
unsigned char DSY_CODE[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
};
unsigned char DSY_COM[]={0x01,0x02,0x04,0x08};
void CLK_config(void)
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1 );//预分频为2,系统默认HSI作为时钟,16MHz
}
void GPIO_config(void)
{
//595
GPIO_Init(GPIOD, GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Fast);
GPIO_Init(GPIOD, GPIO_Pin_2,GPIO_Mode_Out_PP_Low_Fast);
GPIO_Init(GPIOD, GPIO_Pin_3,GPIO_Mode_Out_PP_Low_Fast);
//LED
GPIO_Init(GPIOD, GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Fast);
//CKEY
GPIO_Init(GPIOD, GPIO_Pin_1,GPIO_Mode_In_PU_No_IT);
}
void In_595(uint8_t temp)
{
uint8_t i;
for(i=8;i>0;i--)
{
if(temp&0x80) SER_H();
else SER_L();
SRCK_L();
temp <<= 1;
SRCK_H();
}
}
void Out_595(void)
{
RCK_L() ;
nop();
nop();
RCK_H();
nop();
nop();
RCK_L();
}
void dynamic_display(void)
{
In_595(DSY_COM[0]);
In_595(DSY_CODE[0]);
Out_595();
In_595(DSY_COM[1]);
In_595(DSY_CODE[1]);
Out_595();
In_595(DSY_COM[2]);
In_595(DSY_CODE[2]);
Out_595();
In_595(DSY_COM[3]);
In_595(DSY_CODE[3]);
Out_595();
}
/*****************MAIN**************************/
void main(void)
{
CLK_config();
GPIO_config();
OE1_L();
OE2_L();
while(1)
{
dynamic_display();
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
此帖出自
小平头技术问答
因为这个端口要空出来给别的用
一周热门 更多>