Show Posts
Pages: 1 2 3 [4] 5 6 ... 116
46  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 22, 2014, 02:26:42 am
You probably have the wrong board selected:
This is in your .elf:
Code:
00000070 <port_to_mode_PGM>:
  70: 00 00 00 00 37 00 34 00 31 00                       ....7.4.1.

0000007a <port_to_output_PGM>:
  7a: 00 00 00 00 38 00 35 00 32 00                       ....8.5.2.

00000084 <port_to_input_PGM>:
  84: 00 00 00 00 36 00 33 00 30 00                       ....6.3.0.

This is what it should be:
Code:
00000070 <port_to_mode_PGM>:
  70: 00 00 3a 00 37 00 34 00 31 00 26 00                 ..:.7.4.1.&.

0000007c <port_to_output_PGM>:
  7c: 00 00 3b 00 38 00 35 00 32 00 27 00                 ..;.8.5.2.'.

00000088 <port_to_input_PGM>:
  88: 00 00 39 00 36 00 33 00 30 00 25 00                 ..9.6.3.0.%.
47  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 04:13:07 pm
You should see a line like this at the end of the verbose output:
Code:
C:\Users\Thomas\AppData\Local\Temp\build1687262069962928034.tmp\sketch_jun21a.cpp.elf C:\Users\Thomas\AppData\Local\Temp\build1687262069962928034.tmp\sketch_jun21a.cpp.hex
Binary sketch size: 106 bytes (of a 1,472 byte maximum)
The .elf file named there is the one.
48  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 01:06:23 pm
Its possible. If you compile the sketch with the 'Verbose output' option in the IDE preferences, it will show you the directory in which the compiled sketch is placed. In that folder there will be a ".elf" file. Could you upload that here.
49  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 11:57:58 am
Having read that instructable, it seems it is just the code I wrote a couple of years ago. I have no problem with it. The mapping is nothing really like the ATMega328, they are completely different microcontrollers. I based the numbering scheme on what was easiest to remember for the specific application that I was using at the time.
50  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 11:46:54 am
The mapping is in the pins_arduino.h file, you can change them in there if you want.
Code:
// ATMEL ATMEGA162 / ARDUINO
//
//                  +-\/-+
// PWM  (D 8) PB0  1|    |40  VCC
// PWM  (D 9) PB1  2|    |39  PA0 (D14)
//      (D19) PB2  3|    |38  PA1 (D15)
//      (D18) PB3  4|    |37  PA2 (D16)
// PWM  (D10) PB4  5|    |36  PA3 (D17)
//      (D11) PB5  6|    |35  PA4 (D31)
//      (D12) PB6  7|    |34  PA5 (D32)
//      (D13) PB7  8|    |33  PA6 (D33)
//            RST  9|    |32  PA7 (D34)
//      (D 0) PD0 10|    |31  PE0 (D28)
//      (D 1) PD1 11|    |30  PE1 (D29)
//      (D 2) PD2 12|    |29  PE2 (D30) PWM
//      (D 3) PD3 13|    |28  PC7 (D27)
// PWM  (D 5) PD4 14|    |27  PC6 (D26)
// PWM  (D 6) PD5 15|    |26  PC5 (D25)
//      (D 4) PD6 16|    |25  PC4 (D24)
//      (D 7) PD7 17|    |24  PC3 (D23)
//            XT2 18|    |23  PC2 (D22)
//            XT1 19|    |22  PC1 (D21)
//            GND 20|    |21  PC0 (D20)
//                  +----+
51  Using Arduino / Programming Questions / Re: can I call a function to replace a for statement? on: June 19, 2014, 05:36:56 pm
This would work:
Code:

#define count for (int i=0;i<4;i++)
    ...

     count
     {
       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red
     }

But it ranks highly on the list of the most AWFUL ways of coding anything. Stick to writing out the for loop.

(EDIT: Krupski beat me to it.)


What you could also do is this:
Code:
/*
3 shift registers in, 2 out.
 
 Drive 2 banks of 8 LED's with 2 banks of 8 switches (with 1 extra input bank added for future expansion).
 
 The circuit:
  Connected to 3 cd 4021's with the inputs via switches to 5v and with 10k resistors to 0v.
 Connected to 2 74LS595's with their outputs connected via resistors and LED's to 0v.
 
  Created 09.11.13
  By matelot
  Modified ------------
  By -----------
 set up CD4021 with
 pin1 - i/p 1      pin16 - vcc
 pin2 - n/c        pin15 - i/p 2     
 pin3 - data in    pin14 - i/p 3
 pin4 - i/p 5      pin13 - i/p 4
 pin5 - i/p 6      pin12 - n/c
 pin6 - i/p 7      pin11 - n/c
 pin7 - i/p 8      pin10 - clock
 pin8 - gnd         pin9  - ser/par switch (latch)
 bias inputs with resistors to gnd and put a switch on each input to vcc
 */
//connected to 2 cd4021's with the pin 11 on the first connected to pin 3 on the second.
const int latchPinin  =  5; //4021 pin 9
const int dataPinin   =  6;  //4021 pin 3
const int clockPinin  =  7; //4021 pin 10
/*
set up 74ls595 with
 pin1 - o/p 2      pin16 - vcc
 pin2 - o/p 3      pin15 - o/p 1     
 pin3 - o/p 4      pin14 - data i/p
 pin4 - o/p 5      pin13 - gnd
 pin5 - o/p 6      pin12 - latch
 pin6 - o/p 7      pin11 - clock
 pin7 - o/p 8      pin10 - vcc
 pin8 - gnd        pin9  - to pin 14 of next register
 connected to 2 47LS595's with the pin 9 on the first connected to pin 12 on the second.
 myinput bits are 1.WO1 2.WO2 3.WO3 4.WO4
 myoutput bits are 1.WO1red
 */
const int dataPinout  =  8;   //595 pin 14
const int latchPinout =  9;  //595 pin 11
const int clockPinout = 10; //595 pin 12
int myinput;
int myoutput = 136;
int myoutput2;
int a;
int i;
int shift1[] = {
  0,1,2,3};
int shift2[] = {
  4,5,6,7};
int shift3[] = {
  1,0,0,0};
int shift4[] = {
  0,1,0,0};
int shift5[] = {
  0,1,1,0};
int shift6[] = {
  0,0,0,1};

void setup()
{
  Serial.begin(9600);
  pinMode(dataPinout,  OUTPUT);
  pinMode(latchPinout, OUTPUT);
  pinMode(clockPinout, OUTPUT);
  pinMode(latchPinin,  OUTPUT);
  pinMode(clockPinin,  OUTPUT);
  pinMode(dataPinin,    INPUT);
}

void count(void (*fptr)(int, int), int* a, int* b)
{
  for (i=0;i<4;i++) {
    fptr(a[i],b[i]);
  }
}
void loop()
{
  delayMicroseconds  (20);
  digitalWrite       (clockPinin,HIGH);/*puts the clockpin high so the first
   bit is read before the register is clocked on*/
  digitalWrite       (latchPinin,1);  //set latch to 1 to collect parallel data
  delayMicroseconds  (20);                //pause to let digitalWrite finish
  digitalWrite       (latchPinin,0); //set latch to 0 to transmit data serially
  digitalWrite       (latchPinout, LOW);
  digitalWrite       (clockPinin,HIGH);
  shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput); //Send data
  //   shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput2); //Send data   
  digitalWrite       (latchPinout, HIGH);

  Serial.print    ("   ");       //sets a loop so the input can be bitread
  for (a=0;a<8;a++)              //so that leading 0's line up in the monitor
      Serial.print    (bitRead (myinput,(7-a))); //bit read the contents of input
  Serial.print    ("  ");
  Serial.println   (myinput);//print myinput in decimal

  myinput  = shiftIn (dataPinin, clockPinin, MSBFIRST);//read first register
  /*hct4021 register 1    bit 0 WO1       bit 1 WO2       bit 2 WO3        bit 3 WO4
   (myinput)     bit 4 TS12c     bit 5 TS12d     bit 6            bit 7
   74ls595   register 1    bit 0 WO1 red   bit 1 WO1 yel   bit 2 WO1 yel2   bit 3 WO1 grn
   (myoutput)    bit 4 WO2 red   bit 5 WO2 yel   bit 6 WO2 yel2   bit 7 WO2 grn
   74ls595   register 2    bit 0 WO3 red   bit 1 WO3 yel   bit 2 WO3 grn    bit 3 WO4 red
   (myoutput2)   bit 4 WO4 grn   bit 5 21  red   bit 6 21  yel    bit 7 21  grn
   
   */
  if (bitRead(myinput,0) == 1)//read bit WO1
  {
    count(&output,shift1,shift3);//WO1 to red
  }
  if (bitRead(myinput,1) == 1)//read bit WO2
  {
    for (i=0;i<4;i++)
    {
      output(shift1[i],shift4[i]);//WO1 to yel
      output(shift2[i],shift3[i]);//WO2 to red
    }
  }
  if (bitRead(myinput,2) == 1)//read bit WO3
  {
    if (bitRead(myoutput,1) == 1)//is WO1 yel
    {
      count(&output,shift1,shift5);//WO1 to yel yel
    }
    count(output,shift2,shift4);//WO2 to yel
    count(output2,shift1,shift3);//WO3 to red
  }
  if (bitRead(myinput,3) == 1)//read bit WO4
  {
    if (bitRead(myoutput,2) == 1)//is WO1 yel yel
    {     
      count(&output,shift1,shift6);//WO1 to grn
    }
    if (bitRead(myoutput,5) == 1)//is WO2 yel
    {
      count(&output,shift2,shift5);//WO2 to yel yel
    }
    count(&output2,shift1,shift4);//WO3 to yel
    count(&output2,shift1,shift6);//WO4 red
  }

  //     for (i=0;i<4;i++)
  //     {
  //       output(shift2[i],shift5[i]);//WO2 to yel yel
  //       output(shift1[i],shift4[i]);
  //     }

}
void output(int first,int second)//do void output using the first array in the
//instruction above(shift1[bit i])wherever the variable first is below
//and then the second array in the instruction above (shift2[bit i])
//wherever second appears below.
{
  bitWrite(myoutput,first,second);//fill array (myoutput,bit,high or low)
  Serial.print (first);
  Serial.print ("  ");
  Serial.println (second);
  delay(5);
  //    bitWrite(myoutput,1,0);//put WO1 yel  low
  //    bitWrite(myoutput,2,0);//put WO1 yel2 low
  //    bitWrite(myoutput,3,0);//put WO1 grn  low     
}
void output2(int first,int second)//do void output using the first array in the
//instruction above(shift1[bit i])wherever the variable first is below
//and then the second array in the instruction above (shift2[bit i])
//wherever second appears below.
{
  bitWrite(myoutput2,first,second);//fill array (myoutput,bit,high or low)
  Serial.print (first);
  Serial.print ("  ");
  Serial.println (second);
  delay(5);     
}
Though that would be far far far less efficient that just using a for loop properly.
52  Using Arduino / Microcontrollers / Re: [Arduino UNO] How to disable pullup resistor ?? on: June 17, 2014, 07:59:45 pm
If you want to disable ALL internal pullups:
Code:
MCUSR |= _BV(PUD);
To enable again:
Code:
MCUSR &= ~_BV(PUD);


If they are external pullups, instead of connecting them to +5v, connect them all to one of the Arduino pins and use digitalWrite() to control them.
53  Using Arduino / Microcontrollers / Re: Interval time between 2 consecutive commands. on: June 12, 2014, 03:48:20 am
Code:
if (millis()<1000){
counter +=1;
}
That will keep counting as fast as possible while millis() is less than 1000, i.e. during the first 1000milliseconds of running. Once millis() is larger than that, it will never increment counter again.
In one second of operation, there could be tens of thousands of increments depending on how many instructions it takes to get through the loop() and check millis(). [EDIT: 97610 on my Arduino Mega]
54  Using Arduino / General Electronics / Re: How to distinguish between ‘reset’ and ‘real power loss’ ? on: June 12, 2014, 03:37:31 am
No, it's a hardware feature of the chip.
Partly, but it is also thanks to some of the abstraction from avr-libc.

This is accessing a register without that:
Code:
byte value;
asm volatile ("in %0, 0x34 \n\t" :"=r" (value) );
print("MCUSR = ");
println(value);
or possibly:
Code:
volatile byte* ptr;
ptr = (volatile byte*)0x54; //note adding 0x20 as this treats them as mapped into SRAM.
byte value = *ptr;
print("MCUSR = ");
println(value);
vs. this with the abstraction:
Code:
byte value = MCUSR;
print("MCUSR = ");
println(value);
55  Using Arduino / General Electronics / Re: How to distinguish between ‘reset’ and ‘real power loss’ ? on: June 11, 2014, 05:07:48 pm
This would be a more sensible (and functional) code:
Code:
void setup() {
  Serial.begin(38400);

  Serial.print("MCUSR: ");
  Serial.println(MCUSR);
  
  if (MCUSR & _BV(EXTRF)){
      // Reset button or otherwise some software reset
      Serial.println("Reset button was pressed.");
  }
  if (MCUSR & (_BV(BORF) | _BV(PORF))){
       // Brownout or Power On
       Serial.println("Power loss occured!");
  }
  if (MCUSR & _BV(WDRF)){
       //Watchdog Reset
       Serial.println("Watchdog Reset");
  }
  // Clear all MCUSR registers immediately for 'next use'
  MCUSR = 0;
}


The case statement you had would either work if ONLY and external reset occurred, or if ALL 3 of brownout, power on and reset occurred.
The above code filters the events and detects correctly if either an external reset occurred, or if ANY of brownout or power on occurred. Plus if you press the reset button before it gets to the point of clearing MCUSR, it will allow you to see that too.

Granted you still have the problem of optiboot. Almost worth building a standalone ATMega circuit and using ISP to program it (no bootloader). The bootloader would delay the execution of the reset check code anyway.
56  Using Arduino / General Electronics / Re: New Grad working in Industry, want to use MCU's to collect and send 24v DC on: June 11, 2014, 04:59:48 pm
The simple voltage divider circuit could handle that (peak current ~4 A, typical ~1-2)

You serious??? 2+A through a simple voltage divider? From 24V down to 5V, that would be 38W wasted giving an efficiency of 20%.
57  Using Arduino / Microcontrollers / Re: ATTiny87 core? on: June 11, 2014, 12:17:12 pm
You could go for the ATTiny167, which is supported. It is basically the same as the 87, but with more flash.

The only thing that doesn't support the 167 directly is avrdude, though if you add the lines from the attached file into your avrdude.conf file, it will work fine.
58  International / Generale / Re: Aggiungere una MCU compatibile all'IDE on: June 09, 2014, 06:20:07 pm
Please try the patched version:
https://github.com/TCWORLD/ATTinyCore/issues/16
59  Using Arduino / Programming Questions / Re: TM1638 and the keyboard library, how do I assign keystrokes to a HEX/BIN number? on: June 09, 2014, 04:47:10 am
Hex, Binary, Decimal, Octal. In the end they all compile to the same, there is no difference as far as the processor is concerned.

You will have to be more specific and specify what you want the numbers to look like.
60  Using Arduino / Programming Questions / Re: Cheap and cheerful way to save power? on: June 08, 2014, 03:54:03 am
The best power saving technique, however, is to lower the clock below 16MHz if the design will permit.  In fact, running at 1 MHz is often adequate.
If you are doing something really simple that needs to be ultra low power, you can even run it at 16kHz using the watchdog oscillator and 1/8 prescaler!

As has been said, there is so much other 'stuff' (e.g. usb-serial, power led, regulator, etc.) on an Arduino that there is almost no point sleeping the ATMega chip to save power, it would be like trying to use a pebble to slow a Niagara falls.
For a standalone chip in a carefully designed circuit, the savings are however quite substantial. I've built a dice circuit with a few 1M resistors and an ultra low power op-amp. When running the whole thing draws 1mA (including the LEDS). When the CPU is put into power down mode (to be woken by the op-amp), the whole circuit draws 2uA from a battery - less than the self discharge rate of the coin cell I am using.

The sleep idea can be quite useful for other things than just power saving though. You can use it as part of your program. Imagine that you want to blink an LED, you could toggle the pin and the have a delay(), or you could have a timer running which wakes the CPU at certain intervals and toggles the LED in the interrupt.
Pages: 1 2 3 [4] 5 6 ... 116