Debian Codegraph @

docs/programming/c.md

raw symbols SCIP
1 # Compiling and uploading C Code
2
3 The KuttyPy firmware also contains bootloader logic to enable uploading your own hex files.
4
5 !!! tip "Compiling and uploading C Code"
6 ![](../images/c_code.png "C Code section")
7
8 The above example shown in the KuttypyIDE blinks each LED on PORTB one by one,and loops indefinitely.
9
10 ## Procedure
11
12 * First, open a C file
13 * Then, Compile it, followed by upload if no errors.
14 * Finally, click on the 'RUN' button to start execution of this uploaded code. The UI will be inaccesible until you uncheck the user app button.
15 * If the software is closed, your uploaded code will not automatically run, and a physical cable reconnect will be needed.
16 * If the kuttyPy simply receives a 5V power supply, your uploaded code will start running.
17
18 There are several examples supplied with KuttyPy for dealing with basic I/O,I2C, UART, Persistence of Vision
19 display, counters, timers etc.
20
21 !!! tip "Atmega32 Datasheet"
22 You should probably [Download](http://ww1.microchip.com/downloads/en/devicedoc/doc2503.pdf) the datasheet
23 if you want to understand the functions of each register
24
25 ## IDE Features
26
27 The IDE includes tabbed browsing, and editing multiple files. It also displays the contents
28 of list,map, and hex files when you compile your code successfully. The `ONLINE COMPILER` launches a
29 flask server, and displays an IP address. This IP address can be entered in the KuttyPy app
30 on an android phone on the same network, allowing it to compile code via this IDE.
31
32 The example below shows a program called `dump_integers.c` which outputs newline terminated
33 numbers from 1 to 10000 via the serial port. this is used to demonstrate the serial monitor.
34
35 !!! tip "Compiling and uploading C Code"
36 ![](../images/ide_dumpints.png "IDE Dump integers")
37
38 The raw values in the serial monitor are shown when the code is run, and a GAUGE is also
39 available to view these readings as an analog gauge. It can plot binary data, as well as parsed ASCII data.
40
41 !!! tip "Compiling and uploading C Code"
42 ![](../images/ide_gauge.png "IDE Dump integers")
43
44 A data logger is built into the gauge, and this makes a plot of the values received over the serial port.
45
46 !!! tip "Compiling and uploading C Code"
47 ![](../images/ide_logger.png "IDE Dump integers")
48
49 ## Compiling and uploading blink.c for kuttyPy
50
51 using the command line to compile with avr-gcc, and upload with avrdude. You can also use the graphical software.
52
53 ```bash
54 avr-gcc -Wall -O2 -mmcu=atmega32 -Wl,-Map,blink.map -o blink blink.c
55 avr-objcopy -j .text -j .data -O ihex blink blink.hex
56 avrdude -b 38400 -P /dev/ttyUSB0 -pm328p -c arduino -U flash:w:blink.hex #Verify if the device is ttyUSB0
57 ```
58
59
60 ## Code examples from microhope
61
62 The kuttyPy hardware is closely related to the microhope project, and code examples from Microhope can also be
63 compiled and uploaded for kuttyPy. However, since the bootloaders are different, the upload protocol 'arduino'
64 must be used.
65
66 [List of included C code](../c_examples)
67
68 ## Integrating with the Arduino IDE
69
70 Go to file->preferences->additional boards manager of the Arduino IDE , and paste the following line
71
72 ```python
73 https://scischool.in/kuttypy_arduino.json
74 ```
75
76 Select Tools->board-> Kuttypyplus -> KuttypyPlus(ATMEGA32)
77
78 Now you can run the countless examples in the Arduino ecosystem on your KuttypyPlus as well.
79
80 + sample compile output on the Arduino IDE
81
82 ```bash
83
84 Archiving built core (caching) in: /tmp/arduino_cache_321166/core/core_kuttypy_avr_32_eeprom_keep,BOD_4v0,clock_8MHz_external_94bd17bffe2a8f3c71766d07311a0c7d.a
85 Sketch uses 840 bytes (2%) of program storage space. Maximum is 32256 bytes.
86 Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
87
88 ```