0005En-AVR (CodeVision Tutorial)
0005En-AVR (CodeVision Tutorial)
0. Getting Started
Folder GetStarted
led.prj, led.c,
Project Configuration
In the CodeVisionAVR IDE, pull down Project menu and select
Configure.
In the Configure Project led.prj window,
Select After Make Tab and check on gthe box Program the Chip.
Select C Compile Tab to view and make changes if necessary.
//C Source File: led.c
/*********************************************
This program was produced by the
CodeWizardAVR V1.23.6a Standard
Automatic Program Generator
Copyright 1998-2002 HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro , hpinfotech@xnet.ro
Project :
Version :
Date
: 6/14/2003
Author : Dr. B.C. Chang
Company : Drexel University
Comments:
Chip type
: ATmega163
Program type
: Application
Clock frequency
: 3.680000 MHz
Memory model
: Small
Internal SRAM size : 1024
External SRAM size : 0
Data Stack size
: 256
*********************************************/
//led.c
#include <mega163.h>
//the LED 0 on PORTB will be ON
unsigned char led_status=0xFE;
//11111110
TCNT1L=0xFB;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
GIMSK=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
//Timer/Counter1 Overflow interrupt Enable
//UBRRHI=0x00; //It was generated by CodeWizardAVD,
// seems to be irerelavant.
// Global enable interrupts
#asm("sei")
while (1);
}
1. Tutor1_163
Folder Tutor1_163
tutor1_163.prj,
tutor1_163.c,
Project Configuration
In the CodeVisionAVR IDE, pull down Project menu and select
Configure.
In the Configure Project tutor1_163.prj window,
Select After Make Tab and check on gthe box Program the Chip.
Select C Compile Tab to view and make changes if necessary.
5
Chip type
: ATmega163
Program type
: Application
Clock frequency
: 3.680000 MHz
Memory model
: Small
Internal SRAM size : 1024
External SRAM size : 0
Data Stack size
: 256
*********************************************/
#include <Mega163.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
unsigned char data;
// Input/Output Ports initialization
// Port B initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out Func7=Out
// State0=1 State1=1 State2=1 State3=1 State4=1 State5=1 State6=1 State7=1
PORTB=0xFF;
DDRB=0xFF;
// Port D initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTD=0x00;
DDRD=0x00;
while (1)
{
data=PIND; //read the switches
PORTB=data; //write the data to the output
};
}
2. Tutor2_163
Folder Tutor2_163
Tutor2_163.prj,
tutor2_163.c,
//tutor2_163.c
#include <mega163.h>
// Declare your global variables here
// global character declarations
//
unsigned char data; // global byte giving last switch press
// Prototype declarations
//
void initialize(void);
unsigned char read_switch_bank(void);
void write_to_LEDs(unsigned char ch);
//
// main program
//
void main(void)
{
unsigned char ch;
initialize();
while (1)
{
ch = read_switch_bank();
if ((ch != data) && (ch != 0xff)) // see if it has changed
{
data = ch;
write_to_LEDs(data);
}
}
}
//
// procedure and function definitions
//
void initialize(void)
{
data = 0xff; // starting value
DDRD = 0x00; // all inputs
DDRB = 0xff; // all outputs
PORTB = 0xff; // start by turning all LEDs off
}
unsigned char read_switch_bank(void)
{
unsigned char ch;
ch = PIND;
return (ch);
}
void write_to_LEDs(unsigned char ch)
{
PORTB = ch;
}