本帖最后由 jinglixixi 于 2022-11-6 12:53 编辑
最近得到一个一件法器,它有点类似于风火轮,本是用来进行触摸操作的,但不妨用做一个LED的阵列来使用,见图1所示。 图1触摸功能板 在MCC不给力的情况下,直接选个例程在上面码代码就可完成功能的设计。 该功能扩展板的原理图,见图2所示。 图2原理图 开发板的引脚分布如图3所示,在使用时应避开被占用引脚的使用。 图3引脚分布 LED灯与开发板的引脚连接关系如下: LED16---PF4 LED15---PF3 LED14---PF2 LED13---PD7 LED9---PD6 LED10---PD3 LED11---PD2 LED12---PD1 LED17---PA2 LED18---PA3 LED19---PA4 LED20---PA5 为便于控制,所做的语句定义如下: #define LED0_init() do { PORTF.OUTSET = PIN4_bm; PORTF.DIRSET = PIN4_bm; } while (0)
#define LED0_turnOn() do { PORTF.OUTCLR = PIN4_bm; } while (0)
#define LED0_turnOff() do { PORTF.OUTSET = PIN4_bm; } while (0)
#define LED1_init() do { PORTF.OUTSET = PIN3_bm; PORTF.DIRSET = PIN3_bm; } while (0)
#define LED1_turnOn() do { PORTF.OUTCLR = PIN3_bm; } while (0)
#define LED1_turnOff() do { PORTF.OUTSET = PIN3_bm; } while (0)
#define LED2_init() do { PORTF.OUTSET = PIN2_bm; PORTF.DIRSET = PIN2_bm; } while (0)
#define LED2_turnOn() do { PORTF.OUTCLR = PIN2_bm; } while (0)
#define LED2_turnOff() do { PORTF.OUTSET = PIN2_bm; } while (0)
#define LED3_init() do { PORTD.OUTSET = PIN7_bm; PORTD.DIRSET = PIN7_bm; } while (0)
#define LED3_turnOn() do { PORTD.OUTCLR = PIN7_bm; } while (0)
#define LED3_turnOff() do { PORTD.OUTSET = PIN7_bm; } while (0)
#define LED4_init() do { PORTD.OUTSET = PIN6_bm; PORTD.DIRSET = PIN6_bm; } while (0)
#define LED4_turnOn() do { PORTD.OUTCLR = PIN6_bm; } while (0)
#define LED4_turnOff() do { PORTD.OUTSET = PIN6_bm; } while (0)
#define LED5_init() do { PORTD.OUTSET = PIN3_bm; PORTD.DIRSET = PIN3_bm; } while (0)
#define LED5_turnOn() do { PORTD.OUTCLR = PIN3_bm; } while (0)
#define LED5_turnOff() do { PORTD.OUTSET = PIN3_bm; } while (0)
#define LED6_init() do { PORTD.OUTSET = PIN2_bm; PORTD.DIRSET = PIN2_bm; } while (0)
#define LED6_turnOn() do { PORTD.OUTCLR = PIN2_bm; } while (0)
#define LED6_turnOff() do { PORTD.OUTSET = PIN2_bm; } while (0)
#define LED7_init() do { PORTD.OUTSET = PIN1_bm; PORTD.DIRSET = PIN1_bm; } while (0)
#define LED7_turnOn() do { PORTD.OUTCLR = PIN1_bm; } while (0)
#define LED7_turnOff() do { PORTD.OUTSET = PIN1_bm; } while (0)
#define LED8_init() do { PORTA.OUTSET = PIN2_bm; PORTA.DIRSET = PIN2_bm; } while (0)
#define LED8_turnOn() do { PORTA.OUTCLR = PIN2_bm; } while (0)
#define LED8_turnOff() do { PORTA.OUTSET = PIN2_bm; } while (0)
#define LED9_init() do { PORTA.OUTSET = PIN3_bm; PORTA.DIRSET = PIN3_bm; } while (0)
#define LED9_turnOn() do { PORTA.OUTCLR = PIN3_bm; } while (0)
#define LED9_turnOff() do { PORTA.OUTSET = PIN3_bm; } while (0)
#define LED10_init() do { PORTA.OUTSET = PIN4_bm; PORTA.DIRSET = PIN4_bm; } while (0)
#define LED10_turnOn() do { PORTA.OUTCLR = PIN4_bm; } while (0)
#define LED10_turnOff() do { PORTA.OUTSET = PIN4_bm; } while (0)
#define LED11_init() do { PORTA.OUTSET = PIN5_bm; PORTA.DIRSET = PIN5_bm; } while (0)
#define LED11_turnOn() do { PORTA.OUTCLR = PIN5_bm; } while (0)
#define LED11_turnOff() do { PORTA.OUTSET = PIN5_bm; } while (0)
实现LED灯闪烁效果的主程序为: int main(void) {
int i,j;
//Setup LED
LED_init();
LED0_init();
LED1_init();
LED2_init();
LED3_init();
LED4_init();
LED5_init();
LED6_init();
LED7_init();
LED8_init();
LED9_init();
LED10_init();
LED11_init();
dark();
while (1)
{
light();
for(j=0;j<1000;j++)
for(i=0;i<1000;i++);
dark();
for(j=0;j<1000;j++)
for(i=0;i<1000;i++);
}
}
所用到的2个辅助函数为: void dark(void) {
LED_turnOff();
LED0_turnOff();
LED1_turnOff();
LED2_turnOff();
LED3_turnOff();
LED4_turnOff();
LED5_turnOff();
LED6_turnOff();
LED7_turnOff();
LED8_turnOff();
LED9_turnOff();
LED10_turnOff();
LED11_turnOff();
}
void light(void) {
LED_turnOn();
LED0_turnOn();
LED1_turnOn();
LED2_turnOn();
LED3_turnOn();
LED4_turnOn();
LED5_turnOn();
LED6_turnOn();
LED7_turnOn();
LED8_turnOn();
LED9_turnOn();
LED10_turnOn();
LED11_turnOn();
}
实现LED灯顺时针旋转的程序段为: while (1) {
for(k=0;k<12;k++)
{
if(k==0) LED0_turnOn();
if(k==1) LED1_turnOn();
if(k==2) LED2_turnOn();
if(k==3) LED3_turnOn();
if(k==4) LED4_turnOn();
if(k==5) LED5_turnOn();
if(k==6) LED6_turnOn();
if(k==7) LED7_turnOn();
if(k==8) LED8_turnOn();
if(k==9) LED9_turnOn();
if(k==10) LED10_turnOn();
if(k==11) LED11_turnOn();
for(j=0;j<1000;j++)
for(i=0;i<1000;i++);
}
dark();
for(j=0;j<1000;j++)
for(i=0;i<1000;i++);
}
经程序的编译和下载,其显示效果如图4至图6所示。 图4效果1 图5效果2 图6效果3 视频:
|