|
// icc 6.31a , atmage16, 4m osc
#include <iom16v.h>
#include <macros.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
const uchar disp_table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar led_data[4];
uint Adc_data;
void disp_led(void);
void delay_1ms(void);
void delay_nms(uint n);
void main(void)
{
uint j;
DDRB = 0x03;
PORTB = 0x03;
DDRC = 0xff;
PORTC = 0xff;
DDRD = 0xff;
PORTD = 0xff;
DDRA = 0x00;
CLI();
WDR(); // 使用看門狗
WDTCR = 0x0F;
ADCSRA = 0x00;
ADMUX = (1<<REFS0)|(1<<MUX1)|(1<<MUX0); // 用avcc為參考電壓, 使用pa3,即adc3為ad輸入,
ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1); // adc中斷許可,連續方式, 64分頻
SEI();
PORTB = 0B00000000;
delay_nms(100);
while(1)
{
PORTB = 0B00000010;
delay_1ms();
Adc_data = 49*(ADCH*256+ADCL); // 計算adc的值, 其中49是用vcc 5.02/1023得出來
j = Adc_data/1000; // 過壓檢查
if(j>48)
{
PORTB = ~BIT(0)|~BIT(1);
}
disp_led(); // 顯示adc數值
WDR();
PORTB = 0B00000011;
}
}
void disp_led(void)
{
uchar i;
led_data[0] = Adc_data/10000;
led_data[1] = Adc_data/1000%10;
led_data[2] = Adc_data/100%10;
led_data[3] = Adc_data/10%10;
for (i=0;i<4;i++)
{
PORTC = disp_table[led_data];
PORTD = ~BIT(i);
delay_nms(5);
PORTD = 0xff;
}
}
void delay_1ms(void)
{
uint i;
for (i=0;i<550;i++);
WDR();
}
void delay_nms(uint n)
{
uint i;
for (i=0;i<n;i++)
delay_1ms();
}
#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr(void)
{
ADMUX = (1<<REFS0)|(1<<MUX1)|(1<<MUX0);
ADCSRA |= (1<<ADSC);
}
實驗板接線:
JU7的AREF短接
PC0-PC7 ---------> JC1.a-JC1.p
PD0-PD3 ---------> JC2.1-JC2.4
PA3 ---------> JD1
PB0-PB1 ---------> JA.1-JA.2
調整 wd1 可觀察到數碼管的數值的變化 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|