sample1.c
来自「ATmega16上面的ADC测试」· C语言 代码 · 共 196 行
C
196 行
/*****************************************************
Date : 2007-4-27
Author : Laplace Zhang
Chip type : ATmega16
Program type : Application
Clock frequency : 16.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18
#endasm
#include <lcd.h>
#define ADC_VREF_TYPE 0x40
void main_init(void);
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
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;
}
void lcd_init_display()
{
char line0[17]="Channel 1:0.000V";
char line1[17]="Channel 2:0.000V";
lcd_gotoxy(0,0);
lcd_puts(line0);
lcd_gotoxy(0,1);
lcd_puts(line1);
}
void volt_display(unsigned int volt, unsigned char line)
{
char str[6]="0.000";
float f_volt;
volt &= 0x3FF ;
f_volt=((float)(volt*5))/0x400;
/////////////////////////////////////////////////
// 下面这句话是对结果的Gain Error的修正
// 但是这是人工的,暂时还没有弄懂自动修正的方法
// 不过自动修正也是通过软件来完成的。
// 结果用MATLAB拟合过。
f_volt /= 1.2686;
str[0]=((unsigned char)(f_volt))+48;
str[2]=((unsigned char)(f_volt*10))%10+48;
str[3]=((unsigned char)(f_volt*100))%10+48;
str[4]=((unsigned char)(f_volt*1000))%10+48;
lcd_gotoxy(10,line);
lcd_puts(str);
}
// Declare your global variables here
void main(void)
{
unsigned int volt0=0,volt1=0,tmp;
main_init();
lcd_init_display();
while (1)
{
tmp=read_adc(0);
if((volt0/0x4)!=(tmp/0x4))
{
volt0=tmp;
volt_display(volt0,0);
}
tmp=read_adc(1);
if((volt1/0x4)!=(tmp/0x4))
{
volt1=tmp;
volt_display(volt1,1);
}
}
}
void main_init(void)
{
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=In Func0=In
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=T State0=T
PORTA=0x00;
DDRA=0xFC;
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x87;
SFIOR&=0x0F;
// LCD module initialization
lcd_init(16);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?