사용자 삽입 이미지

//bubble_sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 60000
#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))

int list[MAX_SIZE];
int n;

void bubble_sort(int list[],int n)
{
 int i,j,temp;
 for(i=n-1;i>0;i--){
  for(j=0;j<i;j++)
   if(list[j]>list[j+1])
   SWAP(list[j],list[j+1],temp);
 }
}
void main()
{
 int i;
 clock_t start , end;
 n=MAX_SIZE;
 for(i=0;i<n;i++)
  list[i]=rand()%n;

 start = clock();
 bubble_sort(list,n);
 end = clock();
  printf("%lfsec\n",(end - start)/(double)1000);
}


//insertion_sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SIZE 60000
//#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))

int list[MAX_SIZE];
int n;

void insertion_sort(int list[],int n)
{
 int i,j,key;
 for(i=1;i<n;i++){
  key=list[i];
  for(j=i-1;j>=0 && list[j]>key;j--)
   list[j+1]=list[j];
  list[j+1]=key;
 }
}


void main()
{int i;
 clock_t start , end;
 n=MAX_SIZE;
 for(i=0;i<n;i++)
  list[i]=rand()%n;

 start = clock();
 insertion_sort(list,n);
 end = clock();
  printf("%lfsec\n",(end - start)/(double)1000);
}


//merge_sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 60000

int sorted[MAX_SIZE];
int list[MAX_SIZE];
int n;

void merge(int list[],int left,int mid,int right)
{
 int i,j,k,l;
 i=left, j=mid+1; k=left;

 while(i<=mid && j<=right){
  if(list[i]<=list[j])
   sorted[k++]=list[i++];
  else
   sorted[k++]=list[j++];
 }
 if(i>mid)
  for(l=j;l<=right;l++)
   sorted[k++]=list[l];
 else
  for(l=i;l<=mid;l++);

 for(l=left;l<=right;l++)
  list[l]=sorted[l];
}


void merge_sort(int list[],int left,int right)
{
 int mid;
 if(left<right){
  mid=(left+right)/2;
  merge_sort(list,left,mid);
  merge_sort(list,mid+1,right);
  merge(list,left,mid,right);
 }
}


void main()
{
 int i;
 clock_t start , end;
 n=MAX_SIZE;
 for(i=0;i<n;i++)
  list[i]=rand()%n;

 start = clock();
 merge_sort(list,0,n);
 end = clock();
  printf("%lfsec\n",(end - start)/(double)1000);
}

//quick_sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SIZE 60000
#define SWAP(x,y,t)((t)=(x),(x)=(y),(y)=(t))

int list[MAX_SIZE];
int partition(int left,int right)
{
 int pivot,temp;
 int low,high;
 
 low=left;
 high=right+1;
 pivot=list[left];
 do{
  do
  low++;
  while(low <= right && list[low]<pivot);
  do
  high--;
  while(high >= left && list[high]>pivot);
  if(low<high) SWAP(list[low],list[high],temp);
 }while(low<high);

 SWAP(list[left],list[high],temp);
 return high;
}
void quick_sort(int left,int right)
{
 int q;
 if(left<right){
  q=partition(left,right);
  quick_sort(left,q-1);
  quick_sort(q+1,right);
 }
}

int main()
{
 int i , n;
 clock_t start , end;
 n = MAX_SIZE;
 srand((unsigned)time(NULL));
 for(i = 0 ; i < n ; i++)
  list[i] = rand() % n;
 start = clock();
 quick_sort(0 , n-1);
 end = clock();
 printf("%lfsec \n",(end - start) / (double)1000);
}


//select_sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 60000
#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))

int list[MAX_SIZE];
int n;

void selection_sort(int list[],int n)
{
 int i,j,least,temp;
 for(i=0;i<n-1;i++){
  least=i;
  for(j=i+1;j<n;j++)
   if(list[j]<list[least]) least=j;
  SWAP(list[i],list[least],temp);
 }
}
void main()
{
int i;
 clock_t start , end;
 n=MAX_SIZE;
 for(i=0;i<n;i++)
  list[i]=rand()%n;

 start = clock();
 selection_sort(list,n);
 end = clock();
  printf("%lfsec\n",(end - start)/(double)1000);
}

//shell_sort

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define SIZE 60000

int a[SIZE];

void ShellSort(int n)
{
    int i,j,h,v;
    for(h=1 ; h<n ; h = 3*h+1) ;
    for( ; h > 0 ; h /=3)                     
        for ( i = h+1 ; i<=n ;i++)                                        
        {
            v = a[i];                                      
            j = i;                           
   while (j > h && a[j-h] > v)       
            {
                a[j] = a[j-h];                                                                 
                j = j - h;                                                                
            }a[j] = v;
  }
}

 void main()
{
    int i , n;
    double start_time;
 n = SIZE;

    srand((unsigned)time(NULL));

    for(i = 0; i< n; i++) a[i] = rand() % n;

    start_time = clock();
    ShellSort(n);

 printf("%lfsec\n", (clock()-start_time)/(double)1000);
 }


//heap_sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 60000
int a[SIZE];

typedef struct HeapType {
 int heap[SIZE+1];
 int heap_size;
}HeapType;

void init(HeapType *h)
{
 h->heap_size=0;
}

void insert_max_heap(HeapType *h,int item)
{
 int i;
 i= ++(h->heap_size);

 while((i!=1)&&(item > h->heap[i/2])){
  h->heap[i]=h->heap[i/2];
  i/=2;
 }
 h->heap[i]=item;
}

int delete_max_heap(HeapType *h)
{
 int parent, child;
 int item, temp;

 item=h->heap[1];
 temp=h->heap[(h->heap_size)--];
 parent=1;
 child=2;
 while(child<=h->heap_size){
  if((child<h->heap_size)&&
   (h->heap[child] < h->heap[child+1]))
   child++;
  if(temp >=h->heap[child]) break;
  h->heap[parent]=h->heap[child];
  parent=child;
  child*=2;
 }
 h->heap[parent]=temp;
 return item;
}
void heap_sort(int n)
{
 int i;
 HeapType h;

 init(&h);
 for(i=0;i<n;i++){
  insert_max_heap(&h,a[i]);
 }
 for(i=(n-1);i>=0;i--){
  a[i]=delete_max_heap(&h);
 }
}

void main()
{
 int i , n;
 HeapType h;
 clock_t start , end;
 n = SIZE;

 for(i = 1 ; i <= n ; i++)
  a[i] = rand() % n;
    init(&h);
 start = clock();
 heap_sort(n);
 end = clock();
 printf("%lfsec \n",(end - start)/(double)1000);
 
}

//radix_sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define BUCKETS 10
#define DIGITS 5
#define MAX_QUEUE_SIZE 20000
#define SIZE 60000

typedef int element;
typedef struct{
 element queue[MAX_QUEUE_SIZE];
 int front,rear;
}QueueType;

int list[SIZE];

void error(char* message)
{
 fprintf(stderr,"%s\n",message);
 exit(1);
}

void init(QueueType* q)
{
 q->front=q->rear=0;
}

int is_empty(QueueType* q)
{
 return (q->front == q->rear);
}

int is_Full(QueueType* q)
{
 return ((q->rear+1)%MAX_QUEUE_SIZE ==q->front);
}
void enqueue(QueueType* q,element item)
{
 if(is_Full(q))
  error("큐가 포화상태입니다");
 q->rear=(q->rear+1)%MAX_QUEUE_SIZE;
 q->queue[q->rear]=item;
}

element dequeue(QueueType* q)
{
 if(is_empty(q))
  error("큐가 공백상태입니다");
 q->front=(q->front+1)%MAX_QUEUE_SIZE;
 return q->queue[q->front];
}

void radix_sort(int n)
{
 int i,b,d,factor=1;
 QueueType queues[BUCKETS];

 for(b=0;b<BUCKETS;b++) init(&queues[b]);

 for(d=0;d<DIGITS;d++){
  for(i=0;i<n;i++)
   enqueue(&queues[(list[i]/factor)%10],list[i]);

  for(b=i=0;b<BUCKETS;b++)
   while(!is_empty(&queues[b]))
    list[i++]=dequeue(&queues[b]);
  factor *=10;
 }
}

void main()
{
 int i,n;
 clock_t start , end;
 n=SIZE;
 for(i=0;i<n;i++)
  list[i]=rand()%n;

 start = clock();
 radix_sort(n);
 end = clock();
  printf("%lfsec\n",(end - start)/(double)1000);
}

블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

#include <iostream>

#define MAX_QUEUE_SIZE 100

using std::cout;
using std::endl;
using std::cin;

class queue{
 int rear;
 int front;
 int queues[MAX_QUEUE_SIZE];
public:
 void init();
 void addq(int item);
 void deleteq();
 void show();
};
void queue::init()
{
 rear=front=0;
 queues[rear]=0;
 queues[front]=0;
}
void queue::addq(int item)
{
 rear=(rear+1) % MAX_QUEUE_SIZE;
 if(front == rear){
  cout<<"queue is full";
  }
 queues[rear]=item;
}
void queue::deleteq()
{
 if(front == rear){
  cout<<"queue is empty";
 }
 front=(front+1) % MAX_QUEUE_SIZE;
 return;// queues[front];
}
void queue::show()
{
 cout<<"front : "<<queues[front]<<"  "<<"rear : "<<queues[rear]<<endl;
}

int main(void)
{
 queue q1;
 q1.init();
 q1.show();

 q1.addq(10);
 q1.show();
 q1.addq(20);
 q1.show();
 q1.addq(30);
 q1.show();
 q1.addq(40);
 q1.show();
 q1.addq(50);
 q1.show();
 return 0;
}


블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

LCD모듈에 카운터

Embeded 2007. 8. 29. 18:34

사용자 삽입 이미지


#include<iom128.h>
#include<ina90.h>

unsigned int data=0x00;

#define ENABLE (PORTA_Bit2=1)
#define DISABLE (PORTA_Bit2=0)

void init_PORTA(void)
{
   DDRA = 0xFF;
   PORTA = 0xFF;
}

void wait_busy_flag(void)
{
   unsigned int i;
   for(i=0; i<10000; i++);
}

void m_delay(unsigned int m)
{
   unsigned int i,j;

   for(i=0; i<m; i++)
      for(j=0; j<2650; j++);
}

void ddelay(unsigned int p)
{
   unsigned int i;

   for(i=0; i<p; i++);
}

void instruction_out(unsigned char b)
{
   PORTA=b&0xF0;

   ENABLE;
   DISABLE;

   PORTA=(b<<4)&0xF0;

   ENABLE;
   DISABLE;

   m_delay(2);
}

void char_out(unsigned char b)
{
   PORTA=(b&0xF0) | 0x01;

   ENABLE;
   DISABLE;

   PORTA=((b<<4)&oxF0) | 0x01;

   ENABLE;
   DISABLE;

   m_delay(2);
}

void string_out(unsigned char b, unsigned char* str)
{
   unsigned int i=0;

   instruction_out(b);

   do{
      char_out(str[i]);
   }while(str[++i]!='\0');
}

void init_LCD(void)
{
   init_PORTA();

   m_delay(15);

   ddelay(600);
   instruction_out(0x28);

   m_delay(2);
   instruction_out(0x28);
   m_delay(2);
   instruction_out(0x0C);
   instruction_out(0x06);
   instruction_out(0x02);
   instruction_out(0x01);
   instruction_out(0x01);
}

void lcd_decimal(unsigned char b, unsigned int hex)
{
   unsigned int h;

   instruction_out(b);

   h = (hex/1000);

   if(h != 0)
      char_out(h + 0x30);
   else
      char_out(0x30);

   h= ((hex % 1000) / 100);

   if( h!= 0)
      char_out(h + 0x30);
   else
      char_out(0x30);
  
   h = (((hex % 1000) 100) /10);

   if( h != 0)
      char_out(h + 0x30);
   else
      char_out(0x30);

   h = (((hex % 1000) % 100) % 10);

   if(h != 0)
      char_out(h + 0x30);
   else
      char_out(0x30);
}

void main(void)
{
   DDRB=0xFF;
   PORTB=0xFF;

   DDRD_Bit0=0;

   init_LCD();

   EIMSK |= 0x01;
   EICRA |= 0x03;
   EIFR |= 0x01;

   instruction_out(0x01);
   string_out(0x80,"JSLEE");

   __enable_interrupt()

      while(1)
         lcd_decimal(0xC2,data);
}

#pragma vector=INT0_vect
__interrupt void INT0_interrupt(void)
{
   __disable_interrupt();

   data++;

   __enable_interrupt();
}


블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,



#include<iom128.h>
#include<ina90.h>

unsigned int data=0xFE;

void main(void)
{
  DDRB=0xFF;
  PORTB=0xFF;

  DDRD_Bit0=0;

  EIMSK |= 0x01;
  EICRA |= 0x03;
  EIFR |= 0x01;


  __enable_interrupt();

  while(1);
}

#pragma vector=INT0_vect
__interrupt void INT0_interrupt(void)
{
  __disable_interrupt();

  PORTB=data;

  data--;

  __enable_interrupt();
}


 
블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,



#include<iom128.h>
#include<ina90.h>

void m_delay(unsigned int m)
{
   unsigned int i,j;

   __disable_interrupt();

   for(i=0; i<m; i++)
      for(j=0; j<2650; j++);

   __enable_interrupt();
}

void main(void)
{
   unsigned int L_shift,R_shift,i;

   DDRB=0xFF;
   PORTB=0xFF;

   while(1)
   {
      L_shift=0x01;
      R_shift=0x80;

      for(i=0; i<8; i++)
      {
         PORTB=L_shift;
         L_shift = L_shift << 1;
         m_delay(100);
      }

      for(i=0; i<8; i++)
      {
         PORTB=R_shift;
         R_shift = R_shift >> 1;
         m_delay(100);
      }
   }
}


 
블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,



#include<iom128.h>
#include<ina90.h>
void m_delay(unsigned int m)
{
   unsigned int i,j;
   __disable_interrupt();
   for(i=0; i<m; i++)
      for(j=0; j<2650; j++);
   __enable_interrupt();
}
void main(void)
{
   unsigned int h,j,m;
   h=0xFE;
   j=0x7F;
   DDRB=0xFF;
   PORTB=0xFF;
   do{
      m=h & j;
      PORTB=m;
      m_delay(100);
      if((h==0x7F || j==0xFE))
      {
         h=0xFE;
         j=0x7F;
      }
      else
      {
         h=((h << 1) | 0x01);
         j=((j >> 1) | 0x80);
      }
   }while(1);
}

 
 
 
블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,