-
Small progress: switch interface
03/19/2016 at 08:26 • 0 commentsI just added one switch, which changes LED blinking interval by the following source...
.def R_TEMP1 =R17 .def R_TEMP2 =R18 .def R_TEMP3 =R19 .def R_TEMP4 =R20 .def sw_cond =R21 .equ led_p = 3 .equ sw_in = 4 .equ SUB_COUNT = 8 RJMP MAIN MAIN: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LDI R_TEMP1, 4 ; prescaler of timer0 setting OUT TCCR0B, R_TEMP1; clock of CPU divided by 5= 1024,4=256,3=64 ; real frequency of blink will be (MPU clock)/256/precsaler value ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LDI R_TEMP1, 1<<PUD OUT MCUCR, R_TEMP1 LDI R_TEMP1, (1<<led_p) ;seting PORTB for output OUT DDRB, R_TEMP1;seting led_p for output LDI R_TEMP2, (1<<led_p)|(1<<sw_in);led_p=PB3 on, sw_in=pullup OUT PORTB, R_TEMP2 LOOPPOINT: SBI PINB, led_p; flip led on and off IN R_TEMP3, PINB ; reading state of portb ANDI R_TEMP3, (1<<sw_in) ; checking sw status BRNE ANOTHER; if sw_in not equal 1, jump to ANOTHER LDI sw_cond, 0; putting sw_cond to zero if sw_in is zero RJMP POINT ANOTHER: LDI sw_cond, 1; putting sw_cond to 1 if sw_in is one POINT: RCALL DELAY2 SBI PINB, led_p ; flip led on and off RCALL DELAY2; calling subroutine delay RJMP LOOPPOINT ;jump to LOOPPOINT DELAY2: ; subroutine DELAY LDI R_TEMP4, 1 ANDI sw_cond, 1 ; checking current sw condition BRNE COUNTER2 ; if switch is zero, jump to COUNTER2 LDI R_TEMP4, SUB_COUNT COUNTER2: IN R_TEMP1, TIFR0; loading current state (TIFR0) of TIMER0 ANDI R_TEMP1, 1<<TOV0; checking overflow flag TOV0 BREQ COUNTER2 ; if counter is not overflow, jump to COUNTER2 LDI R_TEMP1, 1<<TOV0 ; writing 1 to TOV0 leads to reset of TIMER0 OUT TIFR0, R_TEMP1 LDI R_TEMP3, 1 SUB R_TEMP4, R_TEMP3 ; decrease 1 to SUB_COUNT number BRNE COUNTER2 ; if count by sub_count is finished, ret,else to counter2 RET
This source provides 227 bytes .hex file. Still I have much room for programming on ATtiny13..
-
Initial test: How Assembly, C, Arduino code are working?
03/13/2016 at 07:26 • 0 commentsI've wanted to make "LED blink" by Assembly, C, and Arduino IDE and compare them, and see how they are working. Here is a result.
Firstly I made a simple Assembly code of LED blinking connecting PB0 and PB1 with LEDs in Atmel Studio. As a initial test, I did not use timer and its internal interrupt. Delay function is made by consuming time by 256x256 step empty loop.
.def R_TEMP1 =R17 .def R_TEMP2 =R18 .def R_TEMP3 =R19 .def R_TEMP4 =R20 .equ COUNT_START = 0xFF;256 .equ COUNT_END = 0x01; counter end in delay loop .equ SUB_COUNT = 0xFF;256 .equ SUBNUM = 0x01; subtract number in delay roop .org 0x0010 RJMP MAIN MAIN: LDI R_TEMP1, 0B11111111 ;seting PORTB for output OUT DDRB, R_TEMP1;seting PORTB for output LDI R_TEMP2, 0B00001000 ;PB3=1(ON), rest are 0(OFF) OUT PORTB, R_TEMP2;PB3=1(ON), rest are 0(OFF) LOOPPOINT: LDI R_TEMP2, 0B00001000 OUT PORTB, R_TEMP2 ; setting PB3=1 (ON) RCALL DELAY; calling subroutine delay LDI R_TEMP2, 0B00010000; setting PB4=1 (ON), rest are off OUT PORTB, R_TEMP2; setting PB4=1 (ON), rest are off RCALL DELAY; calling subroutine delay RJMP LOOPPOINT ;jump to LOOPPOINT DELAY: ; subroutine DELAY LDI R_TEMP4, SUB_COUNT SUB_COUNT1: LDI R_TEMP3, COUNT_END LDI R_TEMP1, COUNT_START LDI R_TEMP2, SUBNUM COUNTER: SUB R_TEMP1, R_TEMP2 CPSE R_TEMP1, R_TEMP3 RJMP COUNTER SUB R_TEMP4, R_TEMP2 CPSE R_TEMP4, R_TEMP3 RJMP SUB_COUNT1 RET
This leads to 161 bytes .hex file.
Let's see next, by C by Atmel Studio C,
#include <avr/io.h> #include <stdlib.h> #include <util/delay.h> int main(void) { unsigned int delay = 100; // Set up Port B pin 3 and 4 mode to output DDRB = 0b00011000; // Set up Port B for initial state (PB4=1, rest are 0) PORTB = 0b00010000; while (1) { // Toggle Port B pin 3 and 4 PORTB ^= 1<<PB4; PORTB ^= 1<<PB3; // delay of delay ms _delay_ms (delay); } }
I am not sure how it realises delay function (should be by timer) but the .hex file reads 255 bytes. We cannot see the bottom of Attiny by C ( we can, but if we use prepared function), but code side is not so big, indeed.Now let's see the case of Arduino..
void setup() { pinMode(3, OUTPUT); pinMode(4, OUTPUT); } void loop() { digitalWrite(3, LOW); digitalWrite(4, HIGH); delay(1000); digitalWrite(3, HIGH); digitalWrite(4, LOW); delay(1000); }
We have no mysterious part (like DDRB and PORTB) and surely very easy to read. But in return, the produced .hex file size is.... 2929 bytes!!!I've thought Assembly will be the best for small binary production but indeed C is comparable. But making Assembly code teach me a lost of basis, like DDRB and register usage (which never appears in Arduino, since those setting is done by simple pinMode()).I will go forward, step by step.... stay tune!