Commit 7565f6e834e70ab20b13e44cf6093008ca4f4b9e

Authored by henyxia
1 parent 20af1a0b

C Style with double check security

Showing 4 changed files with 104 additions and 30 deletions   Show diff stats
.gitignore 0 โ†’ 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +*.hex
  2 +*.o
  3 +*.elf
Makefile 0 โ†’ 100755
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +export CC = avr-gcc
  2 +
  3 +export MCU = atmega328p
  4 +export TARGET_ARCH = -mmcu=$(MCU)
  5 +
  6 +export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os #-g
  7 +export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os
  8 +
  9 +TARGET = usb
  10 +TERM = /dev/ttyACM0
  11 +CPPFLAGS = -mmcu=$(MCU)
  12 +PGMER = -c stk500v1 -b 57600 -P $(TERM)
  13 +PGMERISP = -c stk500v1 -b 115200 -P $(TERM)
  14 +ARVDUDECONF= -C /usr/local/arduino/arduino-0022/hardware/tools/avrdude.conf
  15 +export DUDE = /usr/bin/avrdude -F -v -p $(MCU) $(AVRDUDECONF)
  16 +
  17 +C_SRC = $(wildcard *.c)
  18 +OBJS = $(C_SRC:.c=.o)
  19 +
  20 +all: $(TARGET).hex
  21 +
  22 +clean:
  23 + rm -f *.o *.hex *.elf
  24 +
  25 +%.o:%.c
  26 + $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
  27 +
  28 +$(TARGET).elf: $(OBJS)
  29 + $(CC) $(LDFLAGS) -o $@ $(OBJS)
  30 +
  31 +$(TARGET).hex: $(TARGET).elf
  32 + avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex
  33 + avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(TARGET).elf eeprom.hex
  34 +
  35 +upload: $(TARGET).hex
  36 + stty -F $(TERM) hupcl # reset
  37 + $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex
  38 +
main.c 0 โ†’ 100755
@@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
  1 +#include <avr/io.h>
  2 +#include <util/delay.h>
  3 +
  4 +#define CPU_FREQ 16000000L
  5 +
  6 +unsigned int joystick_lr;
  7 +unsigned int joystick_ud;
  8 +
  9 +void init_serial(unsigned long int speed)
  10 +{
  11 + //UBRR0 = CPU_FREQ/((speed)<<4)-1;
  12 + UBRR0 = 8;
  13 + UCSR0B = (1<<TXEN0 | 1<<RXEN0);
  14 + UCSR0C = (1<<UCSZ01 | 1<<UCSZ00);
  15 + UCSR0A &= (1 << U2X0);
  16 +}
  17 +
  18 +void send_serial(unsigned char c)
  19 +{
  20 + loop_until_bit_is_set(UCSR0A, UDRE0);
  21 + UDR0 = c;
  22 +}
  23 +
  24 +unsigned char get_serial(void)
  25 +{
  26 + loop_until_bit_is_set(UCSR0A, RXC0);
  27 + return UDR0;
  28 +}
  29 +
  30 +void port_initio(void)
  31 +{
  32 + DDRD &= 0xFC;
  33 + DDRD |= 0x3C;
  34 + DDRB &= 0xFE;
  35 + PORTD |= 0x3C;
  36 +}
  37 +
  38 +int main(void)
  39 +{
  40 + init_serial(250000);
  41 + port_initio();
  42 + uint8_t opto0 = 0;
  43 + uint8_t opto2 = 0;
  44 +
  45 + while (1)
  46 + {
  47 + if((opto0 == 1) && ((PIND & 0x40) == 0x40))
  48 + PORTD &= 0xE7;
  49 + else
  50 + PORTD |= 0x18;
  51 + if((opto2 == 1) && ((PIND & 0x80) == 0x80))
  52 + PORTD &= 0xDB;
  53 + else
  54 + PORTD |= 0x24;
  55 +
  56 + if((PINB & 0x01) == 0x01)
  57 + send_serial('U');
  58 + else
  59 + send_serial('D');
  60 + }
  61 +
  62 + return 0;
  63 +}
main.ino deleted
@@ -1,30 +0,0 @@ @@ -1,30 +0,0 @@
1 -int ctrlPump1 = 5;  
2 -int ctrlPump2 = 6;  
3 -int ctrlHeat1 = 7;  
4 -int ctrlHeat2 = 8;  
5 -int pump = 9;  
6 -int heat = 10;  
7 -  
8 -void setup()  
9 -{  
10 - pinMode(ctrlPump1, INPUT);  
11 - pinMode(ctrlPump2, INPUT);  
12 - pinMode(ctrlHeat1, INPUT);  
13 - pinMode(ctrlHeat2, INPUT);  
14 - pinMode(pump, OUTPUT);  
15 - pinMode(heat, OUTPUT);  
16 -}  
17 -  
18 -void loop()  
19 -{  
20 - if((digitalRead(ctrlPump1) == HIGH) && digitalRead(ctrlPump2))  
21 - digitalWrite(pump, LOW);  
22 - else  
23 - digitalWrite(pump, HIGH);  
24 -  
25 - if((digitalRead(ctrlHeat1) == HIGH) && digitalRead(ctrlHeat2))  
26 - digitalWrite(heat, LOW);  
27 - else  
28 - digitalWrite(heat, HIGH);  
29 - delay(1000);  
30 -}