N
N
nickolay_titov2014-07-15 21:30:41
Arduino
nickolay_titov, 2014-07-15 21:30:41

How to upload the code below to the controller using arduino?

#include <attiny2313.h>
#include <stdlib.h>
#include <math.h>
#include <delay.h>
float red, green, blue;                     //now colors
unsigned int tred, tgreen, tblue;            //tagert colors
float delta_red, delta_green, delta_blue;       //величина прибавки за шаг
unsigned int time;                          //длительность шага


void generate_tagert_color(void)
{
      tred = rand();
      tgreen = rand();
      tblue = rand();
      tred = tred & 0xFF;          //to max 255
      tgreen = tgreen & 0xFF;
      tblue = tblue & 0xFF;
};

void delay(unsigned int delayt)               //задержка delay_ms() на вход принимает только константу( 
{
  while (delayt != 0)
  {
    delay_ms(1);
    delayt--;
  };
};

void main(void)
{

unsigned int temp1, temp2, temp3, temp4;

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=In 
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=T 
PORTB=0x00;
DDRB=0x0E;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 8000,000 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA1;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 8000,000 kHz
// Mode: Fast PWM top=FFh
// OC2 output: Non-Inverted PWM
ASSR=0x00;
TCCR2=0x69;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

srand(1);             //инициализируем рандомайзер этим значением


while (1)
      {
            
       do 
       {
        generate_tagert_color();
        temp1 = (abs(tred - tgreen));
        temp2 = (abs(tred - tblue));
        temp3 = (abs(tgreen - tblue));
       }
       while (( temp1 < 150 ) && (temp2 < 150) && (temp3 < 150));  //если все цвета будут примерно равные то будет белый что не интересно
      
       if (rand() < 13100)                                         //c вероятностью 0,4 (32767) вышибаем один цвет
       {
         do
         {
            temp4 = rand() & 0x000F ;
         }
        while ((temp4 == 0) || (temp4 > 3));
       
        if (temp4 == 1)                                            //не повезло красному
        {                                                          //гасим до 0 - 20% от исходного
          do
           {
             temp4 = rand() & 0x0FFF;
           }
          while ((temp4 == 0) || (temp4 > 3276));
          
          tred = ceil ((float) (temp4 / 32767) * tred);
        };
        
        if (temp4 == 2)
        {
          do
           {
             temp4 = rand() & 0x0FFF;
           }
          while ((temp4 == 0) || (temp4 > 3276));
          
          tgreen = ceil ((float) (temp4 / 32767) * tgreen);
        };
        
        if (temp4 == 3)
        {
          do
           {
             temp4 = rand() & 0x0FFF;
           }
          while ((temp4 == 0) || (temp4 > 3276));
          
          tblue = ceil ((float) (temp4 / 32767) * tblue);
        };
        
       }
       

                                                   //значение прибавки. доходим до нужного цвета за 255 шагов
       delta_red = (float) (tred - red) / 255;
       delta_green = (float) (tgreen - green) / 255;
       delta_blue = (float) (tblue - blue) / 255; 
      
       
       do
        {
          temp4 = rand() & 0x00FF;
        }
       while ((temp4 < 50) || (temp4 > 100));                       //скокрость перехода случайна от 5 до 10с
      
       time = ceil( ((float) temp4 / 255) * 100);                    //задержка должна быть целой
       
            
       do 
       {
        if (tred != ceil(red))
         red = red + delta_red;
        if (tgreen != ceil(green))
         green = green + delta_green;
        if (tblue != ceil(blue))
         blue = blue + delta_blue;
       
        OCR1A = ceil(blue);
        OCR1B = ceil(green);
        OCR2 = ceil(red);
      
        delay(time);
       }
       while ((tred != ceil(red)) || (tgreen != ceil(green)) || (tblue != ceil(blue))); 
       
       delay(time * 255);                                    //стремились к этому цвету, задержим его
        
      
      };     
}

cited the code for the product. You need to fill it in attiny2313.
how to do this with arduino, that is, using arduino as a programmer?
1. I'm not going to upload anything to the arduino. I want to use it as a programmer to upload code to the microcontroller for my circuit.
2. I have already uploaded a sketch for turning an arduino into a programmer.
3.! I need to flash the MK, not the arduino.
4. I need to know how to upload this data to atmel attiny2313 microcontroller using arduino as a programmer.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Calc, 2014-07-16
@Calc

arduino 2313?
Programmer via ISP
Here are a couple of articles about AVR
easyelectronics.ru/avr-uchebnyj-kurs-ispolzovanie-...
easyelectronics.ru/skorostnoj-avr-usb-programmator...

S
svd71, 2014-07-16
@svd71

1. The code you specified is not suitable for Ardurin at all: a Calc colleague wrote correctly - it is for the low class of attiny2313 controllers.
2. The code you specified is built using avr studio or avr-gcc, winavr. Then you need to upload a sketch to the arduino FOR CREATING A PROGRAMMER FROM THE ARDUINO. and using it to program the attiny2313 microcontroller, but not the arduino.
3. Or the second option: adopt this code already under the arduino framework and upload it directly to the arduino. You can find out what constructions like PORTx, DDRx, etc. mean from the datasheets on attiny2313 from atmel.com.

V
Vitaly Peretyatko, 2014-11-07
@viperet

pin assignments when using the Arduino ISP sketch:
Pin connections:
Arduino Pin 13 (or SCK of another programmer)
Arduino Pin 12 (or MISO of another programmer)
Arduino Pin 11 (or MOSI of another programmer)
Arduino Pin 10 (or RESET of another programmer )
so the first thing is to connect the arduino and the tinka correctly. (SCK, MISO, MOSI, RESET, GND + power)
if you already have a firmware file - use avrdude to set the fuses and sew a tinka.
if not - the second question - how will you compile you C code for attiny2313. You can do this directly with the Arduino IDE. Put cortex in it to support Attiny microcontrollers ( https://code.google.com/p/arduino-tiny/)open your program, select a controller from the list in the Arduino IDE, "flash the bootloader" (actually just set fuses) and compile and flash your program. More details with pictures here highlowtech.org/?p=1695

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question