我发一下我的代码吧 主函数#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
int main(void)
{
delay_init(168); //³õʼ»¯ÑÓʱº¯Êý
LED_08_GPIO_Config(); //³õʼ»¯LED¶Ë¿Ú
while(1)
{
Text1_Led();
delay_ms(4);
}
}
LED.H
#ifndef _LED_SCREEN_
#define _LED_SCREEN_
#include "sys.h"
//ÖÃ1¸ßµçƽ£¬ÖÃ0µÍµçƽ
#define A(x) x?GPIO_SetBits(GPIOF,GPIO_Pin_15):GPIO_ResetBits(GPIOF,GPIO_Pin_15)
#define B(x) x?GPIO_SetBits(GPIOF,GPIO_Pin_13):GPIO_ResetBits(GPIOF,GPIO_Pin_13)
#define C(x) x?GPIO_SetBits(GPIOC,GPIO_Pin_5):GPIO_ResetBits(GPIOC,GPIO_Pin_5)
#define D(x) x?GPIO_SetBits(GPIOA,GPIO_Pin_7):GPIO_ResetBits(GPIOA,GPIO_Pin_7)
//R0 G0 ¿ØÖÆÉÏ°ëÆÁµÄºìÂÌ£¬R1 G1¿ØÖÆÏ°ëÆÁµÄºìÂÌ£¬1µÄʱºò¸ßµçƽ£¬²»ÁÁ
#define R0(x) x?GPIO_SetBits(GPIOF,GPIO_Pin_9):GPIO_ResetBits(GPIOF,GPIO_Pin_9)
#define R1(x) x?GPIO_SetBits(GPIOF,GPIO_Pin_7):GPIO_ResetBits(GPIOF,GPIO_Pin_7)
#define G0(x) x?GPIO_SetBits(GPIOC,GPIO_Pin_3):GPIO_ResetBits(GPIOC,GPIO_Pin_3)
#define G1(x) x?GPIO_SetBits(GPIOC,GPIO_Pin_1):GPIO_ResetBits(GPIOC,GPIO_Pin_1)
//OEʹÄÜ
#define OE(x) x?GPIO_SetBits(GPIOC,GPIO_Pin_0):GPIO_ResetBits(GPIOC,GPIO_Pin_0)
//STBËø´æ¿Ú
#define STB(x) x?GPIO_SetBits(GPIOF,GPIO_Pin_10):GPIO_ResetBits(GPIOF,GPIO_Pin_10)
//CLKʱÖÓ¿Ú
#define CLK(x) x?GPIO_SetBits(GPIOF,GPIO_Pin_4):GPIO_ResetBits(GPIOF,GPIO_Pin_4)
/*--------------------------1--------------------------*/
void LED_08_GPIO_Config(void);
void Led_Light(void);
void Clean_595(void);//ÇåÆÁ£¬Çå³ýËø´æÆ÷ÀïûÓõÄÐÅÏ¢£¬LEDµãÕóÈ«Ãð
void WriteDataToFirstHalScreen(void);
void SetRowLight(void);
void Text1_Led(void);
#endif
LED.C
#include "led.h"
#include "sys.h"
//LED IO³õʼ»¯
void LED_08_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//ʹÄÜGPIOFʱÖÓ
//GPIOF³õʼ»¯ÉèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_3 ;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
//A B R0 R1 STB CLK¶ÔÓ¦IO¿Ú
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15|GPIO_Pin_13|GPIO_Pin_9|GPIO_Pin_7| GPIO_Pin_10|GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//ÏÂÀ
GPIO_Init(GPIOF, &GPIO_InitStructure);//³õʼ»¯GPIO
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//ʹÄÜGPIOCʱÖÓ
//GPIOC³õʼ»¯ÉèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_2;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
//C G0 G1 OE¶ÔÓ¦µÄIO¿Ú
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5| GPIO_Pin_3| GPIO_Pin_1| GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//ÏÂÀ
GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯GPIO
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//ʹÄÜGPIOBʱÖÓ
//GPIOB³õʼ»¯ÉèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO
GPIO_SetBits(GPIOB,GPIO_Pin_2);//GPIOBÉèÖøߣ¬µÆÃð
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ʹÄÜGPIOAʱÖÓ
//GPIOA³õʼ»¯ÉèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//ÏÂÀ
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
}
/***************************************************************
*º¯ÊýÃû£ºLed_Light
*ÐβÎ:void
*·µ»ØÖµ:void
*º¯Êý¹¦ÄÜ£ºµãÁÁÖ÷³ÌÐò£¬ÏÈÇåÆÁ£¬È»ºóÈúìµÆÈ«ÁÁ
****************************************************************/
void Led_Light(void)
{
OE(1);//¸ßµçƽ£¬Ê¹ÄÜ
Clean_595();//Çå³ýËø´æÆ÷ÐÅÏ¢£¬ºìÂÌÈ«Ãð
while(1)
{
WriteDataToFirstHalScreen();//дÈëÈ«µãÁÁºìµÆ
SetRowLight();
}
}
/***************************************************************
*º¯ÊýÃû:Clean_595
*ÐβÎ:void
*·µ»ØÖµ:void
*º¯Êý¹¦ÄÜ£ºÇåÆÁ£¬ÇåÇå³ýËø´æÆ÷ÀïÃæµÄÐÅÏ¢£¬LEDµãÕóÈ«Ãð£¬ÖøߵçƽºìÂÌ
****************************************************************/
void Clean_595(void)
{
int columnarr[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//¿ØÖÆ16¸ö595
int send_data[64]={0}; //16½øÖƱä2½øÖƱ£´æ£¬·¢Ë͸ø595
int i,j;
STB(0);//Ëø´æµÍµçƽ
for(i=0;i<8;i++)//°ÑËùÓÐÊý¾Ý´æÈësend_dataÖУ¬¿ØÖÆ64ÁÐ
{
for(j=0;j<8;j++)
{
send_data[j+i*8]=columnarr[i]%2;
columnarr[i]=columnarr[i]/2;
}
}
for(i=0;i<64;i++)
{
CLK(0);//ʱÖӵ͵çƽ£¬²»½ÓÊÕ
R0(send_data[i]);//·¢ËÍÏàӦλµçƽ£¬Ò»Î»Ò»Î»·¢ËÍ
G0(send_data[i]);
CLK(1);//ʱÖӸߵçƽ£¬½ÓÊÜ
}
STB(1);
CLK(0);
STB(0);
}
/***************************************************************
*º¯ÊýÃû:WriteDataToFirstHalScreen
*ÐβÎ:void
*·µ»ØÖµ:void
*º¯Êý¹¦ÄÜ£º·¢ËÍÊý¾Ý¿ØÖÆÁÐLEDÁÁÃð/ºìµÆ
****************************************************************/
void WriteDataToFirstHalScreen(void)
{
int i,j;
int columnarr[8]={0};
int send_data[64]={0};
STB(0);//Ëø´æµÍµçƽ£¬²»·¢ËÍ
for(i=0;i<8;i++)//°ÑËùÓÐÊý¾Ý´æÈëaÖУ¬¿ØÖÆ64ÁÐ
{
for(j=0;j<8;j++)
{
send_data[j+i*8]=columnarr[i]%2;
columnarr[i]=columnarr[i]/2;
}
}
for(i=0;i<64;i++)
{
CLK(0);
R0(send_data[i]);//·¢ËÍÏàӦλµÄµçƽ£¬Ò»Î»Ò»Î»·¢ËÍ
CLK(1);//½ÓÊÕÊý¾Ý´æÈëËø´æ
}
STB(1); //Ëø´æÖøߣ¬·¢ËÍÊý¾Ý
CLK(0);//È«²¿Öõͣ¬²»·¢ËͲ»½ÓÊÕ
STB(0);
}
/***************************************************************
*º¯ÊýÃû:SetRowLight
*ÐβÎ:void
*·µ»ØÖµ:void
*º¯Êý¹¦ÄÜ£ºµãÁÁÖ÷³ÌÐò£¬ÏÈÇåÆÁ£¬È»ºóÈúìµÆÈ«ÁÁ
****************************************************************/
/*ÐÐÐźÅÑ¡Ôñº¯Êý£¬ÊäÈëÐк󣬽øÐÐabcdµÄ2½øÖÆת»»Ñ¡Ôñ¸Ä±ä¸ßµÍµçƽ¶øÑ¡ÔñÐÐ*/
void SetRowLight(void)
{
//u8 weixuan[16]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
int rowarr[4] = {0};
int i=0,rownum;
for(rownum=0;rownum<16;rownum++)
{
OE(0);
//»»Ëã2½øÖÆ£¬·¢ËÍÏàÓ¦ÐÐ
while(rownum>0)
{
rowarr[i] = rownum%2;
rownum = rownum/2;
i++;
}
A(rowarr[0]);//·¢ËÍÏàÓ¦µÄµçƽ£¬È·¶¨ÄÄÒ»ÐÐ
B(rowarr[1]);
C(rowarr[2]);
D(rowarr[3]);
OE(1);
}
}
void Text1_Led(void)
{
LED_08_GPIO_Config();
Led_Light();
}
sys.h
#ifndef __SYS_H
#define __SYS_H
#include "stm32f4xx.h"
//0,²»Ö§³Öucos
//1,Ö§³Öucos
#define SYSTEM_SUPPORT_OS 0 //¶¨ÒåϵͳÎļþ¼ÐÊÇ·ñÖ§³ÖUCOS
//λ´ø²Ù×÷,ʵÏÖ51ÀàËƵÄGPIO¿ØÖƹ¦ÄÜ
//¾ßÌåʵÏÖ˼Ïë,²Î¿¼<<CM3ȨÍþÖ¸ÄÏ>>µÚÎåÕÂ(87Ò³~92Ò³).M4ͬM3ÀàËÆ,Ö»ÊǼĴæÆ÷µØÖ·±äÁË.
//IO¿Ú²Ù×÷ºê¶¨Òå
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO¿ÚµØÖ·Ó³Éä
#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
#define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
#define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
//IO¿Ú²Ù×÷,Ö»¶Ôµ¥Ò»µÄIO¿Ú!
//È·±£nµÄֵСÓÚ16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //Êä³ö
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //ÊäÈë
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //Êä³ö
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //ÊäÈë
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //Êä³ö
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //ÊäÈë
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //Êä³ö
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //ÊäÈë
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //Êä³ö
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //ÊäÈë
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //Êä³ö
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //ÊäÈë
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //Êä³ö
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //ÊäÈë
#define PHout(n) BIT_ADDR(GPIOH_ODR_Addr,n) //Êä³ö
#define PHin(n) BIT_ADDR(GPIOH_IDR_Addr,n) //ÊäÈë
#define PIout(n) BIT_ADDR(GPIOI_ODR_Addr,n) //Êä³ö
#define PIin(n) BIT_ADDR(GPIOI_IDR_Addr,n) //ÊäÈë
//ÒÔÏÂΪ»ã±àº¯Êý
void WFI_SET(void); //Ö´ÐÐWFIÖ¸Áî
void INTX_DISABLE(void);//¹Ø±ÕËùÓÐÖжÏ
void INTX_ENABLE(void); //¿ªÆôËùÓÐÖжÏ
void MSR_MSP(u32 addr); //ÉèÖöÑÕ»µØÖ·
#endif
为啥我的LED点阵必须要单片机按复位过后才能亮(闪一下就没了),而且只亮两排。 |