Commit 17cc306f895abc9c04724e1a86a30ef2f654fef8

Authored by henyxia
1 parent d2a71b92

Rev 1.0

Can send and recieve data properly
Showing 1 changed file with 58 additions and 24 deletions   Show diff stats
1 1 #include <avr/io.h>
2 2 #include <util/delay.h>
3 3  
4   -#define CPU_FREQ 16000000L
5   -
6   -unsigned int joystick_lr;
7   -unsigned int joystick_ud;
  4 +#define CPU_FREQ 16000000L
  5 +#define VAL25 895
  6 +#define VAL105 246
8 7  
9 8 void init_serial(int speed)
10 9 {
... ... @@ -30,36 +29,64 @@ void port_initio(void)
30 29 {
31 30 DDRD &= 0xFC;
32 31 DDRD |= 0x3C;
  32 + DDRC = 0x00;
33 33 DDRB &= 0xFE;
34 34 PORTD |= 0x3C;
35 35 }
36 36  
  37 +int analogRead(uint8_t pin)
  38 +{
  39 + uint8_t low, high;
  40 +
  41 + if (pin >= 14)
  42 + pin -= 14; // allow for channel or pin numbers
  43 + ADMUX = (0x01 << 6) | (pin & 0x07);
  44 + ADCSRA|=(1<<ADSC);
  45 + while (bit_is_set(ADCSRA, ADSC));
  46 + low = ADCL;
  47 + high = ADCH;
  48 +
  49 + return (high << 8) | low;
  50 +}
  51 +
  52 +void ad_init(unsigned char channel)
  53 +{
  54 + ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
  55 + ADMUX|=(1<<REFS0)|(1<<ADLAR);
  56 + ADMUX=(ADMUX&0xf0)|channel;
  57 + ADCSRA|=(1<<ADEN);
  58 +}
  59 +
37 60 int main(void)
38 61 {
39 62 init_serial(19200);
40 63 port_initio();
41   - unsigned char ser;
  64 + ad_init(PINC&0x00);
42 65 uint8_t opto0 = 0;
43 66 uint8_t opto1 = 0;
44   - uint8_t once = 1;
45   -
  67 + unsigned char ser;
  68 + unsigned int temp;
  69 + unsigned char ftemp;
  70 +
46 71 while (1)
47 72 {
48   - if((opto0 == 1) && ((PIND & 0x40) == 0x40))
  73 + temp = analogRead(0);
  74 + if(temp > VAL25)
  75 + ftemp = 0;
  76 + else if(temp < VAL105)
49 77 {
50   - if(once)
51   - {
52   - PORTD &= 0xE7;
53   - //_delay_ms(00000);
54   - //PORTD |= 0x18;
55   - //once = 0;
56   - }
  78 + ftemp = 255;
  79 + opto1 = 0;
57 80 }
58 81 else
59 82 {
60   - once = 1;
61   - PORTD |= 0x18;
  83 + temp = (VAL25 - temp) * 255;
  84 + ftemp = temp / (VAL25 - VAL105);
62 85 }
  86 + if((opto0 == 1) && ((PIND & 0x40) == 0x40))
  87 + PORTD &= 0xE7;
  88 + else
  89 + PORTD |= 0x18;
63 90 if((opto1 == 1) && ((PIND & 0x80) == 0x80))
64 91 PORTD &= 0xDB;
65 92 else
... ... @@ -70,14 +97,21 @@ int main(void)
70 97 opto0 = 1;
71 98 else if(ser == 'D')
72 99 opto0 = 0;
73   - send_serial(ser);
74   -/*
75   - if((PINB & 0x01) == 0x01)
76   - send_serial('1');
  100 + else if(ser == 'E')
  101 + opto1 = 1;
  102 + else if(ser == 'G')
  103 + opto1 = 0;
  104 + else if(ser == 'H')
  105 + send_serial(ftemp);
  106 + else if(ser == 'F')
  107 + {
  108 + if((PINB & 0x01) == 0x01)
  109 + send_serial('I');
  110 + else
  111 + send_serial('K');
  112 + }
77 113 else
78   - send_serial('0');
79   - send_serial('\n');
80   -*/
  114 + send_serial('J');
81 115 }
82 116  
83 117 return 0;
... ...