0% found this document useful (0 votes)
80 views13 pages

CodeVisionAVR User Manual (13tr)

Uploaded by

Nhân Mã Hiền
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views13 pages

CodeVisionAVR User Manual (13tr)

Uploaded by

Nhân Mã Hiền
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CodeVisionAVR

4.2 Comments
The character string "/*" marks the beginning of a comment.
The end of the comment is marked with "*/".
Example:

/* This is a comment */
/* This is a
multiple line comment */

One-line comments may be also defined by using the string "//".


Example:

// This is also a comment

Nested comments are not allowed.

© 1998-2014 HP InfoTech S.R.L. Page 137


CodeVisionAVR

4.3 Reserved Keywords


Following is a list of keywords reserved by the compiler.
These can not be used as identifier names.

__eeprom
__flash
__interrupt
__task
_Bool
_Bit
break
bit
bool
case
char
const
continue
default
defined
do
double
eeprom
else
enum
extern
flash
float
for
goto
if
inline
int
interrupt
long
register
return
short
signed
sizeof
sfrb
sfrw
static
struct
switch
typedef
union
unsigned
void
volatile
while

© 1998-2014 HP InfoTech S.R.L. Page 138


CodeVisionAVR

4.4 Identifiers
An identifier is the name you give to a variable, function, label or other object.
An identifier can contain letters (A...Z, a...z) and digits (0...9), as well as the underscore character (_).
However an identifier can only start with a letter or an underscore.
Case is significant; i.e. variable1 is not the same as Variable1.
Identifiers can have up to 64 characters.

4.5 Data Types


The following table lists all the data types supported by the CodeVisionAVR C compiler, their range of
possible values and their size:

Type Size (Bits) Range


bit, _Bit 1 0,1
bool, _Bool 8 0,1
char 8 -128 to 127
unsigned char 8 0 to 255
signed char 8 -128 to 127
int 16 -32768 to 32767
short int 16 -32768 to 32767
unsigned int 16 0 to 65535
signed int 16 -32768 to 32767
long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
signed long int 32 -2147483648 to 2147483647
float 32 ±1.175e-38 to ±3.402e38
double 32 ±1.175e-38 to ±3.402e38

The bit or _Bit data types are not allowed as the type of an array element, structure/union member,
function parameter or return value.

In order to use the bool data type, the stdbool.h header file must be #included in the source files
where this data type is referenced.

If the Project|Configure|C Compiler|Code Generation|char is unsigned option is checked or


#pragma uchar+ is used, then char has by default the range 0..255.

© 1998-2014 HP InfoTech S.R.L. Page 139


CodeVisionAVR

4.8 Defining Data Types


User defined data types are declared using the typedef reserved keyword.
The syntax is:

typedef <type definition> <identifier>;

The symbol name <identifier> is assigned to <type definition>.


Examples:

/* type definitions */
typedef unsigned char byte;
typedef struct {
int a;
char b[5];
} struct_type;

/* variable declarations */
byte alfa;

/* structure stored in RAM */


struct_type struct1;

/* structure stored in FLASH */


flash struct_type struct2;

/* structure stored in EEPROM */


eeprom struct_type struct3;

© 1998-2014 HP InfoTech S.R.L. Page 156


CodeVisionAVR

4.9 Type Conversions


In an expression, if the two operands of a binary operator are of different types, then the compiler will
convert one of the operands into the type of the other.
The compiler uses the following rules:

If either of the operands is of type float then the other operand is converted to the same type.

If either of the operands is of type long int or unsigned long int then the other operand is converted
to the same type.

Otherwise, if either of the operands is of type int or unsigned int then the other operand is converted
to the same type.

Thus char type or unsigned char type gets the lowest priority.

Using casting you can change these rules.


Example:

void main(void) {
int a, c;
long b;
/* The long integer variable b will be treated here as an integer */
c=a+(int) b;
}

It is important to note that if the Project|Configure|C Compiler|Code Generation|Promote char to


int option isn't checked or the #pragma promotechar+ isn't used, the char, respectively unsigned
char, type operands are not automatically promoted to int , respectively unsigned int, as in compilers
targeted for 16 or 32 bit CPUs.
This helps writing more size and speed efficient code for an 8 bit CPU like the AVR.
To prevent overflow on 8 bit addition or multiplication, casting may be required.
The compiler issues warnings in these situations.
Example:

void main(void) {
unsigned char a=30;
unsigned char b=128;
unsigned int c;

/* This will generate an incorrect result, because the multiplication


is done on 8 bits producing an 8 bit result, which overflows.
Only after the multiplication, the 8 bit result is promoted to
unsigned int */
c=a*b;

/* Here casting forces the multiplication to be done on 16 bits,


producing an 16 bit result, without overflow */
c=(unsigned int) a*b;
}

© 1998-2014 HP InfoTech S.R.L. Page 157


CodeVisionAVR

The compiler behaves differently for the following operators:


+=
-=
*=
/=
%=
&=
|=
^=
<<=
>>=

For these operators, the result is to be written back onto the left-hand side operand (which must be a
variable). So the compiler will always convert the right hand side operand into the type of left-hand
side operand.

4.10 Operators
The compiler supports the following operators:

+ -
* /
% ++
-- =
== ~
! !=
< >
<= >=
& &&
| ||
^ ? :
<< >>
-= +=
/= %=
&= *=
^= |=
>>= <<=
sizeof

© 1998-2014 HP InfoTech S.R.L. Page 158


CodeVisionAVR

After being freed, the memory block is available for new allocation.
If ptr is null then it is ignored.

5.4 Mathematical Functions


The prototypes for these functions are placed in the file math.h, located in the .\INC subdirectory. This
file must be #include -d before using the functions.

signed char cmax(signed char a, signed char b)

returns the maximum value of bytes a and b.

int max(int a, int b)

returns the maximum value of integers a and b.

long int lmax(long int a, long int b)

returns the maximum value of long integers a and b.

float fmax(float a, float b)

returns the maximum value of floating point numbers a and b.

signed char cmin(signed char a, signed char b)

returns the minimum value of bytes a and b.

int min(int a, int b)

returns the minimum value of integers a and b.

long int lmin(long int a, long int b)

returns the minimum value of long integers a and b.

float fmin(float a, float b)

returns the minimum value of floating point numbers a and b.

signed char csign(signed char x)

returns -1, 0 or 1 if the byte x is negative, zero or positive.

signed char sign(int x)

returns -1, 0 or 1 if the integer x is negative, zero or positive

signed char lsign(long int x)

returns -1, 0 or 1 if the long integer x is negative, zero or positive.

signed char fsign(float x)

returns -1, 0 or 1 if the floating point number x is negative, zero or positive.

© 1998-2014 HP InfoTech S.R.L. Page 203


CodeVisionAVR

unsigned char isqrt(unsigned int x)

returns the square root of the unsigned integer x.

unsigned int lsqrt(unsigned long x)

returns the square root of the unsigned long integer x.

float sqrt(float x)

returns the square root of the positive floating point number x.

float floor(float x)

returns the smallest integer value of the floating point number x.

float ceil(float x)

returns the largest integer value of the floating point number x.

float fmod(float x, float y)

returns the remainder of x divided by y.

float modf(float x, float *ipart)

splits the floating point number x into integer and fractional components.
The fractional part of x is returned as a signed floating point number.
The integer part is stored as floating point number at ipart.

float ldexp(float x, int expn)


expn
returns x * 2 .

float frexp(float x, int *expn)

returns the mantissa and exponent of the floating point number x.

float exp(float x)

returns ex .

float log(float x)

returns the natural logarithm of the floating point number x.

float log10(float x)

returns the base 10 logarithm of the floating point number x.

float pow(float x, float y)


y
returns x .

float sin(float x)

returns the sine of the floating point number x, where the angle is expressed in radians.

© 1998-2014 HP InfoTech S.R.L. Page 204


CodeVisionAVR

float cos(float x)

returns the cosine of the floating point number x, where the angle is expressed in radians.

float tan(float x)

returns the tangent of the floating point number x, where the angle is expressed in radians.

float sinh(float x)

returns the hyperbolic sine of the floating point number x, where the angle is expressed in radians.

float cosh(float x)

returns the hyperbolic cosine of the floating point number x, where the angle is expressed in
radians.

float tanh(float x)

returns the hyperbolic tangent of the floating point number x, where the angle is expressed in
radians.

float asin(float x)

returns the arc sine of the floating point number x (in the range -PI/2 to PI/2).
x must be in the range -1 to 1.

float acos(float x)

returns the arc cosine of the floating point number x (in the range 0 to PI).
x must be in the range -1 to 1.

float atan(float x)

returns the arc tangent of the floating point number x (in the range -PI/2 to PI/2).

float atan2(float y, float x)

returns the arc tangent of the floating point numbers y/x (in the range -PI to PI).

© 1998-2014 HP InfoTech S.R.L. Page 205


CodeVisionAVR

5.11 Alphanumeric LCD Functions

5.11.1 LCD Functions for displays with up to 2x40 characters


The Alphanumeric LCD Functions are intended for easy interfacing between C programs and
alphanumeric LCD modules built with the Hitachi HD44780 controller or equivalent.
The prototypes for these functions are placed in the file alcd.h, located in the .\INC subdirectory. This
file must be #include -d before using the functions.
For LCD modules that use the Samsung KS0073 controller the alcd_ks0073.h header file must be
used.

The LCD functions do support both the XMEGA and non-XMEGA chips.

The following LCD formats are supported in alcd.h: 1x8, 2x12, 3x12, 1x16, 2x16, 2x20, 4x20, 2x24
and 2x40 characters.

The allocation of LCD module signals to the I/O ports must be specified in the Project|Configure|C
Compiler|Libraries|Alphanumeric LCD menu.
The LCD power supply and contrast control voltage must also be connected according to the module
data sheet.

The low level LCD Functions are:

void _lcd_write_data(unsigned char data)

writes the byte data to the LCD instruction register.


This function may be used for modifying the LCD configuration.
Example:

/* enables the displaying of the cursor */


_lcd_write_data(0xe);

void lcd_write_byte(unsigned char addr, unsigned char data);

writes a byte to the LCD character generator or display RAM.

Example:

/* LCD user defined characters


Chip: ATmega8515

Use an 2x16 alphanumeric LCD connected


to the STK600 PORTC header as follows:

[LCD] [STK600 PORTC HEADER]


1 GND- 9 GND
2 +5V- 10 VCC
3 VLC- LCD HEADER Vo
4 RS - 1 PC0
5 RD - 2 PC1
6 EN - 3 PC2
11 D4 - 5 PC4
12 D5 - 6 PC5
13 D6 - 7 PC6
14 D7 - 8 PC7

The connections must be specified in the


Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu */

© 1998-2014 HP InfoTech S.R.L. Page 216


CodeVisionAVR

/* include the LCD driver routines */


#include <alcd.h>

typedef unsigned char byte;

/* table for the user defined character


arrow that points to the top right corner */
flash byte char0[8]={
0b10000000,
0b10001111,
0b10000011,
0b10000101,
0b10001001,
0b10010000,
0b10100000,
0b11000000};

/* function used to define user characters */


void define_char(byte flash *pc,byte char_code)
{
byte i,a;
a=(char_code<<3) | 0x40;
for (i=0; i<8; i++) lcd_write_byte(a++,*pc++);
}

void main(void)
{
/* initialize the LCD for 2 lines & 16 columns */
lcd_init(16);

/* define user character 0 */


define_char(char0,0);

/* switch to writing in Display RAM */


lcd_gotoxy(0,0);
lcd_putsf("User char 0:");

/* display used defined char 0 */


lcd_putchar(0);

while (1); /* loop forever */


}

unsigned char lcd_read_byte(unsigned char addr);

reads a byte from the LCD character generator or display RAM.

The high level LCD Functions are:

void lcd_init(unsigned char lcd_columns)

initializes the LCD module, clears the display and sets the printing character position at row 0 and
column 0. The numbers of columns of the LCD must be specified (e.g. 16). No cursor is displayed.
This is the first function that must be called before using the other high level LCD Functions.

© 1998-2014 HP InfoTech S.R.L. Page 217


CodeVisionAVR

void lcd_clear(void)

clears the LCD and sets the printing character position at row 0 and column 0.

void lcd_gotoxy(unsigned char x, unsigned char y)

sets the current display position at column x and row y. The row and column numbering starts
from 0.

void lcd_putchar(char c)

displays the character c at the current display position.

void lcd_puts(char *str)

displays at the current display position the string str, located in RAM.

void lcd_putsf(char flash *str)

displays at the current display position the string str, located in FLASH.

void lcd_putse(char eeprom *str)

displays at the current display position the string str, located in EEPROM.

© 1998-2014 HP InfoTech S.R.L. Page 218


CodeVisionAVR

5.21 Delay Functions


These functions are intended for generating delays in C programs.
The prototypes for these functions are placed in the file delay.h, located in the .\INC subdirectory. This
file must be #include -d before using the functions.
Before calling the functions the interrupts must be disabled, otherwise the delays will be much longer
then expected.
Also it is very important to specify the correct AVR chip clock frequency in the Project|Configure|C
Compiler|Code Generation menu.

The functions are:

void delay_us(unsigned int n)

generates a delay of n µseconds. n must be a constant expression.

void delay_ms(unsigned int n)

generates a delay of n milliseconds.


This function automatically resets the wtachdog timer every 1ms by generating the wdr instruction.

Example:

void main(void) {
/* disable interrupts */
#asm("cli")

/* 100µs delay */
delay_us(100);

/* ............. */

/* 10ms delay */
delay_ms(10);

/* enable interrupts */
#asm("sei")

/* ............. */
}

© 1998-2014 HP InfoTech S.R.L. Page 424

You might also like