//ICC-AVR application builder : 2006-3-5 20:09:53
// Target : M8
// Crystal: 4.0000Mhz
#include
#include
#define bitset(var, bitno) ((var) |= (1 << (bitno)))
#define bitclr(var, bitno) ((var) &= ~(1 << (bitno)))
#define uchar unsigned char
#define uint unsigned int
#define suchar static unsigned char
#define suint static unsigned int
suint pot_vol;
suchar uchsw;num;
suchar pwm;
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x02;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER1 initialisation - prescale:1
// WGM: 8) PWM phz freq correct, TOP=ICRn
void timer1_init(void)
{TCCR1B = 0x00; //stop
TCNT1H = 0x00; //setup
TCNT1L = 0x00;
OCR1AH = 0x00;
OCR1AL = 0x00;
OCR1BH = 0x00;
OCR1BL = 0x0A;
ICR1H = 0x00;
ICR1L = 0xCE; //18KHZ
TCCR1A = 0x82;
TCCR1B = 0x19; //start Timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void delay(void)
{
uchar i;
uchar j;
for(i=0;i<150;i++)
{
for(j=0;j<150;j++)
asm("nop");}
}
void pwmopen(uchar i)
{
// TCCR1A = 0x82;
// TCCR1B = 0x19;
//pwm=OCR1AL;
//if(pwm==i) return;
// else if(pwm>i) pwm--;
// else pwm++;
OCR1AH=0;
OCR1AL=i;
}
void clpwm()
{
TCCR1B=0x00;
TCCR1A=0x00;
bitclr(PORTB,1);
TCNT1H=0;TCNT1L=0;
}
void auto_mode(void)
{
unsigned char i;
// bitset(PORTD,0);
//bitclr(PORTD,1);
// while(uchsw)
//{
for (i=0;255>i; i++)
{
if ((PINC&0x02)|(PINC&0x04)) break;
pwmopen(i);
delay();
}
for (i=255;0
{
if ((PINC&0x02)|(PINC&0x04)) break;
pwmopen(i);
delay();
}
}
static unsigned char pot_charge(void) //电位器AD转换。
{
//uchar i;
ADMUX=0;
ADCSR=6; //1/64 prescaler
ADCSR|=(1<
WDR();
do
{
;
}while(!(ADCSR&(1<
//if(vol_avg!=2)
//{
pot_vol=ADCL;
pot_vol|=(uint)(ADCH<<8);
pot_vol=(uchar)(pot_vol/4);
//vol_avg++;
// vol_to=vol_to+(uint)pot_vol;
// return;
//}
// else
// {