Show Posts
Pages: 1 [2] 3 4 ... 116
16  Using Arduino / Microcontrollers / Re: Why 16 Mhz clock??? on: July 16, 2014, 08:22:36 am
1/16000000 = 62.5nS x 16 = 1uS.
I think that works out nice & convenient.
A 16.384MHz crystal would be very useful - would make a very nice millis() function (16384000/64/256 = 1kHz). Granted it would make timing microseconds more difficult and 'nop' instructions would no longer accurately add up to a us.
17  Using Arduino / Microcontrollers / Re: How establish communication between Attiny and Arduino One? on: July 16, 2014, 04:43:55 am
I'm fairly sure he has many Tinys talking to an Arduino Uno, in which case that's all backwards (not useful).

The only bus that easily allows multiple devices to be attached to it is I2C.
Ah, hadn't picked that up from his post. Yes, if you have multiple devices I2C is by far the best bus.
18  Using Arduino / Microcontrollers / Re: How establish communication between Attiny and Arduino One? on: July 16, 2014, 04:09:06 am
Alternatively, if you want to use serial, there is another way.

I presume (unless I am missing something) that an Arduino One is an Uno?
If so you can put it into Synchronous mode, by calling these lines in setup() of the Arduino sketch:
Code:
Serial.begin(2400); //the actual baud rate will be 4 times higher in sync mode, so this is 9600.
UCSR0C &= ~_BV(UMSEL01);
UCSR0C |= _BV(UMSEL00);

Connect the XCX pin (Digital Pin 4) of the Arduino to an interrupt pin of the ATTiny (you can either use the Analog Comparator AIN1 pin, or either the External Interrupt 0 or 1 pins depending on which ATTiny you have). Then connect the RX and TX to any pin on the ATTiny on the same Port as the interrupt pin.
I have attached a library which will allow the ATTiny to be a USART slave. In this library there is a "TinyUSARTConfig.h" file. Set the port bits and names you have used in this file as directed. If you used an interrupt different from INT0, you need to configure that as well.

And the following is an example sketch:
Code:
#include "TinySoftwareUSART.h"
#include "TinyUSARTConfig.h"

USARTRingBuffer rx_buffer = {{0},0,0};
USARTRingBuffer tx_buffer = {{0},0,0};//These two lines are required and are the Serial comms buffer.

TinySoftwareUSART USART = TinySoftwareUSART(); //Make an instance of USART

void setup()
{
    USART.begin(); //No need to specify a baud rate, as that is governed by the Arduino Uno (master).
    USART.println("Welcome!");
}

void loop(){
    if (USART.available()){
        USART.write(USART.read()); //echo.
    }
}

Basically this gives you the advantage of Asyncronous Serial communication, but uses a clock line to time the bits allowing for full duplex operation without the ATTiny having to time out the Serial baud rate.


(Side note, I have tested this on two different ATTiny devices, but it should work on any which have the GPIOR0, GPIOR1 and GPIOR2 listing in the register description of the datasheet, those are used by the library).
19  Using Arduino / Microcontrollers / Re: Why 16 Mhz clock??? on: July 16, 2014, 03:49:00 am
One problem with running at 20MHz is that 20 is not a power of 2 which can make some timings that require divisions complicated.

...R
But then neither is 16MHz, it just has two more 2's as its prime factors (16MHz is divisible by 2^10 vs. 20MHz by 2^8 - they both have lots of 5's in them too).
20  Using Arduino / Microcontrollers / Re: Why 16 Mhz clock??? on: July 16, 2014, 02:41:05 am
All the timers will work in exactly the same way with an external clock vs. a crystal/resonator.

The reason it works without changing the fuse is the way the crystal oscillator work - it is essentially just a NOT gate (1 input, 1 output) which has a crystal connected across it. If you feed a clock signal into the input of the gate it will still feed a clock signal in. Had you connected it to the output of the gate, it wouldn't be happy.
21  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 15, 2014, 06:18:37 pm
This was my *ugly* solution at the time:
Code:
    #ifdef _LIB_SAM_
        #define DEFAULT_DATA_TYPE unsigned int
        #define DEFAULT_MID_DATA_TYPE unsigned int
        #define DEFAULT_WIDE_DATA_TYPE unsigned int
        #define DEFAULT_SIGNED_DATA_TYPE signed int
        #define DEFAULT_MID_SIGNED_DATA_TYPE signed int
        #define DEFAULT_WIDE_SIGNED_DATA_TYPE signed int
    #else
        #define DEFAULT_DATA_TYPE unsigned char
        #define DEFAULT_MID_DATA_TYPE unsigned int
        #define DEFAULT_WIDE_DATA_TYPE unsigned long
        #define DEFAULT_SIGNED_DATA_TYPE signed char
        #define DEFAULT_MID_SIGNED_DATA_TYPE signed int
        #define DEFAULT_WIDE_SIGNED_DATA_TYPE signed long
    #endif
Then I just used those defines for data types, e.g.:
Code:
gLCD(DEFAULT_DATA_TYPE RS, DEFAULT_DATA_TYPE CS, DEFAULT_DATA_TYPE SCLK, DEFAULT_DATA_TYPE SDATA, SpeedMode speed = NORMAL_SPEED);
As I said, it looks *awful*. But myeh, it works.
22  Using Arduino / Microcontrollers / Re: Why 16 Mhz clock??? on: July 15, 2014, 06:17:04 pm
Because not all parts can run at 20MHz, and I believe way back when, the chosen ATMega was limited to 16MHz.
23  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 15, 2014, 04:24:08 pm
Looking at the resultant code, the uint24_t's are pretty good.
Which processor? AVR?
Or on a processor like pic32 or ARM that has actual 32 bit registers?
I'd be curious how it does on the processors with the larger registers.

--- bill

I'm talking about AVRs. Generally with 32bit machines the most efficient variable size is 32bits - take my Nokia 6100 LCD library, that was ported to the Due as well and it was stupidly slow until I changed all the bytes and chars to (unsigned) ints [i.e. 32bit] at which point it sped right up.
24  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 15, 2014, 12:55:10 pm
Looking at the resultant code, the uint24_t's are pretty good. Haven't done any speed tests on multiply/divide and such yet, but for addition/subtraction/general stuff, it comes out great - especially if you want 24bit colour (RGB) and don't want to waste time processing the extra byte.

You do have to add this to your sketch:
typedef __uint24 uint24_t;
typedef __int24 int24_t;
As they have slightly different names than usual. But after that it is fine.
25  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 15, 2014, 11:46:16 am
I like avr-gcc 4.8, it has a uint24_t and int24_t smiley-grin
26  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 14, 2014, 05:31:52 pm
Traditionally, the "int" type is the "natural" type for the processor - i.e., it's the size of the processor's registers.  This can be 4, 8, 16, 32... bits, but usually the processor can handle it as fast as possible.

Type "long" is for when you need more precision, even at the expense of slower code.  Type "byte" is good when you need to control the size, regardless of the processor's register size, or if memory space is at a premium.  In classic C, type "char" is usually 8 bits long and hence is used a a form of "byte".

Then there's type "short"....

Logically, many things would actually be well suited to type "bool" --- i.e., only two possible values.  I don't know how type "bool" is implemented, but I expect it takes one byte of storage per variable.

However with avr-gcc, and int isn't the native size for 8bit uCs, its 16bit (presumably because a byte was considered too small, not sure). A short is the same size as an int in this environment.
Also, a long doesn't really give more 'precision' per say, it gives a greater 'range'. All of the integer types have a precision of 1, hence they are integers.
Now if you were doing fixed point maths with them, then yes you could use greater precision, or greater range, or a bit of both.
27  Using Arduino / Programming Questions / Re: why do we use int instead of long on: July 13, 2014, 06:25:34 pm
Thanks for fast reply smiley
Then why dont we use byte ledPin=13; instead of int ledPin=13; ?

Because large parts of the Arduino core and many of the examples supplied are either badly written or hacked together without consideration for efficiency.
28  Using Arduino / Programming Questions / Re: Why arduino "ignore" % on: July 09, 2014, 10:36:16 am
The loop() will happen tens of thousands of times a second in that code, so unless you can see a light blinking that quickly, it will look like the light is always on.
Just to prove a point, add the following line at the end of your loop:
Code:
delay(100);
Is it doing what you expect now?
Try reducing the delay time. Notice what is happening?


As a side note, don't compare the return value of digitalRead() to 1 or 0, compare it to HIGH or LOW. For all you know, one day Arduino might make digitalRead() return 2 and your code won't work anymore, whereas if you use HIGH, it probably would still.
29  Using Arduino / General Electronics / Re: Circuit doesn't power off after disconnecting it's power supply on: July 07, 2014, 06:05:29 pm
The attached diagram might help.
30  Using Arduino / Microcontrollers / Re: Pin Change Interrupt during ISR on: July 07, 2014, 02:16:03 pm
That's fine then. Just wanted to make sure the pin change interrupt wasn't a strange one (or at least no more so than it already is).
Pages: 1 [2] 3 4 ... 116