#include <mega8535.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
// SPI functions
#include <spi.h>
#include <delay.h>
#include <stdlib.h>
#define ADC_VREF_TYPE 0x00
#define A PIND.0
#define B PIND.1
#define C PIND.2
#define D PIND.3
// I2C Bus functions
#asm
.equ __i2c_port=0x12 ;PORTD
.equ __sda_bit=4
.equ __scl_bit=5
#endasm
#include <i2c.h>
// Declare signatures of a user's functions
void read_analog_inputs (void);
unsigned int read_internal_adc(unsigned char adc_input);
unsigned int read_external_adc(void);
unsigned int getY(unsigned int in[]);
void getZ(void);
void i2c_write_uint(unsigned char address, unsigned int data);
#define NUMBER_OF_ANALOG_INPUTS 18
// Declare your global variables here
unsigned int count = 2;
unsigned int voltage_ADC [ NUMBER_OF_ANALOG_INPUTS ];
unsigned long int summ;
unsigned long int num_count;
flash const unsigned char ADDRESS = 0b00011000;
void main(void){
// Declare your local variables here
unsigned int buffer;
// Input/Output Ports initialization
// Port A initialization (In,In,In,In,Out,Out,Out,Out) (T,T,T,T,0,0,0,0)
PORTA=0x00;
DDRA=0x0F;
// Port B initialization (Out,In,In,In,In,In,Out,In) (0,T,P,P,P,P,0,P)
PORTB=0x82;
DDRB=0x3C;
// Port C initialization (Out,Out,Out,Out,In,Out,Out,Out) (0,0,0,0,0,0,0,0)
PORTC=0x08;
DDRC=0xF7;
// Port D initialization (Out,Out,Out,Out,In,In,In,In) (0,0,0,0,T,T,T,T)
PORTD=0x00;
DDRD=0xF0;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2000,000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x00;
// ADC initialization
// ADC Clock frequency: 1000,000 kHz, ADC Voltage Reference: AREF pin
// ADC High Speed Mode: On , ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x82;
SFIOR&=0xEF;
SFIOR|=0x10;
// Timer/Counter 0 initialization
// Clock source: System Clock , Clock value: 3,906 kHz
// Mode: Normal top=FFh , OC0 output: Disconnected
TCCR0=0x05;
TCNT0=0x00;
OCR0=0x00;
// Global enable interrupts
#asm("sei")
// I2C Bus initialization
i2c_init();
// LCD module initialization
lcd_init(12);
while (1){
read_analog_inputs();
buffer = getY(voltage_ADC);
#asm("cli")
i2c_write_uint(ADDRESS, buffer);
#asm("sei")
summ += buffer;
num_count++;
//Z1
PIND.6 = (|(AB+CD+AC+BC));
}
}
// reads array of analog inputs and write it to voltage_ADC[]
void read_analog_inputs (void){
int i;
for (i = 0; i < NUMBER_OF_ANALOG_INPUTS; i++){
PORTB = 2;
voltage_ADC[i] = read_external_adc();
// switch input
++PORTA;
}
PORTA = 0;
}
// Write value to DAC by I2C
// @ address - address byte
// @ data - byte of data
void i2c_write_uint(unsigned char address, unsigned int data){
data = data <<4;
register char *ptr = (char*)&data;
i2c_start();
i2c_write(address);
i2c_write(*--ptr);
i2c_write(*++ptr);
i2c_stop();
}
// Read the AD conversion result from internal ADC
// @ adc_input - number of ADC inputs
// @ return ADCW - 10 bit, result of conversion
unsigned int read_internal_adc(unsigned char adc_input){
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
#define VREF 5000L
#define n_convst PORTB.1
// Read the AD conversion result from external ADC
// @ return voltage - significant lower 12 bit, result of conversion
unsigned int read_external_adc(void){
unsigned int voltage;
// Start the AD conversion and transfer
n_convst = 0;
// Reads
voltage = spi(0);
voltage = voltage << 8;
voltage |= spi(0);
// Completed
n_convst = 1;
//Significant Low Byte
voltage = voltage >> 4;
voltage = voltage & 0x0F;
return voltage;
}
// Y = ((N3-N2)/N1)+((N6-N5)/N4)+…+((Np-Np-1)/Np-2)
// gets result of function Y
// @ in[] - array of analog inputs
// @ returt out - result of function Y
unsigned int getY(unsigned int in[]){
unsigned int out = 0;
int i = 0;
while (i < 6){
out = ( );
}
return out;
}
// T = 256*1024/4000000 = 65 536 us
// N=0...15 <=> 0,1...10 c
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void){
char string[5];
unsigned int compare = 2 + (unsigned)(((unsigned long)read_internal_adc(7)*153)/1024L);
if (count >= compare){
count = 0;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("averaged: ");
lcd_gotoxy(11,0);
//convert int to string
ltoa((summ*VREF/1024L*num_count), string);
summ = num_count = 0;
lcd_puts(string);
}
count++;
}