วิธีสร้างโปรเจคด้วย CodeWizardAVR ของ CodeVision

วันที่โพสต์: Feb 14, 2011 6:10:51 AM

วิธีสร้างโปรเจคด้วย CodeWizardAVR ของ CodeVision

จากตัวอย่างที่แล้ว ผมลืมแนะนำวิธีการสร้างโปรเจค CodeVision ด้วย CodeWizardAVR บทความตอนนี้ จะเป็นตอนที่เราสร้างโปรเจคง่ายๆ ด้วย CodeWizardAVR ของ โปรแกรม CodeVision ครับ 

จากตอนที่แล้ว วงจรที่แสดงให้เห็น เป็นวงจรง่ายๆ เป็นการควบคุม การเปิดหลอด LED ด้วย Push Button แต่ละตัว ควบคุมมแต่ละหลอด เมื่อพิจารณาในแง่การทำงานที่ขาของไมโครคอนโทรลเลอร์ เราสามารถพิจารณาออกเป็น 4 อินพุต 4 เอาท์พุท โดยให้ขา PIN PB0..PB3 เป็นขาอินพุต รับลอจิกจาก Push button และให้ PB4...PB7 เป็นขาเอาท์พุต สั่งให้ LED ติด  

1. ที่เมนู File .. เลือก New จะมีหน้าต่าง Dialog ให้เราเลือก Project เำำพราะเราจะสร้างโปรเจคใหม่่ไปพร้อมกับ main ไฟล์เลย 

2. หลังจากเลือกโปรเจคแล้ว จะมีหน้าต่างถามว่าเราจะสร้าง new project ด้วย CodeWizardAVR ไหม ให้เราเลือก Yes เลยครัีบ 

3. เลือกไมโครอคนโทรลเลอร์ ในที่นี้ ผมใช้ ATMEGA48V-10PU

4. จะปรากฏหน้าต่าง CodeWizardAVR ที่แท๊ป Chip ให้เราเลือกไมโครคอนโทรลเลอร์ ATMEGA48V และค่า Clock 8 MHz

5. ที่แท๊ป Ports ตั้งค่า Port B โดยทำการกดปุ่มเพื่อเปลี่ยนค่า Data Direction โดย Bit 0 คือตำแหน่งของ PB0 ไปตามลำดับ จนถึงตำแหน่ง PB7  ให้เราตั้งค่า Data Direction ตามวงจรที่เราได้ออกแบบไว้แล้ว 

6. ที่เมนู Program เราสามารถเลืิอก Preview เพื่อดูโค๊ดที่ CodeWizardAVR จะทำการ generate ออกมาให้ได้ก่อน แล้วทำการเลือก Generate, Save and Exit 

7. ทำการบันทึก File source code เป็น main.c และทำการบันทึกโปรเจค เป็น LED.cwp และ LED.prj ตามลำดับ ทั้งหมด เราจะได้ไฟล์และโปรเจคไว้ในโฟลเดอร์ที่เรากำหนด 

8. โปรแกรม CodeWizardAVR จะทำการ generate code ออกมาให้เรา ตามที่เราได้เลือกค่าไว้ และที่เหลือ ให้เราเพิ่มโค๊ดตามที่เราต้องการ 

9. ที่เมนู Project เลือก Build All ถ้าไม่มีอะไรผิดพลาดเี่ราจะได้ Hex file อยู่ที่โฟลเดอร์ Exe เพื่อนำไปเบิร์นลงไมโครคอนโทรลเลอร์อีกที 

10 ทดลองต่อวงจรตามรูป แล้วทดสอบโปรแกรม 

จะเห็นได้ว่า CodeWizardAVR มีประโยชน์มากๆ ทำให้เราสามารถพัฒนางานได้อย่างรวดเร็ว และมีความถูกต้อง ลดปัญหาความผิดพลาด นับว่าเป็นจุดเด่น ของ Complier CodeVision  อยากให้เพื่อนๆ ลองใช้งานดูครับ แล้วจะติดใจ 

/*****************************************************

This program was produced by the

CodeWizardAVR V2.05.0 Evaluation

Automatic Program Generator

ฉ Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project : 

Version : 

Date    : 2/14/2011

Author  : Freeware, for evaluation and non-commercial use only

Company : 

Comments: 

Chip type               : ATmega48V

AVR Core Clock frequency: 8.000000 MHz

Memory model            : Small

External RAM size       : 0

Data Stack size         : 128

*****************************************************/

#include <mega48.h>

// Declare your global variables here

void main(void)

{

// Declare your local variables here

// Crystal Oscillator division factor: 1

#pragma optsize-

CLKPR=0x80;

CLKPR=0x00;

#ifdef _OPTIMIZE_SIZE_

#pragma optsize+

#endif

// Input/Output Ports initialization

// Port B initialization

// Func7=Out Func6=Out Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In 

// State7=0 State6=0 State5=0 State4=0 State3=T State2=T State1=T State0=T 

PORTB=0x00;

DDRB=0xF0;

// Port C initialization

// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 

// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 

PORTC=0x00;

DDRC=0x00;

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 

// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 

PORTD=0x00;

DDRD=0x00;

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=0xFF

// OC0A output: Disconnected

// OC0B output: Disconnected

TCCR0A=0x00;

TCCR0B=0x00;

TCNT0=0x00;

OCR0A=0x00;

OCR0B=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer1 Stopped

// Mode: Normal top=0xFFFF

// OC1A output: Discon.

// OC1B output: Discon.

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

TCCR1A=0x00;

TCCR1B=0x00;

TCNT1H=0x00;

TCNT1L=0x00;

ICR1H=0x00;

ICR1L=0x00;

OCR1AH=0x00;

OCR1AL=0x00;

OCR1BH=0x00;

OCR1BL=0x00;

// Timer/Counter 2 initialization

// Clock source: System Clock

// Clock value: Timer2 Stopped

// Mode: Normal top=0xFF

// OC2A output: Disconnected

// OC2B output: Disconnected

ASSR=0x00;

TCCR2A=0x00;

TCCR2B=0x00;

TCNT2=0x00;

OCR2A=0x00;

OCR2B=0x00;

// External Interrupt(s) initialization

// INT0: Off

// INT1: Off

// Interrupt on any change on pins PCINT0-7: Off

// Interrupt on any change on pins PCINT8-14: Off

// Interrupt on any change on pins PCINT16-23: Off

EICRA=0x00;

EIMSK=0x00;

PCICR=0x00;

// Timer/Counter 0 Interrupt(s) initialization

TIMSK0=0x00;

// Timer/Counter 1 Interrupt(s) initialization

TIMSK1=0x00;

// Timer/Counter 2 Interrupt(s) initialization

TIMSK2=0x00;

// USART initialization

// USART disabled

UCSR0B=0x00;

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

ACSR=0x80;

ADCSRB=0x00;

DIDR1=0x00;

// ADC initialization

// ADC disabled

ADCSRA=0x00;

// SPI initialization

// SPI disabled

SPCR=0x00;

// TWI initialization

// TWI disabled

TWCR=0x00;

while (1)

      {

      // Place your code here

      }

}

 

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

แก้ไข เพิ่มเติม ได้เป็น 

ซอร์ซโค๊ดใหม่ หลังการแก้ไข  

/*****************************************************

This program was produced by the

CodeWizardAVR V2.05.0 Evaluation

Automatic Program Generator

ฉ Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project : 

Version : 

Date    : 2/14/2011

Author  : Freeware, for evaluation and non-commercial use only

Company : 

Comments: 

Chip type               : ATmega48V

AVR Core Clock frequency: 8.000000 MHz

Memory model            : Small

External RAM size       : 0

Data Stack size         : 128

*****************************************************/

#include <mega48.h>

#include <delay.h>

#define LED1 PORTB.4

#define LED2 PORTB.5

#define LED3 PORTB.6

#define LED4 PORTB.7

#define SW1 PINB.0

#define SW2 PINB.1

#define SW3 PINB.2

#define SW4 PINB.3

// Declare your global variables here

void main(void)

{

// Declare your local variables here

// Crystal Oscillator division factor: 1

#pragma optsize-

CLKPR=0x80;

CLKPR=0x00;

#ifdef _OPTIMIZE_SIZE_

#pragma optsize+

#endif

// Input/Output Ports initialization

// Port B initialization

// Func7=Out Func6=Out Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In 

// State7=0 State6=0 State5=0 State4=0 State3=T State2=T State1=T State0=T 

PORTB=0x00;

DDRB=0xF0;

// Port C initialization

// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 

// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 

PORTC=0x00;

DDRC=0x00;

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 

// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 

PORTD=0x00;

DDRD=0x00;

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=0xFF

// OC0A output: Disconnected

// OC0B output: Disconnected

TCCR0A=0x00;

TCCR0B=0x00;

TCNT0=0x00;

OCR0A=0x00;

OCR0B=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer1 Stopped

// Mode: Normal top=0xFFFF

// OC1A output: Discon.

// OC1B output: Discon.

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

TCCR1A=0x00;

TCCR1B=0x00;

TCNT1H=0x00;

TCNT1L=0x00;

ICR1H=0x00;

ICR1L=0x00;

OCR1AH=0x00;

OCR1AL=0x00;

OCR1BH=0x00;

OCR1BL=0x00;

// Timer/Counter 2 initialization

// Clock source: System Clock

// Clock value: Timer2 Stopped

// Mode: Normal top=0xFF

// OC2A output: Disconnected

// OC2B output: Disconnected

ASSR=0x00;

TCCR2A=0x00;

TCCR2B=0x00;

TCNT2=0x00;

OCR2A=0x00;

OCR2B=0x00;

// External Interrupt(s) initialization

// INT0: Off

// INT1: Off

// Interrupt on any change on pins PCINT0-7: Off

// Interrupt on any change on pins PCINT8-14: Off

// Interrupt on any change on pins PCINT16-23: Off

EICRA=0x00;

EIMSK=0x00;

PCICR=0x00;

// Timer/Counter 0 Interrupt(s) initialization

TIMSK0=0x00;

// Timer/Counter 1 Interrupt(s) initialization

TIMSK1=0x00;

// Timer/Counter 2 Interrupt(s) initialization

TIMSK2=0x00;

// USART initialization

// USART disabled

UCSR0B=0x00;

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

ACSR=0x80;

ADCSRB=0x00;

DIDR1=0x00;

// ADC initialization

// ADC disabled

ADCSRA=0x00;

// SPI initialization

// SPI disabled

SPCR=0x00;

// TWI initialization

// TWI disabled

TWCR=0x00;

while (1)

      {

      // Place your code here

            if(!SW1){

                delay_ms(10);

                LED1 = 1;

            }

            else if(!SW2){

                delay_ms(10);

                LED2 = 1;

            }

            else if(!SW3){

                //delay_ms(10);

                LED3 = 1;

            }

            else if(!SW4){

                //delay_ms(10);

                LED4 = 1;

            }

            else{ 

                //delay_ms(10);

                LED1 = 0;

                LED2 = 0;

                LED3 = 0;

                LED4 = 0;

            }

      }

}