編譯環(huán)境為CodeWizardAVR
#include
#include
#include
#include
#define Voltage_UP PINB.0
#define Voltage_Down PINB.2
#define UD PORTB.3
#define CS PORTB.5
#define INC PORTB.4
#define Voltage_LED PORTB.6
#define Current_LED PORTB.7
#define Current_Detect 0
#define Out_Detect 1
#define Display_DP PORTC.2
#define Display_D3 PORTC.3
#define Display_D2 PORTC.4
#define Display_D1 PORTC.5
#define Current_UP PIND.2
#define Current_Down PIND.3
#define Display PORTD
#define AD_quotiety 0.014560//0.015856
#define ADC_VREF_TYPE 0xc0
unsigned char select_led = 1;
unsigned char Led_left;
unsigned char Led_middle;
unsigned char Led_right;
unsigned int read_adc(unsigned char adc_input)//參數(shù)為ADC通道選擇
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
//used to display
TCNT0 = 0x00; //8Mhz時鐘,1024分頻,中斷TCNT0重填0
if(select_led == 1)
{
Display = (Led_left << 4) | 0X0F;
Display_D1 = 0;
Display_D2 = 1;
Display_D3 = 1;
select_led = 2;
}
else if(select_led == 2)
{
Display = (Led_middle << 4) | 0X0F;
Display_D1 = 1;
Display_D2 = 0;
Display_D3 = 1;
select_led = 3;
}
else if(select_led == 3)
{
Display = (Led_right << 4) | 0X0F;
Display_D1 = 1;
Display_D2 = 1;
Display_D3 = 0;
select_led = 1;
}
}
void calculate_voltage(unsigned char adc_channel)
{
unsigned char Led_left_temp;
int adc_da
unsigned int voltage_value;
adc_da
voltage_value = (int)(adc_da
Led_left = (voltage_value / 100);
voltage_value = voltage_value % 100; //15
if(Led_left >= 0x0a) // 11.5
{
Led_left_temp = Led_left;
Led_left = Led_left / 10;
Led_middle = Led_left_temp % 10;
Led_right = voltage_value / 10;
}
else // 1.15
{
Led_middle = voltage_value / 10;
Led_right = voltage_value % 10;
}
}
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{ //used to read adc
TCNT1H=0x00;
TCNT1L=0x00;
calculate_voltage(1);
}
void Wiper_Up(void)
{
INC = 1; //inc
CS = 0; //cs
UD = 1; //ud
delay_us(10);
INC = 0; //inc
delay_us(10);
CS = 1; //cs
delay_us(10);
}
void Wiper_down(void)
{
INC = 1; //inc
CS = 0; //cs
UD = 0; //ud
delay_us(10);
INC = 0; //inc
delay_us(10);
CS = 1; //cs
delay_us(10);
}
void save(void)
{
INC = 1; //inc
CS = 0; //cs
delay_us(10);
INC = 1; //inc
delay_us(10);
CS = 1; //cs
delay_us(10);
}
void port_init(void)
{
PORTB = 0xFF; DDRB = 0xFA;
//PORTB2.3輸出PWM,要設為輸出
PORTC = 0x3C; DDRC = 0x3C;
PORTD = 0xFF; DDRD = 0xF0;
}
void timer0_init(void)
{
// Timer/Counter 0 initialization
// Clock source: System Clock
TCCR0 = 0x03; //clk/1024
TCNT0 = 0x00;
TIMSK = 0x05; //open timer0 and timer1
}
void timer1_init(void)
{
// Timer/Counter 0 initialization
// Clock source: System Clock
TCCR1A=0x00;
TCCR1B=0x03; //clk/64
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
}
void uart_init(void)
{
// USART Baud rate: 19200
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;
}
void adc_init(void)
{
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x87;
SFIOR&=0xEF;
SFIOR|=0x10;
ACSR=0x80;
}
void check_key(void)
{
if(Current_UP == 0)
{
Wiper_Up();
//calculate_voltage(1);
do
{
delay_ms(15);
}while(Current_UP == 0);
save();
}
else if(Current_Down == 0)
{
Wiper_down();
//calculate_voltage(1);
do
{
delay_ms(15);
}while(Current_Down == 0);
save();
}
else if(Voltage_UP == 0)
{
Wiper_Up();
do
{
delay_ms(15);
}while(Voltage_UP == 0);
save();
}
else if(Voltage_Down == 0)
{
Wiper_down();
do
{
delay_ms(15);
}while(Voltage_Down == 0);
save();
}
}
void main(void)
{
port_init();
timer0_init();
timer1_init();
uart_init();
adc_init();
#asm("sei")
while(1)
{
check_key();
};
}