Arduino C programming

Date: 2016-07-04
//#define F_CPU=16000000UL
//#define __AVR_ATmega328P__

#include <avr/io.h>
#include <avr/interrupt.h>

#ifdef SPECIFIC_DETAILS
#include 
#endif // SPECIFIC_DETAILS



//#include <util/delay.h>


//number |= 1 << x; // set a bit
//number &= ~(1 << x); // clear a bit
//number ^= 1 << x; // toggle a bit
//bit = number & (1 << x); // check a bit


//number = number | (1 << x); // set a bit
//number = number & ~(1 << x); // clear a bit
//number = number ^ (1 << x); // toggle a bit
//bit = number & (1 << x); // check a bit



//// timer0 overflow
//ISR(TIMER0_OVF_vect)
//{
//    /* set pin 5 low to turn led off */
//    PORTB &= (1 << PORTB5);
//}

static unsigned int state;
static unsigned long elapsed_ms = 0UL;

void on_timer2();

// timer1 overflow
ISR(TIMER2_OVF_vect)
{
    TCNT2 = 6;     // reset timer ct to 5 out of 255 (after 250 counts an overflow)
    TIFR2 = 0x00;
    on_timer2();
}

//SIGNAL(SIG_OUTPUT_COMPARE0)

void init_timer2()
{
    TCCR2B = 0x00;        // Disable Timer2 while we set it up

    TCNT2  = 6;         // Reset Timer Count  (255-250) = execute ev -th T/C clock
    TIFR2  = 0x00;        // Timer2 INT Flag Reg: Clear Timer Overflow Flag
    //TIMSK2 = 0x01;        // Timer2 INT Reg: Timer2 Overflow Interrupt Enable
    TIMSK2 = (1<<TOIE2);

    TCCR2A = 0x00;        // Timer2 Control Reg A: Wave Gen Mode normal
    TCCR2B = 0x04;        // Timer2 Control Reg B: Timer Prescaler set to 64 //1024

    //PORTB |= (1 << PORT5);
}

void on_timer2()
{
    elapsed_ms+=1;
    if(elapsed_ms % 10000 == 0)
    {
        /* toggle pin 5 */
        state = state ^ (1 << 5);
        PORTB = state;

        // PORTD = PORTD ^ (1 << 1); // Transmit LED
    }
}

void init_ports()
{
    // set pin 5 of PORTB for output
    DDRB |= (1 << 5);

    //DDRC |= (1 << 1);

    // Set transmit led for output
    //DDRD |= (1 << 1);

    // Clear pin 5
    PORTB &= ~(1 << 5);

    // Clear transmit LED
    //PORTD &= ~(1 << 1);

    state = PORTB;
}

int main (void)
{
    init_ports();

    init_timer2();

    // enable interrupts
    sei();

    while (1)
    asm volatile("nop" ::);

//    // Endless loop
//    while(1)
//    {
//        _delay_ms(100);
//        PORTB ^= (1 << PORT5);
//    }

    // Clear interrupts
    //cli();
    return 0;
}

int get_timer_count(unsigned int frequency, unsigned long clock_frequency)
{
    return (1 / frequency) / (1 / clock_frequency) - 1;
}

int get_elapsed_2(int counter, unsigned long clock_frequency, unsigned prescale_value)
{
    return counter * (clock_frequency / prescale_value);
}

int get_elapsed(int counter)
{
    return get_elapsed_2(counter, F_CPU, 1024);
}


void serial_begin(long speed)
{

}

void serial_end()
{

}

void serial_txrx_delay()
{

}

void serial_set_tx_bit(uint8_t bit)
{

}

uint8_t serial_get_rx_bit()
{
    return 0;
}

void serial_write(uint8_t byte)
{
    uint8_t cnt = 0;

    for(cnt=0; cnt<8; cnt+=1)
    {
        uint8_t hasbit = byte & (1 << cnt);

        serial_set_tx_bit(hasbit);

        serial_txrx_delay();
    }

    serial_txrx_delay();
}

void serial_read(uint8_t byte)
{

}



/*
F_PRESCALER = 64;
F_OVERFLOWS = F_CPU / F_PRESCALER;
F_COUNT = F_OVERFLOWS / 1000; // 250


F_CPU = 8,000,000hz
F_OVERFLOW = 100 //hz
CYCLES_PER_OVERFLOW = F_CPU / F_OVERFLOW  //, i.e.: An overflow should occur each 32,000 CPU clock cycles
CYCLES_PER_TIMER_TICK = CYCLES_PER_OVERFLOW / 256 // = 125 = prescaler value
*/

/*
    // Configure PORTA as output
    DDRA = 0xFF;
    PORTA = 0xFF;

    // enable timer overflow interrupt for both Timer0 and Timer1
    //TIMSK = (1 << TOIE0) | (1 << TOIE1);

//    // set timer0 counter initial value to 0
//    TCNT0 = 0x00;
//
//    // start timer0 with /1024 prescaler
//    TCCR0 = (1 << CS02) | (1 << CS00);
//
//    // lets turn on 16 bit timer1 also with /1024
//    TCCR1B |= (1 << CS10) | (1 << CS12);



avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:led.hex

avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:arduino1.elf.hex

*/
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
avr-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  
Output file is bin/Debug/arduino1.elf with size 14.59 KB
Running project post-build steps
avr-size bin/Debug/arduino1.elf
avr-objcopy -O ihex -R .eeprom -R .eesafe bin/Debug/arduino1.elf bin/Debug/arduino1.elf.hex
   text	   data	    bss	    dec	    hex	filename
   1190	      0	      6	   1196	    4ac	bin/Debug/arduino1.elf
avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex bin/Debug/arduino1.elf bin/Debug/arduino1.elf.eep.hex
avr-objdump -h -S bin/Debug/arduino1.elf > bin/Debug/arduino1.elf.lss
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

# upload the file to the arduino
avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:bin/Debug/arduino1.elf.hex

https://www.codementor.io/@dewetvanthomas/tutorial-arduino-programming-in-c-12b7cztyui

https://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/

http://what-when-how.com/8051-microcontroller/serial-port-programming-in-c/

https://www.norwegiancreations.com/2016/10/embedded-tutorial-basic-serial-communication/

1990cookie-checkArduino C programming