| 1 | # C Code examples |
| 2 | |
| 3 | There are several example programs to help you get started with coding in C. Some are described below. |
| 4 | |
| 5 | ## blink2.c |
| 6 | |
| 7 | Alternately enables the first four pins of PORTB (PB0,PB1,PB2,PB3), and last four PORTB (PB4,PB5,PB6,PB7) every 500mS |
| 8 | |
| 9 | ```c |
| 10 | #include "mh-utils.c" //library containing the delay_ms function |
| 11 | |
| 12 | int main (void) |
| 13 | { |
| 14 | DDRB = 255; // Data Direction Register for port B. Make all 8 pins output type |
| 15 | for(;;) |
| 16 | { |
| 17 | PORTB = 15; //0b00001111 . First four pins ON |
| 18 | delay_ms(500); //wait for half a second. |
| 19 | PORTB = 240; //0b11110000 . Next four pins ON. First four OFF. |
| 20 | delay_ms(500); |
| 21 | } |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | ``` |
| 26 | |
| 27 | ## pov2.c |
| 28 | |
| 29 | Make a persistence of vision display with 8 LEDs connected to PORTB. |
| 30 | It is currently designed to flash `KUTTYPY` , but can be modified easily to write anything. |
| 31 | |
| 32 | !!! tip "Persistence of vision display" |
| 33 |  |
| 34 | |
| 35 | A persistence of vision display made with C code! Write text in thin air using 8 LEDs on PORTB. |
| 36 | |
| 37 | ## counter.c |
| 38 | |
| 39 | counts input pulses on PB1 in 2 second intervals, and sends the results via UART to the serial monitor of kuttypy. BAUD rate 38400 8N1. |
| 40 | |
| 41 | can only count up to 255, following which an overflow will occur, and it will start over from 0. For higher count rates, refer |
| 42 | to the next section on 16 bit timers. |
| 43 | |
| 44 | !!! tip "Counters are useful for measuring frequencies, or signals from instruments such as Geiger counters" |
| 45 | ## counter16bit_plaintext.c |
| 46 | |
| 47 | counts input pulses on PB1 in 10 second intervals, and sends the results via UART to the serial monitor of kuttypy. BAUD rate 38400. |
| 48 | |
| 49 | can count up to 65535 per interval, following which an overflow will occur, and it will start over from 0. The counts are parsed |
| 50 | into human readable text format, and sent via serial port. Each number is terminated by a newline character. |
| 51 | |
| 52 | ## stepper.c |
| 53 | |
| 54 | control a stepper motor using firt four pins of PORTB. The program signals 360 steps clockwise, and then 360 backwards in a continuously oscillating fashion. |
| 55 | You can also [control stepper motors from the kuttyPy GUI](../../stepper) |
| 56 | |
| 57 | ## R2RDAC_wavegen.c |
| 58 | |
| 59 | Create a sine wave generator using an 8 bit R2R DAC connected to PORTB. This program has its own page with results, images, and |
| 60 | table creation code [here](../r2r_wavegen) |
| 61 | |
| 62 | !!! tip "ExpEYES17 oscilloscope window showing the 0-5V waveform" |
| 63 |  |
| 64 | Scipy based sine fitting reveals the frequency to be 1958.7Hz |
| 65 | |
| 66 | !!! tip "KuttyPy with R2R DAC Connected to ExpEYES17 oscilloscope" |
| 67 |  |
| 68 |