Nugroho's blog.

Thursday, May 21, 2015

Serial Communication ATTiny13A (#1)

ATTiny didn't come with TX/RX Pins, so I have to make it by myself. 

Got it from http://www.bot-thoughts.com/2012/12/attiny-software-serial-tx-in-assembly.html
Slightly modify it because avra didn’t compile it original source

.include "../tn13Adef.inc" ; definitions for ATtiny13A
.cseg
.org 0x00
rjmp start ; executed after reset

start:
    ldi r16, 0x6D ; 'm' 
    ldi r17, 8 ; put 8 in counter register
txloop:
    lsr r16 ; shift off the next bit, LSB first
    brcs Send1 ; if it is 1 (C=1) then send 1
Send0:
    cbi PORTB, PB0 ; send a 0=low
    rjmp BitDone
Send1:
    sbi PORTB, PB0 ; send a 1=high
BitDone:
    dec r17 ; bitcnt--
    breq start ; if bitcnt == 0, start over
    rjmp txloop ; else do the next bit


Build, compile and upload it

Nugrohos-MacBook-Air:serial nugroho$ avra -o serialTX.hex serialTX.s 

AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)
Copyright (C) 1998-2010. Check out README file for more info

AVRA is an open source assembler for Atmel AVR microcontroller family
It can be used as a replacement of 'AVRASM32.EXE' the original assembler
shipped with AVR Studio. We do not guarantee full compatibility for avra.

AVRA comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of avra under the terms
of the GNU General Public License.
For more information about these matters, see the files named COPYING.

Pass 1...
Pass 2...
done

Used memory blocks:
Code : Start = 0x0000, End = 0x000A, Length = 0x000B

Assembly complete with no errors.
Segment usage:
Code : 11 words (22 bytes)
Data : 0 bytes
EEPROM : 0 bytes
Nugrohos-MacBook-Air:serial nugroho$

Nugrohos-MacBook-Air:serial nugroho$ ls
serialTX.s serialTX.s.eep.hex serialTX.s.obj
serialTX.s.cof serialTX.s.hex serialTX1.s
Nugrohos-MacBook-Air:serial nugroho$ avrdude -p t13 -c usbasp -P usb -B4 -n

avrdude: set SCK frequency to 187500 Hz
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done. Thank you.

Nugrohos-MacBook-Air:serial nugroho$ avrdude -p t13 -c usbasp -P usb -B4 -U flash:w:serialTX.s.hex

avrdude: set SCK frequency to 187500 Hz
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: set SCK frequency to 187500 Hz
avrdude: reading input file "serialTX.s.hex"
avrdude: input file serialTX.s.hex auto detected as Intel Hex
avrdude: writing flash (22 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 22 bytes of flash written
avrdude: verifying flash memory against serialTX.s.hex:
avrdude: load data flash data from input file serialTX.s.hex:
avrdude: input file serialTX.s.hex auto detected as Intel Hex
avrdude: input file serialTX.s.hex contains 22 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: 22 bytes of flash verified

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done. Thank you.

Nugrohos-MacBook-Air:serial nugroho$





I use USB-to-TTL, and after several fail attempt to compile and upload using usbasp, I’m getting annoyed, so I use hc-05 (a bluetooth module) to communicate with ATTiny. Just connect the RX pin to PB0.

Nugrohos-MacBook-Air:serial nugroho$ screen /dev/cu.HC-05-DevB 
avr


modify it 

note that 

L1:     dec  r20
        brne L1

will generate error
Pass 1...
Abort trap: 6

edit it to 
L1:     
        dec  r20
        brne L1

not sure why avra refuse the first formatting

(of course the code above still won't work, it lacks the baud rate :) )


Akhirnya ...


Ujian pertama KBK Elektronika dan Instrumentasi Fisika
Sangat langka memang, dan dapat hadiah uang dari P Samsul, :) 





Wednesday, May 20, 2015

ADC on ATTiny13A with PWM Output in Assembler

Yep, here we are. I use pin B3 as ADC input, and B0 as output. The delay's vary; depend on r16 value, which is ADC value. Since the output's in PWM mode, if we connect LED to B0, the brightness is vary too.

.include "../tn13Adef.INC" 

.org $0000 
init:                       ;prescaler 50-200KHz, sys clock 1.2MHz
    ldi   r16,0b11100011    ;[ADEN(ENABLE),ADSC(START),ADATE,ADIF,ADIE,ADPS2,ADPS1,ADPS0]
    out   ADCSRA,r16        ;start adc
    ldi   r16,0b00000011    ;r16 = 3
    out   ADMUX,r16         ;select channel 3 (PORT B3)
    sbi   PORTB,PORTB3      ;enable pullup
    sbi   DDRB,0            ;set PORT B0 as output
main:
    in    r16,ADCL         
    in    r17,ADCH        
    rcall delay          
    sbi   PINB,0         
    rjmp  main

delay: 
    dec r16
    brne delay

    ret


PWM (Pulse-Width Modulation) di AVR memungkinkan output di AVR ATTiny13 berkelakuan seperti analog, dengan resolusi 256 atau, jika dihubungkan ke LED, memiliki 256 tingkat ke-terang-an (redup-terang).

Program diatas menggunakan ADC di port B3 untuk mengontrol tingkat terang-redup LED di port B0



Full Rest, Tepar ...


Tidur jam 5 sore sampai jam 5 pagi.

Setelah melewati tiga ujian skripsi, satu ujian pra-skripsi dan mencari jarum dalam jerami versi sensor digital 40-bit untuk mikrokontroler.

12 jam tidur tanpa mimpi.

Segar lagi, :)

Tuesday, May 19, 2015

nDesa


(berangkat ngampus ingat lagu ini, sering diputar di TVRI semasa SD) 

Hei aku anak desa
yang selalu gembira
tak kenal putus asa
slalu cerah ceria
 
Di desa ku tertawa
Di desa ku gembira
Di desa ku berkarya
untuk cita-citaku

Hidup damai ayah bunda bersamaku
Pastikan burung terbang dilangitku

Hutan pantai sawah sungai sahabatku
Aku senang ku bangga di desaku

(lirik belum tentu benar, tapi itu yang saya ingat)

Sunday, May 17, 2015

Bumpy Lemans

Lorenzo, Rossi, Dovi, and crazy battle for 4th position.

Reset clock fuse bit on AVR

My ATTiny13A LED’s still blinking, but I can’t program it anymore.



Here's the message

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -n

avrdude: error: programm enable: target doesn't answer. 1
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.


avrdude done. Thank you.

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -n -F

avrdude: error: programm enable: target doesn't answer. 1
avrdude: initialization failed, rc=-1
avrdude: AVR device initialized and ready to accept instructions
avrdude: Device signature = 0x000000 (retrying)
avrdude: Device signature = 0x000000 (retrying)
avrdude: Device signature = 0x000000
avrdude: Yikes! Invalid device signature.
avrdude: Expected signature for ATtiny13 is 1E 90 07

avrdude done. Thank you.

Nugrohos-MacBook-Air:~ nugroho$

It can still be programmed using -B4 option however

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -B4 -n  

avrdude: set SCK frequency to 187500 Hz
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done. Thank you.

Nugrohos-MacBook-Air:~ nugroho$


From evilmadscientist.com, it apparently my ATTiny clock is slower than my USBASP. We have to erase the chip and reset the clock.

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -tuF   

avrdude: error: programm enable: target doesn't answer. 1
avrdude: initialization failed, rc=-1
avrdude: AVR device initialized and ready to accept instructions
avrdude: Device signature = 0x000000 (retrying)
avrdude: Device signature = 0x000000 (retrying)
avrdude: Device signature = 0x000000
avrdude: Yikes! Invalid device signature.
avrdude: Expected signature for ATtiny13 is 1E 90 07
avrdude> sck 1000
>>> sck 1000
avrdude: set SCK frequency to 1000 Hz
avrdude> e
>>> e
avrdude: erasing chip
avrdude: error: programm enable: target doesn't answer. 1
avrdude> sck 10
>>> sck 10
avrdude: set SCK frequency to 93750 Hz
avrdude> quit
>>> quit

avrdude done. Thank you.

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -n

avrdude: error: programm enable: target doesn't answer. 1
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.


avrdude done. Thank you.

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -B4 -n

avrdude: set SCK frequency to 187500 Hz
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done. Thank you.

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -B4 -tuF

avrdude: set SCK frequency to 187500 Hz
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007
avrdude> sck 1000
>>> sck 1000
avrdude: set SCK frequency to 1000 Hz
avrdude> e
>>> e
avrdude: erasing chip
avrdude: set SCK frequency to 187500 Hz
avrdude> quit
>>> quit

avrdude done. Thank you.

Nugrohos-MacBook-Air:~ nugroho$

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -c usbasp -P usb -U lfuse:w:0x6A:m -U hfuse:w:0xFF:m -B250



avrdude: set SCK frequency to 4000 Hz

avrdude: AVR device initialized and ready to accept instructions



Reading | ################################################## | 100% 0.03s



avrdude: Device signature = 0x1e9007

avrdude: reading input file "0x6A"

avrdude: writing lfuse (1 bytes):



Writing | ################################################## | 100% 0.01s



avrdude: 1 bytes of lfuse written

avrdude: verifying lfuse memory against 0x6A:

avrdude: load data lfuse data from input file 0x6A:

avrdude: input file 0x6A contains 1 bytes

avrdude: reading on-chip lfuse data:



Reading | ################################################## | 100% 0.01s



avrdude: verifying ...

avrdude: 1 bytes of lfuse verified

avrdude: reading input file "0xFF"

avrdude: writing hfuse (1 bytes):



Writing | ################################################## | 100% 0.01s



avrdude: 1 bytes of hfuse written

avrdude: verifying hfuse memory against 0xFF:

avrdude: load data hfuse data from input file 0xFF:

avrdude: input file 0xFF contains 1 bytes

avrdude: reading on-chip hfuse data:



Reading | ################################################## | 100% 0.01s



avrdude: verifying ...

avrdude: 1 bytes of hfuse verified



avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)



avrdude done. Thank you.



Nugrohos-MacBook-Air:~ nugroho$

...

It’s bit different than on http://www.evilmadscientist.com/2007/fixing-a-bad-frequency-fuse-bit-on-an-avr/ because the Attiny13A clock automatically back to 187500 without manual set.



########


Untuk “menghidupkan” lagi AVR yang bermasalah dengan fuse-bit (mungkin kita tidak sengaja mensetting clock terlalu lambat, lebih lambat dari clock USBASP atau ISP kita sehingga program tidak dapat diupload), kita harus menghapus program yang terupload di AVR.

Gunakan avrdude dalam mode interaktif seperti di atas

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)