-
Notifications
You must be signed in to change notification settings - Fork 0
/
myio.c
executable file
·202 lines (150 loc) · 3.58 KB
/
myio.c
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
/*
* File: myio.c
*
* implementation for myio.h
* contains the function for basic io
* and serial communuation over USART
*
* NOTE, it is possible to use the AVR Libc
* port of stdio.h but it requires a lot of
* space and processing to set up the macros
* that the functions require hence the
* decision was made to implement our own
* library as we dont need all the functionality
* provided by stdio.h
*
*/
//for testing
//#define DEBUG
#include "myio.h"
#include <stdlib.h>
#include <string.h>
uint8_t sendBuff(char c[], int ctr)
{
int i;
for(i = 0; i < ctr; i++)
{
while ( !(UCSR0A & (1 << UDRE0)));//wait
UDR0 = c[i];
}
return i + 1;
}
void readUDR(char c[], volatile int *pos)
{
#ifdef DEBUG
sendStr("function readUDR\n\r");
char* str = "";
sendStr("bPos: ");
itoa(*pos,str,10);
sendStr(str);
sendStr("\n\r");
#endif
//make buffer circular we can then
//do a check in main for the correct starting
//char in main, if it isnt then we have an
//error and can deal with it
if(*pos == 23)
{
*pos = 0;
rxBuff[*pos] = UDR0;
(*pos)++;
}
rxBuff[*pos] = UDR0;
(*pos)++;
}
void putC(char c)
{
//wait till ready to send
while ( !(UCSR0A & (1 << UDRE0)));
//send byte
UDR0 = c;
//increment count
bSent++;
}
void sendInt(int i)
{
//we have to dynamicly allocate memory here
//for the char* that will be copied to
//in itoa()
//also note here type int is in fact
//a signed 16bit int see <stdint.h>
//might be safer to use ltoa() and
//do a cast before calling the function...
//allocate the memory
char* str;
str = malloc(sizeof(i)+1);//+1 for NULL terminator
itoa(i,str,10);
sendStr(str);
sendStr("\n\r");
//free the memory
free(str);
}
int sendStr(char* s)
{
bSent = 0;
while(*s) putC(*s++);
return bSent;
}
void rxEnable(void)
{
UCSR0B |= (1 << RXEN0);
}
void rxBlock(void)
{
UCSR0B &= ~(1 << RXEN0);
}
void readSensor(int pin, int vals[], int *vPos)
{
if(*vPos > MAX_VBUFF_SZ - 1)
*vPos = 0;
//set ADMUX input via pin
ADMUX |= (pin << MUX0);
ADCSRA |=(1<<ADSC);
while( ! (ADCSRA & (1<<ADIF)));
vals[*vPos] = ADCL;
vals[*vPos] |= (ADCH << 8);
(*vPos)++;
ADCSRA |= (1 << ADIF);//reset ADIF flag
}
void readSensorISR(int pin, int *flag)
{
//set global flag
*flag = pin;
ADMUX |= (pin << MUX0);
ADCSRA |= (1 << ADIF);//reset ADIF flag to be sure
ADCSRA |=(1<<ADIE);//enable ISR
//set idle mode for ADC noise reduce
SMCR |= (0 << SM0);
sleep_cpu();//start the conversion
}
void adcinit(void)
{
//block digital on analogue pins
DIDR0 = 0x3f;
//setup ADR circuitry
//ADCSRA |= ((1 << ADEN) | (1 << REFS0) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0));
ADCSRA = ( (1<<ADEN) | (7 << ADPS0) | (1 << ADIF) );//using ISR
//vcc as ref
ADMUX = 0x01;
//ADMUX = 0x00;
}
//this needs to have default mode
//which it currently is i.e if n other option
//use these defaults
void ioinit(void)
{
DDRC = INPUT; //set to input
DDRB = OUTPUT; //set to output
//DDRD = OUTPUT;
//set LEDS off
PORTB &= ~(1 << PORTB2);
PORTB &= ~(1 << PORTB4);
//init USART
//set TX and RX
UCSR0B |= (1<<TXEN0) | (1<<RXEN0);
//set frame size, no parity 1 stop bit 8 data bits
UCSR0C |= 3 << UCSZ00;
//set baud rate
UBRR0H = BAUD_PRESCALER >> 8;
UBRR0L = BAUD_PRESCALER;
}