summaryrefslogtreecommitdiff
path: root/Keithley179.c
blob: 4205c6fd5e2913e471e921c8c1538d04c9375224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Interface for ICL71c03 ADC 
* Designed for the Keithley 179 True RMS Multimeter
*/


//#ifndef F_CPU
#define F_CPU 4000000UL //4MHz
//#endif

#include <avr/io.h>
#include <avr/interrupt.h>

#define UART_BAUDRATE 960

#define baudrate ((F_CPU / UART_BAUDRATE * 16) - 1)

//Locations of ADC Outups (ICL71c03)
#define b1 PC0
#define b2 PC1
#define b3 PC2
#define b4 PC3

#define d1 PD7
#define d2 PD4
#define d3 PD5
#define d4 PD6
#define d5 PC3

#define busy PB7 //Busy not Used

//Status
uint8_t UART_RX;
uint8_t UART_RX_END;
uint8_t UART_isInit;

//Buffers
char out[5]; //Buffer for ADC Input
char in[16]; //UART Input Buffer
uint8_t in_cntr; //Counter for UART Input Buffer

//UART Rcieve Interrupt
ISR(USART_RXC_vect)
{
	cli();
	in[in_cntr] = UDR;
	if(in[in_cntr] == '\n')
		UART_RX_END = 1; //Set RX End Flag
	
	//Check if Buffer is full
	if(in_cntr < 15)
	in_cntr ++;
	else
	{
		in_cntr = 0;
		UART_RX_END = 1; //Set RX End Flag
	}
	sei();
}//USART_RXC_vect

//Prototypen
void UART_Init();
/*
*Initializes the  UART Interface
*/

void UART_Handle();
/*
* Handles UART Communication
* Not Implemented
*/

void UART_Send(char *_cSend);
/*
*Sends String over UART
*/

void UART_Putc(unsigned char c);

//Startpoint
int main(void)
{
	//clear Buffers
	for(int i = 0; i < 5;i++)
	{
		out[i] = 0;
	}
	
	for(int i = 0; i < 16;i++)
	{
		in[i] = 0;
	}
	
	//Init Status vars
	char temp = 0;
	in_cntr = 0;
	
	UART_RX = 0;
	UART_RX_END = 0;
	UART_isInit = 0;
	
	//Set Inputs
	DDRD = 0;
	DDRC = 0;
	
	DDRD |= (1<<PD1); //TX Pin
	
	sei();
	
	//INIT UART Interface
	UART_Init();
	//UART_Send("Hello World!");
	UART_Putc('A');
	//main loop
	while(1)
	{
		while(!(PORTD & (1<<d1)));
		temp = PINB << 4;
		out[4] = temp >> 4;
		temp = 0;
		
		while(!(PORTD & (1<<d2)));
		temp = PINB << 4;
		out[3] = temp >> 4;
		temp = 0;
		
		while(!(PORTD & (1<<d3)));
		temp = PINB << 4;
		out[2] = temp >> 4;
		temp = 0;
		
		while(!(PORTD & (1<<d4)));
		temp = PINB << 4;
		out[1] = temp >> 4;
		temp = 0;
		
		while(!(PORTC & (1<<d5)));
		temp = PINB << 4;
		out[0] = temp >> 4;
		temp = 0;
		
		
		//Convert to ASCII
		for(int i = 0; i < 5;i++)
		{
			out[i] = out[i] + 48;
		}
		
		UART_Send(out);
		//Handle UART
		//UART_Handle(); //Not Used
		
		//Reset Buffer
		for(int i = 0; i < 5;i++)
		{
			out[i] = 0;
		}
		
	}
	
	return 0;
}//main

void UART_Init()
{
	if(UART_isInit == 0)
	{
		//Calculate Value for Register
		//uint16_t baudrate = (F_CPU / (UART_BAUDRATE * 16)) - 1;
		
		//Set Baudrate
		UBRRL = baudrate;
		UBRRH = (baudrate >> 8);
		
		UCSRB |= (1<<RXEN) | (1<<TXEN); //Enable Reciever and Transmitter
		UCSRB |= (1<<RXCIE); //Enable Recieve Complete Interrupt
		
		UART_isInit = 1;
	}
	
}//UART_Init



void UART_Handle()
{
	if(UART_isInit == 1 && UART_RX_END == 1)
	{
		//Not Used
		//char in[16] is UART InputBuffer
	}
}//UART_Handle

void UART_Send(char *_cSend)
{
	if(UART_isInit == 1)
	{
		while(*_cSend)
		{
			/*while (!(UCSRA & (1<<UDRE)));
			UDR = *_cSend;*/
			UART_Putc(*_cSend);
			_cSend++;
		}
	}
}//UART_send

void UART_Putc(unsigned char c)
{
	while (!(UCSRA & (1<<UDRE)));
	UDR = c;
}