📄 led.c
字号:
/*
* 流水灯程序
* EasyAVR-M16 开发板例程
* CopyRights@EES
*
* 实现LED 流水显示
*
*/
/*
* File Name:led.c
* CopyRights @ EES
*
* Author:Einsn Liu
*
* Date:2007-5-24
*/
#include <inttypes.h>
#include <util/delay.h> //调用winavr提供的延时程序
#include <avr/io.h>
#include <stdio.h>
/*
* Hardware define
*
*/
#define LED0 PA1
#define LED1 PA2
#define LED2 PA3
#define LED3 PA4
#define LED4 PD0
#define LED5 PD1
#define LED6 PC0
#define LED7 PC1
#define LEDON(i) do{\
if(i<4)PORTA&=~(1<<(i+1));\
else if(i<6)PORTD&=~(1<<(i-4));\
else if(i<8)PORTC&=~(1<<(i-6));\
}while(0)
//由于PA是1~4,i从0开始算,所以当i<4时要+1,才可以得到需要的1~4的数值,来控制PA口;同理PD,PC
#define LEDOFF(i) do{\
if(i<4)PORTA|=(1<<(i+1));\
else if(i<6)PORTD|=(1<<(i-4));\
else if(i<8)PORTC|=(1<<(i-6));\
}while(0)
#define LEDPORTINIT() do{\
DDRA|=(1<<PA1)|(1<<PA2)|(1<<PA3)|(1<<PA4);\
DDRD|=(1<<PD0)|(1<<PD1);\
DDRC|=(1<<PC0)|(1<<PC1);\
}while(0)
//PA1~4;PD0,1;PC0,1为输出,低电平点亮LED,高电平熄灭
//End Hardware define
/*
* Function: LEDOutput()
* IN:val : bitx is the value of the ledx such as: bit0 = led0 :when 1:led on ;0 led off
* OUT: None
* Author:Einsn
* Date:2007-04-21
* Others:
*/
void LEDOutput(uint8_t val)
{
uint8_t i;
for(i=0;i<8;i++)
{
if(val&0x01)
{
LEDON(i);
}
else
{
LEDOFF(i);
}
val>>=1;
}
}
/*
* Function: delay_s()
* IN:ss delay times in second
* OUT: None
* Author:Einsn
* Date:2007-04-21
* Others:use the WinAVR API _delay_ms
*/
void delay_s(uint8_t ss)
{
uint8_t i,t;
for(i=0;i<ss;i++)
{
t=100;
while(--t>0)
{
_delay_ms(10);
}
}
}
/*
* Function: main()
* IN:None
* OUT: None
* Author:Einsn
* Date:2007-04-21
* Others:
*/
int main(void)
{
uint8_t i,led;
uint8_t val[8]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
LEDPORTINIT();
while(1)
{
for(i=0;i<8;i++)
{
led=val[i];
do
{
LEDOutput(led);
delay_s(1);
led<<=1;
}
while(!(led&0x80));
if(i==7)break;
LEDOutput(led);
delay_s(1);
while(!(led&0x01))
{
led>>=1;
LEDOutput(led);
delay_s(1);
};
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -