File: main.cpp

package info (click to toggle)
simulavr 1.0.0%2Bgit20160221.e53413b-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,748 kB
  • sloc: cpp: 35,491; python: 6,991; ansic: 3,567; makefile: 1,072; sh: 653; asm: 414; tcl: 320
file content (136 lines) | stat: -rw-r--r-- 2,209 bytes parent folder | download | duplicates (2)
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
/* 
 * Demonstrate the use of the SpiSource, SpiSink, and PinMonitor.
 */

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

uint8_t				txData;
volatile uint8_t	rxData;
uint8_t				mux;

static void startADC()
{
	uint8_t	value;

	value	= ADMUX;
	value	&= ~(	(0<<MUX3)
				|	(0<<MUX2)
				|	(0<<MUX1)
				|	(0<<MUX0)
				);

	switch(mux){
		case 0:
			value	|= 0x05;
		case 1:
			value	|= 0x06;
		case 2:
			value	|= 0x07;
		};

	ADMUX	= value;

	++mux;

	if(mux > 2){
		mux	= 0;
		}


	ADCSRA	=	(	(1<<ADEN)	// ADC Enabled
				|	(1<<ADSC)	// Start Conversion NOW
				|	(1<<ADIF)	// Interrupt Flag Acknowledge
				|	(1<<ADIE)	// Interrupt enabled
				|	(0<<ADPS2)	// Prescaler should get between 50KHz-200KHz for max resolution
				|	(1<<ADPS1)	//   System Clock = 1MHz
				|	(1<<ADPS0)	//		Prescaler = 1MHz/100KHz = ~10 thus = 8 : 1MHz/8 = 120KHz
				) ;
}

static void	assertPB0(){
	// asserted is LOW
	PORTB	&= ~(1<<PB0);
	}

static void	negatePB0(){
	// negated is HIG
	PORTB	|= (1<<PB0);
	}

uint8_t	count;
uint8_t	adcData;

ISR(ADC_vect)
{
	adcData	= ADCH;
	startADC();
}

ISR(SPI_STC_vect)
{
	rxData	= SPDR;
	if(count % 2){
		SPDR	= rxData;
		}
	else {
		SPDR	= adcData;
		}

	if(count == 0){
		assertPB0();
		}

	if(count == 128){
		negatePB0();
		}
	++count;
}

int main(int argc,char *argv[]){
	ADMUX	=		(0<<REFS1)	// Use Vcc as ADC reference
				|	(1<<REFS0)	// Use Vcc as ADC reference
				|	(1<<ADLAR)	// Left justify result (percentage FS)
				|	(0<<MUX3)
				|	(0<<MUX2)
				|	(0<<MUX1)
				|	(0<<MUX0)
				;

	PORTB	=		(1<<PB0)	// Interrupt output
				|	(0<<PB2)	// /SS input
				|	(0<<PB3)	// MOSI input
				|	(1<<PB4)	// MISO output
				|	(0<<PB5)	// SCK input
				;

	DDRB	=		(1<<PB0)	// Interrupt output
				|	(0<<PB2)	// /SS input
				|	(0<<PB3)	// MOSI input
				|	(1<<PB4)	// MISO output
				|	(0<<PB5)	// SCK input
				;

	SPCR	=		(1<<SPIE)	// interrupt enable
				|	(1<<SPE)	// SPI enable
				|	(0<<DORD)	// MSB first
				|	(0<<MSTR)	// Slave Mode
				|	(1<<CPOL)	// Clock HIGH when idle
				|	(0<<CPHA)	// Sample on leading edge
				|	(0<<SPR1)	// Slave has no affect
				|	(0<<SPR0)	// Slave has no affect
				;

	startADC();

	sei();

	for(;;);

    return 0;
}