|
//ICC-AVR application builder : 2006-3-5 20:09:53
// Target : M8
// Crystal: 4.0000Mhz
#include <iom8v.h>
#include <macros.h>
#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<i; i--)
{
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<<ADEN);ADCSR|=(1<<ADSC);
WDR();
do
{
;
}while(!(ADCSR&(1<<ADIF)));
//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
// {
// adc_l=(uchar)(vol_to/2);//high bit
// vol_to=0;
// }
return pot_vol;
}
void manu_mode(void)
{
//unsigned char i;
//bitset(PORTD,1);
//bitclr(PORTD,0);
//if((PINC&0x02))
pwmopen(pot_charge());
}
void sw_check(void)
{
if (!(PINC&0x02)){uchsw=1;auto_mode();}//自动模式
else {uchsw=0;manu_mode();}//手动模式
}
void vlr_v(void)
{
num=1;
pot_vol=0;
uchsw=0;
pwm=0;
}
void main(void)
{
init_devices();
vlr_v();
TCCR1B=0x00; //停止PWM
TCCR1A=0x00;
while(1)
{
if (!(PINC&0x04))
{
PORTD=0;
DDRD=0;
TCCR1A = 0x82; //打开PWM
TCCR1B = 0x19;
sw_check();
}
else
{
clpwm();
bitclr(PORTB,1);
for(num=1;0x80>num;num=num*2) //由亮变暗
{
if (!(PINC&0x04)) break;
PORTD=num;
DDRD=num;
delay();
delay();
delay();
}
for(num=0x80;1<num;num=num/2)//由暗变亮
{
if (PINC&0x04) break;
PORTD=num;
DDRD=num;
delay();
delay();
delay();
}
}
}
}
在公司无法上传文件,晚上把仿真文件上传! |
|