Thứ Năm, 28 tháng 2, 2013

Tự học AVR ATmega8 - 01 Leds

Trước khi sử dụng các IO của ATmega8, chúng ta phải config IO là Input hoặc Output:
  • Ví dụ config PD7 là Output:
    •  DDRD |= (1 << PD7);
  •  Ví dụ config PD7 là Input:
    •  DDRD &= ~ (1 << PD7);
--------------------------------------------------------------------
Source code 1: Nhấp nháy led 500ms
#include <avr/io.h>           /* khai báo IO cho Mega8 */
#include <util/delay.h>      /* khai báo cho hàm  _delay_ms */
/* main function */
int main(void)
{
  DDRD |= (1 << PD7); //PD7 is Output:
  while(1)
  {
     //turn on/turn off LED after 500ms
     PORTD |= (1 << PD7);
      _delay_ms(500);
      PORTD &= ~(1 << PD7);
      _delay_ms(500);
  }
}

------------------------------------------------------------------------
Để souce code dễ đọc hơn, ta dùng macro:

#include <avr/io.h>           /* khai báo IO cho Mega8 */
#include <util/delay.h>      /* khai báo cho hàm  _delay_ms */

#define LED_PIN          PD7
#define LED_PORT       PORTD
#define LED_DDR         DDRD

#define LED_ON         LED_PORT |= 1 << LED_PIN
#define LED_OFF       LED_PORT &= ~(1 << LED_PIN)

/* main function */
int main(void)
{
  LED_DDR |= (1 << LED_PIN); //PD7 is Output:
  while(1)
  {
     //turn on/turn off LED after 500ms
     LED_ON;
      _delay_ms(500);
      LED_OFF;
      _delay_ms(500);
  }
}

Mạch nguồn DC 5V 3A ngon bổ rẻ

Mạch nguồn DC 5V 3A ngon bổ rẻ, dùng lm2576-5_0




Các linh kiện đều có bán ở Thiên Minh tme





Thứ Tư, 27 tháng 2, 2013

Dùng Visual C++ Express làm IDE lập trình cho AVR

Tạo makefile bằng chương trình Mfile của WinAVR
Ví dụ dưới đây tạo make file cho ATMega8, 12Mhz, 1 file source là main.c:

Start > All Programs > WinAVR > MFile
  • Main file name: main --> Click OK
  • MCU name:
    •  MCUType = atmega8

  • F_CPU: Makefile > Enable Editing of Makefile
    • F_CPU = 12000000
  • C/C++ source files...: mặc định có file main.c

  • Chọn programmer (mạch nạp)
  • Chọn Port (cổng COM, hoặc LPT, hoặc USB )
  • Những cái khác thì để mặc định
Save Makefile: File > Save As ...: save vào folder có chứa file main.c

------------------------------------------------------------------------------------------------------
Khởi động  Visual C++ 2008 Express:
        Start > All Programs > Micosoft Visual C++ 2008 Express
        File > New Project > Chọn Project types: General và Template: Makefile Project


Debug Configuration Settings:
  • Built Command Line:         make
  • Re-built Command Line:    make all
  • Clean Command Line:       make clean
  • Output (for debugging):    your_project_name.hex




Release Configuration Settings:
  • Chose Same as debug configuration
  • Finish.



Open Properties page:
  • Project > Properties > Configuration Properties > NMake
  • Common Language Runtime Support: Common Language Runtime Support (/clr). Chức năng này để khởi động IntelliSence củaVisual C++.
  • Apply > OK
 

 Add source file vào project:
  • Add file main.c
  • Copy Make file vào project folder






Build project:
  • Build
  • Rebuild
  • Clean