Sign in

username:

password:



Not a member?

Search avrclub



Search tips

Subscribe to avrclub



avrclub by Keywords

AT90S2313 | AT90S8515 | ATMega | ATmega128 | ECL | FETS | IAR | Keyboard | LCD | STK50 | TMOS | UART

Ads

Discussion Groups

Discussion Groups | AVRclub | Re: Re: [AVR club] Problem with function pointers AVR simulation

Atmel AVR Microcontroller discussion group.

[AVR club] Problem with function pointers AVR simulation - Aravind - Apr 8 10:39:33 2008

Hi, i'm developing some Menu structures for my LCD. Each menu=20
display=20
is associated with a state and each state is associated with a=20
handling function. I have created an array functions to handle each=20
state and need to call the function depending on the state. When i=20
simulate in Avr studio, the control does not jump to the called=20
function instead carries on as if FPtr is just another variable. On=20
examining the listing file, i find the function pointers do not=20
point=20
to the actual location of the function and also on adding watch to=20
the=20
function pointer array, avr studio shows that the pointers point to=20
a=20
location on SRAM rather than FLASH. I'm new to using pointers, the=20
problem could be a silly mistake, please help. Thanks.=20

#include =20
#include =20
#include =20
#include =20
#include "global.h"=20
#include "twi.h"=20
#include "timertick.h"=20
#include "ks0066u.h"=20
#include "uart_com.h"=20
#include "spi_comm.h"=20
typedef void (*FuncPtr)(void);=20
//function pointer=20
FuncPtr FPtr;=20
#define front 1=20
#define rear 2=20
#define csub 3=20
#define headphone 4=20
#define all_reg 7=20
#define mux_select 0=20
#define gain 1=20
#define volume 2=20
#define bass 3=20
#define mid 4=20
#define treble 5=20
#define atten_l 6=20
#define atten_r 7=20
#define gain_max 0x0F=20
#define gain_min 0x00=20
#define volume_max 0x00=20
#define volume_min 0x2F=20
#define mute 0x38=20
#define atten_max 0x4F=20
#define atten_min 0x00=20
#define atten_mute 0x78=20
#define equ_max 0x0F=20
#define equ_min 0x00=20
//=20
*********************************************************************
******=AD**=20
//***************************Display State=20
Definitions*************************=20
#define power_dwn_s 0=20
#define main_disp_s 1=20
#define main_menu_s 2=20
#define speak_set_s 3=20
#define sourc_set_s 4=20
#define front_sel_s 5=20
#define rear__sel_s 6=20
#define csub__sel_s 7=20
#define headp_sel_s 8=20
#define volum_set_s 9=20
#define mastr_set_s 10=20
#define chanl_vol_s 11=20
#define front_vol_s 12=20
#define rear__vol_s 13=20
#define csub__vol_s 14=20
#define headp_vol_s 15=20
#define gain__set_s 16=20
#define gainf_set_s 17=20
#define gainr_set_s 18=20
#define gainc_set_s 19=20
#define gainh_set_s 20=20
#define equlz_set_s 21=20
#define equf__set_s 22=20
#define equfb_set_s 23=20
#define equfm_set_s 24=20
#define equft_set_s 25=20
#define equrb_set_s 26=20
#define equrm_set_s 27=20
#define equrt_set_s 28=20
#define equcb_set_s 29=20
#define equcm_set_s 30=20
#define equct_set_s 31=20
#define equhb_set_s 32=20
#define equhm_set_s 33=20
#define equht_set_s 34=20
#define disp__set_s 35=20
#define LCDBL_set_s 36=20
#define VUdis_set_s 37=20
#define systm_set_s 38=20
#define temp__dis_s 39=20
#define fansp_set_s 40=20
#define fanma_set_s 41=20
//=20
*********************************************************************
******=AD**=20
//***************************Function=20
Definitions******************************=20
void cmd_proc(void);=20
bool call_bindfunc(void);=20
void call_def_func(void);=20
//=20
*********************************************************************
******=AD**=20
//*******************State Handling Function=20
Definitions***********************=20
void power_dwn_f(void);=20
void main_disp_f(void);=20
void main_menu_f(void);=20
//=20
*********************************************************************
******=AD**=20
//***************************Global Variable=20
Declarations**********************=20
//***************************Function Pointer=20
Declarations*********************=20
const FuncPtr dis_handle[] PROGMEM =3D=20
{power_dwn_f,main_disp_f,main_menu_f};=20
//********************* Function - Key Binding Table Declarations=20
*************=20
//const uint8_t power_dwn_bk[] =3D {0x02,0x03,0xFE};=20
//const uint8_t main_disp_bk[] =3D=20
{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE};=20
const uint8_t fkeybind[60][60] PROGMEM=3D=20=20=20
{{0x02,0x03,0xFE}, //=20
power_dwn_s=20
=
=20
{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE}}; //main_disp_s=20
//***************************Other Variable=20
Declarations***********************=20
const uint8_t equ_range[15] =3D=20
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,=20
=
=20
0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08};=20
const uint8_t def_7439[8] =3D=20
{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};=20
const uint8_t defcsub_7439[8] =3D=20
{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};=20
uint8_t front_7439[8],rear_7439[7],csub_7439[7],headp_7439[7];=20
uint8_t=20
prev_front_7439[8],prev_rear_7439[7],prev_csub_7439
[7],prev_headp_7439[7];=20
bool noslave;=20
unsigned int d_state,pstate,opt_set;=20
unsigned int cmd,prev1_cmd,prev2_cmd;=20
volatile bool new_cmd;=20
//=20
*********************************************************************
******=AD**=20
//***************************LCD Display=20
Strings*******************************=20
const char main_disp1[20] PROGMEM =3D " APAG2 ";=20
const char volume1[20] PROGMEM =3D " Master Volume: ";=20
//=20
*********************************************************************
******=AD**=20
//***************************Main=20
Entry****************************************=20
int main(void)=20
{=20
char init1[20] =3D " Initialized..... ";=20
// char init2[20] =3D " Volume up ";=20
// char init3[20] =3D " Volume down ";=20
sei();=20
systemtick_init();=20
lcd_pwminit();=20
spisetup();=20
spitx(0xAA);=20
i2c_init();=20
set_7439all(front,(uint8_t*) &def_7439);=20
set_7439all(rear,(uint8_t*) &def_7439);=20
set_7439all(csub,(uint8_t*) &defcsub_7439);=20
set_7439all(headphone,(uint8_t*) &def_7439);=20
init_uart0();=20
init_uart1();=20
// txuchar1('R');=20
// txuchar1('e');=20
// txuchar1('a');=20
// txuchar1('d');=20
// txuchar1('y');=20
// while(!uart0_ch_rx)=20
// {=20
// txuchar0(0xAE);=20
// txuchar1(0xAE);=20
// }=20
uart0_ch_rx =3D false;=20
if(!(uart0_ch =3D=3D 0xDD))=20
noslave =3D false;=20
else=20
noslave =3D true;=20
txuchar1(noslave);=20
lcd_init();=20
lcd_write(1,init1);=20
d_state =3D power_dwn_s;=20
while(1)=20
{=20
if(uart0_ch_rx)=20
cmd_proc();=20
if(new_cmd)=20
if(!call_bindfunc())=20
call_def_func();=20
}=20
/* while(1){=20
while(!uart0_ch_rx);=20
uart0_ch_rx =3D false;=20
if(uart0_ch =3D=3D 0xCC)=20
{=20
while(!uart0_ch_rx);=20
uart0_ch_rx =3D false;=20
if(uart0_ch =3D=3D 0x70)=20
{=20
while(!uart0_ch_rx);=20
uart0_ch_rx =3D false;=20
if(uart0_ch =3D=3D 0x07)=20
lcd_write(2,init3);=20
else if (uart0_ch =3D=3D 0x0B)=20
lcd_write(2,init2);=20
}=20
}=20
}=20
txuchar0(0xEA);*/=20
while(1);=20

}=20
void cmd_proc(void)=20
{=20
uart0_ch_rx =3D false;=20

if((uart0_ch > 0) && (uart0_ch <=3D0x2E))=20
{=20
new_cmd =3D true;=20
cmd =3D uart0_ch;=20
}=20

}=20
bool call_bindfunc(void)=20
{=20
unsigned int i,n;=20
n =3D 0;=20
for(;;)=20
{=20
i =3D pgm_read_byte(&fkeybind[d_state][n]);=20
n++;=20
if(i =3D=3D cmd)=20
{=20
FPtr =3D (FuncPtr)pgm_read_word(*dis_handle
[d_state]);=20
return true;=20
}else if(i =3D=3D 0xFE)=20
break;=20
}=20
return false;=20
}=20
void call_def_func(void)=20
{=20
int i;=20
i =3D12;=20
}=20
void main_disp_f(void)=20
{=20

}=20
void main_menu_f(void)=20
{=20

}=20
void power_dwn_f(void)=20
{=20
}
------------------------------------



(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )


Re: [AVR club] Problem with function pointers AVR simulation - reza asghari - Apr 10 8:29:04 2008

Hi,
Here is some codes that I implemented in IAR-AVR v4.12 .
Good luck.

/*********************************************************************
Pointer to Functions type definitions
*********************************************************************/
typedef void (*pt2Function) (void);
typedef void (*pt2CharFunction) (char x_char);

// Some functions
void ShowSubMenu1_1(void);
void ShowSubMenu1_2(void);

pt2Function SubMenu_1_ShowValFunc[2] =
{
ShowSubMenu1_1 ,
ShowSubMenu1_2 ,
};

void ShowSubMenu1_1(void)
{
// Some codes
}
void ShowSubMenu1_2(void)
{
// Some codes
}

// Usage example:
SubMenu_1_ShowValFunc[x](); // x = 0 or 1

Aravind wrote:
Hi, i'm developing some Menu structures for my LCD. Each menu
display
is associated with a state and each state is associated with a
handling function. I have created an array functions to handle each
state and need to call the function depending on the state. When i
simulate in Avr studio, the control does not jump to the called
function instead carries on as if FPtr is just another variable. On
examining the listing file, i find the function pointers do not
point
to the actual location of the function and also on adding watch to
the
function pointer array, avr studio shows that the pointers point to
a
location on SRAM rather than FLASH. I'm new to using pointers, the
problem could be a silly mistake, please help. Thanks.

#include
#include
#include
#include
#include "global.h"
#include "twi.h"
#include "timertick.h"
#include "ks0066u.h"
#include "uart_com.h"
#include "spi_comm.h"

typedef void (*FuncPtr)(void);
//function pointer
FuncPtr FPtr;

#define front 1
#define rear 2
#define csub 3
#define headphone 4

#define all_reg 7

#define mux_select 0
#define gain 1
#define volume 2
#define bass 3
#define mid 4
#define treble 5
#define atten_l 6
#define atten_r 7

#define gain_max 0x0F
#define gain_min 0x00

#define volume_max 0x00
#define volume_min 0x2F

#define mute 0x38

#define atten_max 0x4F
#define atten_min 0x00
#define atten_mute 0x78

#define equ_max 0x0F
#define equ_min 0x00
//
*********************************************************************
******­**
//***************************Display State
Definitions*************************
#define power_dwn_s 0

#define main_disp_s 1

#define main_menu_s 2

#define speak_set_s 3

#define sourc_set_s 4

#define front_sel_s 5
#define rear__sel_s 6
#define csub__sel_s 7
#define headp_sel_s 8

#define volum_set_s 9

#define mastr_set_s 10
#define chanl_vol_s 11
#define front_vol_s 12
#define rear__vol_s 13
#define csub__vol_s 14
#define headp_vol_s 15

#define gain__set_s 16

#define gainf_set_s 17
#define gainr_set_s 18
#define gainc_set_s 19
#define gainh_set_s 20

#define equlz_set_s 21

#define equf__set_s 22
#define equfb_set_s 23
#define equfm_set_s 24
#define equft_set_s 25

#define equrb_set_s 26
#define equrm_set_s 27
#define equrt_set_s 28

#define equcb_set_s 29
#define equcm_set_s 30
#define equct_set_s 31

#define equhb_set_s 32
#define equhm_set_s 33
#define equht_set_s 34

#define disp__set_s 35
#define LCDBL_set_s 36
#define VUdis_set_s 37

#define systm_set_s 38
#define temp__dis_s 39
#define fansp_set_s 40
#define fanma_set_s 41
//
*********************************************************************
******­**
//***************************Function
Definitions******************************

void cmd_proc(void);
bool call_bindfunc(void);
void call_def_func(void);
//
*********************************************************************
******­**
//*******************State Handling Function
Definitions***********************
void power_dwn_f(void);
void main_disp_f(void);
void main_menu_f(void);
//
*********************************************************************
******­**
//***************************Global Variable
Declarations**********************
//***************************Function Pointer
Declarations*********************
const FuncPtr dis_handle[] PROGMEM =
{power_dwn_f,main_disp_f,main_menu_f};
//********************* Function - Key Binding Table Declarations
*************
//const uint8_t power_dwn_bk[] = {0x02,0x03,0xFE};
//const uint8_t main_disp_bk[] =
{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE};

const uint8_t fkeybind[60][60] PROGMEM=
{{0x02,0x03,0xFE}, //
power_dwn_s

{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE}}; //main_disp_s
//***************************Other Variable
Declarations***********************
const uint8_t equ_range[15] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,

0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08};

const uint8_t def_7439[8] =
{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};
const uint8_t defcsub_7439[8] =
{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};

uint8_t front_7439[8],rear_7439[7],csub_7439[7],headp_7439[7];
uint8_t
prev_front_7439[8],prev_rear_7439[7],prev_csub_7439
[7],prev_headp_7439[7];

bool noslave;

unsigned int d_state,pstate,opt_set;

unsigned int cmd,prev1_cmd,prev2_cmd;
volatile bool new_cmd;

//
*********************************************************************
******­**
//***************************LCD Display
Strings*******************************
const char main_disp1[20] PROGMEM = " APAG2 ";
const char volume1[20] PROGMEM = " Master Volume: ";

//
*********************************************************************
******­**
//***************************Main
Entry****************************************

int main(void)
{

char init1[20] = " Initialized..... ";
// char init2[20] = " Volume up ";
// char init3[20] = " Volume down ";
sei();

systemtick_init();
lcd_pwminit();
spisetup();
spitx(0xAA);

i2c_init();
set_7439all(front,(uint8_t*) &def_7439);
set_7439all(rear,(uint8_t*) &def_7439);
set_7439all(csub,(uint8_t*) &defcsub_7439);
set_7439all(headphone,(uint8_t*) &def_7439);

init_uart0();
init_uart1();

// txuchar1('R');
// txuchar1('e');
// txuchar1('a');
// txuchar1('d');
// txuchar1('y');

// while(!uart0_ch_rx)
// {
// txuchar0(0xAE);
// txuchar1(0xAE);
// }
uart0_ch_rx = false;
if(!(uart0_ch == 0xDD))
noslave = false;
else
noslave = true;
txuchar1(noslave);
lcd_init();
lcd_write(1,init1);

d_state = power_dwn_s;

while(1)
{
if(uart0_ch_rx)
cmd_proc();

if(new_cmd)
if(!call_bindfunc())
call_def_func();
}

/* while(1){
while(!uart0_ch_rx);
uart0_ch_rx = false;
if(uart0_ch == 0xCC)
{
while(!uart0_ch_rx);
uart0_ch_rx = false;
if(uart0_ch == 0x70)
{
while(!uart0_ch_rx);
uart0_ch_rx = false;
if(uart0_ch == 0x07)
lcd_write(2,init3);
else if (uart0_ch == 0x0B)
lcd_write(2,init2);
}
}
}
txuchar0(0xEA);*/
while(1);

}

void cmd_proc(void)
{
uart0_ch_rx = false;

if((uart0_ch > 0) && (uart0_ch <=0x2E))
{
new_cmd = true;
cmd = uart0_ch;
}

}

bool call_bindfunc(void)
{
unsigned int i,n;
n = 0;
for(;;)
{
i = pgm_read_byte(&fkeybind[d_state][n]);
n++;
if(i == cmd)
{
FPtr = (FuncPtr)pgm_read_word(*dis_handle
[d_state]);
return true;
}else if(i == 0xFE)
break;
}
return false;

}

void call_def_func(void)
{
int i;
i =12;

}

void main_disp_f(void)
{

}

void main_menu_f(void)
{

}

void power_dwn_f(void)
{
}

__________________________________________________

[Non-text portions of this message have been removed]
------------------------------------



(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: Re: [AVR club] Problem with function pointers AVR simulation - raj - Apr 12 22:07:42 2008

Hi Reza,

This is kundan, I am also new with AVR. Can you help me. I have =
everything to work with stk500,AVRISPmk2 and all the chips.can u send me so=
me C files so that i can work on stk board and also how to confifure timers=
in different way.Are u from india.I will wait for your reply.

Best regards,
kundan
FAE
E.E India pte Ltd.
New Delhi
+91-11-9811675688 =A0
On Thu, 10 Apr 2008 reza asghari wrote :
>Hi,
> Here is some codes that I implemented in IAR-AVR v4.12 .
> Good luck.
>
> /*********************************************************************
> Pointer to Functions type definitions
>*********************************************************************/
>typedef void (*pt2Function) (void);
>typedef void (*pt2CharFunction) (char x_char);
>
> // Some functions
> void ShowSubMenu1_1(void);
>void ShowSubMenu1_2(void);
>
>pt2Function SubMenu_1_ShowValFunc[2] =3D
>{
> ShowSubMenu1_1 ,
> ShowSubMenu1_2 ,
>};
>
>void ShowSubMenu1_1(void)
>{
> // Some codes
>}
> void ShowSubMenu1_2(void)
>{
> // Some codes
>}
>
> // Usage example:
> SubMenu_1_ShowValFunc[x](); // x =3D 0 or 1
>Aravind wrote:
> Hi, i'm developing some Menu structures for my LCD. Each menu
>display
>is associated with a state and each state is associated with a
>handling function. I have created an array functions to handle each
>state and need to call the function depending on the state. When i
>simulate in Avr studio, the control does not jump to the called
>function instead carries on as if FPtr is just another variable. On
>examining the listing file, i find the function pointers do not
>point
>to the actual location of the function and also on adding watch to
>the
>function pointer array, avr studio shows that the pointers point to
>a
>location on SRAM rather than FLASH. I'm new to using pointers, the
>problem could be a silly mistake, please help. Thanks.
>
>#include
>#include
>#include
>#include
>#include "global.h"
>#include "twi.h"
>#include "timertick.h"
>#include "ks0066u.h"
>#include "uart_com.h"
>#include "spi_comm.h"
>
>typedef void (*FuncPtr)(void);
>//function pointer
>FuncPtr FPtr;
>
>#define front 1
>#define rear 2
>#define csub 3
>#define headphone 4
>
>#define all_reg 7
>
>#define mux_select 0
>#define gain 1
>#define volume 2
>#define bass 3
>#define mid 4
>#define treble 5
>#define atten_l 6
>#define atten_r 7
>
>#define gain_max 0x0F
>#define gain_min 0x00
>
>#define volume_max 0x00
>#define volume_min 0x2F
>
>#define mute 0x38
>
>#define atten_max 0x4F
>#define atten_min 0x00
>#define atten_mute 0x78
>
>#define equ_max 0x0F
>#define equ_min 0x00
>//
>*********************************************************************
>******=AD**
>//***************************Display State
>Definitions*************************
>#define power_dwn_s 0
>
>#define main_disp_s 1
>
>#define main_menu_s 2
>
>#define speak_set_s 3
>
>#define sourc_set_s 4
>
>#define front_sel_s 5
>#define rear__sel_s 6
>#define csub__sel_s 7
>#define headp_sel_s 8
>
>#define volum_set_s 9
>
>#define mastr_set_s 10
>#define chanl_vol_s 11
>#define front_vol_s 12
>#define rear__vol_s 13
>#define csub__vol_s 14
>#define headp_vol_s 15
>
>#define gain__set_s 16
>
>#define gainf_set_s 17
>#define gainr_set_s 18
>#define gainc_set_s 19
>#define gainh_set_s 20
>
>#define equlz_set_s 21
>
>#define equf__set_s 22
>#define equfb_set_s 23
>#define equfm_set_s 24
>#define equft_set_s 25
>
>#define equrb_set_s 26
>#define equrm_set_s 27
>#define equrt_set_s 28
>
>#define equcb_set_s 29
>#define equcm_set_s 30
>#define equct_set_s 31
>
>#define equhb_set_s 32
>#define equhm_set_s 33
>#define equht_set_s 34
>
>#define disp__set_s 35
>#define LCDBL_set_s 36
>#define VUdis_set_s 37
>
>#define systm_set_s 38
>#define temp__dis_s 39
>#define fansp_set_s 40
>#define fanma_set_s 41
>//
>*********************************************************************
>******=AD**
>//***************************Function
>Definitions******************************
>
>void cmd_proc(void);
>bool call_bindfunc(void);
>void call_def_func(void);
>//
>*********************************************************************
>******=AD**
>//*******************State Handling Function
>Definitions***********************
>void power_dwn_f(void);
>void main_disp_f(void);
>void main_menu_f(void);
>//
>*********************************************************************
>******=AD**
>//***************************Global Variable
>Declarations**********************
>//***************************Function Pointer
>Declarations*********************
>const FuncPtr dis_handle[] PROGMEM =3D
>{power_dwn_f,main_disp_f,main_menu_f};
>//********************* Function - Key Binding Table Declarations
>*************
>//const uint8_t power_dwn_bk[] =3D {0x02,0x03,0xFE};
>//const uint8_t main_disp_bk[] =3D
>{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE};
>
>const uint8_t fkeybind[60][60] PROGMEM=3D
>{{0x02,0x03,0xFE}, //
>power_dwn_s
>
>{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE}}; //main_disp_s
>//***************************Other Variable
>Declarations***********************
>const uint8_t equ_range[15] =3D
>{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
>
>0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08};
>
>const uint8_t def_7439[8] =3D
>{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};
>const uint8_t defcsub_7439[8] =3D
>{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};
>
>uint8_t front_7439[8],rear_7439[7],csub_7439[7],headp_7439[7];
>uint8_t
>prev_front_7439[8],prev_rear_7439[7],prev_csub_7439
>[7],prev_headp_7439[7];
>
>bool noslave;
>
>unsigned int d_state,pstate,opt_set;
>
>unsigned int cmd,prev1_cmd,prev2_cmd;
>volatile bool new_cmd;
>
>//
>*********************************************************************
>******=AD**
>//***************************LCD Display
>Strings*******************************
>const char main_disp1[20] PROGMEM =3D " APAG2 ";
>const char volume1[20] PROGMEM =3D " Master Volume: ";
>
>//
>*********************************************************************
>******=AD**
>//***************************Main
>Entry****************************************
>
>int main(void)
>{
>
>char init1[20] =3D " Initialized..... ";
>// char init2[20] =3D " Volume up ";
>// char init3[20] =3D " Volume down ";
>sei();
>
>systemtick_init();
>lcd_pwminit();
>spisetup();
>spitx(0xAA);
>
>i2c_init();
>set_7439all(front,(uint8_t*) &def_7439);
>set_7439all(rear,(uint8_t*) &def_7439);
>set_7439all(csub,(uint8_t*) &defcsub_7439);
>set_7439all(headphone,(uint8_t*) &def_7439);
>
>init_uart0();
>init_uart1();
>
>// txuchar1('R');
>// txuchar1('e');
>// txuchar1('a');
>// txuchar1('d');
>// txuchar1('y');
>
>// while(!uart0_ch_rx)
>// {
>// txuchar0(0xAE);
>// txuchar1(0xAE);
>// }
>uart0_ch_rx =3D false;
>if(!(uart0_ch =3D=3D 0xDD))
>noslave =3D false;
>else
>noslave =3D true;
>txuchar1(noslave);
>lcd_init();
>lcd_write(1,init1);
>
>d_state =3D power_dwn_s;
>
>while(1)
>{
>if(uart0_ch_rx)
>cmd_proc();
>
>if(new_cmd)
>if(!call_bindfunc())
>call_def_func();
>}
>
>/* while(1){
>while(!uart0_ch_rx);
>uart0_ch_rx =3D false;
>if(uart0_ch =3D=3D 0xCC)
>{
>while(!uart0_ch_rx);
>uart0_ch_rx =3D false;
>if(uart0_ch =3D=3D 0x70)
>{
>while(!uart0_ch_rx);
>uart0_ch_rx =3D false;
>if(uart0_ch =3D=3D 0x07)
>lcd_write(2,init3);
>else if (uart0_ch =3D=3D 0x0B)
>lcd_write(2,init2);
>}
>}
>}
>txuchar0(0xEA);*/
>while(1);
>
>}
>
>void cmd_proc(void)
>{
>uart0_ch_rx =3D false;
>
>if((uart0_ch > 0) && (uart0_ch <=3D0x2E))
>{
>new_cmd =3D true;
>cmd =3D uart0_ch;
>}
>
>}
>
>bool call_bindfunc(void)
>{
>unsigned int i,n;
>n =3D 0;
>for(;;)
>{
>i =3D pgm_read_byte(&fkeybind[d_state][n]);
>n++;
>if(i =3D=3D cmd)
>{
>FPtr =3D (FuncPtr)pgm_read_word(*dis_handle
>[d_state]);
>return true;
>}else if(i =3D=3D 0xFE)
>break;
>}
>return false;
>
>}
>
>void call_def_func(void)
>{
>int i;
>i =3D12;
>
>}
>
>void main_disp_f(void)
>{
>
>}
>
>void main_menu_f(void)
>{
>
>}
>
>void power_dwn_f(void)
>{
>}
>
> __________________________________________________
>[Non-text portions of this message have been removed]
>
[Non-text portions of this message have been removed]
------------------------------------



(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: Re: [AVR club] Problem with function pointers AVR simulation - reza asghari - Apr 13 11:25:02 2008

Hi,
I don't work on STK500. You can search ATMEL web site for example code that implemented on STK series starter kits. So you can use wizard tools of CodeVision and ImageCraft for generating all you need for timers, USART, SPI & etc. Those are free demo downloadable with full wizard fuction.
good luck.

raj wrote:
Hi Reza,

This is kundan, I am also new with AVR. Can you help me. I have everything to work with stk500,AVRISPmk2 and all the chips.can u send me some C files so that i can work on stk board and also how to confifure timers in different way.Are u from india.I will wait for your reply.

Best regards,
kundan
FAE
E.E India pte Ltd.
New Delhi
+91-11-9811675688

On Thu, 10 Apr 2008 reza asghari wrote :
>Hi,
> Here is some codes that I implemented in IAR-AVR v4.12 .
> Good luck.
>
> /*********************************************************************
> Pointer to Functions type definitions
>*********************************************************************/
>typedef void (*pt2Function) (void);
>typedef void (*pt2CharFunction) (char x_char);
>
> // Some functions
> void ShowSubMenu1_1(void);
>void ShowSubMenu1_2(void);
>
>pt2Function SubMenu_1_ShowValFunc[2] =
>{
> ShowSubMenu1_1 ,
> ShowSubMenu1_2 ,
>};
>
>void ShowSubMenu1_1(void)
>{
> // Some codes
>}
> void ShowSubMenu1_2(void)
>{
> // Some codes
>}
>
> // Usage example:
> SubMenu_1_ShowValFunc[x](); // x = 0 or 1
>Aravind wrote:
> Hi, i'm developing some Menu structures for my LCD. Each menu
>display
>is associated with a state and each state is associated with a
>handling function. I have created an array functions to handle each
>state and need to call the function depending on the state. When i
>simulate in Avr studio, the control does not jump to the called
>function instead carries on as if FPtr is just another variable. On
>examining the listing file, i find the function pointers do not
>point
>to the actual location of the function and also on adding watch to
>the
>function pointer array, avr studio shows that the pointers point to
>a
>location on SRAM rather than FLASH. I'm new to using pointers, the
>problem could be a silly mistake, please help. Thanks.
>
>#include
>#include
>#include
>#include
>#include "global.h"
>#include "twi.h"
>#include "timertick.h"
>#include "ks0066u.h"
>#include "uart_com.h"
>#include "spi_comm.h"
>
>typedef void (*FuncPtr)(void);
>//function pointer
>FuncPtr FPtr;
>
>#define front 1
>#define rear 2
>#define csub 3
>#define headphone 4
>
>#define all_reg 7
>
>#define mux_select 0
>#define gain 1
>#define volume 2
>#define bass 3
>#define mid 4
>#define treble 5
>#define atten_l 6
>#define atten_r 7
>
>#define gain_max 0x0F
>#define gain_min 0x00
>
>#define volume_max 0x00
>#define volume_min 0x2F
>
>#define mute 0x38
>
>#define atten_max 0x4F
>#define atten_min 0x00
>#define atten_mute 0x78
>
>#define equ_max 0x0F
>#define equ_min 0x00
>//
>*********************************************************************
>******­**
>//***************************Display State
>Definitions*************************
>#define power_dwn_s 0
>
>#define main_disp_s 1
>
>#define main_menu_s 2
>
>#define speak_set_s 3
>
>#define sourc_set_s 4
>
>#define front_sel_s 5
>#define rear__sel_s 6
>#define csub__sel_s 7
>#define headp_sel_s 8
>
>#define volum_set_s 9
>
>#define mastr_set_s 10
>#define chanl_vol_s 11
>#define front_vol_s 12
>#define rear__vol_s 13
>#define csub__vol_s 14
>#define headp_vol_s 15
>
>#define gain__set_s 16
>
>#define gainf_set_s 17
>#define gainr_set_s 18
>#define gainc_set_s 19
>#define gainh_set_s 20
>
>#define equlz_set_s 21
>
>#define equf__set_s 22
>#define equfb_set_s 23
>#define equfm_set_s 24
>#define equft_set_s 25
>
>#define equrb_set_s 26
>#define equrm_set_s 27
>#define equrt_set_s 28
>
>#define equcb_set_s 29
>#define equcm_set_s 30
>#define equct_set_s 31
>
>#define equhb_set_s 32
>#define equhm_set_s 33
>#define equht_set_s 34
>
>#define disp__set_s 35
>#define LCDBL_set_s 36
>#define VUdis_set_s 37
>
>#define systm_set_s 38
>#define temp__dis_s 39
>#define fansp_set_s 40
>#define fanma_set_s 41
>//
>*********************************************************************
>******­**
>//***************************Function
>Definitions******************************
>
>void cmd_proc(void);
>bool call_bindfunc(void);
>void call_def_func(void);
>//
>*********************************************************************
>******­**
>//*******************State Handling Function
>Definitions***********************
>void power_dwn_f(void);
>void main_disp_f(void);
>void main_menu_f(void);
>//
>*********************************************************************
>******­**
>//***************************Global Variable
>Declarations**********************
>//***************************Function Pointer
>Declarations*********************
>const FuncPtr dis_handle[] PROGMEM =
>{power_dwn_f,main_disp_f,main_menu_f};
>//********************* Function - Key Binding Table Declarations
>*************
>//const uint8_t power_dwn_bk[] = {0x02,0x03,0xFE};
>//const uint8_t main_disp_bk[] =
>{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE};
>
>const uint8_t fkeybind[60][60] PROGMEM=
>{{0x02,0x03,0xFE}, //
>power_dwn_s
>
>{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE}}; //main_disp_s
>//***************************Other Variable
>Declarations***********************
>const uint8_t equ_range[15] =
>{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
>
>0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08};
>
>const uint8_t def_7439[8] =
>{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};
>const uint8_t defcsub_7439[8] =
>{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};
>
>uint8_t front_7439[8],rear_7439[7],csub_7439[7],headp_7439[7];
>uint8_t
>prev_front_7439[8],prev_rear_7439[7],prev_csub_7439
>[7],prev_headp_7439[7];
>
>bool noslave;
>
>unsigned int d_state,pstate,opt_set;
>
>unsigned int cmd,prev1_cmd,prev2_cmd;
>volatile bool new_cmd;
>
>//
>*********************************************************************
>******­**
>//***************************LCD Display
>Strings*******************************
>const char main_disp1[20] PROGMEM = " APAG2 ";
>const char volume1[20] PROGMEM = " Master Volume: ";
>
>//
>*********************************************************************
>******­**
>//***************************Main
>Entry****************************************
>
>int main(void)
>{
>
>char init1[20] = " Initialized..... ";
>// char init2[20] = " Volume up ";
>// char init3[20] = " Volume down ";
>sei();
>
>systemtick_init();
>lcd_pwminit();
>spisetup();
>spitx(0xAA);
>
>i2c_init();
>set_7439all(front,(uint8_t*) &def_7439);
>set_7439all(rear,(uint8_t*) &def_7439);
>set_7439all(csub,(uint8_t*) &defcsub_7439);
>set_7439all(headphone,(uint8_t*) &def_7439);
>
>init_uart0();
>init_uart1();
>
>// txuchar1('R');
>// txuchar1('e');
>// txuchar1('a');
>// txuchar1('d');
>// txuchar1('y');
>
>// while(!uart0_ch_rx)
>// {
>// txuchar0(0xAE);
>// txuchar1(0xAE);
>// }
>uart0_ch_rx = false;
>if(!(uart0_ch == 0xDD))
>noslave = false;
>else
>noslave = true;
>txuchar1(noslave);
>lcd_init();
>lcd_write(1,init1);
>
>d_state = power_dwn_s;
>
>while(1)
>{
>if(uart0_ch_rx)
>cmd_proc();
>
>if(new_cmd)
>if(!call_bindfunc())
>call_def_func();
>}
>
>/* while(1){
>while(!uart0_ch_rx);
>uart0_ch_rx = false;
>if(uart0_ch == 0xCC)
>{
>while(!uart0_ch_rx);
>uart0_ch_rx = false;
>if(uart0_ch == 0x70)
>{
>while(!uart0_ch_rx);
>uart0_ch_rx = false;
>if(uart0_ch == 0x07)
>lcd_write(2,init3);
>else if (uart0_ch == 0x0B)
>lcd_write(2,init2);
>}
>}
>}
>txuchar0(0xEA);*/
>while(1);
>
>}
>
>void cmd_proc(void)
>{
>uart0_ch_rx = false;
>
>if((uart0_ch > 0) && (uart0_ch <=0x2E))
>{
>new_cmd = true;
>cmd = uart0_ch;
>}
>
>}
>
>bool call_bindfunc(void)
>{
>unsigned int i,n;
>n = 0;
>for(;;)
>{
>i = pgm_read_byte(&fkeybind[d_state][n]);
>n++;
>if(i == cmd)
>{
>FPtr = (FuncPtr)pgm_read_word(*dis_handle
>[d_state]);
>return true;
>}else if(i == 0xFE)
>break;
>}
>return false;
>
>}
>
>void call_def_func(void)
>{
>int i;
>i =12;
>
>}
>
>void main_disp_f(void)
>{
>
>}
>
>void main_menu_f(void)
>{
>
>}
>
>void power_dwn_f(void)
>{
>}

------------------------------------



(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: Re: Re: [AVR club] Problem with function pointers AVR simulation - raj - Apr 15 8:31:07 2008

Hi Reza,

I am not talking about to work with stk500. Send me some code s=
o that i can configure some peripherals , sply in C. this is the issue. I w=
ant to use codevision for programming.=A0Suppose I want to write a program=
=20

1. main function
2. In main a subroutine to be called so that timer runs for 3 sec and som=
e port bit is set low during this time.

best rgds/
kundan=20
On Sun, 13 Apr 2008 reza asghari wrote :
>Hi,
> I don't work on STK500. You can search ATMEL web site for example code =
that implemented on STK series starter kits. So you can use wizard tools of=
CodeVision and ImageCraft for generating all you need for timers, USART, S=
PI & etc. Those are free demo downloadable with full wizard fuction.
> good luck.
>
>raj wrote:
> Hi Reza,
>
>This is kundan, I am also new with AVR. Can you help me. I have everything=
to work with stk500,AVRISPmk2 and all the chips.can u send me some C files=
so that i can work on stk board and also how to confifure timers in differ=
ent way.Are u from india.I will wait for your reply.
>
>Best regards,
>kundan
>FAE
>E.E India pte Ltd.
>New Delhi
>+91-11-9811675688
>
>On Thu, 10 Apr 2008 reza asghari wrote :
> >Hi,
> > Here is some codes that I implemented in IAR-AVR v4.12 .
> > Good luck.
> >
> > /*********************************************************************
> > Pointer to Functions type definitions
> >*********************************************************************/
> >typedef void (*pt2Function) (void);
> >typedef void (*pt2CharFunction) (char x_char);
> >
> > // Some functions
> > void ShowSubMenu1_1(void);
> >void ShowSubMenu1_2(void);
> >
> >pt2Function SubMenu_1_ShowValFunc[2] =3D
> >{
> > ShowSubMenu1_1 ,
> > ShowSubMenu1_2 ,
> >};
> >
> >void ShowSubMenu1_1(void)
> >{
> > // Some codes
> >}
> > void ShowSubMenu1_2(void)
> >{
> > // Some codes
> >}
> >
> > // Usage example:
> > SubMenu_1_ShowValFunc[x](); // x =3D 0 or 1
> >
> >
> >
> >
> >Aravind wrote:
> > Hi, i'm developing some Menu structures for my LCD. Each menu
> >display
> >is associated with a state and each state is associated with a
> >handling function. I have created an array functions to handle each
> >state and need to call the function depending on the state. When i
> >simulate in Avr studio, the control does not jump to the called
> >function instead carries on as if FPtr is just another variable. On
> >examining the listing file, i find the function pointers do not
> >point
> >to the actual location of the function and also on adding watch to
> >the
> >function pointer array, avr studio shows that the pointers point to
> >a
> >location on SRAM rather than FLASH. I'm new to using pointers, the
> >problem could be a silly mistake, please help. Thanks.
> >
> >#include
> >#include
> >#include
> >#include
> >#include "global.h"
> >#include "twi.h"
> >#include "timertick.h"
> >#include "ks0066u.h"
> >#include "uart_com.h"
> >#include "spi_comm.h"
> >
> >typedef void (*FuncPtr)(void);
> >//function pointer
> >FuncPtr FPtr;
> >
> >#define front 1
> >#define rear 2
> >#define csub 3
> >#define headphone 4
> >
> >#define all_reg 7
> >
> >#define mux_select 0
> >#define gain 1
> >#define volume 2
> >#define bass 3
> >#define mid 4
> >#define treble 5
> >#define atten_l 6
> >#define atten_r 7
> >
> >#define gain_max 0x0F
> >#define gain_min 0x00
> >
> >#define volume_max 0x00
> >#define volume_min 0x2F
> >
> >#define mute 0x38
> >
> >#define atten_max 0x4F
> >#define atten_min 0x00
> >#define atten_mute 0x78
> >
> >#define equ_max 0x0F
> >#define equ_min 0x00
> >//
> >*********************************************************************
> >******=AD**
> >//***************************Display State
> >Definitions*************************
> >#define power_dwn_s 0
> >
> >#define main_disp_s 1
> >
> >#define main_menu_s 2
> >
> >#define speak_set_s 3
> >
> >#define sourc_set_s 4
> >
> >#define front_sel_s 5
> >#define rear__sel_s 6
> >#define csub__sel_s 7
> >#define headp_sel_s 8
> >
> >#define volum_set_s 9
> >
> >#define mastr_set_s 10
> >#define chanl_vol_s 11
> >#define front_vol_s 12
> >#define rear__vol_s 13
> >#define csub__vol_s 14
> >#define headp_vol_s 15
> >
> >#define gain__set_s 16
> >
> >#define gainf_set_s 17
> >#define gainr_set_s 18
> >#define gainc_set_s 19
> >#define gainh_set_s 20
> >
> >#define equlz_set_s 21
> >
> >#define equf__set_s 22
> >#define equfb_set_s 23
> >#define equfm_set_s 24
> >#define equft_set_s 25
> >
> >#define equrb_set_s 26
> >#define equrm_set_s 27
> >#define equrt_set_s 28
> >
> >#define equcb_set_s 29
> >#define equcm_set_s 30
> >#define equct_set_s 31
> >
> >#define equhb_set_s 32
> >#define equhm_set_s 33
> >#define equht_set_s 34
> >
> >#define disp__set_s 35
> >#define LCDBL_set_s 36
> >#define VUdis_set_s 37
> >
> >#define systm_set_s 38
> >#define temp__dis_s 39
> >#define fansp_set_s 40
> >#define fanma_set_s 41
> >//
> >*********************************************************************
> >******=AD**
> >//***************************Function
> >Definitions******************************
> >
> >void cmd_proc(void);
> >bool call_bindfunc(void);
> >void call_def_func(void);
> >//
> >*********************************************************************
> >******=AD**
> >//*******************State Handling Function
> >Definitions***********************
> >void power_dwn_f(void);
> >void main_disp_f(void);
> >void main_menu_f(void);
> >//
> >*********************************************************************
> >******=AD**
> >//***************************Global Variable
> >Declarations**********************
> >//***************************Function Pointer
> >Declarations*********************
> >const FuncPtr dis_handle[] PROGMEM =3D
> >{power_dwn_f,main_disp_f,main_menu_f};
> >//********************* Function - Key Binding Table Declarations
> >*************
> >//const uint8_t power_dwn_bk[] =3D {0x02,0x03,0xFE};
> >//const uint8_t main_disp_bk[] =3D
> >{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE};
> >
> >const uint8_t fkeybind[60][60] PROGMEM=3D
> >{{0x02,0x03,0xFE}, //
> >power_dwn_s
> >
> >{0x02,0x03,0x07,0x0B,0x1A,0x1B,0xFE}}; //main_disp_s
> >//***************************Other Variable
> >Declarations***********************
> >const uint8_t equ_range[15] =3D
> >{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
> >
> >0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08};
> >
> >const uint8_t def_7439[8] =3D
> >{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};
> >const uint8_t defcsub_7439[8] =3D
> >{0x03,0x00,0x38,0x07,0x07,0x07,0x00,0x00};
> >
> >uint8_t front_7439[8],rear_7439[7],csub_7439[7],headp_7439[7];
> >uint8_t
> >prev_front_7439[8],prev_rear_7439[7],prev_csub_7439
> >[7],prev_headp_7439[7];
> >
> >bool noslave;
> >
> >unsigned int d_state,pstate,opt_set;
> >
> >unsigned int cmd,prev1_cmd,prev2_cmd;
> >volatile bool new_cmd;
> >
> >//
> >*********************************************************************
> >******=AD**
> >//***************************LCD Display
> >Strings*******************************
> >const char main_disp1[20] PROGMEM =3D " APAG2 ";
> >const char volume1[20] PROGMEM =3D " Master Volume: ";
> >
> >//
> >*********************************************************************
> >******=AD**
> >//***************************Main
> >Entry****************************************
> >
> >int main(void)
> >{
> >
> >char init1[20] =3D " Initialized..... ";
> >// char init2[20] =3D " Volume up ";
> >// char init3[20] =3D " Volume down ";
> >sei();
> >
> >systemtick_init();
> >lcd_pwminit();
> >spisetup();
> >spitx(0xAA);
> >
> >i2c_init();
> >set_7439all(front,(uint8_t*) &def_7439);
> >set_7439all(rear,(uint8_t*) &def_7439);
> >set_7439all(csub,(uint8_t*) &defcsub_7439);
> >set_7439all(headphone,(uint8_t*) &def_7439);
> >
> >init_uart0();
> >init_uart1();
> >
> >// txuchar1('R');
> >// txuchar1('e');
> >// txuchar1('a');
> >// txuchar1('d');
> >// txuchar1('y');
> >
> >// while(!uart0_ch_rx)
> >// {
> >// txuchar0(0xAE);
> >// txuchar1(0xAE);
> >// }
> >uart0_ch_rx =3D false;
> >if(!(uart0_ch =3D=3D 0xDD))
> >noslave =3D false;
> >else
> >noslave =3D true;
> >txuchar1(noslave);
> >lcd_init();
> >lcd_write(1,init1);
> >
> >d_state =3D power_dwn_s;
> >
> >while(1)
> >{
> >if(uart0_ch_rx)
> >cmd_proc();
> >
> >if(new_cmd)
> >if(!call_bindfunc())
> >call_def_func();
> >}
> >
> >/* while(1){
> >while(!uart0_ch_rx);
> >uart0_ch_rx =3D false;
> >if(uart0_ch =3D=3D 0xCC)
> >{
> >while(!uart0_ch_rx);
> >uart0_ch_rx =3D false;
> >if(uart0_ch =3D=3D 0x70)
> >{
> >while(!uart0_ch_rx);
> >uart0_ch_rx =3D false;
> >if(uart0_ch =3D=3D 0x07)
> >lcd_write(2,init3);
> >else if (uart0_ch =3D=3D 0x0B)
> >lcd_write(2,init2);
> >}
> >}
> >}
> >txuchar0(0xEA);*/
> >while(1);
> >
> >}
> >
> >void cmd_proc(void)
> >{
> >uart0_ch_rx =3D false;
> >
> >if((uart0_ch > 0) && (uart0_ch <=3D0x2E))
> >{
> >new_cmd =3D true;
> >cmd =3D uart0_ch;
> >}
> >
> >}
> >
> >bool call_bindfunc(void)
> >{
> >unsigned int i,n;
> >n =3D 0;
> >for(;;)
> >{
> >i =3D pgm_read_byte(&fkeybind[d_state][n]);
> >n++;
> >if(i =3D=3D cmd)
> >{
> >FPtr =3D (FuncPtr)pgm_read_word(*dis_handle
> >[d_state]);
> >return true;
> >}else if(i =3D=3D 0xFE)
> >break;
> >}
> >return false;
> >
> >}
> >
> >void call_def_func(void)
> >{
> >int i;
> >i =3D12;
> >
> >}
> >
> >void main_disp_f(void)
> >{
> >
> >}
> >
> >void main_menu_f(void)
> >{
> >
> >}
> >
> >void power_dwn_f(void)
> >{
> >}
[Non-text portions of this message have been removed]
------------------------------------



(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )