summaryrefslogtreecommitdiff
path: root/Keithley179.c
blob: 734336ca4f49ea0d46a5bc85dfb59d433b5648b3 (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
#ifndef F_CPU
#define F_CPU 4000000UL
#endif

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

#define UART_BAUDRATE 9600

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

#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

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

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

void UART_Handle();
/*
* should be called on Interrupt of UART
* Handles UART Communication
*/

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

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;
	}
	
	
	char temp = 0;
	in_cntr = 0;
	
	UART_RX = 0;
	UART_RX_END = 0;
	UART_isInit = 0;
	
	//Set DDRD as Input
	DDRD = 0;
	DDRC = 0;
	
	DDRD |= (1<<PD1); //TX Pin
	
	sei();
	
	//INIT UART Interface
	UART_Init();
	UART_Send("Hello World!");
	
	//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();
		
		//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 = (4000000 / (9600 * 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_Handle

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