Download CodeVisionAVR User Manual

Transcript
CodeVisionAVR
#asm
.equ __lcd_port=0x15
#endasm
#include
#include
#include
#include
#include
<lcd.h> // LCD driver routines
<spi.h> // SPI driver routine
<90s8515.h>
<stdio.h>
<delay.h>
// AD7896 reference voltage [mV]
#define VREF 5000L
// AD7896 control signals PORTB bit allocation
#define ADC_BUSY PINB.0
#define NCONVST PORTB.1
// LCD display buffer
char lcd_buffer[33];
unsigned read_adc(void)
{
unsigned result;
// start conversion in mode 1
// (high sampling performance)
NCONVST=0;
NCONVST=1;
// wait for the conversion to complete
while (ADC_BUSY);
// read the MSB using SPI
result=(unsigned) spi(0)<<8;
// read the LSB using SPI and combine with MSB
result|=spi(0);
// calculate the voltage in [mV]
result=(unsigned) (((unsigned long) result*VREF)/4096L);
// return the measured voltage
return result;
}
void main(void)
{
// initialize PORTB
// PB.0 input from AD7896 BUSY
// PB.1 output to AD7896 /CONVST
// PB.2 & PB.3 inputs
// PB.4 output (SPI /SS pin)
// PB.5 input
// PB.6 input (SPI MISO)
// PB.7 output to AD7896 SCLK
DDRB=0x92;
// initialize the SPI in master mode
// no interrupts, MSB first, clock phase negative
// SCK low when idle, clock phase=0
// SCK=fxtal/4
SPCR=0x54;
// the AD7896 will work in mode 1
// (high sampling performance)
// /CONVST=1, SCLK=0
© 1998-2007 HP InfoTech S.R.L.
Page 171