{"id":199,"date":"2016-07-04T23:14:13","date_gmt":"2016-07-04T22:14:13","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=199"},"modified":"2023-02-08T16:28:03","modified_gmt":"2023-02-08T15:28:03","slug":"arduino-c-programming","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/arduino-c-programming\/","title":{"rendered":"Arduino C programming"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">\/\/#define F_CPU=16000000UL\n\/\/#define __AVR_ATmega328P__\n\n#include &lt;avr\/io.h&gt;\n#include &lt;avr\/interrupt.h&gt;\n\n#ifdef SPECIFIC_DETAILS\n#include \n#endif \/\/ SPECIFIC_DETAILS\n\n\n\n\/\/#include &lt;util\/delay.h&gt;\n\n\n\/\/number |= 1 &lt;&lt; x; \/\/ set a bit\n\/\/number &amp;= ~(1 &lt;&lt; x); \/\/ clear a bit\n\/\/number ^= 1 &lt;&lt; x; \/\/ toggle a bit\n\/\/bit = number &amp; (1 &lt;&lt; x); \/\/ check a bit\n\n\n\/\/number = number | (1 &lt;&lt; x); \/\/ set a bit\n\/\/number = number &amp; ~(1 &lt;&lt; x); \/\/ clear a bit\n\/\/number = number ^ (1 &lt;&lt; x); \/\/ toggle a bit\n\/\/bit = number &amp; (1 &lt;&lt; x); \/\/ check a bit\n\n\n\n\/\/\/\/ timer0 overflow\n\/\/ISR(TIMER0_OVF_vect)\n\/\/{\n\/\/    \/* set pin 5 low to turn led off *\/\n\/\/    PORTB &amp;= (1 &lt;&lt; PORTB5);\n\/\/}\n\nstatic unsigned int state;\nstatic unsigned long elapsed_ms = 0UL;\n\nvoid on_timer2();\n\n\/\/ timer1 overflow\nISR(TIMER2_OVF_vect)\n{\n    TCNT2 = 6;     \/\/ reset timer ct to 5 out of 255 (after 250 counts an overflow)\n    TIFR2 = 0x00;\n    on_timer2();\n}\n\n\/\/SIGNAL(SIG_OUTPUT_COMPARE0)\n\nvoid init_timer2()\n{\n    TCCR2B = 0x00;        \/\/ Disable Timer2 while we set it up\n\n    TCNT2  = 6;         \/\/ Reset Timer Count  (255-250) = execute ev -th T\/C clock\n    TIFR2  = 0x00;        \/\/ Timer2 INT Flag Reg: Clear Timer Overflow Flag\n    \/\/TIMSK2 = 0x01;        \/\/ Timer2 INT Reg: Timer2 Overflow Interrupt Enable\n    TIMSK2 = (1&lt;&lt;TOIE2);\n\n    TCCR2A = 0x00;        \/\/ Timer2 Control Reg A: Wave Gen Mode normal\n    TCCR2B = 0x04;        \/\/ Timer2 Control Reg B: Timer Prescaler set to 64 \/\/1024\n\n    \/\/PORTB |= (1 &lt;&lt; PORT5);\n}\n\nvoid on_timer2()\n{\n    elapsed_ms+=1;\n    if(elapsed_ms % 10000 == 0)\n    {\n        \/* toggle pin 5 *\/\n        state = state ^ (1 &lt;&lt; 5);\n        PORTB = state;\n\n        \/\/ PORTD = PORTD ^ (1 &lt;&lt; 1); \/\/ Transmit LED\n    }\n}\n\nvoid init_ports()\n{\n    \/\/ set pin 5 of PORTB for output\n    DDRB |= (1 &lt;&lt; 5);\n\n    \/\/DDRC |= (1 &lt;&lt; 1);\n\n    \/\/ Set transmit led for output\n    \/\/DDRD |= (1 &lt;&lt; 1);\n\n    \/\/ Clear pin 5\n    PORTB &amp;= ~(1 &lt;&lt; 5);\n\n    \/\/ Clear transmit LED\n    \/\/PORTD &amp;= ~(1 &lt;&lt; 1);\n\n    state = PORTB;\n}\n\nint main (void)\n{\n    init_ports();\n\n    init_timer2();\n\n    \/\/ enable interrupts\n    sei();\n\n    while (1)\n    asm volatile(\"nop\" ::);\n\n\/\/    \/\/ Endless loop\n\/\/    while(1)\n\/\/    {\n\/\/        _delay_ms(100);\n\/\/        PORTB ^= (1 &lt;&lt; PORT5);\n\/\/    }\n\n    \/\/ Clear interrupts\n    \/\/cli();\n    return 0;\n}\n\nint get_timer_count(unsigned int frequency, unsigned long clock_frequency)\n{\n    return (1 \/ frequency) \/ (1 \/ clock_frequency) - 1;\n}\n\nint get_elapsed_2(int counter, unsigned long clock_frequency, unsigned prescale_value)\n{\n    return counter * (clock_frequency \/ prescale_value);\n}\n\nint get_elapsed(int counter)\n{\n    return get_elapsed_2(counter, F_CPU, 1024);\n}\n\n\nvoid serial_begin(long speed)\n{\n\n}\n\nvoid serial_end()\n{\n\n}\n\nvoid serial_txrx_delay()\n{\n\n}\n\nvoid serial_set_tx_bit(uint8_t bit)\n{\n\n}\n\nuint8_t serial_get_rx_bit()\n{\n    return 0;\n}\n\nvoid serial_write(uint8_t byte)\n{\n    uint8_t cnt = 0;\n\n    for(cnt=0; cnt&lt;8; cnt+=1)\n    {\n        uint8_t hasbit = byte &amp; (1 &lt;&lt; cnt);\n\n        serial_set_tx_bit(hasbit);\n\n        serial_txrx_delay();\n    }\n\n    serial_txrx_delay();\n}\n\nvoid serial_read(uint8_t byte)\n{\n\n}\n\n\n\n\/*\nF_PRESCALER = 64;\nF_OVERFLOWS = F_CPU \/ F_PRESCALER;\nF_COUNT = F_OVERFLOWS \/ 1000; \/\/ 250\n\n\nF_CPU = 8,000,000hz\nF_OVERFLOW = 100 \/\/hz\nCYCLES_PER_OVERFLOW = F_CPU \/ F_OVERFLOW  \/\/, i.e.: An overflow should occur each 32,000 CPU clock cycles\nCYCLES_PER_TIMER_TICK = CYCLES_PER_OVERFLOW \/ 256 \/\/ = 125 = prescaler value\n*\/\n\n\/*\n    \/\/ Configure PORTA as output\n    DDRA = 0xFF;\n    PORTA = 0xFF;\n\n    \/\/ enable timer overflow interrupt for both Timer0 and Timer1\n    \/\/TIMSK = (1 &lt;&lt; TOIE0) | (1 &lt;&lt; TOIE1);\n\n\/\/    \/\/ set timer0 counter initial value to 0\n\/\/    TCNT0 = 0x00;\n\/\/\n\/\/    \/\/ start timer0 with \/1024 prescaler\n\/\/    TCCR0 = (1 &lt;&lt; CS02) | (1 &lt;&lt; CS00);\n\/\/\n\/\/    \/\/ lets turn on 16 bit timer1 also with \/1024\n\/\/    TCCR1B |= (1 &lt;&lt; CS10) | (1 &lt;&lt; CS12);\n\n\n\navrdude -F -V -c arduino -p ATMEGA328P -P \/dev\/ttyACM0 -b 115200 -U flash:w:led.hex\n\navrdude -F -V -c arduino -p ATMEGA328P -P \/dev\/ttyACM0 -b 115200 -U flash:w:arduino1.elf.hex\n\n*\/\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">avr-gcc -Wall -mmcu=atmega328p -DF_CPU=16000000UL -D__AVR_ATmega328P__ -g -I\/usr\/avr\/include\/ -I\/usr\/include -c main.c -o obj\/Debug\/main.o\navr-g++ -L\/usr\/avr\/lib -L\/usr\/lib -o bin\/Debug\/arduino1.elf obj\/Debug\/main.o  -mmcu=atmega328p -Wl,-Map=bin\/Debug\/arduino1.elf.map,--cref  \nOutput file is bin\/Debug\/arduino1.elf with size 14.59 KB\nRunning project post-build steps\navr-size bin\/Debug\/arduino1.elf\navr-objcopy -O ihex -R .eeprom -R .eesafe bin\/Debug\/arduino1.elf bin\/Debug\/arduino1.elf.hex\n   text\t   data\t    bss\t    dec\t    hex\tfilename\n   1190\t      0\t      6\t   1196\t    4ac\tbin\/Debug\/arduino1.elf\navr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex bin\/Debug\/arduino1.elf bin\/Debug\/arduino1.elf.eep.hex\navr-objdump -h -S bin\/Debug\/arduino1.elf &gt; bin\/Debug\/arduino1.elf.lss\nProcess terminated with status 0 (0 minute(s), 0 second(s))\n0 error(s), 0 warning(s) (0 minute(s), 0 second(s))\n\n# upload the file to the arduino\navrdude -F -V -c arduino -p ATMEGA328P -P \/dev\/ttyACM0 -b 115200 -U flash:w:bin\/Debug\/arduino1.elf.hex\n<\/pre>\n\n\n\n<p><a href=\"https:\/\/www.codementor.io\/@dewetvanthomas\/tutorial-arduino-programming-in-c-12b7cztyui\">https:\/\/www.codementor.io\/@dewetvanthomas\/tutorial-arduino-programming-in-c-12b7cztyui<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/balau82.wordpress.com\/2011\/03\/29\/programming-arduino-uno-in-pure-c\/\">https:\/\/balau82.wordpress.com\/2011\/03\/29\/programming-arduino-uno-in-pure-c\/<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/what-when-how.com\/8051-microcontroller\/serial-port-programming-in-c\/\">http:\/\/what-when-how.com\/8051-microcontroller\/serial-port-programming-in-c\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.norwegiancreations.com\/2016\/10\/embedded-tutorial-basic-serial-communication\/\">https:\/\/www.norwegiancreations.com\/2016\/10\/embedded-tutorial-basic-serial-communication\/<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/mohitjoshi999.files.wordpress.com\/2009\/08\/080309_1406_serialportp2.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/mohitjoshi999.files.wordpress.com\/2009\/08\/080309_1406_serialportp3.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.lehelmatyus.com\/wp-content\/uploads\/2016\/12\/arduino-nano-pins.png\" alt=\"\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\/\/#define F_CPU=16000000UL \/\/#define __AVR_ATmega328P__ #include &lt;avr\/io.h&gt; #include &lt;avr\/interrupt.h&gt; #ifdef SPECIFIC_DETAILS #include #endif \/\/ SPECIFIC_DETAILS \/\/#include &lt;util\/delay.h&gt; \/\/number |= 1 &lt;&lt; x; \/\/ set a bit \/\/number &amp;= ~(1 &lt;&lt; x); \/\/ clear a bit \/\/number ^= 1 &lt;&lt; x; \/\/ toggle a bit \/\/bit = number &amp; (1 &lt;&lt; x); \/\/ check a bit \/\/number [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-199","post","type-post","status-publish","format-standard","hentry","category-other-scripts"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=199"}],"version-history":[{"count":12,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/199\/revisions"}],"predecessor-version":[{"id":7333,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/199\/revisions\/7333"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}