ARM assembly를 좋아하지 않을 수 있을리가...



	// Get Greatest Common Divider (GCD)
	// using Euclid's algorithm
	//
	// while (a != b) {
	//	if (a > b)
	//		a = a - b;
	//	else
	//		b = b - a;
	// }

	.global	example8_5
	.global	gcd

example8_5:
gcd:
	cmp  	r0, r1
	subgt	r0, r1			// if r0 > r1, r0 -= r1
	sublt	r1, r0			// if r0 < r1, r1 -= r0
	bne	gcd

	bx  	lr			// if r0 == r1, return

	.end



Posted by 쿨한넘
book/Make: AVR Programming2014. 7. 29. 01:48

책에서 구현한 방법의 원리를 짚고 넘어가자.

은박지로 capacitive sensor를 만들었다. 책에서는 그라운드에 연결되는 면은 크게, mcu의 입력이 되는 면은 작게 만들라해서 일단 그렇게. 중간엔 종이를 넣었다. 이면지 활용인데, 2009년도 영수증이네.



아래 도면과 같이 atmega328p에 연결. 100K ohm 대신 20K 사용.



코드는,



/*
   Capacitive touch sensor demo
*/

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include "pinDefines.h"
#include "USART.h"


#define SENSE_TIME   25
#define THRESHOLD    8000


// -------  Global Variables ---------- //

volatile uint16_t chargeCycleCount;


// ------- Functions -------- //

void initPinChangeInterrupt(void)
{
	PCICR |= (1 << PCIE1);	/* enable Pin-change interrupts 1 (bank C) */
	PCMSK1 |= (1 << PC1);	/* enable specific interrupt for our pin PC1 */
}


ISR (PCINT1_vect)
{
	chargeCycleCount++;	/* count this change */

	CAP_SENSOR_DDR |= (1 << CAP_SENSOR);	/* output mode */
	_delay_us(1);				/* charging delay */

	CAP_SENSOR_DDR &= ~(1 << CAP_SENSOR);	/* set as input */
	PCIFR |= (1 << PCIF1);			/* clear the pin-change interrupt */
}


int main(void)
{
	// -------- Inits --------- //
	clock_prescale_set(clock_div_1);	/* full speed */
	initUSART();
	printString("==[ Cap Sensor ]==\r\n\r\n");


	LED_DDR = 0xff;
	MCUCR |= (1 << PUD);			/* disable all pull-ups */
	CAP_SENSOR_PORT |= (1 << CAP_SENSOR);	/* we can leave output high */

	initPinChangeInterrupt();


	// ------ Event loop ------ //
	while (1) {

		chargeCycleCount = 0;		/* reset counter */
		CAP_SENSOR_DDR |= (1 << CAP_SENSOR);	/* start with cap charged */

		sei();				/* start up interrupts, counting */
		_delay_ms(SENSE_TIME);
		cli();				/* done */

		if (chargeCycleCount < THRESHOLD) {
			LED_PORT = 0xff;
		} else {
			LED_PORT = 0;
		}

		printWord(chargeCycleCount);	/* for fine tuning */
		printString("\r\n");

	}	/* End event loop */

	return (0);				/* This line is never reached */
}


한마디로 요약하자면, SENSE_TIME 동안 센서의 캐패시터에 충전하고 방전시켜 그 사이클 수를 측정하여 터치를 인식. 터치가 되면 캐패시턴스가 증가하여, 방전시간이 길어져, 사이클 수가 줄어든다.

책에선 SENSE_TIME 은 50을, THRESHOLD 는 12000 을 지정했고, 운 좋게도 이 값으로 동작을 했다. 센서의 캐패시턴스 값, 연결하는 저항의 값에 따라서 THRESHOLD 값은 조정해야 한다. 빠른 인식을 원해서 나는 SENSE_TIME을 25ms 로 줄였고, 이에 맞춰 THRESHOLD 값도 변경하였다.




PC1 핀을 스코프로 들여다 봤다. 위 사진은 터치가 되지 않았을 경우, 아래 사진은 터치를 했을 경우다. 이 센서의 경우 무엇으로도 터치가 되었고, 터치되는 면적이 넓을수록 캐패시턴스가 증가하는 것 같다.

Posted by 쿨한넘




--
--	VHDL을 이용한 FPGA 설계
--
--	3장. Section08. '1' 개수 카운터
--
library ieee;
use ieee.std_logic_1164.all;


entity OneCounter is

	port (
		d		:	in		std_logic_vector (7 downto 0);
		seg		:	out		std_logic_vector (6 downto 0)
	);
	
end OneCounter;


architecture arc of OneCounter is

    -- convert integer value to drive 7-segment
    --
    function toSeg(
        in_value    :   in  integer range 0 to 15
    ) return std_logic_vector is
    begin
     
        case in_value is
         
            when 0      =>   return "1000000";
            when 1      =>   return "1111001";
            when 2      =>   return "0100100";
            when 3      =>   return "0110000";
            when 4      =>   return "0011001";
            when 5      =>   return "0010010";
            when 6      =>   return "0000010";
            when 7      =>   return "1011000";
            when 8      =>   return "0000000";
            when 9      =>   return "0011000";
            when 10     =>   return "0001000";
            when 11     =>   return "0000011";
            when 12     =>   return "0100111";
            when 13     =>   return "0100001";
            when 14     =>   return "0000110";
            when 15     =>   return "0001110";
            when others =>   return "1111111";
 
        end case;
             
    end toSeg;

begin


	process (d)

		variable oneCount	:	integer;
		
	begin
	
	
		oneCount	:=	0;
		
		for i in d'range loop
		
			if d(i) = '1' then
				oneCount	:=	oneCount + 1;
			end if;
			
		end loop;
		
		seg		<=	toSeg(oneCount);


	end process;


end arc;



Posted by 쿨한넘

de2-115 의 I/O 에 맞게 구현.




--
--	VHDL을 이용한 FPGA 설계
--	3장. section07. n비트 가산 / 감산기
--		DE2-117 구현
--
library ieee;
use ieee.std_logic_1164.all;

package my_package is

	constant	adder_width		:	integer := 4;
	constant	result_width	:	integer := 5;
	constant	max_result		:	integer := 2**result_width - 1;
	constant	min_result		:	integer := -2**result_width;
	
	subtype		adder_value		is	integer range 0 to 2**adder_width - 1;
	subtype		result_value	is	integer range min_result to max_result;
	
end my_package;


library ieee;
use ieee.std_logic_1164.all;
use work.my_package.all;

entity nbitAddSub is

	port (
		a, b		:	in		adder_value;
		mode		:	in		std_logic;
		sega		:	out		std_logic_vector (6 downto 0);	-- hex6 of de2-117
		segb		:	out		std_logic_vector (6 downto 0);	-- hex4 of de2-117
		segmode		:	out		std_logic_vector (6 downto 0);	-- hex3 of de2-117
		segsign		:	out		std_logic;						-- hex2 of de2-117
		seg1		:	out		std_logic_vector (6 downto 0);	-- hex0 of de2-117
		seg2		:	out		std_logic_vector (6 downto 0);	-- hex1 of de2-117
		underflow	:	out		std_logic;
		overflow	:	out		std_logic
	);
	
end nbitAddSub;


architecture arc of nbitAddSub is

	signal	over, under	:	std_logic;
	signal	seg1Reg, seg2Reg	:	integer range 0 to 15;


	-- convert integer value to drive 7-segment
	--
	function toSeg(
		in_value	:	in	integer range 0 to 15
	) return std_logic_vector is
	begin
	
		case in_value is
		
			when 0	 	=>	return "1000000";
			when 1		=>	return "1111001";
			when 2		=>	return "0100100";
			when 3		=>	return "0110000";
			when 4		=>	return "0011001";
			when 5		=>	return "0010010";
			when 6		=>	return "0000010";
			when 7		=>	return "1011000";
			when 8		=>	return "0000000";
			when 9		=>	return "0011000";
			when 10		=>	return "0001000";
			when 11		=>	return "0000011";
			when 12		=>	return "0100111";
			when 13		=>	return "0100001";
			when 14		=>	return "0000110";
			when 15		=>	return "0001110";
			when others	=>	return "1111111";

		end case;
			
	end toSeg;

begin


	process (mode, a, b, over, under)
		variable res	:	result_value;
	begin

		if (mode = '1') then		-- if key0 is not pressed
			res		:=	a + b;
			segmode	<=	toSeg(10);	-- display 'A' for mode add
		else
			res		:=	a - b;
			segmode	<=	toSeg(5);	--	display 'S' for mode subtraction
		end if;

	
		if (res > max_result) then
			over	<=	'1';
		else
			over	<=	'0';
		end if;


		if (res < min_result) then
			under	<=	'1';
		else
			under	<=	'0';
		end if;


		if (res < 0) then
			segsign	<=	'0';		-- turn on minus sign
			seg2Reg	<=	-res / 16;
			seg1Reg	<=	-res mod 16;
		else
			segsign	<=	'1';		-- turn off minus sign
			seg2Reg	<=	res / 16;
			seg1Reg	<=	res mod 16;
		end if;

		overflow	<=	over;
		underflow	<=	under;

	end process;


	sega	<=	toSeg(a);
	segb	<=	toSeg(b);
	seg1	<=	toSeg(seg1Reg);
	seg2	<=	toSeg(seg2Reg);

	
end arc;



'book > VHDL을 이용한 FPGA 디지털 설계' 카테고리의 다른 글

'1' 개수 카운터  (0) 2014.06.25
lab06. 수의 정렬회로 설계  (0) 2014.06.25
7-segment decoder  (0) 2014.06.05
xor을 이용한 전감산기 설계  (0) 2014.06.04
xor을 이용한 전가산기 설계  (0) 2014.06.04
Posted by 쿨한넘

...




--
-- VDHL을 이용한 FPGA 설계
--
-- 3장 Lab06. 수의 정렬 회로 설계
--
--	de2-115 로 구현.
--		input a 	=>	SW17~SW14
--		input b 	=>	SW3~SW0
--		value a		=>	HEX6
--		value b 	=>	HEX4
--		value max 	=>	HEX3
--		value min 	=>	HEX0
--		input ena	=>	KEY0
--
package my_package is

	constant input_width	:	integer := 4;
	constant output_width	:	integer := 4;
	
	subtype input_value		is integer range 0 to 2**input_width - 1;
	subtype output_value	is integer range 0 to 2**output_width - 1;
	
end my_package;


library ieee;
use ieee.std_logic_1164.all;
use work.my_package.all;


entity lab06 is

	port (
		a		:	in		input_value;
		b		:	in		input_value;
		ena		:	in		std_logic;

		led_a	:	out		std_logic_vector (6 downto 0);
		led_b	:	out		std_logic_vector (6 downto 0);
		
		led_max	:	out		std_logic_vector (6 downto 0);
		led_min	:	out		std_logic_vector (6 downto 0)

	);
	
end lab06;


architecture arc of lab06 is

	signal	max		:	output_value;
	signal	min		:	output_value;

	-- convert integer value to drive 7-segment
	--
	function to_hex(
		in_value	:	in	input_value
	) return std_logic_vector is
	begin
	
		case in_value is
		
			when 0	 	=>	return "1000000";
			when 1		=>	return "1111001";
			when 2		=>	return "0100100";
			when 3		=>	return "0110000";
			when 4		=>	return "0011001";
			when 5		=>	return "0010010";
			when 6		=>	return "0000010";
			when 7		=>	return "1011000";
			when 8		=>	return "0000000";
			when 9		=>	return "0011000";
			when 10		=>	return "0001000";
			when 11		=>	return "0000011";
			when 12		=>	return "0100111";
			when 13		=>	return "0100001";
			when 14		=>	return "0000110";
			when 15		=>	return "0001110";
			when others	=>	return "1111111";

		end case;
			
	end to_hex;
		
begin


	led_a	<=	to_hex(a);
	led_b	<=	to_hex(b);
	led_max	<=	to_hex(max);
	led_min	<=	to_hex(min);


	process (a, b, ena)
	begin
	
		if (ena = '0') then
		
			if (a > b) then
			
				max	<=	a;
				min	<=	b;
			
			else
			
				max	<=	b;
				min	<=	a;

			end if;
			
		end if;

	end process;
		
	
end arc;


'book > VHDL을 이용한 FPGA 디지털 설계' 카테고리의 다른 글

'1' 개수 카운터  (0) 2014.06.25
n비트 가산/감산기 vhdl 설계  (0) 2014.06.25
7-segment decoder  (0) 2014.06.05
xor을 이용한 전감산기 설계  (0) 2014.06.04
xor을 이용한 전가산기 설계  (0) 2014.06.04
Posted by 쿨한넘

4bit 스위치 입력을 받아 7 세그먼트에 표시한다.

DE2-115에 맞게 바꿔었다.

numeric_std 을 사용하기 위해서는 std_logic_arith 을 인클루드 하지 말아야.

http://www.lothar-miller.de/s9y/uploads/Bilder/Usage_of_numeric_std.pdf 참조.


--
-- VHDL을 이용한 FPGA 디지털 설계
-- section_03 Lab4 7-segment decoder
--

library ieee;
use ieee.std_logic_1164.all;
--use ieee.std_logic_arith.all;
use ieee.numeric_std.all;


entity Lab4_7seg is

	port (
		CLOCK_50		:	in		std_logic;
		SW				:	in		std_logic_vector (17 downto 0);
		HEX0			:	out		std_logic_vector (6 downto 0)
	);
	
end Lab4_7seg;


architecture arc of Lab4_7seg is

	signal	keyVal		:	integer range 0 to 15;

begin


	keyVal <= to_integer(unsigned(SW (3 downto 0)) );


	process (CLOCK_50)
	begin
	
		if rising_edge(CLOCK_50) then
		
			case keyVal is
										--	"gfedcba"
				when 0		=>	HEX0	<=	"1000000";
				when 1		=>	HEX0	<=	"1111001";
				when 2		=>	HEX0	<=	"0100100";
				when 3		=>	HEX0	<=	"0110000";
				when 4		=>	HEX0	<=	"0011001";
				when 5		=>	HEX0	<=	"0010010";
				when 6		=>	HEX0	<=	"0000010";
				when 7		=>	HEX0	<=	"1011000";
				when 8		=>	HEX0	<=	"0000000";
				when 9		=>	HEX0	<=	"0011000";
				when 10		=>	HEX0	<=	"0001000";
				when 11		=>	HEX0	<=	"0000011";
				when 12		=>	HEX0	<=	"0100111";
				when 13		=>	HEX0	<=	"0100001";
				when 14		=>	HEX0	<=	"0000110";
				when 15		=>	HEX0	<=	"0001110";
				when others	=>	null;

			end case;
	
		end if;
		
	end process;

end arc;



Posted by 쿨한넘

뭐...



library ieee;
use ieee.std_logic_1164.all;


entity FullSubtractor_xor_vhdl is

	port (
		x, y, z		:	in		std_logic;
		D, B		:	out		std_logic
	);

end FullSubtractor_xor_vhdl;


architecture arc of FullSubtractor_xor_vhdl is
begin

	D	<=	x xor y xor z;
	B	<=	((not (x xor y)) and z) or ((not x) and y);
	
end arc;



'book > VHDL을 이용한 FPGA 디지털 설계' 카테고리의 다른 글

lab06. 수의 정렬회로 설계  (0) 2014.06.25
7-segment decoder  (0) 2014.06.05
xor을 이용한 전가산기 설계  (0) 2014.06.04
전감산기의 설계  (0) 2014.06.04
전가산기의 VHDL 설계  (0) 2014.06.03
Posted by 쿨한넘

입력이 2개인 경우에 두 입력이 서로 다른 값일 경우 xor의 출력은 '1'. 입력이 세개인 경우 '1'값이 홀수개인 경우에 xor의 출력이 '1'.

이를 이용하여 전가산기 설계.



library ieee;
use ieee.std_logic_1164.all;


entity FullAdder_xor_vhdl is

	port (
		x, y, z		:	in		std_logic;
		S, C		:	out		std_logic
	);
	
end FullAdder_xor_vhdl;


architecture arc of FullAdder_xor_vhdl is
begin

	S	<=	x xor y xor z;
	C	<=	(z and (x xor y)) or (x and y);
	
end arc;



'book > VHDL을 이용한 FPGA 디지털 설계' 카테고리의 다른 글

7-segment decoder  (0) 2014.06.05
xor을 이용한 전감산기 설계  (0) 2014.06.04
전감산기의 설계  (0) 2014.06.04
전가산기의 VHDL 설계  (0) 2014.06.03
VHDL을 이용한 FPGA 디지털 설계  (0) 2014.06.03
Posted by 쿨한넘

D = (X - Bi) - Y

Bi는 아래 자리에 빌려준 값. 입력이 된다.

위에서 빌린 수는 B로 표현되고, 진리표를 작성해보면 쉽다.




--
--
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;


entity FullSubtractor_vhdl is

	port (
		X, Y, bi	:	in	integer range 0 to 1;
		D, B		:	out	std_logic
	);

end FullSubtractor_vhdl;


architecture arc of FullSubtractor_vhdl is

	signal	diff	:	integer range -2 to 1;

begin

	process(X, Y, bi)
	begin
	
		diff <= X - bi - Y;
		
		if diff = -2 then		-- XYbi = 011

			D	<=	'0';
			B	<=	'1';
			
		elsif diff = -1 then	-- XYbi = 001, 010, 111
		
			D	<=	'1';
			B	<=	'1';
			
		elsif diff = 0 then		-- XYbi = 000, 101, 110
		
			D	<=	'0';
			B	<=	'0';
			
		else					-- XYbi = 100
		
			D	<=	'1';
			B	<=	'0';
			
		end if;

	
	end process;


end arc;



Posted by 쿨한넘



--
-- VHDL을 이용한 FPGA 디지털 설계
--  3장. 조합논리 회로의 설계
--   section 02. 전가산기
--

library ieee;
use ieee.std_logic_1164.all;


entity FullAdder1_vhdl is

	port (
		x, y, z		:	in	std_logic;
		S, C		:	out	std_logic
	);

end FullAdder1_vhdl;


architecture arc of FullAdder1_vhdl is

	signal	k	:	std_logic_vector(2 downto 0);

begin

	k	<=	x & y & z;

	
	process(k)
	begin
	
		case k is

			when "000"	=>
				S	<=	'0';
				C	<=	'0';

			when "001"	=>
				S	<=	'1';
				C	<=	'0';

			when "010"	=>
				S	<=	'1';
				C	<=	'0';

			when "011"	=>
				S	<=	'0';
				C	<=	'1';

			when "100"	=>
				S	<=	'1';
				C	<=	'0';

			when "101"	=>
				S	<=	'0';
				C	<=	'1';

			when "110"	=>
				S	<=	'0';
				C	<=	'1';

			when "111"	=>
				S	<=	'1';
				C	<=	'1';

		end case;
		
	end process;

	
end arc;


별다른 것은 없고...

27번 라인, &로 signal들을 묶어 vector로 변환이 가능하다.

30번 라인, process는 순차기술문 (sequential statement) 을 제공하기 위한 기본 구조체. sensitivity list 에 포함된 signal (이 경우 k) 의 값이 변할 때마다 process 내의 문장들이 순차적으로 실행된다.

진리표를 옮길 때에는 case~when 구문이 편리하다.


다른 설계.

=========


카르노 맵을 이용하면 S, C는 다음과 같이 간소화 할 수 있다.

S = x'y'z + x'yz' + xy'z' + xyz

C = xy + xz + yz


이를 이용하여 구현.




library ieee;
use ieee.std_logic_1164.all;


entity FullAdder2_vhdl is

	port (
		x, y, z		:	in	std_logic;
		S, C		:	out	std_logic
	);
	
end FullAdder2_vhdl;


architecture arc of FullAdder2_vhdl is

	signal nx, ny, nz	:	std_logic;

begin

	nx	<=	not x;
	ny	<=	not y;
	nz	<=	not z;

--
-- S = x'y'z + x'yz' + xy'z' + xyz
-- C = xy + xz + yz
--

	S	<=	(nx and ny and z) or
			(nx and y and nz) or
			(x and ny and nz) or
			(x and y and z);

	C	<=	(x and y ) or
			(x and z) or
			(y and z);

end arc;


특이한 점은 없다.


또 다른 구현

===========




--
-- VHDL을 이용한 FPGA 디지털 설계
--  3장. 조합논리 회로의 설계
--   section 02. 전가산기
--    코드 3-5	

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;


entity FullAdder3_vhdl is

	port (
		x, y, z		:	in	integer range 0 to 1;
		S, C		:	out	std_logic
	);
	
end FullAdder3_vhdl;


architecture arc of FullAdder3_vhdl is

	signal sum	:	std_logic_vector(1 downto 0);
	
begin

	--
	-- conv_std_logic_vector 의 사용 예.
	-- 참고: conv_integer, conv_unsigned, conv_signed
	--

	process(x, y, z)
	begin
	
		sum		<=	conv_std_logic_vector(x + y + z, 2);
		
	end process;
	
	
	S	<=	sum(0);
	C	<=	sum(1);


end arc;


x, y, z를 integer 로 선언. 그리고 conv_std_logic_vector 함수를 사용하였다.

이를 위해 ieee.set_logic_arith 를 인클루드.


* 변환 함수:

conv_integer    :    unsigned, signed 또는 std_logic 값을 integer 값으로 변환한다. 이 함수의 범위는 -2147483647 ~ 2147483647 (응?) 로 31비트 unsigned 값 또는 32비트 signed 값에 해당한다.


conv_unsigned    :    변환될 비트의 수 (size bit)와 함께 integer, unsigned, signed 또는 std_ulogic을 unsigned 값으로 변환한다.


conv_signed    :    변환될 비트의 수와 함께 integer, unsigned, signed 또는 std_ulogic 값을 signed 값으로 변환한다.


conv_std_logic_vector    :    변환될 비트의 수와 함께 integer, unsigned, signed 또는 std_ulogic 값을 std_logic_vector 값으로 변환한다.


좀 더 찾아보자.

'book > VHDL을 이용한 FPGA 디지털 설계' 카테고리의 다른 글

7-segment decoder  (0) 2014.06.05
xor을 이용한 전감산기 설계  (0) 2014.06.04
xor을 이용한 전가산기 설계  (0) 2014.06.04
전감산기의 설계  (0) 2014.06.04
VHDL을 이용한 FPGA 디지털 설계  (0) 2014.06.03
Posted by 쿨한넘