1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <avr/wdt.h>
|
4 | #include <stdio.h>
|
5 | #include <stdlib.h>
|
6 | #include "usbdrv.h"
|
7 | #include <util/delay.h>
|
8 |
|
9 | /*Define zur hilfe Stellung*/
|
10 | #define High 1
|
11 | #define Low 0
|
12 |
|
13 | /*Case Defines*/
|
14 | #define USB_LED_ON 10 //war 1
|
15 | #define USB_LED_OFF 11 //war 0
|
16 | #define USB_ALL_OFF 2
|
17 | #define USB_DATA_OUT 4
|
18 | #define USB_Check 5
|
19 | #define USB_Disco 6
|
20 | #define USB_ADC 7
|
21 | #define USB_Trans 8
|
22 |
|
23 |
|
24 | /*Varibalen und Strings*/
|
25 | uint16_t adc_Value = 0;
|
26 | uchar Dataout[17];
|
27 | uchar Buffer[11];
|
28 | uchar replyBuf[] = "Hello, User!";
|
29 | uchar Highpin[5] = "High";
|
30 | uchar Lowpin[4] = "Low";
|
31 | static uchar dataReceived = 0, dataLength = 0; // for USB_DATA_IN
|
32 |
|
33 | /*ADC-Init */
|
34 | void adc_init(void)
|
35 | {
|
36 | //ReferenzSpannung AVcc | Kanal 0 ADC0(PC0)
|
37 | //ADC enable| Singel Mode| Frequenzvorteiler F_CPU/128
|
38 | uint16_t result;
|
39 | ADMUX = (1<<REFS0);
|
40 | ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
|
41 |
|
42 | ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
|
43 | while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten
|
44 | result = ADCW;
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 | /*ADC-channel-Funktion*/
|
50 | uint16_t ADC_Read( uint8_t channel )
|
51 | {
|
52 | // Kanal waehlen, ohne andere Bits zu beeinflußen
|
53 | ADMUX = (ADMUX & ~(0b00001111)) | (channel & 0b00001111);
|
54 | ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
|
55 | while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten
|
56 | return ADCW; // ADC auslesen und zurückgeben
|
57 | }
|
58 |
|
59 |
|
60 | // this gets called when custom control message is received
|
61 | USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) {
|
62 |
|
63 | usbRequest_t *rq = (void *)data; // cast data to correct type
|
64 |
|
65 |
|
66 | switch(rq->bRequest) { // custom command is in the bRequest field
|
67 |
|
68 | case USB_LED_ON:// Works
|
69 | DDRB |= (1<<PB0);
|
70 | PORTB |= (1<<PB1); // turn LED on
|
71 | return 0;
|
72 |
|
73 | case USB_LED_OFF:// Works
|
74 | DDRB |= (1<<PB5);
|
75 | PORTB &= ~(1<<PB1); // turn LED off
|
76 | return 0;
|
77 |
|
78 | case USB_ALL_OFF:// Works
|
79 | PORTB &= ~(1<<PB0); // turn LED1 LED0 off
|
80 | PORTB &= ~(1<<PB1);
|
81 | PORTB &= ~(1<<PB2);
|
82 | return 0;
|
83 |
|
84 | case USB_DATA_OUT: // Works
|
85 | usbMsgPtr = replyBuf;
|
86 | return sizeof(replyBuf);
|
87 |
|
88 | case USB_Check:// Works
|
89 | if(PINB &(1<<PINB3))//wenn PB3 HIGH ist
|
90 | {
|
91 | usbMsgPtr = Highpin;
|
92 | PORTB |= (1<<PB1);
|
93 | return sizeof(Highpin);
|
94 | }
|
95 |
|
96 | else
|
97 | {
|
98 | usbMsgPtr = Lowpin;
|
99 | PORTB |= (1<<PB0);
|
100 | return sizeof(Lowpin);
|
101 | }
|
102 |
|
103 | case USB_Disco://works
|
104 | PORTB |= (1<<PB0);
|
105 | _delay_ms(50);
|
106 | PORTB |= (1<<PB1);
|
107 | _delay_ms(50);
|
108 | PORTB |= (1<<PB2);
|
109 | _delay_ms(50);
|
110 | PORTB &= ~(1<<PB0);
|
111 | PORTB &= ~(1<<PB1);
|
112 | PORTB &= ~(1<<PB2);
|
113 | return 0;
|
114 |
|
115 | case USB_ADC://Didn't works out put on cmd-window 1023
|
116 | adc_Value = ADC_Read(0);
|
117 | itoa(adc_Value, Buffer, 10);
|
118 | usbMsgPtr = Buffer;
|
119 | return sizeof(Buffer);
|
120 |
|
121 | case USB_Trans://Transfer didn't works on cmd-windows 0
|
122 | DDRB |= (1<<PB5); //Ausgang als CLK
|
123 | DDRB &= ~(1<<PB4); //Eingang als MISO
|
124 | PORTB |= (1<<PB4); //PULL-UP an
|
125 |
|
126 | uint16_t Datain;
|
127 | for(uint16_t a = 0; a>=15 ; a++)
|
128 | {
|
129 | PORTB |= (1<<PB5); //CLK High
|
130 | if(PINB &(1<<PINB4))
|
131 | {
|
132 | Datain = (1<<High);
|
133 | }
|
134 | else
|
135 | {
|
136 | Datain = (1<<Low);
|
137 | }
|
138 | PORTB &= ~(1<<PB5); //CLK Low
|
139 | Datain <<=1;
|
140 | }
|
141 |
|
142 | itoa(Datain,Dataout,10);
|
143 | usbMsgPtr = Dataout;
|
144 | return sizeof(Dataout);
|
145 |
|
146 |
|
147 | }
|
148 | return 0; // should not get here
|
149 |
|
150 | }
|
151 |
|
152 | USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len) {
|
153 | uchar i;
|
154 |
|
155 | for(i = 0; dataReceived < dataLength && i < len; i++, dataReceived++)
|
156 | replyBuf[dataReceived] = data[i];
|
157 |
|
158 | return (dataReceived == dataLength); // 1 if we received it all, 0 if not
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 | int main() {
|
164 | uchar i;
|
165 |
|
166 | wdt_enable(WDTO_1S); // enable 1s watchdog timer
|
167 | adc_init();
|
168 | usbInit();
|
169 |
|
170 | usbDeviceDisconnect(); // enforce re-enumeration
|
171 | for(i = 0; i<250; i++) { // wait 500 ms
|
172 | wdt_reset(); // keep the watchdog happy
|
173 | _delay_ms(2);
|
174 | }
|
175 | usbDeviceConnect();
|
176 |
|
177 | sei(); // Enable interrupts after re-enumeration
|
178 |
|
179 |
|
180 | while(1) {
|
181 | wdt_reset(); // keep the watchdog happy
|
182 | usbPoll();
|
183 | }
|
184 |
|
185 | return 0;
|
186 | }
|