📄 led-2gj.c
字号:
#include <iom48v.h>
#include <macros.h>
#include <eeprom.h>
#define uchar unsigned char
#define unit unsigned int
#define ulong unsigned long
#define xtal 8
#define fosc 8000000 //晶振8MHZ
#define baud 9600 //波特率
#define a10 PORTC&=~0X01
#define a11 PORTC|=0X01
#define a20 PORTC&=~0X02
#define a21 PORTC|=0X02
#define a30 PORTC&=~0X04;
#define a31 PORTC|=0X04;
#define a40 PORTC&=~0X08;
#define a41 PORTC|=0X08;
#define Add ((PINC&0X02)==0x02)
#define Sub ((PINC&0X01)==0x01)
uchar n;
uchar shu[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar tm[4]={0,0,0,0};
void port_init(void)
{
PORTB = 0x00;
DDRB = 0xff;
PORTC = 0x0f; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xff;
}
//Watchdog initialize
// prescale: 32K
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
WDTCSR = 0x0e; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
watchdog_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/* 微秒级延时程序 */
void del(int time)
{
do
{
time--;
}
while (time>1);
}
/* 毫秒级延时程序 */
void delay_1ms(void)
{ unsigned int i1;
for(i1=1;i1<(unsigned int)(xtal*143-2);i1++)
;
}
void delay(unsigned int n)
{
unsigned int i2=0;
while(i2<n)
{delay_1ms();
i2++; WDR();
}
}
void scan(void)
{
if(!Add)
{
delay(100);
if(!Add)
{
n++;
EEPROMwrite(1,n);
tm[0]=n/10; if(tm[0]==0) tm[0]=11; tm[1]=n%10;
delay(200);
}
}
if(!Sub)
{
delay(100);
if(!Sub)
{
n--; if(n==255) n=0;
EEPROMwrite(1,n);
tm[0]=n/10; if(tm[0]==0) tm[0]=11; tm[1]=n%10;
delay(200);
}
}
}
void main(void)
{
uchar i;
delay(200);
OSCCAL=0x5b;
n=EEPROMread(1);
if(n==255)
{ n=34;
EEPROMwrite(1,n);
}
init_devices();
tm[0]=n/10; if(tm[0]==0) tm[0]=11; tm[1]=n%10;
while(1)
{
scan();
PORTB=~shu[tm[0]];
PORTD=~shu[tm[1]];
delay(100);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -