본문 바로가기
코스웨어/16년 스마트컨트롤러

2016-05-16_조재찬_업무일지_AVR 입출력/ C#기초

by 알 수 없는 사용자 2016. 5. 16.
728x90
반응형


bcd.DSN


proteus에서 회로도 그리고 아래 소스를 시뮬레이션


avr studio에서 컴파일시 Frequency 설정할 것



hex파일을 프로그램으로 올려줌



7 Segment - up & down 카운트


#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
	unsigned char a;

	DDRA = 0x00;	// A 포트는 입력으로 사용, 생략 가능
	DDRB = 0xFF;	// B 포트는 출력으로 사용
	DDRC = 0xFF;	// 미사용 포트는 출력으로 설정
	DDRD = 0xFF;	// 미사용 포트는 출력으로 설정
	DDRE = 0xFF;	// 미사용 포트는 출력으로 설정
	DDRF = 0xFF;	// 미사용 포트는 출력으로 설정
	DDRG = 0xFF;	// 미사용 포트는 출력으로 설정
	DDRH = 0xFF;	// 미사용 포트는 출력으로 설정
	DDRJ = 0xFF;	// 미사용 포트는 출력으로 설정
	DDRK = 0xFF 	// 미사용 포트는 출력으로 설정

	while(1)
	{
		a = PINA;
		switch(a)
		{
			case 0x01;
					PORTB = 0x01;
					break;
			case 0x02;
					PORTB = 0x02;
					break;
			case 0x04;
					PORTB = 0x04;
					break;
			case 0x08;
					PORTB = 0x08;
					break;

			case 0x10;
					PORTB = 0x10;
					break;
			case 0x20;
					PORTB = 0x20;
					break;
			case 0x40;
					PORTB = 0x40;
					break;
			case 0x80;
					PORTB = 0x80;
					break;
			default :
				PORTB = 0;
				break;
		}
		PORTB = a;
	}
	return 0;
}






7 Segment - 입출력

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
	unsigned char in, out =0;
	
	DDRA = 0b11111100;		// A0, A1만 입력 (0xFC) / 사용하지 않는 포트는 출력 
	DDRB = 0xFF;
	while(1)
	{
			in = PINA;
			switch (in)
			{
					case 0x01 :		// Up count
							while(PINA=0x01);	// 스위치 때는 순간 while문 탈출(high->low)
							out++;
							break;
					case 0x02 :
							while(PINA=0x02);
							out--;
							break;
			}
			PORTB = out;
			//_delay_ms(50);
	}
		return 0;
}








C# 구구단 소스


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace gugudan2
{   
        class Program
    {


        static void Main(string[] args)
        {    
            int i;
            int j;
            string select;

            Console.WriteLine("=======================");
            Console.WriteLine("1. 전체 출력 2 선택출력");
            select = Console.ReadLine();
            if (select == "1")
            { 

                for (i = 2; 7 >= i; i++)
                {
                    j = 1;

                    for (j = 1; 10 > j; j++)
                    {
                        Console.Write(i + "x" + j + " = " + i * j + "\t ");                     
                    }
                    Console.WriteLine(" ");
                }
            }

            else if(select=="2")
            {

                Console.Write("몇단을 셀지 입력하시오: ");
                i = Convert.ToInt32(Console.ReadLine());

                for (j = 1; 10 > j; j++)
                    Console.Write("{0} X {1} = {2} \t ", i, j, i * j);
            }

            else
            {
                Console.WriteLine("잘못입력하였습니다");
            }

        }
    }
}



Output:


1. 전체 출력


2. 선택 출력 


3. 잘못 입력한 경우




별표 출력하는 소스


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace star
{
    class Program
    {
        static void Main(string[] args)
        {            
            Console.WriteLine("반복횟수를 입력하세요: ");
            string input = Console.ReadLine();
            int number = Convert.ToInt32(input);

            if(number>=1)
            for (int i=0; i<number; i++)
            {                
                for (int j=0; j<=i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            else
                Console.WriteLine("0보다 작거나 같은 수는 입력할 수 없습니다. ");
        }
    }
}


Output:


반복횟수 (7) 입력 



반복횟수 0보다 작은 수 입력 




728x90