📄 11._c
字号:
//ICC-AVR application builder : 2005-3-15 10:10:23
// Target : M48
// Crystal: 8.0000Mhz
#include <iom48v.h>
#include <macros.h>
unsigned int adc_w;
unsigned int adc_old,bjz;
unsigned char ad_high,ad_low;
unsigned int adc[4]= {0x333,0x266,0x199,0xcc};
/*
#define adc_4 0x333;
#define adc_3 0x266;
#define adc_2 0x199;
#define adc_1 0xcc;
*/
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0x07;
PORTC = 0x7F; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0xFF;
}
//ADC initialize
// Conversion time: 13uS
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x40; //select adc input 0
ACSR = 0x80;
ADCSRB = 0x00;
ADCSRA = 0xCB;
}
#pragma interrupt_handler adc_isr:22
void adc_isr(void)
{
adc_w = ADC;
ADMUX = 0x40;
ADCSRA = 0xCB;
//ad_high = adch;
//ad_low = adcl;
//conversion complete, read value (int) using...
// value=ADCL; //Read 8 low bits first (important)
// value|=(int)ADCH << 8; //read 2 high bits and shift into top byte
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
adc_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 display(unsigned int com)
{
unsigned int i;
while(bjz != com)
{
bjz = com;
if(com > adc[1])
{
i = com;
if(i > adc[0])
{alarm();}
else
PORTB = 0XFB;
}
else
{
i = com;
if(i > adc[2])
PORTB = 0XFD;
else
{
if(i>adc[3])
PORTB = 0XFE;
else
PORTB = 0XFF;
}
}
}
}
void delay_us(int time)
{ //微秒延时
do
{
time--;
}
while (time>1);
}
void delay_ms(unsigned int time)
{ //毫秒延时
while(time!=0)
{
delay_us(1000);
time--;
}
}
/*报警函数*/
void alarm(void)
{
while(1){
//i=PIND&0x04; //读出PD2口状态
if(adc_old >= adc[0])
{PORTD^=0x08;
delay_ms(100);
}
else
PORTD=0XFF;
}
}
void main()
{
init_devices();
while(1){
if(adc_w != adc_old){
adc_old = adc_w;
display(adc_old);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -