/*includes*/
#include <mega163.h>
#include <delay.h>
/*ADMUX Selection Register Bit Definitions*/
#define REFS1 7
#define REFS0 6
#define ADLAR 5
#define MUX4 4
#define MUX3 3
#define MUX2 2
#define MUX1 1
#define MUX0 0
/*ADC Control and Status Register-ADCSR*/
#define ADEN 7
#define ADSC 6
#define ADFR 5
#define ADIF 4
#define ADIE 3
#define ADPS2 2
#define ADPS1 1
#define ADPS0 0
/*UART Control Register Bit Definitions */
#define RXCIE 7
#define TXCIE 6
#define UDRIE 5
#define RXEN 4
#define TXEN 3
#define CHR9 2
#define RXB8 1
#define TXB8 0
/* UART Status Register Bit Definitions */
#define RXC 7
#define TXC 6
#define UDRE 5
#define FE 4
#define OR 3
/* Analog Comparator */
#define ACD 7
/*Variables*/
/*unsigned char LowByte;*/
unsigned char HighByte;
unsigned char HeaderByte=0x80;
/*unsigned char StartByte = 255;
unsigned char StopByte = 0;*/
register static unsigned char x=0;
/*Prtototypes*/
void InitUART( unsigned char baudrate );
void TransmitByte( unsigned char data );
/*Main- My Adaptation of AVR's Test Program*/
void main (void)
{
ACSR=(1<<ACD); /*Reduces Current draw by turning off Analog Comparator */
ADCSR=((1<<ADEN)|(0<<ADFR)|(1<<ADPS2)|(1<<ADPS0)|(0<<ADPS1)); /*ADC Enable and prescaler settings*/
ADMUX=((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); /*Set Mux Ref Voltage = Vcc(01=Vcc, 11=2.56V), and Left Adjusts*/
InitUART(51); /*Sets BuadRate at 9600. 51=4800 25=9600 103=2400 */
while (1)
{
switch (x)
{
case 0:
break;
case 1:
ADMUX.0=1; /* This has to be here or the last byte will be transmitted twice */
ADMUX.1=1;
ADMUX.2=1;
ADMUX.3=1;
ADMUX.4=1;
TransmitByte(HeaderByte);/*transmitt a headerbyte before the data*/
break;
case 2: ADMUX.0=0;
ADMUX.1=0;
ADMUX.2=0;
ADMUX.3=0;
ADMUX.4=0;
break;
case 3: ADMUX.0=1;
ADMUX.1=0;
ADMUX.2=0;
ADMUX.3=0;
ADMUX.4=0;
break;
case 4: ADMUX.0=0;
ADMUX.1=1;
ADMUX.2=0;
ADMUX.3=0;
ADMUX.4=0;
break;
case 5: ADMUX.0=1;
ADMUX.1=1;
ADMUX.2=0;
ADMUX.3=0;
ADMUX.4=0;
break;
case 6: ADMUX.0=0;
ADMUX.1=0;
ADMUX.2=1;
ADMUX.3=0;
ADMUX.4=0;
break;
case 7: ADMUX.0=1;
ADMUX.1=0;
ADMUX.2=1;
ADMUX.3=0;
ADMUX.4=0;
break;
case 8: ADMUX.0=0;
ADMUX.1=1;
ADMUX.2=1;
ADMUX.3=0;
ADMUX.4=0;
break;
case 9: ADMUX.0=1;
ADMUX.1=1;
ADMUX.2=1;
ADMUX.3=0;
ADMUX.4=0;
break;
}
/*LowByte=ADCL;
TransmitByte(StartByte);*/
ADCSR.6=1;
loop: if (ADCSR.6=1) /*wait until conversion is complete */
{
delay_ms(1);
goto loop;
}
HighByte=ADCH;
if(x!=1|x!=0)TransmitByte(HighByte); /*If this is not the first intial run or the header, then transmit*/
/*TransmitByte(HighByte); */
/*TransmitByte(StopByte);
TransmitByte(LowByte);*/
if (++x>(9)) /*change to 9 if I need the last Case switch*/
x=1;
}
}
/*initialize UART */
void InitUART( unsigned char baudrate)
{
UBRR= baudrate;
UCSRB=(0<<CHR9);
UCSRB=(1<<TXEN);
}
/* Write Functions*/
void TransmitByte(unsigned char data)
{
while ( !(UCSRA & (1<<UDRE)))
;
UDR=data;
}