Skip to content

Write C language program to AVR-1 series

Note

Arduino IDE and arduino-cli does not support “.c” file but only support “.ino” file. Process at this page is “hacky” as making C language file “.ino” for using Arduino tool chain.

Target board

ATtiny1614_hello_buzzer.md

  • Microcontroller: ATtiny1614

  • Pin-out:

    • PA4: Tactile switch (with pullup resister)
    • PA5: electromagnetic buzzer
    • PA7: LED
    • PA0: UPDI
    • PB2: TXD
    • PB3: RXD

Put following code to Arduino IDE sketch. The Arduino IDE generates source file with .ino extension in a directory with the same name of source file.

b1614.blink-sw.c/t1614.blink-sw.c.ino

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <avr/io.h>

#define F_CPU 20000000
#include <util/delay.h>

int buttonState = 0;

int main(void)
{
  CPU_CCP = CCP_IOREG_gc; // Un-protect protected I/O registers - allow clock changes
  CLKCTRL_MCLKCTRLB = 0; // disable clock prescaler - clock is now equal to system clock - 20MHz

  VPORTA_DIR |= PIN7_bm; // set PA7 as ouput (LED)
  VPORTA_IN |= PIN4_bm;  // set PA4 as input (switch)

  while(1)
  {
    buttonState = VPORTA_IN & PIN4_bm; 
    // If button is not pressed
    if (buttonState > 0) {
      PORTA_OUTTGL = PIN7_bm;  // Toggle PORTA (PA7)
      _delay_ms(200); // gives delay
    }
  }
}

Ref. ATtiny412.3.blink.c at Embedded Programming

Write program from Arduino IDE

The process is the same with the way to write .ino file.

  • Arduino IDE: 1.18.10
  • Board: megaTinycoe 2.2.9
    • Boards Manager URL: http://drazzy.com/package_drazzy.com_index.json

On Arduino IDE.

  • Board: ATtiny1614
  • Serial Port: /dev/cu/usbserial-D207RG9Y
    • check it by ls /dev | grep -i usb on your terminal
  • Programmer: Serial Port and 4.7k(pyupdi style) t1614_board

Then click “Upload”(arrow mark) button. t1614_write_c


Write program from arduino-cli

Install arduino-cli and configuration for megaTinyCore

Please see Build .hex file from C language program, FA2020

Compile

1
2
3
4
5
6
# cd <sketch directory for c program>
$ cd ~/Documents/Arduino/t1614.simpleblink.c
$ ls
t1614.simpleblink.c.ino

$ arduino-cli compile --fqbn megaTinyCore:megaavr:atxy4 --build-path ~/Arduino/build t1614.simpleblink.c -v

Then you will see t1614.simpleblink.c.ino.hex the build path directory.

1
2
$ ls ~/Arduino/build/*hex
~~/Arduino/build/t1614.simpleblink.c.ino.hex

Write program

If you do not have pyupdi in your local PC, please see setup pyupdi

Run pyupdi (python virtual environment is recommended option. ref. run pyupdi)

1
2
3
$ cd ~/Arduino/build
$ pyupdi.py -d tiny1614 -c /dev/tty.usbserial-D307RG9Y -b 57600 -f t1614.simpleblink.c.ino.hex
Programming successful

Last update: March 21, 2021