ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 13.06.2025

Просмотров: 2214

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

CodeVisionAVR

4.6 Variable Length Argument Lists Macros

These macros are defined in the file stdarg.h, located in the ..\INC subdirectory. This file must be #include -ed before using the macros.

void va_start(argptr, previous_par)

This macro, when used in a function with a variable length argument list, initializes the argptr pointer of va_list type, for subsequent use by the va_arg and va_end macros.

The previous_par argument must be the name of the function argument immediately preceding the optional arguments.

The va_start macro must be called prior to any access using the va_arg macro.

type va_arg(argptr, type)

This macro is used to extract successive arguments from the variable length argument list referenced by argptr.

type specifies the data type of the argument to extract.

The va_arg macro can be called only once for each argument. The order of the parameters in the argument list must be observed.

On the first call va_arg returns the first argument after the previous_par argument specified in the va_start macro. Subsequent calls to va_arg return the remaining arguments in succession.

void va_end(argptr)

This macro is used to terminate use of the variable length argument list pointer argptr, initialized using the va_start macro.

Example:

#include <stdarg.h>

/* declare a function with a variable number of arguments */ int sum_all(int nsum, ...)

{

va_list argptr; int i, result=0;

/* initialize argptr */ va_start(argptr,nsum);

/* add all the function arguments after nsum */ for (i=1; i <= nsum; i++)

/* add each argument */ result+=va_arg(argptr,int);

/* terminate the use of argptr */ va_end(argptr);

return result;

}

void main(void)

{

int s;

/* calculate the sum of 5 arguments */ s=sum_all(5,10,20,30,40,50);

}

© 1998-2007 HP InfoTech S.R.L.

Page 128

CodeVisionAVR

4.7 Non-local Jump Functions

These functions can execute a non-local goto.

They are usually used to pass control to an error recovery routine.

The prototypes for the non-local jump functions are placed in the file setjmp.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.

int setjmp(char *env)

This function saves the current CPU state (Y, SP, SREG registers and the current instruction address) in the env variable.

The CPU state can then be restored by subsequently calling the longjmp function. Execution is then resumed immediately after the setjmp function call.

The setjmp function will return 0 when the current CPU state is saved in the env variable.

If the function returns a value different from 0, it signals that a longjmp function was executed.

In this situation the returned value is the one that was passed as the retval argument to the longjmp function.

In order to preserve the local variables in the function where setjmp is used, these must be declared with the volatile attribute.

void longjmp(char *env, int retval)

This function restores the CPU state that was previously saved in the env variable by a call to setjmp. The retval argument holds the integer non-zero value that will be returned by setjmp after the call to longjmp. If a 0 value is passed as the retval argument then it will be substituted with 1.

In order to facilitate the usage of these functions, the setjmp.h header file also contains the definition of the jmp_buf data type, which is used when declaring the env variables.

Example:

#include <90s8515.h> #include <stdio.h> #include <setjmp.h>

/* declare the variable used to hold the CPU state */ jmp_buf cpu_state;

void foo(void)

{

printf("Now we will make a long jump to main()\n\r"); longjmp(cpu_state,1);

}

void main(void)

{

/* this local variable will be preserved after a longjmp */ volatile int i;

/* this local variable will not be preserved after a longjmp */ int j;

/* init the AT90S8515 UART */ UCR=8;

/* Baud=9600 @ 4MHz */ UBRR=25;

© 1998-2007 HP InfoTech S.R.L.

Page 129


CodeVisionAVR

if (setjmp(cpu_state)==0)

{

printf("First call to setjmp\n\r"); foo();

}

else

printf("We jumped here from foo()\n\r");

}

4.8 BCD Conversion Functions

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

unsigned char bcd2bin(unsigned char n)

Converts the number n from BCD representation to it's binary equivalent.

unsigned char bin2bcd(unsigned char n)

Converts the number n from binary representation to it's BCD equivalent.

The number n values must be from 0 to 99.

4.9 Gray Code Conversion Functions

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

unsigned char gray2binc(unsigned char n) unsigned char gray2bin(unsigned int n) unsigned char gray2binl(unsigned long n)

Convert the number n from Gray code representation to it's binary equivalent.

unsigned char bin2grayc(unsigned char n) unsigned char bin2gray(unsigned int n) unsigned char bin2grayl(unsigned long n)

Convert the number n from binary representation to it's Gray code equivalent.

© 1998-2007 HP InfoTech S.R.L.

Page 130

CodeVisionAVR

4.10 Memory Access Functions

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

void pokeb(unsigned int addr, unsigned char data)

this function writes the byte data to SRAM at address addr.

void pokew(unsigned int addr, unsigned int data)

this function writes the word data to SRAM at address addr.

The LSB is written at address addr and the MSB is written at address addr+1.

unsigned char peekb(unsigned int addr)

this function reads a byte located in SRAM at address addr.

unsigned int peekw (unsigned int addr)

this function reads a word located in SRAM at address addr.

The LSB is read from address addr and the MSB is read from address addr+1.

© 1998-2007 HP InfoTech S.R.L.

Page 131


CodeVisionAVR

4.11 LCD Functions

4.11.1 LCD Functions for displays with up to 2x40 characters

The LCD Functions are intended for easy interfacing between C programs and alphanumeric LCD modules built with the Hitachi HD44780 chip or equivalent.

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

Prior to #include -ing the lcd.h file, you must declare which microcontroller port is used for communication with the LCD module.

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

Example:

/* the LCD module is connected to PORTC */ #asm

.equ __lcd_port=0x15 #endasm

/* now you can include the LCD Functions */ #include <lcd.h>

The LCD module must be connected to the port bits as follows:

[LCD]

[AVR Port]

RS (pin4) ------

bit 0

RD (pin 5) ------

bit 1

EN (pin 6) ------

bit 2

DB4 (pin 11) ---

bit 4

DB5 (pin 12) ---

bit 5

DB6 (pin 13) ---

bit 6

DB7 (pin 14) ---

bit 7

You must also connect the LCD power supply and contrast control voltage, according to the data sheet.

The low level LCD Functions are:

void _lcd_ready(void)

waits until the LCD module is ready to receive data.

This function must be called prior to writing data to the LCD with the _lcd_write_data function.

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_ready();

_lcd_write_data(0xe);

© 1998-2007 HP InfoTech S.R.L.

Page 132

CodeVisionAVR

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: AT90S8515

Memory Model: SMALL

Data Stack Size: 128 bytes

Use an 2x16 alphanumeric LCD connected to the STK200+ PORTC header as follows:

[LCD]

[STK200+ 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 LCD is connected to PORTC outputs */ #asm

.equ __lcd_port=0x15 ;PORTC #endasm

/* include the LCD driver routines */ #include <lcd.h>

typedef unsigned char byte;

/* table for the user defined character

arrow that points to the top right corner */ flash byte char0[8]={

0b0000000,

0b0001111,

0b0000011,

0b0000101,

0b0001001,

0b0010000,

0b0100000,

0b1000000};

/* 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++);

}

© 1998-2007 HP InfoTech S.R.L.

Page 133


CodeVisionAVR

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:

unsigned char 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. The function returns 1 if the LCD module is detected and 0 if it is not.

This is the first function that must be called before using the other high level LCD Functions.

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 SRAM.

void lcd_putsf(char flash *str)

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

© 1998-2007 HP InfoTech S.R.L.

Page 134

CodeVisionAVR

4.11.2 LCD Functions for displays with 4x40 characters

The LCD Functions are intended for easy interfacing between C programs and alphanumeric LCD modules with 4x40 characters, built with the Hitachi HD44780 chip or equivalent.

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

Prior to #include -ing the lcd4x40.h file, you must declare which microcontroller port is used for communication with the LCD module.

Example:

/* the LCD module is connected to PORTC */ #asm

.equ __lcd_port=0x15 #endasm

/* now you can include the LCD Functions */ #include <lcd4x40.h>

The LCD module must be connected to the port bits as follows:

[LCD]

[AVR Port]

RS

(pin 11) ---

bit 0

RD

(pin 10) ---

bit 1

EN1 (pin 9) ----

bit 2

EN2 (pin 15) --

bit 3

DB4 (pin 4) ----

bit 4

DB5 (pin 3) ----

bit 5

DB6 (pin 2) ----

bit 6

DB7 (pin 1) ----

bit 7

You must also connect the LCD power supply and contrast control voltage, according to the data sheet.

The low level LCD Functions are:

void _lcd_ready(void)

waits until the LCD module is ready to receive data.

This function must be called prior to writing data to the LCD with the _lcd_write_data function.

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.

Prior calling the low level functions _lcd_ready and _lcd_write_data, the global variable _en1_msk must be set to LCD_EN1, respectively LCD_EN2, to select the upper, respectively lower half, LCD controller.

Example:

/* enables the displaying of the cursor on the upper half of the LCD */

_en1_msk=LCD_EN1; _lcd_ready(); _lcd_write_data(0xe);

© 1998-2007 HP InfoTech S.R.L.

Page 135


CodeVisionAVR

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

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

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:

unsigned char lcd_init(void)

initializes the LCD module, clears the display and sets the printing character position at row 0 and column 0. No cursor is displayed.

The function returns 1 if the LCD module is detected and 0 if it is not.

This is the first function that must be called before using the other high level LCD Functions.

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 SRAM.

void lcd_putsf(char flash *str)

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

© 1998-2007 HP InfoTech S.R.L.

Page 136