📄 test.c
字号:
#include <avr/io.h>
#include <avr/iom48.h>
#include <avr/interrupt.h>
//#define OSC_Frequency 20000000
/*We can search to own function fast under the program*/
#if 0
void init_ext_interrupt(char int0_tm, char int1_tm);
void Power_Reduction();
void sleep_mode();
void init_IO();
unsigned char Get_digital(char ch);
void init_ADC();
void init_timer();
#endif
void init_ext_interrupt(char int0_tm, char int1_tm)
{
/*
We need to select INT toggle in this register.There are four mode that we can choose.
In mode 0: The low level of INT generates an interrupt request.
In mode 1:Any logical change on INT generates an interrupt request.
In mode 2:The falling edge of INT generates an interrupt request.
In mode 3:The rising edge of INT generates an interrupt request.
*/
//DDRD&=0xF3;
//sei(); //When using interrupt, SREG's I bit must be set.
#if 0 //choose INT toggle and enable.
char temp_ext;
temp_ext=int0_tm+(int1_tm<<2);
EICRA=temp_ext;
EIMSK=0x03; //Enable INT interrupt. pin 0 is INT0, pin1 is INT1.
//'EIFR' is external interrupt flag register.
#endif
#if 0 //Pin change interrupt control.
PCICR=0x07; //Enable Pin change interrupt, pin0 is portB, pin1 is portC, pin2 is portD.
//'PCIFR' is pin change interrupt flag register.
PCMSK0=0xff //Enable pin to use for portB.
PCMSK1=0xff //Enable pin to use for portC.
PCMSK2=0xff //Enable pin to use for portD.
#endif
}
void Power_Reduction()
{
#if 0
PPR=0x00; //No reduction the power
#endif
#if 0 //When want to redue
char *mode;
switch(mode)
{
case "TWI":
{
PRR|=0x80;
}
break;
case "Timer2":
{
PRR|=0x40;
}
break;
case "Timer0":
{
PRR|=0x20;
}
break;
case "Timer1":
{
PRR|=0x08;
}
break;
case "SPI":
{
PRR|=0x04;
}
break;
case "USART0":
{
PRR|=0x02;
}
break;
case "ADC":
{
PRR|=0x01;
}
break;
}
#endif
}
void sleep_mode()
{
#if 0
SMCR|=0x01; //sleep mode on
#endif
#if 0
SMCR&=~0x01; //sleep mode off
#endif
#if 0 //when sleep mode must on
char *mode;
switch(mode)
{
case "Idle":
{
SMCR=0x01;
}
break;
case "ADC":
{
SMCR=0x03;
}
break;
case "Pdown":
{
SMCR=0x04;
}
break;
case "Psave":
{
SMCR=0x07;
}
break;
case "Standby":
{
SMCR=0x0d;
}
break;
}
#endif
}
void init_IO()
{
//MCUCR.4 can set Pull-up Disable
//Control the I/O Port
#if 0 //set to be out
DDRB=0xff;
DDRC=0xff;
DDRD=0xff;
#endif
#if 0 //be set in
DDRB=0x00;
DDRC=0x00;
DDRD=0x00;
#endif
}
unsigned char Get_digital(char ch)
{
#if 0 //For portB and DDRB must be set 0x00
char port_temp;
port_temp=PINB;
if (port_temp&(1<<ch))
return 1;
else
return 0;
#endif
#if 0 //For portC and DDRC must be set 0x00
char port_temp;
port_temp=PINC;
if (port_temp&(1<<ch))
return 1;
else
return 0;
#endif
#if 1 //For portD and DDRD must be set 0x00
char port_temp;
port_temp=PIND;
if (port_temp&(1<<ch))
return 1;
else
return 0;
#endif
}
void init_ADC()
{
#if 0
//DDRC=0x00; //Remember change pin by input.
ACSR=0x80; //Disable the analog comparator.
ADMUX=0x40; //Select the ADC chanel and chooses Vref from AVcc.
ADCSRA=0x80 //ADC function and interrupt Enable register.
// ADCW, when ADC conversion is over, then be token the result in ADCW.
ADCSRB=0x00; //the ADC Auto Trigger Source chooses.
DIDR0=0x3F; //Digital Input Disable
#endif
}
//Global variable set
//int adc_num;
ISR(ADC_vect)
{
#if 0
adc_num=ACD&0x3ff;
#endif
}
void init_timer()
{
//Timer0 Register parameter
#if 0//if you want to use this program, please set 1.
TCCR0A=0x83; // It is used Fast PWM Mode and non-inverting mode for OC0A pin. if TCON0<OCR0A then set pin, else clear pin.
TCCR0B=0x01; //The timer clock chooses in this register, set 0: stop, set 1: fclk, set 2: fclk/8, set 3: fclk/64,
//set 4: fclk/256, set 5: fclk/1024, set 6: falling edge, set 7: rising. fclk is system clock/256
TCNT0=0x00; //The timer counts initial value.
OCR0A=0x00; //The compare register A.
//OCR0B=0x00; //The compare register B.
//sei(); //The Global interrupt Enable when not Enable.
TIMSK0=0x01; //The timer overflow interrupt Enable.
//'TIFR0' can read interrupt flag, the bit is set when interrupt.
#endif
//Timer1 Register parameter
#if 0
TCCR1A=0x83;
TCCR1B=0x09; //Set 10-bit Fast PWM Mode from TCCR1A and TCCR1B.
//TCCR1C=0x00; //CTC Mode used.
TCNT1=0x0000;
OCR1A=0x0000;
//OCR1B=0x0000;
//sei();
//ICR1=0x00; //CTC Mode used.
TIMSK1=0x01;
//'TIFR1' can read interrupt flag, the bit is set when interrupt.
#endif
//Timer2 Register parameter
#if 0
TCCR2A=0x83;
TCCR2B=0x01;
TCNT2=0x00;
OCR2A=0x00;
//OCR2B=0x00;
//sei();
TIMSK2=0x01;
//'TIFR2' can read interrupt flag, the bit is set when interrupt.
//ASSR=0x00 - Asynchronous Status Register
//GTCCR=0x00 - General Timer/Counter Control Register
#endif
}
int main()
{
int x,y[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
x=0;
DDRB=0xFF;
while(1)
{
for(x=0;x<=7;x++)
{
PORTB=~y[x];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -